File: client_storage_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (85 lines) | stat: -rw-r--r-- 3,098 bytes parent folder | download | duplicates (8)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/policy/test_support/client_storage.h"

#include <string_view>

#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace policy {

namespace {

constexpr const char kDeviceId1[] = "1";
constexpr const char kDeviceId2[] = "2";
constexpr const char kStateKey1[] = "bbb";
constexpr const char kStateKey2[] = "ggg";
constexpr const char kStateKey3[] = "fff";
constexpr const char kStateKey4[] = "ccc";
constexpr const char kDeviceToken[] = "device-token";
constexpr const char kNonExistingDeviceToken[] = "non-existing-device-token";
constexpr const uint64_t kModulus = 3;
constexpr const uint64_t kRemainder = 2;
// Following SHA256 hashes produce |kRemainder| when divided by |kModulus|.
constexpr std::string_view kSHA256HashForStateKey1(
    "\x3e\x74\x4b\x9d\xc3\x93\x89\xba\xf0\xc5\xa0\x66\x05\x89\xb8\x40\x2f\x3d"
    "\xbb\x49\xb8\x9b\x3e\x75\xf2\xc9\x35\x58\x52\xa3\xc6\x77",
    32);
constexpr std::string_view kSHA256HashForStateKey4(
    "\x64\xda\xa4\x4a\xd4\x93\xff\x28\xa9\x6e\xff\xab\x6e\x77\xf1\x73\x2a\x3d"
    "\x97\xd8\x32\x41\x58\x1b\x37\xdb\xd7\x0a\x7a\x49\x00\xfe",
    32);

void RegisterClient(const std::string& device_token,
                    ClientStorage* client_storage) {
  ClientStorage::ClientInfo client_info;
  client_info.device_id = kDeviceId1;
  client_info.device_token = device_token;

  client_storage->RegisterClient(client_info);
  ASSERT_EQ(client_storage->GetNumberOfRegisteredClients(), 1u);
  ASSERT_EQ(client_storage->GetClient(kDeviceId1).device_token, device_token);
}

}  // namespace

TEST(ClientStorageTest, Unregister_Success) {
  ClientStorage client_storage;
  RegisterClient(kDeviceToken, &client_storage);

  ASSERT_TRUE(client_storage.DeleteClient(kDeviceToken));
  EXPECT_EQ(client_storage.GetNumberOfRegisteredClients(), 0u);
}

TEST(ClientStorageTest, Unregister_NonExistingClient) {
  ClientStorage client_storage;
  RegisterClient(kDeviceToken, &client_storage);

  ASSERT_FALSE(client_storage.DeleteClient(kNonExistingDeviceToken));
  ASSERT_EQ(client_storage.GetNumberOfRegisteredClients(), 1u);
  EXPECT_EQ(client_storage.GetClient(kDeviceId1).device_token, kDeviceToken);
}

TEST(ClientStorageTest, GetMatchingStateKeyHashes) {
  ClientStorage client_storage;
  ClientStorage::ClientInfo client_info1;
  client_info1.device_id = kDeviceId1;
  client_info1.state_keys = {kStateKey1, kStateKey2};
  client_storage.RegisterClient(client_info1);
  ClientStorage::ClientInfo client_info2;
  client_info2.device_id = kDeviceId2;
  client_info2.state_keys = {kStateKey3, kStateKey4};
  client_storage.RegisterClient(client_info2);

  std::vector<std::string> matching_hashes =
      client_storage.GetMatchingStateKeyHashes(kModulus, kRemainder);

  EXPECT_THAT(matching_hashes,
              testing::UnorderedElementsAreArray(
                  {kSHA256HashForStateKey1, kSHA256HashForStateKey4}));
}

}  // namespace policy