OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/gcm_driver/crypto/gcm_key_store.h" | 5 #include "components/gcm_driver/crypto/gcm_key_store.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/test/histogram_tester.h" |
12 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
13 #include "components/gcm_driver/crypto/p256_key_util.h" | 14 #include "components/gcm_driver/crypto/p256_key_util.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace gcm { | 17 namespace gcm { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 const char kFakeAppId[] = "my_app_id"; | 21 const char kFakeAppId[] = "my_app_id"; |
21 const char kSecondFakeAppId[] = "my_other_app_id"; | 22 const char kSecondFakeAppId[] = "my_other_app_id"; |
(...skipping 25 matching lines...) Expand all Loading... |
47 | 48 |
48 // Callback to use with GCMKeyStore::{GetKeys, CreateKeys} calls. | 49 // Callback to use with GCMKeyStore::{GetKeys, CreateKeys} calls. |
49 void GotKeys(KeyPair* pair_out, std::string* auth_secret_out, | 50 void GotKeys(KeyPair* pair_out, std::string* auth_secret_out, |
50 const KeyPair& pair, const std::string& auth_secret) { | 51 const KeyPair& pair, const std::string& auth_secret) { |
51 *pair_out = pair; | 52 *pair_out = pair; |
52 *auth_secret_out = auth_secret; | 53 *auth_secret_out = auth_secret; |
53 } | 54 } |
54 | 55 |
55 protected: | 56 protected: |
56 GCMKeyStore* gcm_key_store() { return gcm_key_store_.get(); } | 57 GCMKeyStore* gcm_key_store() { return gcm_key_store_.get(); } |
| 58 base::HistogramTester* histogram_tester() { return &histogram_tester_; } |
57 | 59 |
58 private: | 60 private: |
59 base::MessageLoop message_loop_; | 61 base::MessageLoop message_loop_; |
60 base::ScopedTempDir scoped_temp_dir_; | 62 base::ScopedTempDir scoped_temp_dir_; |
| 63 base::HistogramTester histogram_tester_; |
61 | 64 |
62 scoped_ptr<GCMKeyStore> gcm_key_store_; | 65 scoped_ptr<GCMKeyStore> gcm_key_store_; |
63 }; | 66 }; |
64 | 67 |
65 TEST_F(GCMKeyStoreTest, CreatedByDefault) { | 68 TEST_F(GCMKeyStoreTest, EmptyByDefault) { |
| 69 // The key store is initialized lazily, so this histogram confirms that |
| 70 // calling the constructor does not in fact cause initialization. |
| 71 histogram_tester()->ExpectTotalCount( |
| 72 "GCM.Crypto.InitKeyStoreSuccessRate", 0); |
| 73 |
66 KeyPair pair; | 74 KeyPair pair; |
67 std::string auth_secret; | 75 std::string auth_secret; |
68 gcm_key_store()->GetKeys(kFakeAppId, | 76 gcm_key_store()->GetKeys(kFakeAppId, |
69 base::Bind(&GCMKeyStoreTest::GotKeys, | 77 base::Bind(&GCMKeyStoreTest::GotKeys, |
70 base::Unretained(this), &pair, | 78 base::Unretained(this), &pair, |
71 &auth_secret)); | 79 &auth_secret)); |
72 | 80 |
73 base::RunLoop().RunUntilIdle(); | 81 base::RunLoop().RunUntilIdle(); |
74 | 82 |
75 ASSERT_FALSE(pair.IsInitialized()); | 83 ASSERT_FALSE(pair.IsInitialized()); |
76 EXPECT_FALSE(pair.has_type()); | 84 EXPECT_FALSE(pair.has_type()); |
77 EXPECT_EQ(0u, auth_secret.size()); | 85 EXPECT_EQ(0u, auth_secret.size()); |
| 86 |
| 87 histogram_tester()->ExpectBucketCount( |
| 88 "GCM.Crypto.GetKeySuccessRate", 0, 1); // failure |
78 } | 89 } |
79 | 90 |
80 TEST_F(GCMKeyStoreTest, CreateAndGetKeys) { | 91 TEST_F(GCMKeyStoreTest, CreateAndGetKeys) { |
81 KeyPair pair; | 92 KeyPair pair; |
82 std::string auth_secret; | 93 std::string auth_secret; |
83 gcm_key_store()->CreateKeys(kFakeAppId, | 94 gcm_key_store()->CreateKeys(kFakeAppId, |
84 base::Bind(&GCMKeyStoreTest::GotKeys, | 95 base::Bind(&GCMKeyStoreTest::GotKeys, |
85 base::Unretained(this), &pair, | 96 base::Unretained(this), &pair, |
86 &auth_secret)); | 97 &auth_secret)); |
87 | 98 |
88 base::RunLoop().RunUntilIdle(); | 99 base::RunLoop().RunUntilIdle(); |
89 | 100 |
90 ASSERT_TRUE(pair.IsInitialized()); | 101 ASSERT_TRUE(pair.IsInitialized()); |
91 ASSERT_TRUE(pair.has_private_key()); | 102 ASSERT_TRUE(pair.has_private_key()); |
92 ASSERT_TRUE(pair.has_public_key()); | 103 ASSERT_TRUE(pair.has_public_key()); |
93 | 104 |
94 EXPECT_GT(pair.public_key().size(), 0u); | 105 EXPECT_GT(pair.public_key().size(), 0u); |
95 EXPECT_GT(pair.private_key().size(), 0u); | 106 EXPECT_GT(pair.private_key().size(), 0u); |
96 | 107 |
97 ASSERT_GT(auth_secret.size(), 0u); | 108 ASSERT_GT(auth_secret.size(), 0u); |
98 | 109 |
| 110 histogram_tester()->ExpectBucketCount( |
| 111 "GCM.Crypto.CreateKeySuccessRate", 1, 1); // success |
| 112 |
99 KeyPair read_pair; | 113 KeyPair read_pair; |
100 std::string read_auth_secret; | 114 std::string read_auth_secret; |
101 gcm_key_store()->GetKeys(kFakeAppId, | 115 gcm_key_store()->GetKeys(kFakeAppId, |
102 base::Bind(&GCMKeyStoreTest::GotKeys, | 116 base::Bind(&GCMKeyStoreTest::GotKeys, |
103 base::Unretained(this), &read_pair, | 117 base::Unretained(this), &read_pair, |
104 &read_auth_secret)); | 118 &read_auth_secret)); |
105 | 119 |
106 base::RunLoop().RunUntilIdle(); | 120 base::RunLoop().RunUntilIdle(); |
107 | 121 |
108 ASSERT_TRUE(read_pair.IsInitialized()); | 122 ASSERT_TRUE(read_pair.IsInitialized()); |
109 | 123 |
110 EXPECT_EQ(pair.type(), read_pair.type()); | 124 EXPECT_EQ(pair.type(), read_pair.type()); |
111 EXPECT_EQ(pair.private_key(), read_pair.private_key()); | 125 EXPECT_EQ(pair.private_key(), read_pair.private_key()); |
112 EXPECT_EQ(pair.public_key(), read_pair.public_key()); | 126 EXPECT_EQ(pair.public_key(), read_pair.public_key()); |
113 | 127 |
114 EXPECT_EQ(auth_secret, read_auth_secret); | 128 EXPECT_EQ(auth_secret, read_auth_secret); |
| 129 |
| 130 histogram_tester()->ExpectBucketCount( |
| 131 "GCM.Crypto.GetKeySuccessRate", 1, 1); // failure |
115 } | 132 } |
116 | 133 |
117 TEST_F(GCMKeyStoreTest, KeysPersistenceBetweenInstances) { | 134 TEST_F(GCMKeyStoreTest, KeysPersistenceBetweenInstances) { |
118 KeyPair pair; | 135 KeyPair pair; |
119 std::string auth_secret; | 136 std::string auth_secret; |
120 gcm_key_store()->CreateKeys(kFakeAppId, | 137 gcm_key_store()->CreateKeys(kFakeAppId, |
121 base::Bind(&GCMKeyStoreTest::GotKeys, | 138 base::Bind(&GCMKeyStoreTest::GotKeys, |
122 base::Unretained(this), &pair, | 139 base::Unretained(this), &pair, |
123 &auth_secret)); | 140 &auth_secret)); |
124 | 141 |
125 base::RunLoop().RunUntilIdle(); | 142 base::RunLoop().RunUntilIdle(); |
126 | 143 |
127 ASSERT_TRUE(pair.IsInitialized()); | 144 ASSERT_TRUE(pair.IsInitialized()); |
128 | 145 |
| 146 histogram_tester()->ExpectBucketCount( |
| 147 "GCM.Crypto.InitKeyStoreSuccessRate", 1, 1); // success |
| 148 histogram_tester()->ExpectBucketCount( |
| 149 "GCM.Crypto.LoadKeyStoreSuccessRate", 1, 1); // success |
| 150 |
129 // Create a new GCM Key Store instance. | 151 // Create a new GCM Key Store instance. |
130 CreateKeyStore(); | 152 CreateKeyStore(); |
131 | 153 |
132 KeyPair read_pair; | 154 KeyPair read_pair; |
133 std::string read_auth_secret; | 155 std::string read_auth_secret; |
134 gcm_key_store()->GetKeys(kFakeAppId, | 156 gcm_key_store()->GetKeys(kFakeAppId, |
135 base::Bind(&GCMKeyStoreTest::GotKeys, | 157 base::Bind(&GCMKeyStoreTest::GotKeys, |
136 base::Unretained(this), &read_pair, | 158 base::Unretained(this), &read_pair, |
137 &read_auth_secret)); | 159 &read_auth_secret)); |
138 | 160 |
139 base::RunLoop().RunUntilIdle(); | 161 base::RunLoop().RunUntilIdle(); |
140 | 162 |
141 ASSERT_TRUE(read_pair.IsInitialized()); | 163 ASSERT_TRUE(read_pair.IsInitialized()); |
142 EXPECT_TRUE(read_pair.has_type()); | 164 EXPECT_TRUE(read_pair.has_type()); |
143 EXPECT_GT(read_auth_secret.size(), 0u); | 165 EXPECT_GT(read_auth_secret.size(), 0u); |
| 166 |
| 167 histogram_tester()->ExpectBucketCount( |
| 168 "GCM.Crypto.InitKeyStoreSuccessRate", 1, 2); // success |
| 169 histogram_tester()->ExpectBucketCount( |
| 170 "GCM.Crypto.LoadKeyStoreSuccessRate", 1, 2); // success |
144 } | 171 } |
145 | 172 |
146 TEST_F(GCMKeyStoreTest, CreateAndRemoveKeys) { | 173 TEST_F(GCMKeyStoreTest, CreateAndRemoveKeys) { |
147 KeyPair pair; | 174 KeyPair pair; |
148 std::string auth_secret; | 175 std::string auth_secret; |
149 gcm_key_store()->CreateKeys(kFakeAppId, | 176 gcm_key_store()->CreateKeys(kFakeAppId, |
150 base::Bind(&GCMKeyStoreTest::GotKeys, | 177 base::Bind(&GCMKeyStoreTest::GotKeys, |
151 base::Unretained(this), &pair, | 178 base::Unretained(this), &pair, |
152 &auth_secret)); | 179 &auth_secret)); |
153 | 180 |
(...skipping 10 matching lines...) Expand all Loading... |
164 | 191 |
165 base::RunLoop().RunUntilIdle(); | 192 base::RunLoop().RunUntilIdle(); |
166 | 193 |
167 ASSERT_TRUE(read_pair.IsInitialized()); | 194 ASSERT_TRUE(read_pair.IsInitialized()); |
168 EXPECT_TRUE(read_pair.has_type()); | 195 EXPECT_TRUE(read_pair.has_type()); |
169 | 196 |
170 gcm_key_store()->RemoveKeys(kFakeAppId, base::Bind(&base::DoNothing)); | 197 gcm_key_store()->RemoveKeys(kFakeAppId, base::Bind(&base::DoNothing)); |
171 | 198 |
172 base::RunLoop().RunUntilIdle(); | 199 base::RunLoop().RunUntilIdle(); |
173 | 200 |
| 201 histogram_tester()->ExpectBucketCount( |
| 202 "GCM.Crypto.RemoveKeySuccessRate", 1, 1); // success |
| 203 |
174 gcm_key_store()->GetKeys(kFakeAppId, | 204 gcm_key_store()->GetKeys(kFakeAppId, |
175 base::Bind(&GCMKeyStoreTest::GotKeys, | 205 base::Bind(&GCMKeyStoreTest::GotKeys, |
176 base::Unretained(this), &read_pair, | 206 base::Unretained(this), &read_pair, |
177 &read_auth_secret)); | 207 &read_auth_secret)); |
178 | 208 |
179 base::RunLoop().RunUntilIdle(); | 209 base::RunLoop().RunUntilIdle(); |
180 | 210 |
181 ASSERT_FALSE(read_pair.IsInitialized()); | 211 ASSERT_FALSE(read_pair.IsInitialized()); |
182 } | 212 } |
183 | 213 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 // to the asynchronous nature of operations, however, we can't rely on the | 270 // to the asynchronous nature of operations, however, we can't rely on the |
241 // write to have finished before the read begins. | 271 // write to have finished before the read begins. |
242 base::RunLoop().RunUntilIdle(); | 272 base::RunLoop().RunUntilIdle(); |
243 | 273 |
244 EXPECT_TRUE(pair.IsInitialized()); | 274 EXPECT_TRUE(pair.IsInitialized()); |
245 } | 275 } |
246 | 276 |
247 } // namespace | 277 } // namespace |
248 | 278 |
249 } // namespace gcm | 279 } // namespace gcm |
OLD | NEW |