OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chromeos/cryptohome/homedir_methods.h" | 5 #include "chromeos/cryptohome/homedir_methods.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "chromeos/dbus/cryptohome/rpc.pb.h" | 15 #include "chromeos/dbus/cryptohome/rpc.pb.h" |
16 #include "chromeos/dbus/cryptohome_client.h" | 16 #include "chromeos/dbus/cryptohome_client.h" |
17 #include "chromeos/dbus/dbus_method_call_status.h" | 17 #include "chromeos/dbus/dbus_method_call_status.h" |
18 #include "chromeos/dbus/dbus_thread_manager.h" | 18 #include "chromeos/dbus/dbus_thread_manager.h" |
19 #include "chromeos/dbus/mock_cryptohome_client.h" | 19 #include "chromeos/dbus/mock_cryptohome_client.h" |
| 20 #include "components/signin/core/account_id/account_id.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 | 23 |
23 using testing::_; | 24 using testing::_; |
24 using testing::Invoke; | 25 using testing::Invoke; |
25 using testing::WithArg; | 26 using testing::WithArg; |
26 | 27 |
27 namespace cryptohome { | 28 namespace cryptohome { |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 MATCHER_P(EqualsProto, expected_proto, "") { | 32 MATCHER_P(EqualsProto, expected_proto, "") { |
32 std::string expected_value; | 33 std::string expected_value; |
33 expected_proto.SerializeToString(&expected_value); | 34 expected_proto.SerializeToString(&expected_value); |
34 std::string actual_value; | 35 std::string actual_value; |
35 arg.SerializeToString(&actual_value); | 36 arg.SerializeToString(&actual_value); |
36 return actual_value == expected_value; | 37 return actual_value == expected_value; |
37 } | 38 } |
38 | 39 |
| 40 MATCHER_P(EqualsIdentification, expected_identification, "") { |
| 41 return arg == expected_identification; |
| 42 } |
| 43 |
39 } // namespace | 44 } // namespace |
40 | 45 |
41 const char kUserID[] = "user@example.com"; | 46 const char kUserID[] = "user@example.com"; |
42 const char kKeyLabel[] = "key_label"; | 47 const char kKeyLabel[] = "key_label"; |
43 | 48 |
44 const int64_t kKeyRevision = 123; | 49 const int64_t kKeyRevision = 123; |
45 const char kProviderData1Name[] = "data_1"; | 50 const char kProviderData1Name[] = "data_1"; |
46 const int64_t kProviderData1Number = 12345; | 51 const int64_t kProviderData1Number = 12345; |
47 const char kProviderData2Name[] = "data_2"; | 52 const char kProviderData2Name[] = "data_2"; |
48 const char kProviderData2Bytes[] = "data_2 bytes"; | 53 const char kProviderData2Bytes[] = "data_2 bytes"; |
(...skipping 12 matching lines...) Expand all Loading... |
61 | 66 |
62 void StoreGetKeyDataExResult( | 67 void StoreGetKeyDataExResult( |
63 bool success, | 68 bool success, |
64 MountError return_code, | 69 MountError return_code, |
65 const std::vector<KeyDefinition>& key_definitions); | 70 const std::vector<KeyDefinition>& key_definitions); |
66 | 71 |
67 protected: | 72 protected: |
68 chromeos::MockCryptohomeClient* cryptohome_client_; | 73 chromeos::MockCryptohomeClient* cryptohome_client_; |
69 | 74 |
70 // The reply that |cryptohome_client_| will make. | 75 // The reply that |cryptohome_client_| will make. |
71 cryptohome::BaseReply cryptohome_reply_; | 76 BaseReply cryptohome_reply_; |
72 | 77 |
73 // The results of the most recent |HomedirMethods| method call. | 78 // The results of the most recent |HomedirMethods| method call. |
74 bool success_; | 79 bool success_; |
75 MountError return_code_; | 80 MountError return_code_; |
76 std::vector<KeyDefinition> key_definitions_; | 81 std::vector<KeyDefinition> key_definitions_; |
77 | 82 |
78 private: | 83 private: |
79 DISALLOW_COPY_AND_ASSIGN(HomedirMethodsTest); | 84 DISALLOW_COPY_AND_ASSIGN(HomedirMethodsTest); |
80 }; | 85 }; |
81 | 86 |
(...skipping 30 matching lines...) Expand all Loading... |
112 bool success, | 117 bool success, |
113 MountError return_code, | 118 MountError return_code, |
114 const std::vector<KeyDefinition>& key_definitions) { | 119 const std::vector<KeyDefinition>& key_definitions) { |
115 success_ = success; | 120 success_ = success; |
116 return_code_ = return_code; | 121 return_code_ = return_code; |
117 key_definitions_ = key_definitions; | 122 key_definitions_ = key_definitions; |
118 } | 123 } |
119 | 124 |
120 // Verifies that the result of a GetKeyDataEx() call is correctly parsed. | 125 // Verifies that the result of a GetKeyDataEx() call is correctly parsed. |
121 TEST_F(HomedirMethodsTest, GetKeyDataEx) { | 126 TEST_F(HomedirMethodsTest, GetKeyDataEx) { |
122 AccountIdentifier expected_id; | 127 const Identification expected_id(AccountId::FromUserEmail(kUserID)); |
123 expected_id.set_email(kUserID); | 128 const AuthorizationRequest expected_auth; |
124 const cryptohome::AuthorizationRequest expected_auth; | 129 GetKeyDataRequest expected_request; |
125 cryptohome::GetKeyDataRequest expected_request; | 130 expected_request.mutable_key()->mutable_data()->set_label(kKeyLabel); |
126 expected_request.mutable_key()->mutable_data()->set_label(kKeyLabel); | |
127 | 131 |
128 EXPECT_CALL(*cryptohome_client_, | 132 EXPECT_CALL(*cryptohome_client_, |
129 GetKeyDataEx(EqualsProto(expected_id), | 133 GetKeyDataEx(EqualsIdentification(expected_id), |
130 EqualsProto(expected_auth), | 134 EqualsProto(expected_auth), |
131 EqualsProto(expected_request), | 135 EqualsProto(expected_request), _)) |
132 _)) | |
133 .Times(1) | 136 .Times(1) |
134 .WillOnce(WithArg<3>(Invoke( | 137 .WillOnce(WithArg<3>( |
135 this, | 138 Invoke(this, &HomedirMethodsTest::RunProtobufMethodCallback))); |
136 &HomedirMethodsTest::RunProtobufMethodCallback))); | |
137 | 139 |
138 // Set up the reply that |cryptohome_client_| will make. | 140 // Set up the reply that |cryptohome_client_| will make. |
139 cryptohome::GetKeyDataReply* reply = | 141 GetKeyDataReply* reply = |
140 cryptohome_reply_.MutableExtension(cryptohome::GetKeyDataReply::reply); | 142 cryptohome_reply_.MutableExtension(GetKeyDataReply::reply); |
141 KeyData* key_data = reply->add_key_data(); | 143 KeyData* key_data = reply->add_key_data(); |
142 key_data->set_type(KeyData::KEY_TYPE_PASSWORD); | 144 key_data->set_type(KeyData::KEY_TYPE_PASSWORD); |
143 key_data->set_label(kKeyLabel); | 145 key_data->set_label(kKeyLabel); |
144 key_data->mutable_privileges()->set_update(false); | 146 key_data->mutable_privileges()->set_update(false); |
145 key_data->set_revision(kKeyRevision); | 147 key_data->set_revision(kKeyRevision); |
146 key_data->add_authorization_data()->set_type( | 148 key_data->add_authorization_data()->set_type( |
147 KeyAuthorizationData::KEY_AUTHORIZATION_TYPE_HMACSHA256); | 149 KeyAuthorizationData::KEY_AUTHORIZATION_TYPE_HMACSHA256); |
148 KeyProviderData* data = key_data->mutable_provider_data(); | 150 KeyProviderData* data = key_data->mutable_provider_data(); |
149 KeyProviderData::Entry* entry = data->add_entry(); | 151 KeyProviderData::Entry* entry = data->add_entry(); |
150 entry->set_name(kProviderData1Name); | 152 entry->set_name(kProviderData1Name); |
151 entry->set_number(kProviderData1Number); | 153 entry->set_number(kProviderData1Number); |
152 entry = data->add_entry(); | 154 entry = data->add_entry(); |
153 entry->set_name(kProviderData2Name); | 155 entry->set_name(kProviderData2Name); |
154 entry->set_bytes(kProviderData2Bytes); | 156 entry->set_bytes(kProviderData2Bytes); |
155 | 157 |
156 // Call GetKeyDataEx(). | 158 // Call GetKeyDataEx(). |
157 HomedirMethods::GetInstance()->GetKeyDataEx( | 159 HomedirMethods::GetInstance()->GetKeyDataEx( |
158 Identification(kUserID), | 160 Identification(AccountId::FromUserEmail(kUserID)), kKeyLabel, |
159 kKeyLabel, | 161 base::Bind(&HomedirMethodsTest::StoreGetKeyDataExResult, |
160 base::Bind(&HomedirMethodsTest::StoreGetKeyDataExResult, | 162 base::Unretained(this))); |
161 base::Unretained(this))); | |
162 | 163 |
163 // Verify that the call was successful and the result was correctly parsed. | 164 // Verify that the call was successful and the result was correctly parsed. |
164 EXPECT_TRUE(success_); | 165 EXPECT_TRUE(success_); |
165 EXPECT_EQ(MOUNT_ERROR_NONE, return_code_); | 166 EXPECT_EQ(MOUNT_ERROR_NONE, return_code_); |
166 ASSERT_EQ(1u, key_definitions_.size()); | 167 ASSERT_EQ(1u, key_definitions_.size()); |
167 const KeyDefinition& key_definition = key_definitions_.front(); | 168 const KeyDefinition& key_definition = key_definitions_.front(); |
168 EXPECT_EQ(KeyDefinition::TYPE_PASSWORD, key_definition.type); | 169 EXPECT_EQ(KeyDefinition::TYPE_PASSWORD, key_definition.type); |
169 EXPECT_EQ(PRIV_MOUNT | PRIV_ADD | PRIV_REMOVE, | 170 EXPECT_EQ(PRIV_MOUNT | PRIV_ADD | PRIV_REMOVE, |
170 key_definition.privileges); | 171 key_definition.privileges); |
171 EXPECT_EQ(kKeyRevision, key_definition.revision); | 172 EXPECT_EQ(kKeyRevision, key_definition.revision); |
172 ASSERT_EQ(1u, key_definition.authorization_data.size()); | 173 ASSERT_EQ(1u, key_definition.authorization_data.size()); |
173 EXPECT_EQ(KeyDefinition::AuthorizationData::TYPE_HMACSHA256, | 174 EXPECT_EQ(KeyDefinition::AuthorizationData::TYPE_HMACSHA256, |
174 key_definition.authorization_data.front().type); | 175 key_definition.authorization_data.front().type); |
175 ASSERT_EQ(2u, key_definition.provider_data.size()); | 176 ASSERT_EQ(2u, key_definition.provider_data.size()); |
176 const KeyDefinition::ProviderData* provider_data = | 177 const KeyDefinition::ProviderData* provider_data = |
177 &key_definition.provider_data[0]; | 178 &key_definition.provider_data[0]; |
178 EXPECT_EQ(kProviderData1Name, provider_data->name); | 179 EXPECT_EQ(kProviderData1Name, provider_data->name); |
179 ASSERT_TRUE(provider_data->number); | 180 ASSERT_TRUE(provider_data->number); |
180 EXPECT_EQ(kProviderData1Number, *provider_data->number.get()); | 181 EXPECT_EQ(kProviderData1Number, *provider_data->number.get()); |
181 EXPECT_FALSE(provider_data->bytes); | 182 EXPECT_FALSE(provider_data->bytes); |
182 provider_data = &key_definition.provider_data[1]; | 183 provider_data = &key_definition.provider_data[1]; |
183 EXPECT_EQ(kProviderData2Name, provider_data->name); | 184 EXPECT_EQ(kProviderData2Name, provider_data->name); |
184 EXPECT_FALSE(provider_data->number); | 185 EXPECT_FALSE(provider_data->number); |
185 ASSERT_TRUE(provider_data->bytes); | 186 ASSERT_TRUE(provider_data->bytes); |
186 EXPECT_EQ(kProviderData2Bytes, *provider_data->bytes.get()); | 187 EXPECT_EQ(kProviderData2Bytes, *provider_data->bytes.get()); |
187 } | 188 } |
188 | 189 |
189 } // namespace cryptohome | 190 } // namespace cryptohome |
OLD | NEW |