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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
ash / components / kcer / token_key_finder_unittest.cc [blame]
// 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 "ash/components/kcer/token_key_finder.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "base/timer/timer.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace kcer::internal {
namespace {
class KcerTokenKeyFinderTest : public ::testing::Test {
protected:
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
};
// Test that TokenKeyFinder can collect one "key found" result which is returned
// synchronously.
TEST_F(KcerTokenKeyFinderTest, OneKeyFoundResultSync) {
base::test::TestFuture<base::expected<std::optional<Token>, Error>>
result_waiter;
auto finder = TokenKeyFinder::Create(
/*results_to_receive=*/1, result_waiter.GetCallback());
auto finder_callback = finder->GetCallback(Token::kDevice);
finder.reset(); // It should be ok to delete the original ref-pointer now.
EXPECT_FALSE(result_waiter.IsReady());
std::move(finder_callback).Run(/*key_exists=*/true);
EXPECT_TRUE(result_waiter.IsReady());
ASSERT_TRUE(result_waiter.Get().has_value());
EXPECT_EQ(result_waiter.Get().value(), Token::kDevice);
}
// Test that TokenKeyFinder can collect one "key found" result which is returned
// asynchronously.
TEST_F(KcerTokenKeyFinderTest, OneKeyFoundResultAsync) {
base::test::TestFuture<base::expected<std::optional<Token>, Error>>
result_waiter;
auto finder = TokenKeyFinder::Create(
/*results_to_receive=*/1, result_waiter.GetCallback());
base::OneShotTimer fake_token; // Runs a callback after a delay.
fake_token.Start(FROM_HERE, base::Seconds(10),
base::BindOnce(finder->GetCallback(Token::kDevice),
/*key_exists=*/true));
finder.reset(); // It should be ok to delete the original ref-pointer now.
EXPECT_FALSE(result_waiter.IsReady());
task_environment_.FastForwardBy(base::Seconds(5));
EXPECT_FALSE(result_waiter.IsReady());
task_environment_.FastForwardBy(base::Seconds(6));
EXPECT_TRUE(result_waiter.IsReady());
ASSERT_TRUE(result_waiter.Get().has_value());
EXPECT_EQ(result_waiter.Get().value(), Token::kDevice);
}
// Test that TokenKeyFinder can collect one "key not found" result.
TEST_F(KcerTokenKeyFinderTest, OneKeyNotFoundResult) {
base::test::TestFuture<base::expected<std::optional<Token>, Error>>
result_waiter;
auto finder = TokenKeyFinder::Create(
/*results_to_receive=*/1, result_waiter.GetCallback());
base::OneShotTimer fake_token; // Runs a callback after a delay.
fake_token.Start(FROM_HERE, base::Seconds(10),
base::BindOnce(finder->GetCallback(Token::kDevice),
/*key_exists=*/false));
finder.reset(); // It should be ok to delete the original ref-pointer now.
EXPECT_FALSE(result_waiter.IsReady());
task_environment_.FastForwardBy(base::Seconds(11));
EXPECT_TRUE(result_waiter.IsReady());
ASSERT_TRUE(result_waiter.Get().has_value());
EXPECT_FALSE(result_waiter.Get().value().has_value());
}
// Test that TokenKeyFinder can collect one failure result.
TEST_F(KcerTokenKeyFinderTest, OneFailureResult) {
base::test::TestFuture<base::expected<std::optional<Token>, Error>>
result_waiter;
auto finder = TokenKeyFinder::Create(
/*results_to_receive=*/1, result_waiter.GetCallback());
base::OneShotTimer fake_token; // Runs a callback after a delay.
fake_token.Start(FROM_HERE, base::Seconds(10),
base::BindOnce(finder->GetCallback(Token::kUser),
/*key_exists=*/base::unexpected(
Error::kTokenInitializationFailed)));
finder.reset(); // It should be ok to delete the original ref-pointer now.
EXPECT_FALSE(result_waiter.IsReady());
task_environment_.FastForwardBy(base::Seconds(11));
EXPECT_TRUE(result_waiter.IsReady());
ASSERT_FALSE(result_waiter.Get().has_value());
EXPECT_EQ(result_waiter.Get().error(), Error::kTokenInitializationFailed);
}
// Test that TokenKeyFinder indicates when a key is found when it's found on
// both tokens (not supposed to happen, but still a theoretically possible edge
// case).
TEST_F(KcerTokenKeyFinderTest, TwoKeysFound) {
base::test::TestFuture<base::expected<std::optional<Token>, Error>>
result_waiter;
auto finder = TokenKeyFinder::Create(
/*results_to_receive=*/2, result_waiter.GetCallback());
base::OneShotTimer fake_token_1; // Runs a callback after a delay.
fake_token_1.Start(FROM_HERE, base::Seconds(3),
base::BindOnce(finder->GetCallback(Token::kUser),
/*key_exists=*/true));
base::OneShotTimer fake_token_2; // Runs a callback after a delay.
fake_token_2.Start(FROM_HERE, base::Seconds(5),
base::BindOnce(finder->GetCallback(Token::kDevice),
/*key_exists=*/true));
finder.reset(); // It should be ok to delete the original ref-pointer now.
EXPECT_FALSE(result_waiter.IsReady());
task_environment_.FastForwardBy(base::Seconds(6));
EXPECT_TRUE(result_waiter.IsReady());
ASSERT_TRUE(result_waiter.Get().has_value());
EXPECT_TRUE(result_waiter.Get().value().has_value());
}
// Test that TokenKeyFinder indicates that the key was not found when it was not
// found on both tokens.
TEST_F(KcerTokenKeyFinderTest, TwoTokensKeyNotFound) {
base::test::TestFuture<base::expected<std::optional<Token>, Error>>
result_waiter;
auto finder = TokenKeyFinder::Create(
/*results_to_receive=*/2, result_waiter.GetCallback());
base::OneShotTimer fake_token_1; // Runs a callback after a delay.
fake_token_1.Start(FROM_HERE, base::Seconds(3),
base::BindOnce(finder->GetCallback(Token::kUser),
/*key_exists=*/false));
base::OneShotTimer fake_token_2; // Runs a callback after a delay.
fake_token_2.Start(FROM_HERE, base::Seconds(5),
base::BindOnce(finder->GetCallback(Token::kDevice),
/*key_exists=*/false));
finder.reset(); // It should be ok to delete the original ref-pointer now.
EXPECT_FALSE(result_waiter.IsReady());
task_environment_.FastForwardBy(base::Seconds(6));
EXPECT_TRUE(result_waiter.IsReady());
ASSERT_TRUE(result_waiter.Get().has_value());
EXPECT_FALSE(result_waiter.Get().value().has_value());
}
// Test that TokenKeyFinder indicates that the key was found when it was found
// on one of the tokens.
TEST_F(KcerTokenKeyFinderTest, TwoTokensOneKeyFound) {
base::test::TestFuture<base::expected<std::optional<Token>, Error>>
result_waiter;
auto finder = TokenKeyFinder::Create(
/*results_to_receive=*/2, result_waiter.GetCallback());
base::OneShotTimer fake_token_1; // Runs a callback after a delay.
fake_token_1.Start(FROM_HERE, base::Seconds(10),
base::BindOnce(finder->GetCallback(Token::kUser),
/*key_exists=*/false));
base::OneShotTimer fake_token_2; // Runs a callback after a delay.
fake_token_2.Start(FROM_HERE, base::Seconds(20),
base::BindOnce(finder->GetCallback(Token::kDevice),
/*key_exists=*/true));
finder.reset(); // It should be ok to delete the original ref-pointer now.
EXPECT_FALSE(result_waiter.IsReady());
task_environment_.FastForwardBy(base::Seconds(15));
EXPECT_FALSE(result_waiter.IsReady());
task_environment_.FastForwardBy(base::Seconds(7));
EXPECT_TRUE(result_waiter.IsReady());
ASSERT_TRUE(result_waiter.Get().has_value());
EXPECT_EQ(result_waiter.Get().value(), Token::kDevice);
}
// Test that TokenKeyFinder returns an error when the key is not found on one
// token and another one returned an error.
TEST_F(KcerTokenKeyFinderTest, OneErrorOneNotFound) {
base::test::TestFuture<base::expected<std::optional<Token>, Error>>
result_waiter;
auto finder = TokenKeyFinder::Create(
/*results_to_receive=*/2, result_waiter.GetCallback());
base::OneShotTimer fake_token_1; // Runs a callback after a delay.
fake_token_1.Start(FROM_HERE, base::Seconds(3),
base::BindOnce(finder->GetCallback(Token::kUser),
/*key_exists=*/base::unexpected(
Error::kTokenInitializationFailed)));
base::OneShotTimer fake_token_2; // Runs a callback after a delay.
fake_token_2.Start(FROM_HERE, base::Seconds(4),
base::BindOnce(finder->GetCallback(Token::kDevice),
/*key_exists=*/false));
finder.reset(); // It should be ok to delete the original ref-pointer now.
EXPECT_FALSE(result_waiter.IsReady());
task_environment_.FastForwardBy(base::Seconds(7));
EXPECT_TRUE(result_waiter.IsReady());
ASSERT_FALSE(result_waiter.Get().has_value());
EXPECT_EQ(result_waiter.Get().error(), Error::kTokenInitializationFailed);
}
} // namespace
} // namespace kcer::internal