Skip to content

Commit

Permalink
[ads] General code health
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Feb 11, 2025
1 parent 7d67928 commit 963d499
Show file tree
Hide file tree
Showing 124 changed files with 1,186 additions and 765 deletions.
36 changes: 19 additions & 17 deletions components/brave_ads/core/internal/account/account_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

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

#include <cstddef>

#include "base/test/scoped_feature_list.h"
#include "brave/components/brave_ads/core/internal/common/test/test_base.h"
#include "brave/components/brave_ads/core/internal/settings/settings_test_util.h"
Expand All @@ -18,9 +20,9 @@ class BraveAdsAccountUtilTest : public test::TestBase {};

TEST_F(BraveAdsAccountUtilTest, AlwaysAllowDepositsForRewardsUser) {
// Act & Assert
for (int i = 0; i < static_cast<int>(mojom::AdType::kMaxValue); ++i) {
for (int j = 0; j < static_cast<int>(mojom::ConfirmationType::kMaxValue);
++j) {
for (size_t i = 0; i < static_cast<size_t>(mojom::AdType::kMaxValue); ++i) {
for (size_t j = 0;
j < static_cast<size_t>(mojom::ConfirmationType::kMaxValue); ++j) {
EXPECT_TRUE(IsAllowedToDeposit(static_cast<mojom::AdType>(i),
static_cast<mojom::ConfirmationType>(j)));
}
Expand All @@ -33,8 +35,8 @@ TEST_F(BraveAdsAccountUtilTest,
test::DisableBraveRewards();

// Act & Assert
for (int i = 0; i < static_cast<int>(mojom::ConfirmationType::kMaxValue);
++i) {
for (size_t i = 0;
i < static_cast<size_t>(mojom::ConfirmationType::kMaxValue); ++i) {
EXPECT_TRUE(IsAllowedToDeposit(mojom::AdType::kInlineContentAd,
static_cast<mojom::ConfirmationType>(i)));
}
Expand All @@ -46,8 +48,8 @@ TEST_F(BraveAdsAccountUtilTest,
test::DisableBraveRewards();

// Act & Assert
for (int i = 0; i < static_cast<int>(mojom::ConfirmationType::kMaxValue);
++i) {
for (size_t i = 0;
i < static_cast<size_t>(mojom::ConfirmationType::kMaxValue); ++i) {
EXPECT_TRUE(IsAllowedToDeposit(mojom::AdType::kPromotedContentAd,
static_cast<mojom::ConfirmationType>(i)));
}
Expand All @@ -59,8 +61,8 @@ TEST_F(BraveAdsAccountUtilTest,
test::DisableBraveRewards();

// Act & Assert
for (int i = 0; i < static_cast<int>(mojom::ConfirmationType::kMaxValue);
++i) {
for (size_t i = 0;
i < static_cast<size_t>(mojom::ConfirmationType::kMaxValue); ++i) {
EXPECT_FALSE(IsAllowedToDeposit(mojom::AdType::kNewTabPageAd,
static_cast<mojom::ConfirmationType>(i)));
}
Expand All @@ -76,8 +78,8 @@ TEST_F(
test::DisableBraveRewards();

// Act & Assert
for (int i = 0; i < static_cast<int>(mojom::ConfirmationType::kMaxValue);
++i) {
for (size_t i = 0;
i < static_cast<size_t>(mojom::ConfirmationType::kMaxValue); ++i) {
EXPECT_TRUE(IsAllowedToDeposit(mojom::AdType::kNewTabPageAd,
static_cast<mojom::ConfirmationType>(i)));
}
Expand All @@ -89,8 +91,8 @@ TEST_F(BraveAdsAccountUtilTest,
test::DisableBraveRewards();

// Act & Assert
for (int i = 0; i < static_cast<int>(mojom::ConfirmationType::kMaxValue);
++i) {
for (size_t i = 0;
i < static_cast<size_t>(mojom::ConfirmationType::kMaxValue); ++i) {
EXPECT_FALSE(IsAllowedToDeposit(mojom::AdType::kNotificationAd,
static_cast<mojom::ConfirmationType>(i)));
}
Expand All @@ -103,8 +105,8 @@ TEST_F(
test::DisableBraveRewards();

// Act & Assert
for (int i = 0; i < static_cast<int>(mojom::ConfirmationType::kMaxValue);
++i) {
for (size_t i = 0;
i < static_cast<size_t>(mojom::ConfirmationType::kMaxValue); ++i) {
EXPECT_FALSE(IsAllowedToDeposit(mojom::AdType::kSearchResultAd,
static_cast<mojom::ConfirmationType>(i)));
}
Expand All @@ -120,8 +122,8 @@ TEST_F(
test::DisableBraveRewards();

// Act & Assert
for (int i = 0; i < static_cast<int>(mojom::ConfirmationType::kMaxValue);
++i) {
for (size_t i = 0;
i < static_cast<size_t>(mojom::ConfirmationType::kMaxValue); ++i) {
const auto confirmation_type = static_cast<mojom::ConfirmationType>(i);

const bool is_allowed_to_deposit =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ namespace brave_ads::test {
std::vector<mojom::ConfirmationType>
BuildConfirmationTypeForCountAndIntersperseOtherTypes(
mojom::ConfirmationType mojom_confirmation_type,
int count) {
CHECK_GT(count, 0);
size_t count) {
CHECK_GT(count, 0U);

const int confirmation_type_max_value =
static_cast<int>(mojom::ConfirmationType::kMaxValue);
static_cast<size_t>(mojom::ConfirmationType::kMaxValue);

std::vector<mojom::ConfirmationType> mojom_confirmation_types;
mojom_confirmation_types.reserve(count + confirmation_type_max_value - 1);

for (int i = 0; i < count; ++i) {
for (size_t i = 0; i < count; ++i) {
mojom_confirmation_types.push_back(mojom_confirmation_type);
}

// Sprinkle in one of each confirmation type, other than `confirmation_type`.
for (int i = 0; i < confirmation_type_max_value; ++i) {
for (size_t i = 0; i < confirmation_type_max_value; ++i) {
const auto other_mojom_confirmation_type =
static_cast<mojom::ConfirmationType>(i);
if (other_mojom_confirmation_type == mojom_confirmation_type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_ACCOUNT_CONFIRMATIONS_CONFIRMATION_TYPE_TEST_UTIL_H_
#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_ACCOUNT_CONFIRMATIONS_CONFIRMATION_TYPE_TEST_UTIL_H_

#include <cstddef>
#include <vector>

#include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.h"
Expand All @@ -15,7 +16,7 @@ namespace brave_ads::test {
std::vector<mojom::ConfirmationType>
BuildConfirmationTypeForCountAndIntersperseOtherTypes(
mojom::ConfirmationType mojom_confirmation_type,
int count);
size_t count);

} // namespace brave_ads::test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ TEST_F(BraveAdsConfirmationPayloadJsonWriterTest,
R"(
{
"blindedPaymentTokens": [
"Ev5JE4/9TZI/5TqyN9JWfJ1To0HBwQw2rWeAPcdjX3Q="
"+qJiMi6k0hRzRAEN239nLthLqrNm53O78x/PV8I/JS0="
],
"buildChannel": "release",
"catalog": [
Expand All @@ -66,7 +66,7 @@ TEST_F(BraveAdsConfirmationPayloadJsonWriterTest,
"creativeInstanceId": "546fe7b0-5047-4f28-a11c-81f14edcf0f6",
"diagnosticId": "c1298fde-7fdb-401f-a3ce-0b58fe86e6e2",
"platform": "windows",
"publicKey": "RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk=",
"publicKey": "OqhZpUC8B15u+Gc11rQYRl8O3zOSAUIEC2JuDHI32TM=",
"rotatingHash": "jBdiJH7Hu3wj31WWNLjKV5nVxFxWSDWkYh5zXCS3rXY=",
"segment": "untargeted",
"studies": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ TEST_F(BraveAdsRewardConfirmationPayloadUtilTest,
R"(
{
"blindedPaymentTokens": [
"Ev5JE4/9TZI/5TqyN9JWfJ1To0HBwQw2rWeAPcdjX3Q="
"+qJiMi6k0hRzRAEN239nLthLqrNm53O78x/PV8I/JS0="
],
"publicKey": "RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="
"publicKey": "OqhZpUC8B15u+Gc11rQYRl8O3zOSAUIEC2JuDHI32TM="
})"),
reward_confirmation_payload);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void SaveConfirmationQueueItems(
}

void BuildAndSaveConfirmationQueueItems(const ConfirmationInfo& confirmation,
int count) {
size_t count) {
const ConfirmationQueueItemList confirmation_queue_items =
BuildConfirmationQueueItems(confirmation, count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void SaveConfirmationQueueItems(
const ConfirmationQueueItemList& confirmation_queue_items);

void BuildAndSaveConfirmationQueueItems(const ConfirmationInfo& confirmation,
int count);
size_t count);

} // namespace test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

namespace brave_ads {

class BraveAdsConfirmationQueueItemDelayTest : public test::TestBase {};
class BraveAdsConfirmationQueueItemUtilTest : public test::TestBase {};

TEST_F(BraveAdsConfirmationQueueItemDelayTest,
TEST_F(BraveAdsConfirmationQueueItemUtilTest,
CalculateDelayBeforeProcessingConfirmationQueueItem) {
// Arrange
test::MockTokenGenerator(/*count=*/1);
Expand All @@ -48,7 +48,7 @@ TEST_F(BraveAdsConfirmationQueueItemDelayTest,
EXPECT_EQ(base::Hours(1), delay_before_processing_confirmation_queue_item);
}

TEST_F(BraveAdsConfirmationQueueItemDelayTest,
TEST_F(BraveAdsConfirmationQueueItemUtilTest,
CalculateDelayBeforeProcessingPastDueConfirmationQueueItem) {
// Arrange
test::MockTokenGenerator(/*count=*/1);
Expand All @@ -71,7 +71,7 @@ TEST_F(BraveAdsConfirmationQueueItemDelayTest,
delay_before_processing_confirmation_queue_item);
}

TEST_F(BraveAdsConfirmationQueueItemDelayTest,
TEST_F(BraveAdsConfirmationQueueItemUtilTest,
CalculateMinimumDelayBeforeProcessingConfirmationQueueItem) {
// Arrange
test::MockTokenGenerator(/*count=*/1);
Expand All @@ -95,7 +95,7 @@ TEST_F(BraveAdsConfirmationQueueItemDelayTest,
delay_before_processing_confirmation_queue_item);
}

TEST_F(BraveAdsConfirmationQueueItemDelayTest,
TEST_F(BraveAdsConfirmationQueueItemUtilTest,
RebuildConfirmationWithoutDynamicUserData) {
// Arrange
test::MockTokenGenerator(/*count=*/1);
Expand Down Expand Up @@ -131,7 +131,7 @@ TEST_F(BraveAdsConfirmationQueueItemDelayTest,
expected_reward, expected_user_data));
}

TEST_F(BraveAdsConfirmationQueueItemDelayTest,
TEST_F(BraveAdsConfirmationQueueItemUtilTest,
RebuildConfirmationDynamicUserData) {
// Arrange
test::MockTokenGenerator(/*count=*/1);
Expand Down Expand Up @@ -164,6 +164,17 @@ TEST_F(BraveAdsConfirmationQueueItemDelayTest,
"systemTimestamp": "1996-07-08T10:00:00.000Z"
})");

EXPECT_EQ(rebuilt_confirmation.reward->blinded_token,
expected_reward.blinded_token);
EXPECT_EQ(rebuilt_confirmation.reward->unblinded_token,
expected_reward.unblinded_token);
EXPECT_EQ(rebuilt_confirmation.reward->token, expected_reward.token);
EXPECT_EQ(rebuilt_confirmation.reward->credential_base64url,
expected_reward.credential_base64url);
EXPECT_EQ(rebuilt_confirmation.reward->public_key,
expected_reward.public_key);
EXPECT_EQ(rebuilt_confirmation.reward->signature, expected_reward.signature);

EXPECT_THAT(
rebuilt_confirmation,
::testing::FieldsAre(test::kTransactionId, test::kCreativeInstanceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Included when redeeming an anonymous confirmation token:
| creativeInstanceId | {"creativeInstanceId":"e4958d00-e35c-4134-a408-1fbcf274d5ae"} | An id that references the specific ad creative that the user engaged with. This will be the same for any user that engages with this ad. |
| transactionId | {"transactionId":"c14d370c-1178-4c73-8385-1cfa17200646"} | A unique id for the transaction, which is not linkable between confirmation token redemptions. |
| type | {"type":"view"} | Action or interaction that occurred within an advertisement, such as a user clicking the ad.<br><br>Supported types:<br><br>- view<br>- click<br>- landed<br>- conversion<br>- interaction[^1]<br>- media_play[^1]<br>- media_25[^1]<br>- media_100[^1]<br>- upvote<br>- downvote<br>- flag<br>- bookmark &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |
| confirmation token | {"blindedPaymentTokens": ["Ev5JE4/9TZI/5TqyN9JWfJ1To0HBwQw2rWeAPcdjX3Q="],<br> "publicKey": "RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="} | See [security and privacy model for ad confirmations](https://github.com/brave/brave-browser/wiki/Security-and-privacy-model-for-ad-confirmations). |
| confirmation token | {"blindedPaymentTokens": ["+qJiMi6k0hRzRAEN239nLthLqrNm53O78x/PV8I/JS0="],<br> "publicKey": "OqhZpUC8B15u+Gc11rQYRl8O3zOSAUIEC2JuDHI32TM="} | See [security and privacy model for ad confirmations](https://github.com/brave/brave-browser/wiki/Security-and-privacy-model-for-ad-confirmations). |
| user data | {"buildChannel":"nightly",<br>"catalog":[{"id":"29e5c8bc0ba319069980bb390d8e8f9b58c05a20"}],<br>"conversion":[{"action":"view"}],<br>"createdAtTimestamp":"2020-11-18T12:00:00.000Z",<br>"httpResponseStatus":"500",<br>"platform":"windows",<br>"rotatingHash":"I6KM54gXOrWqRHyrD518LmhePLHpIk4KSgCKOl0e3sc=",<br>"segment":"sports",<br>"studies":[{"group":"GroupA","name":"BraveAds.FooStudy"},{"group":"GroupB","name":"BraveAds.BarStudy"}],<br>"versionNumber":"1.2.3.4"} | See [user data](../../user_data/README.md#brave-rewards-users). |

Please add to it!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,25 @@ std::optional<ConfirmationInfo> BuildRewardConfirmationWithoutDynamicUserData(
RewardInfo BuildReward(const ConfirmationInfo& confirmation) {
RewardInfo reward;

// TODO(tmancey): Decouple to cbr::test::GetToken.
reward.token = cbr::Token(
R"(aXZNwft34oG2JAVBnpYh/ktTOzr2gi0lKosYNczUUz6ZS9gaDTJmU2FHFps9dIq+QoDwjSjctR5v0rRn+dYo+AHScVqFAgJ5t2s4KtSyawW10gk6hfWPQw16Q0+8u5AG)");
R"(/mfTAAjHrWmAlLiEktbqNS/dxoMVdnz1esoVplQUs7yG/apAq2K6OeST6lBTKFJmOq7rV8QbY/DF2HFRMcz/JVkVTu9dLQdR595gZf/D4PvSuhgk5RcoBm3fSFGI4JQF)");

// TODO(tmancey): Decouple to cbr::test::GetBlindedToken.
reward.blinded_token =
cbr::BlindedToken(R"(Ev5JE4/9TZI/5TqyN9JWfJ1To0HBwQw2rWeAPcdjX3Q=)");
cbr::BlindedToken(R"(+qJiMi6k0hRzRAEN239nLthLqrNm53O78x/PV8I/JS0=)");

// TODO(tmancey): Decouple to cbr::test::GetUnblindedToken.
reward.unblinded_token = cbr::UnblindedToken(
R"(PLowz2WF2eGD5zfwZjk9p76HXBLDKMq/3EAZHeG/fE2XGQ48jyte+Ve50ZlasOuYL5mwA8CU2aFMlJrt3DDgC3B1+VD/uyHPfa/+bwYRrpVH5YwNSDEydVx8S4r+BYVY)");
R"(/mfTAAjHrWmAlLiEktbqNS/dxoMVdnz1esoVplQUs7yG/apAq2K6OeST6lBTKFJmOq7rV8QbY/DF2HFRMcz/JTrpqSWv/sNVO/Pi8nHDyl3CET+S2CKkMmYlXW3DgqxW)");

// TODO(tmancey): Decouple to cbr::test::GetPublicKey.
reward.public_key =
cbr::PublicKey(R"(RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk=)");
cbr::PublicKey(R"(OqhZpUC8B15u+Gc11rQYRl8O3zOSAUIEC2JuDHI32TM=)");

// TODO(tmancey): Decouple to cbr::test::GetVerificationSignature.
reward.signature =
R"(+yxJmIDobOZ5DBncIVuzjQEZfIa0+UPrSQhzA5pwEAL9qC4UW7A1H35nKAhVLehJlXnnfMVKV02StVO3fBU5CQ==)";
R"(pWHhVf6jDdMbt2tKKk3E0JJAB7J5lGnJej/Vi9/UgQpdqw9kKBgvmj4ke0R2MP2n2ynhRjM1sRVZiez0G2hpCA==)";

const std::optional<std::string> reward_credential_base64url =
BuildRewardCredential(confirmation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ TEST_F(BraveAdsRewardConfirmationUtilTest, BuildRewardCredential) {

// Assert
EXPECT_EQ(
R"(eyJzaWduYXR1cmUiOiJNem1obU8zak5rbDVNOHBGNG96ejJaVlMycVdKblJwaWJSR1B0UmJNMWY4NHhPZWhmQmVBdkxsbkpJSlVObStvNmtQMkxXYTF0TkFIT2VjRGF6dUlGUT09IiwidCI6IlBMb3d6MldGMmVHRDV6Zndaams5cDc2SFhCTERLTXEvM0VBWkhlRy9mRTJYR1E0OGp5dGUrVmU1MFpsYXNPdVlMNW13QThDVTJhRk1sSnJ0M0REZ0N3PT0ifQ==)",
R"(eyJzaWduYXR1cmUiOiIvcGR1dXhwdk5DZmUrOUorWG5xb3JnRXMxRE14WTAzRXNGZXg4S1NhczUzREh2Rm1DSlRyRW5yYVErL3lybHZyaHFXbGs5SEcyVU0rWDdVbGZZanZ4Zz09IiwidCI6Ii9tZlRBQWpIcldtQWxMaUVrdGJxTlMvZHhvTVZkbnoxZXNvVnBsUVVzN3lHL2FwQXEySzZPZVNUNmxCVEtGSm1PcTdyVjhRYlkvREYySEZSTWN6L0pRPT0ifQ==)",
reward_credential);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ TEST_F(BraveAdsRewardCredentialJsonWriterTest, WriteRewardCredential) {
ASSERT_TRUE(reward_credential);

// Assert
// TODO(tmancey): Should signature not match
// challenge_bypass_ristretto_test_constants.h
EXPECT_EQ(base::test::ParseJsonDict(
R"(
{
"signature": "XsaQ/XqKiWfeTCjFDhkyldsx0086qu6tjgJDCKo+f7kA0eA+mdf3Ae+BjPcDDQ8JfVbVQkI5ub394qdTmE2bRw==",
"t": "PLowz2WF2eGD5zfwZjk9p76HXBLDKMq/3EAZHeG/fE2XGQ48jyte+Ve50ZlasOuYL5mwA8CU2aFMlJrt3DDgCw=="
"signature": "jo8LVg8FpHtLKMakT7WcfGulrCB6ttSJpAaMcD95pDKTcdBRVf1yUDEZmhLKXWTIPDCDRCPKEx3gChO4S5GtHw==",
"t": "/mfTAAjHrWmAlLiEktbqNS/dxoMVdnz1esoVplQUs7yG/apAq2K6OeST6lBTKFJmOq7rV8QbY/DF2HFRMcz/JQ=="
})"),
base::test::ParseJsonDict(*reward_credential));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace brave_ads {

BASE_DECLARE_FEATURE(kIssuersFeature);

inline constexpr base::FeatureParam<int> kMaximumTokenIssuerPublicKeys{
inline constexpr base::FeatureParam<size_t> kMaximumTokenIssuerPublicKeys{
&kIssuersFeature, "maximum_token_issuer_public_keys", 6};

} // namespace brave_ads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ TEST(BraveAdsIssuersFeatureTest, MaximumTokenIssuerPublicKeys) {
kIssuersFeature, {{"maximum_token_issuer_public_keys", "1"}});

// Act & Assert
EXPECT_EQ(1, kMaximumTokenIssuerPublicKeys.Get());
EXPECT_EQ(1U, kMaximumTokenIssuerPublicKeys.Get());
}

TEST(BraveAdsIssuersFeatureTest, DefaultMaximumTokenIssuerPublicKeys) {
// Act & Assert
EXPECT_EQ(6, kMaximumTokenIssuerPublicKeys.Get());
EXPECT_EQ(6U, kMaximumTokenIssuerPublicKeys.Get());
}

TEST(BraveAdsIssuersFeatureTest,
Expand All @@ -48,7 +48,7 @@ TEST(BraveAdsIssuersFeatureTest,
scoped_feature_list.InitAndDisableFeature(kIssuersFeature);

// Act & Assert
EXPECT_EQ(6, kMaximumTokenIssuerPublicKeys.Get());
EXPECT_EQ(6U, kMaximumTokenIssuerPublicKeys.Get());
}

} // namespace brave_ads
Loading

0 comments on commit 963d499

Please sign in to comment.