client_storage_unittest.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/policy/test_support/client_storage.h"
  5. #include "base/strings/string_piece.h"
  6. #include "testing/gmock/include/gmock/gmock.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace policy {
  9. namespace {
  10. constexpr const char kDeviceId1[] = "1";
  11. constexpr const char kDeviceId2[] = "2";
  12. constexpr const char kStateKey1[] = "bbb";
  13. constexpr const char kStateKey2[] = "ggg";
  14. constexpr const char kStateKey3[] = "fff";
  15. constexpr const char kStateKey4[] = "ccc";
  16. constexpr const char kDeviceToken[] = "device-token";
  17. constexpr const char kNonExistingDeviceToken[] = "non-existing-device-token";
  18. constexpr const uint64_t kModulus = 3;
  19. constexpr const uint64_t kRemainder = 2;
  20. // Following SHA256 hashes produce |kRemainder| when divided by |kModulus|.
  21. constexpr base::StringPiece kSHA256HashForStateKey1(
  22. "\x3e\x74\x4b\x9d\xc3\x93\x89\xba\xf0\xc5\xa0\x66\x05\x89\xb8\x40\x2f\x3d"
  23. "\xbb\x49\xb8\x9b\x3e\x75\xf2\xc9\x35\x58\x52\xa3\xc6\x77",
  24. 32);
  25. constexpr base::StringPiece kSHA256HashForStateKey4(
  26. "\x64\xda\xa4\x4a\xd4\x93\xff\x28\xa9\x6e\xff\xab\x6e\x77\xf1\x73\x2a\x3d"
  27. "\x97\xd8\x32\x41\x58\x1b\x37\xdb\xd7\x0a\x7a\x49\x00\xfe",
  28. 32);
  29. void RegisterClient(const std::string& device_token,
  30. ClientStorage* client_storage) {
  31. ClientStorage::ClientInfo client_info;
  32. client_info.device_id = kDeviceId1;
  33. client_info.device_token = device_token;
  34. client_storage->RegisterClient(client_info);
  35. ASSERT_EQ(client_storage->GetNumberOfRegisteredClients(), 1u);
  36. ASSERT_EQ(client_storage->GetClient(kDeviceId1).device_token, device_token);
  37. }
  38. } // namespace
  39. TEST(ClientStorageTest, Unregister_Success) {
  40. ClientStorage client_storage;
  41. RegisterClient(kDeviceToken, &client_storage);
  42. ASSERT_TRUE(client_storage.DeleteClient(kDeviceToken));
  43. EXPECT_EQ(client_storage.GetNumberOfRegisteredClients(), 0u);
  44. }
  45. TEST(ClientStorageTest, Unregister_NonExistingClient) {
  46. ClientStorage client_storage;
  47. RegisterClient(kDeviceToken, &client_storage);
  48. ASSERT_FALSE(client_storage.DeleteClient(kNonExistingDeviceToken));
  49. ASSERT_EQ(client_storage.GetNumberOfRegisteredClients(), 1u);
  50. EXPECT_EQ(client_storage.GetClient(kDeviceId1).device_token, kDeviceToken);
  51. }
  52. TEST(ClientStorageTest, GetMatchingStateKeyHashes) {
  53. ClientStorage client_storage;
  54. ClientStorage::ClientInfo client_info1;
  55. client_info1.device_id = kDeviceId1;
  56. client_info1.state_keys = {kStateKey1, kStateKey2};
  57. client_storage.RegisterClient(client_info1);
  58. ClientStorage::ClientInfo client_info2;
  59. client_info2.device_id = kDeviceId2;
  60. client_info2.state_keys = {kStateKey3, kStateKey4};
  61. client_storage.RegisterClient(client_info2);
  62. std::vector<std::string> matching_hashes =
  63. client_storage.GetMatchingStateKeyHashes(kModulus, kRemainder);
  64. EXPECT_THAT(matching_hashes,
  65. testing::UnorderedElementsAreArray(
  66. {kSHA256HashForStateKey1, kSHA256HashForStateKey4}));
  67. }
  68. } // namespace policy