OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/cryptauth/background_eid_generator.h" | 5 #include "components/cryptauth/background_eid_generator.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 BeaconSeed CreateBeaconSeed(const std::string& data, | 41 BeaconSeed CreateBeaconSeed(const std::string& data, |
42 const int64_t start_timestamp_ms, | 42 const int64_t start_timestamp_ms, |
43 const int64_t end_timestamp_ms) { | 43 const int64_t end_timestamp_ms) { |
44 BeaconSeed seed; | 44 BeaconSeed seed; |
45 seed.set_data(data); | 45 seed.set_data(data); |
46 seed.set_start_time_millis(start_timestamp_ms); | 46 seed.set_start_time_millis(start_timestamp_ms); |
47 seed.set_end_time_millis(end_timestamp_ms); | 47 seed.set_end_time_millis(end_timestamp_ms); |
48 return seed; | 48 return seed; |
49 } | 49 } |
50 | 50 |
51 std::string CreateEid(const std::string& eid_seed, | 51 DataWithTimestamp CreateEid(const std::string& eid_seed, |
52 int64_t start_of_period_timestamp_ms) { | 52 int64_t start_of_period_timestamp_ms) { |
53 return eid_seed + "|" + std::to_string(start_of_period_timestamp_ms); | 53 std::string data = |
| 54 eid_seed + "|" + std::to_string(start_of_period_timestamp_ms); |
| 55 return DataWithTimestamp(data, start_of_period_timestamp_ms, |
| 56 start_of_period_timestamp_ms + kEidPeriodMs); |
54 } | 57 } |
55 | 58 |
56 class TestRawEidGenerator : public RawEidGenerator { | 59 class TestRawEidGenerator : public RawEidGenerator { |
57 public: | 60 public: |
58 TestRawEidGenerator() {} | 61 TestRawEidGenerator() {} |
59 ~TestRawEidGenerator() override {} | 62 ~TestRawEidGenerator() override {} |
60 | 63 |
61 // RawEidGenerator: | 64 // RawEidGenerator: |
62 std::string GenerateEid(const std::string& eid_seed, | 65 std::string GenerateEid(const std::string& eid_seed, |
63 int64_t start_of_period_timestamp_ms, | 66 int64_t start_of_period_timestamp_ms, |
64 std::string const* extra_entropy) override { | 67 std::string const* extra_entropy) override { |
65 // ASSERT_FALSE(extra_entropy); | 68 EXPECT_FALSE(extra_entropy); |
66 return CreateEid(eid_seed, start_of_period_timestamp_ms); | 69 return CreateEid(eid_seed, start_of_period_timestamp_ms).data; |
67 } | 70 } |
68 | 71 |
69 private: | 72 private: |
70 DISALLOW_COPY_AND_ASSIGN(TestRawEidGenerator); | 73 DISALLOW_COPY_AND_ASSIGN(TestRawEidGenerator); |
71 }; | 74 }; |
72 | 75 |
73 } // namespace | 76 } // namespace |
74 | 77 |
75 class CryptAuthBackgroundEidGeneratorTest : public testing::Test { | 78 class CryptAuthBackgroundEidGeneratorTest : public testing::Test { |
76 protected: | 79 protected: |
(...skipping 27 matching lines...) Expand all Loading... |
104 } | 107 } |
105 | 108 |
106 std::unique_ptr<BackgroundEidGenerator> eid_generator_; | 109 std::unique_ptr<BackgroundEidGenerator> eid_generator_; |
107 base::SimpleTestClock* test_clock_; | 110 base::SimpleTestClock* test_clock_; |
108 std::vector<BeaconSeed> beacon_seeds_; | 111 std::vector<BeaconSeed> beacon_seeds_; |
109 }; | 112 }; |
110 | 113 |
111 TEST_F(CryptAuthBackgroundEidGeneratorTest, BeaconSeedsExpired) { | 114 TEST_F(CryptAuthBackgroundEidGeneratorTest, BeaconSeedsExpired) { |
112 SetTestTime(beacon_seeds_[beacon_seeds_.size() - 1].end_time_millis() + | 115 SetTestTime(beacon_seeds_[beacon_seeds_.size() - 1].end_time_millis() + |
113 kEidCount * kEidPeriodMs); | 116 kEidCount * kEidPeriodMs); |
114 std::vector<std::string> eids = | 117 std::vector<DataWithTimestamp> eids = |
115 eid_generator_->GenerateNearestEids(beacon_seeds_); | 118 eid_generator_->GenerateNearestEids(beacon_seeds_); |
116 EXPECT_EQ(0u, eids.size()); | 119 EXPECT_EQ(0u, eids.size()); |
117 } | 120 } |
118 | 121 |
119 TEST_F(CryptAuthBackgroundEidGeneratorTest, BeaconSeedsValidInFuture) { | 122 TEST_F(CryptAuthBackgroundEidGeneratorTest, BeaconSeedsValidInFuture) { |
120 SetTestTime(beacon_seeds_[0].start_time_millis() - kEidCount * kEidPeriodMs); | 123 SetTestTime(beacon_seeds_[0].start_time_millis() - kEidCount * kEidPeriodMs); |
121 std::vector<std::string> eids = | 124 std::vector<DataWithTimestamp> eids = |
122 eid_generator_->GenerateNearestEids(beacon_seeds_); | 125 eid_generator_->GenerateNearestEids(beacon_seeds_); |
123 EXPECT_EQ(0u, eids.size()); | 126 EXPECT_EQ(0u, eids.size()); |
124 } | 127 } |
125 | 128 |
126 TEST_F(CryptAuthBackgroundEidGeneratorTest, EidsUseSameBeaconSeed) { | 129 TEST_F(CryptAuthBackgroundEidGeneratorTest, EidsUseSameBeaconSeed) { |
127 int64_t start_period_ms = | 130 int64_t start_period_ms = |
128 beacon_seeds_[0].start_time_millis() + kEidCount * kEidPeriodMs; | 131 beacon_seeds_[0].start_time_millis() + kEidCount * kEidPeriodMs; |
129 SetTestTime(start_period_ms + kEidPeriodMs / 2); | 132 SetTestTime(start_period_ms + kEidPeriodMs / 2); |
130 | 133 |
131 std::vector<std::string> eids = | 134 std::vector<DataWithTimestamp> eids = |
132 eid_generator_->GenerateNearestEids(beacon_seeds_); | 135 eid_generator_->GenerateNearestEids(beacon_seeds_); |
133 | 136 |
134 std::string seed = beacon_seeds_[0].data(); | 137 std::string seed = beacon_seeds_[0].data(); |
135 EXPECT_EQ(kEidCount, eids.size()); | 138 EXPECT_EQ(kEidCount, eids.size()); |
136 EXPECT_EQ(CreateEid(seed, start_period_ms - 2 * kEidPeriodMs), eids[0]); | 139 EXPECT_EQ(CreateEid(seed, start_period_ms - 2 * kEidPeriodMs), eids[0]); |
137 EXPECT_EQ(CreateEid(seed, start_period_ms - 1 * kEidPeriodMs), eids[1]); | 140 EXPECT_EQ(CreateEid(seed, start_period_ms - 1 * kEidPeriodMs), eids[1]); |
138 EXPECT_EQ(CreateEid(seed, start_period_ms + 0 * kEidPeriodMs), eids[2]); | 141 EXPECT_EQ(CreateEid(seed, start_period_ms + 0 * kEidPeriodMs), eids[2]); |
139 EXPECT_EQ(CreateEid(seed, start_period_ms + 1 * kEidPeriodMs), eids[3]); | 142 EXPECT_EQ(CreateEid(seed, start_period_ms + 1 * kEidPeriodMs), eids[3]); |
140 EXPECT_EQ(CreateEid(seed, start_period_ms + 2 * kEidPeriodMs), eids[4]); | 143 EXPECT_EQ(CreateEid(seed, start_period_ms + 2 * kEidPeriodMs), eids[4]); |
141 } | 144 } |
142 | 145 |
143 TEST_F(CryptAuthBackgroundEidGeneratorTest, EidsAcrossBeaconSeeds) { | 146 TEST_F(CryptAuthBackgroundEidGeneratorTest, EidsAcrossBeaconSeeds) { |
144 int64_t end_period_ms = beacon_seeds_[0].end_time_millis(); | 147 int64_t end_period_ms = beacon_seeds_[0].end_time_millis(); |
145 int64_t start_period_ms = beacon_seeds_[1].start_time_millis(); | 148 int64_t start_period_ms = beacon_seeds_[1].start_time_millis(); |
146 SetTestTime(start_period_ms + kEidPeriodMs / 2); | 149 SetTestTime(start_period_ms + kEidPeriodMs / 2); |
147 | 150 |
148 std::vector<std::string> eids = | 151 std::vector<DataWithTimestamp> eids = |
149 eid_generator_->GenerateNearestEids(beacon_seeds_); | 152 eid_generator_->GenerateNearestEids(beacon_seeds_); |
150 | 153 |
151 std::string seed0 = beacon_seeds_[0].data(); | 154 std::string seed0 = beacon_seeds_[0].data(); |
152 std::string seed1 = beacon_seeds_[1].data(); | 155 std::string seed1 = beacon_seeds_[1].data(); |
153 EXPECT_EQ(kEidCount, eids.size()); | 156 EXPECT_EQ(kEidCount, eids.size()); |
154 EXPECT_EQ(CreateEid(seed0, end_period_ms - 2 * kEidPeriodMs), eids[0]); | 157 EXPECT_EQ(CreateEid(seed0, end_period_ms - 2 * kEidPeriodMs), eids[0]); |
155 EXPECT_EQ(CreateEid(seed0, end_period_ms - 1 * kEidPeriodMs), eids[1]); | 158 EXPECT_EQ(CreateEid(seed0, end_period_ms - 1 * kEidPeriodMs), eids[1]); |
156 EXPECT_EQ(CreateEid(seed1, start_period_ms + 0 * kEidPeriodMs), eids[2]); | 159 EXPECT_EQ(CreateEid(seed1, start_period_ms + 0 * kEidPeriodMs), eids[2]); |
157 EXPECT_EQ(CreateEid(seed1, start_period_ms + 1 * kEidPeriodMs), eids[3]); | 160 EXPECT_EQ(CreateEid(seed1, start_period_ms + 1 * kEidPeriodMs), eids[3]); |
158 EXPECT_EQ(CreateEid(seed1, start_period_ms + 2 * kEidPeriodMs), eids[4]); | 161 EXPECT_EQ(CreateEid(seed1, start_period_ms + 2 * kEidPeriodMs), eids[4]); |
159 } | 162 } |
160 | 163 |
161 TEST_F(CryptAuthBackgroundEidGeneratorTest, CurrentTimeAtStartOfRange) { | 164 TEST_F(CryptAuthBackgroundEidGeneratorTest, CurrentTimeAtStartOfRange) { |
162 int64_t start_period_ms = beacon_seeds_[0].start_time_millis(); | 165 int64_t start_period_ms = beacon_seeds_[0].start_time_millis(); |
163 SetTestTime(start_period_ms + kEidPeriodMs / 2); | 166 SetTestTime(start_period_ms + kEidPeriodMs / 2); |
164 | 167 |
165 std::vector<std::string> eids = | 168 std::vector<DataWithTimestamp> eids = |
166 eid_generator_->GenerateNearestEids(beacon_seeds_); | 169 eid_generator_->GenerateNearestEids(beacon_seeds_); |
167 | 170 |
168 std::string seed = beacon_seeds_[0].data(); | 171 std::string seed = beacon_seeds_[0].data(); |
169 EXPECT_EQ(3u, eids.size()); | 172 EXPECT_EQ(3u, eids.size()); |
170 EXPECT_EQ(CreateEid(seed, start_period_ms + 0 * kEidPeriodMs), eids[0]); | 173 EXPECT_EQ(CreateEid(seed, start_period_ms + 0 * kEidPeriodMs), eids[0]); |
171 EXPECT_EQ(CreateEid(seed, start_period_ms + 1 * kEidPeriodMs), eids[1]); | 174 EXPECT_EQ(CreateEid(seed, start_period_ms + 1 * kEidPeriodMs), eids[1]); |
172 EXPECT_EQ(CreateEid(seed, start_period_ms + 2 * kEidPeriodMs), eids[2]); | 175 EXPECT_EQ(CreateEid(seed, start_period_ms + 2 * kEidPeriodMs), eids[2]); |
173 } | 176 } |
174 | 177 |
175 TEST_F(CryptAuthBackgroundEidGeneratorTest, CurrentTimeAtEndOfRange) { | 178 TEST_F(CryptAuthBackgroundEidGeneratorTest, CurrentTimeAtEndOfRange) { |
176 int64_t start_period_ms = beacon_seeds_[3].end_time_millis() - kEidPeriodMs; | 179 int64_t start_period_ms = beacon_seeds_[3].end_time_millis() - kEidPeriodMs; |
177 SetTestTime(start_period_ms + kEidPeriodMs / 2); | 180 SetTestTime(start_period_ms + kEidPeriodMs / 2); |
178 | 181 |
179 std::vector<std::string> eids = | 182 std::vector<DataWithTimestamp> eids = |
180 eid_generator_->GenerateNearestEids(beacon_seeds_); | 183 eid_generator_->GenerateNearestEids(beacon_seeds_); |
181 | 184 |
182 std::string seed = beacon_seeds_[3].data(); | 185 std::string seed = beacon_seeds_[3].data(); |
183 EXPECT_EQ(3u, eids.size()); | 186 EXPECT_EQ(3u, eids.size()); |
184 EXPECT_EQ(CreateEid(seed, start_period_ms - 2 * kEidPeriodMs), eids[0]); | 187 EXPECT_EQ(CreateEid(seed, start_period_ms - 2 * kEidPeriodMs), eids[0]); |
185 EXPECT_EQ(CreateEid(seed, start_period_ms - 1 * kEidPeriodMs), eids[1]); | 188 EXPECT_EQ(CreateEid(seed, start_period_ms - 1 * kEidPeriodMs), eids[1]); |
186 EXPECT_EQ(CreateEid(seed, start_period_ms - 0 * kEidPeriodMs), eids[2]); | 189 EXPECT_EQ(CreateEid(seed, start_period_ms - 0 * kEidPeriodMs), eids[2]); |
187 } | 190 } |
188 | 191 |
189 } // namespace cryptauth | 192 } // namespace cryptauth |
OLD | NEW |