File: auction_nonce_manager_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (106 lines) | stat: -rw-r--r-- 3,947 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/interest_group/auction_nonce_manager.h"

#include <memory>
#include <string>

#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/uuid.h"
#include "content/public/test/browser_test_utils.h"
#include "content/test/test_render_frame_host.h"
#include "content/test/test_render_view_host.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace content {

class AuctionNonceManagerTest : public RenderViewHostImplTestHarness {
 public:
  void SetUp() override {
    RenderViewHostImplTestHarness::SetUp();
    NavigateAndCommit(GURL("https://test.org"));
    nonce_manager_ = std::make_unique<AuctionNonceManager>(main_test_rfh());
  }

  void TearDown() override {
    nonce_manager_.reset();
    RenderViewHostImplTestHarness::TearDown();
  }

  // Simpler version of what NavigatorAuction::createAuctionNonce does, which
  // is to take an AuctionNonce and change the last few digits to make it
  // unique.
  AuctionNonce CreateAuctionNonce(uint8_t suffix) {
    base::Uuid base_auction_nonce = main_test_rfh()->GetBaseAuctionNonce();
    std::string nonce = base::StringPrintf(
        "%s%02x", base_auction_nonce.AsLowercaseString().substr(0, 34).c_str(),
        suffix);
    return AuctionNonce(base::Uuid::ParseLowercase(nonce));
  }

  AuctionNonceManager& nonce_manager() { return *nonce_manager_; }

 private:
  std::unique_ptr<AuctionNonceManager> nonce_manager_;
};

TEST_F(AuctionNonceManagerTest, AuctionNonceIsAvailable) {
  AuctionNonce nonce1 = CreateAuctionNonce(1);
  EXPECT_TRUE(nonce_manager().ClaimAuctionNonceIfAvailable(nonce1));

  AuctionNonce nonce2 = CreateAuctionNonce(2);
  EXPECT_TRUE(nonce_manager().ClaimAuctionNonceIfAvailable(nonce2));
}

TEST_F(AuctionNonceManagerTest, AuctionNonceCanOnlyBeClaimedOnce) {
  AuctionNonce nonce = CreateAuctionNonce(1);
  nonce_manager().ClaimAuctionNonceIfAvailable(nonce);
  EXPECT_FALSE(nonce_manager().ClaimAuctionNonceIfAvailable(nonce));

  // Monitor the console errors.
  WebContentsConsoleObserver console_observer(web_contents());
  console_observer.SetFilter(base::BindLambdaForTesting(
      [](const content::WebContentsConsoleObserver::Message& msg) {
        return msg.log_level == blink::mojom::ConsoleMessageLevel::kError;
      }));
  console_observer.SetPattern(
      "Invalid AuctionConfig. The config provided an auctionNonce value "
      "that was _not_ created by a previous call to createAuctionNonce.");

  EXPECT_FALSE(nonce_manager().ClaimAuctionNonceIfAvailable(
      static_cast<AuctionNonce>(base::Uuid::GenerateRandomV4())));

  // Verify the expected error is logged to the console.
  ASSERT_TRUE(console_observer.Wait());
  EXPECT_EQ(console_observer.messages().size(), 1u);
}

TEST_F(AuctionNonceManagerTest, AuctionNonceIsNotAvailable) {
  AuctionNonce nonce(
      base::Uuid::ParseLowercase("a4245d17-4287-47c2-9c1a-1809cf150f76"));

  // Monitor the console errors.
  WebContentsConsoleObserver console_observer(web_contents());
  console_observer.SetFilter(base::BindLambdaForTesting(
      [](const content::WebContentsConsoleObserver::Message& msg) {
        return msg.log_level == blink::mojom::ConsoleMessageLevel::kError;
      }));
  console_observer.SetPattern(
      "Invalid AuctionConfig. The config provided an auctionNonce value "
      "that was _not_ created by a previous call to createAuctionNonce.");

  EXPECT_FALSE(nonce_manager().ClaimAuctionNonceIfAvailable(
      static_cast<AuctionNonce>(base::Uuid::GenerateRandomV4())));

  // Verify the expected error is logged to the console.
  ASSERT_TRUE(console_observer.Wait());
  EXPECT_EQ(console_observer.messages().size(), 1u);
}

}  // namespace content