Skip to content

Commit

Permalink
Merge pull request #26303 from brave/issues/41985
Browse files Browse the repository at this point in the history
[ads] General code health
  • Loading branch information
tmancey authored Oct 30, 2024
2 parents 62e8e1f + 107c420 commit c19f88c
Show file tree
Hide file tree
Showing 78 changed files with 498 additions and 381 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "brave/components/brave_ads/core/internal/account/confirmations/reward/reward_confirmation_util.h"

#include <utility>
#include <vector>

#include "base/base64url.h"
#include "base/check.h"
Expand Down Expand Up @@ -39,14 +38,12 @@ std::optional<RewardInfo> BuildReward(const ConfirmationInfo& confirmation) {
RewardInfo reward;

// Token
const std::vector<cbr::Token> tokens =
GetTokenGenerator()->Generate(/*count=*/1);
const cbr::TokenList tokens = GetTokenGenerator()->Generate(/*count=*/1);
CHECK(!tokens.empty());
reward.token = tokens.front();

// Blinded token
const std::vector<cbr::BlindedToken> blinded_tokens =
cbr::BlindTokens(tokens);
const cbr::BlindedTokenList blinded_tokens = cbr::BlindTokens(tokens);
CHECK(!blinded_tokens.empty());
reward.blinded_token = blinded_tokens.front();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

#include "brave/components/brave_ads/core/internal/account/tokens/token_generator.h"

#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/token.h"

namespace brave_ads {

std::vector<cbr::Token> TokenGenerator::Generate(const size_t count) const {
return std::vector<cbr::Token>(count);
cbr::TokenList TokenGenerator::Generate(const size_t count) const {
return cbr::TokenList(count);
}

} // namespace brave_ads
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@
#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_ACCOUNT_TOKENS_TOKEN_GENERATOR_H_

#include <cstddef>
#include <vector>

#include "brave/components/brave_ads/core/internal/account/tokens/token_generator_interface.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/token.h"

namespace brave_ads {

namespace cbr {
class Token;
} // namespace cbr

class TokenGenerator : public TokenGeneratorInterface {
public:
std::vector<cbr::Token> Generate(size_t count) const override;
cbr::TokenList Generate(size_t count) const override;
};

} // namespace brave_ads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@
#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_ACCOUNT_TOKENS_TOKEN_GENERATOR_INTERFACE_H_

#include <cstddef>
#include <vector>

namespace brave_ads {
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/token.h"

namespace cbr {
class Token;
} // namespace cbr
namespace brave_ads {

class TokenGeneratorInterface {
public:
virtual ~TokenGeneratorInterface() = default;

virtual std::vector<cbr::Token> Generate(size_t count) const = 0;
virtual cbr::TokenList Generate(size_t count) const = 0;
};

} // namespace brave_ads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_ACCOUNT_TOKENS_TOKEN_GENERATOR_MOCK_H_

#include <cstddef>
#include <vector>

#include "brave/components/brave_ads/core/internal/account/tokens/token_generator.h"
#include "brave/components/brave_ads/core/internal/account/tokens/token_generator_interface.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/token.h"
#include "testing/gmock/include/gmock/gmock.h"

Expand All @@ -27,7 +26,7 @@ class TokenGeneratorMock : public TokenGeneratorInterface {

~TokenGeneratorMock() override;

MOCK_METHOD(std::vector<cbr::Token>, Generate, (const size_t count), (const));
MOCK_METHOD(cbr::TokenList, Generate, (const size_t count), (const));
};

} // namespace brave_ads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
#include "brave/components/brave_ads/core/internal/account/tokens/token_generator_test_util.h"

#include <string>
#include <vector>

#include "base/check_op.h"
#include "base/no_destructor.h"
#include "brave/components/brave_ads/core/internal/account/tokens/token_generator_interface.h"
#include "brave/components/brave_ads/core/internal/account/tokens/token_generator_mock.h"
#include "brave/components/brave_ads/core/internal/ads_core/ads_core_util.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/token.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace brave_ads::test {
Expand Down Expand Up @@ -149,12 +148,12 @@ void MockTokenGenerator(const size_t count) {
.WillByDefault(::testing::Return(BuildTokens(count)));
}

std::vector<cbr::Token> BuildTokens(const size_t count) {
cbr::TokenList BuildTokens(const size_t count) {
CHECK_GT(count, 0U);

const size_t modulo = Tokens().size();

std::vector<cbr::Token> tokens;
cbr::TokenList tokens;

for (size_t i = 0; i < count; ++i) {
const std::string& token_base64 = Tokens().at(i % modulo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,18 @@
#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_ACCOUNT_TOKENS_TOKEN_GENERATOR_TEST_UTIL_H_

#include <cstddef>
#include <vector>

namespace brave_ads {
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/token.h"

namespace cbr {
class Token;
} // namespace cbr

namespace test {
namespace brave_ads::test {

// Call this function to mock the token generator for testing purposes if code
// paths call `RefillConfirmationTokens::MaybeRefill`, `Confirmations::Confirm`,
// `BuildRewardConfirmation`, or `BuildReward`.
void MockTokenGenerator(size_t count);

std::vector<cbr::Token> BuildTokens(size_t count);

} // namespace test
cbr::TokenList BuildTokens(size_t count);

} // namespace brave_ads
} // namespace brave_ads::test

#endif // BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_ACCOUNT_TOKENS_TOKEN_GENERATOR_TEST_UTIL_H_
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ TEST(BraveAdsTokenGeneratorTest, Generate) {
const TokenGenerator token_generator;

// Act
const std::vector<cbr::Token> tokens = token_generator.Generate(5);
const cbr::TokenList tokens = token_generator.Generate(5);

// Assert
EXPECT_THAT(tokens, ::testing::SizeIs(5));
Expand All @@ -29,7 +29,7 @@ TEST(BraveAdsTokenGeneratorTest, GenerateZero) {
const TokenGenerator token_generator;

// Act
const std::vector<cbr::Token> tokens = token_generator.Generate(0);
const cbr::TokenList tokens = token_generator.Generate(0);

// Assert
EXPECT_THAT(tokens, ::testing::IsEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ RedeemRewardConfirmation::HandleFetchPaymentTokenUrlResponse(
/*should_retry=*/true));
}

const std::optional<std::vector<cbr::UnblindedToken>> unblinded_tokens =
const std::optional<cbr::UnblindedTokenList> unblinded_tokens =
ParseVerifyAndUnblindTokens(
*payment_token_dict, {confirmation.reward->token},
{confirmation.reward->blinded_token}, *public_key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ RefillConfirmationTokens::HandleGetSignedTokensUrlResponse(
/*should_retry=*/true));
}

const std::optional<std::vector<cbr::UnblindedToken>> unblinded_tokens =
const std::optional<cbr::UnblindedTokenList> unblinded_tokens =
ParseVerifyAndUnblindTokens(*dict, *tokens_, *blinded_tokens_,
*public_key);
if (!unblinded_tokens) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <optional>
#include <string>
#include <tuple>
#include <vector>

#include "base/check_op.h"
#include "base/memory/raw_ptr.h"
Expand Down Expand Up @@ -89,8 +88,8 @@ class RefillConfirmationTokens final {

std::optional<std::string> nonce_;

std::optional<std::vector<cbr::Token>> tokens_;
std::optional<std::vector<cbr::BlindedToken>> blinded_tokens_;
std::optional<cbr::TokenList> tokens_;
std::optional<cbr::BlindedTokenList> blinded_tokens_;

bool is_refilling_ = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "brave/components/brave_ads/core/internal/account/tokens/confirmation_tokens/confirmation_tokens_util.h"
#include "brave/components/brave_ads/core/internal/account/wallet/wallet_info.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/public_key.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/unblinded_token.h"
#include "brave/components/brave_ads/core/internal/common/crypto/crypto_util.h"
#include "brave/components/brave_ads/core/internal/common/logging_util.h"

Expand All @@ -32,7 +31,7 @@ std::optional<std::string> Sign(const cbr::UnblindedToken& unblinded_token,
}

ConfirmationTokenList BuildConfirmationTokens(
const std::vector<cbr::UnblindedToken>& unblinded_tokens,
const cbr::UnblindedTokenList& unblinded_tokens,
const cbr::PublicKey& public_key,
const WalletInfo& wallet) {
ConfirmationTokenList confirmation_tokens;
Expand Down Expand Up @@ -60,7 +59,7 @@ std::optional<std::string> ParseCaptchaId(const base::Value::Dict& dict) {
}

void BuildAndAddConfirmationTokens(
const std::vector<cbr::UnblindedToken>& unblinded_tokens,
const cbr::UnblindedTokenList& unblinded_tokens,
const cbr::PublicKey& public_key,
const WalletInfo& wallet) {
const ConfirmationTokenList confirmation_tokens =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@

#include <optional>
#include <string>
#include <vector>

#include "base/values.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/unblinded_token.h"

namespace brave_ads {

namespace cbr {
class PublicKey;
class UnblindedToken;
} // namespace cbr

struct WalletInfo;

std::optional<std::string> ParseCaptchaId(const base::Value::Dict& dict);

void BuildAndAddConfirmationTokens(
const std::vector<cbr::UnblindedToken>& unblinded_tokens,
const cbr::UnblindedTokenList& unblinded_tokens,
const cbr::PublicKey& public_key,
const WalletInfo& wallet);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ std::string BuildDigestHeaderValue(const std::string& body) {

RequestSignedTokensUrlRequestBuilder::RequestSignedTokensUrlRequestBuilder(
WalletInfo wallet,
std::vector<cbr::BlindedToken> blinded_tokens)
cbr::BlindedTokenList blinded_tokens)
: wallet_(std::move(wallet)), blinded_tokens_(std::move(blinded_tokens)) {
CHECK(wallet_.IsValid());
CHECK(!blinded_tokens_.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RequestSignedTokensUrlRequestBuilder final
: public UrlRequestBuilderInterface {
public:
RequestSignedTokensUrlRequestBuilder(WalletInfo wallet,
std::vector<cbr::BlindedToken> tokens);
cbr::BlindedTokenList tokens);

RequestSignedTokensUrlRequestBuilder(
const RequestSignedTokensUrlRequestBuilder&) = delete;
Expand All @@ -47,7 +47,7 @@ class RequestSignedTokensUrlRequestBuilder final
std::string BuildBody() const;

const WalletInfo wallet_;
const std::vector<cbr::BlindedToken> blinded_tokens_;
const cbr::BlindedTokenList blinded_tokens_;
};

} // namespace brave_ads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ class BraveAdsRequestSignedTokensUrlRequestBuilderTest : public test::TestBase {

TEST_F(BraveAdsRequestSignedTokensUrlRequestBuilderTest, BuildUrl) {
// Arrange
const std::vector<cbr::Token> tokens = test::BuildTokens(/*count=*/3);
const std::vector<cbr::BlindedToken> blinded_tokens =
cbr::BlindTokens(tokens);
const cbr::TokenList tokens = test::BuildTokens(/*count=*/3);
const cbr::BlindedTokenList blinded_tokens = cbr::BlindTokens(tokens);

RequestSignedTokensUrlRequestBuilder url_request_builder(test::Wallet(),
blinded_tokens);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

#include "brave/components/brave_ads/core/internal/account/utility/tokens_util.h"

#include <string>

#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/batch_dleq_proof.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/public_key.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/signed_token.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/unblinded_token.h"
#include "brave/components/brave_ads/core/internal/common/logging_util.h"

namespace brave_ads {
Expand All @@ -35,14 +35,14 @@ std::optional<cbr::PublicKey> ParsePublicKey(const base::Value::Dict& dict) {
return public_key;
}

std::optional<std::vector<cbr::SignedToken>> ParseSignedTokens(
std::optional<cbr::SignedTokenList> ParseSignedTokens(
const base::Value::Dict& dict) {
const base::Value::List* const list = dict.FindList(kSignedTokensKey);
if (!list) {
return std::nullopt;
}

std::vector<cbr::SignedToken> signed_tokens;
cbr::SignedTokenList signed_tokens;

for (const auto& value : *list) {
const std::string* const signed_token_base64 = value.GetIfString();
Expand All @@ -62,10 +62,10 @@ std::optional<std::vector<cbr::SignedToken>> ParseSignedTokens(
return signed_tokens;
}

std::optional<std::vector<cbr::UnblindedToken>> ParseVerifyAndUnblindTokens(
std::optional<cbr::UnblindedTokenList> ParseVerifyAndUnblindTokens(
const base::Value::Dict& dict,
const std::vector<cbr::Token>& tokens,
const std::vector<cbr::BlindedToken>& blinded_tokens,
const cbr::TokenList& tokens,
const cbr::BlindedTokenList& blinded_tokens,
const cbr::PublicKey& public_key) {
if (!public_key.has_value()) {
BLOG(1, "Invalid public key");
Expand All @@ -85,14 +85,14 @@ std::optional<std::vector<cbr::UnblindedToken>> ParseVerifyAndUnblindTokens(
return std::nullopt;
}

const std::optional<std::vector<cbr::SignedToken>> signed_tokens =
const std::optional<cbr::SignedTokenList> signed_tokens =
ParseSignedTokens(dict);
if (!signed_tokens) {
BLOG(1, "Failed to parse signed tokens");
return std::nullopt;
}

const std::optional<std::vector<cbr::UnblindedToken>> unblinded_tokens =
const std::optional<cbr::UnblindedTokenList> unblinded_tokens =
batch_dleq_proof.VerifyAndUnblind(tokens, blinded_tokens, *signed_tokens,
public_key);
if (!unblinded_tokens || unblinded_tokens->empty()) {
Expand Down
Loading

0 comments on commit c19f88c

Please sign in to comment.