OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromeos/cryptohome/homedir_methods.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "chromeos/dbus/cryptohome/rpc.pb.h" |
| 12 #include "chromeos/dbus/cryptohome_client.h" |
| 13 #include "chromeos/dbus/dbus_method_call_status.h" |
| 14 #include "chromeos/dbus/dbus_thread_manager.h" |
| 15 #include "chromeos/dbus/mock_cryptohome_client.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 using testing::_; |
| 20 using testing::Invoke; |
| 21 using testing::WithArg; |
| 22 |
| 23 namespace cryptohome { |
| 24 |
| 25 namespace { |
| 26 |
| 27 MATCHER_P(EqualsProto, expected_proto, "") { |
| 28 std::string expected_value; |
| 29 expected_proto.SerializeToString(&expected_value); |
| 30 std::string actual_value; |
| 31 arg.SerializeToString(&actual_value); |
| 32 return actual_value == expected_value; |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 const char kUserID[] = "user@example.com"; |
| 38 const char kKeyLabel[] = "key_label"; |
| 39 |
| 40 const int64 kKeyRevision = 123; |
| 41 const char kProviderData1Name[] = "data_1"; |
| 42 const int64 kProviderData1Number = 12345; |
| 43 const char kProviderData2Name[] = "data_2"; |
| 44 const char kProviderData2Bytes[] = "data_2 bytes"; |
| 45 |
| 46 class HomedirMethodsTest : public testing::Test { |
| 47 public: |
| 48 HomedirMethodsTest(); |
| 49 virtual ~HomedirMethodsTest(); |
| 50 |
| 51 // testing::Test: |
| 52 virtual void SetUp() OVERRIDE; |
| 53 virtual void TearDown() OVERRIDE; |
| 54 |
| 55 void RunProtobufMethodCallback( |
| 56 const chromeos::CryptohomeClient::ProtobufMethodCallback& callback); |
| 57 |
| 58 void StoreGetKeyDataExResult(bool success, |
| 59 MountError return_code, |
| 60 ScopedVector<RetrievedKeyData> key_data); |
| 61 |
| 62 protected: |
| 63 chromeos::MockCryptohomeClient* cryptohome_client_; |
| 64 |
| 65 // The reply that |cryptohome_client_| will make. |
| 66 cryptohome::BaseReply cryptohome_reply_; |
| 67 |
| 68 // The results of the most recent |HomedirMethods| method call. |
| 69 bool success_; |
| 70 MountError return_code_; |
| 71 ScopedVector<RetrievedKeyData> key_data_; |
| 72 |
| 73 private: |
| 74 DISALLOW_COPY_AND_ASSIGN(HomedirMethodsTest); |
| 75 }; |
| 76 |
| 77 HomedirMethodsTest::HomedirMethodsTest() : cryptohome_client_(NULL), |
| 78 success_(false), |
| 79 return_code_(MOUNT_ERROR_FATAL) { |
| 80 } |
| 81 |
| 82 HomedirMethodsTest::~HomedirMethodsTest() { |
| 83 } |
| 84 |
| 85 void HomedirMethodsTest::SetUp() { |
| 86 scoped_ptr<chromeos::MockCryptohomeClient> cryptohome_client( |
| 87 new chromeos::MockCryptohomeClient); |
| 88 cryptohome_client_ = cryptohome_client.get(); |
| 89 chromeos::DBusThreadManager::GetSetterForTesting()->SetCryptohomeClient( |
| 90 cryptohome_client.PassAs<chromeos::CryptohomeClient>()); |
| 91 HomedirMethods::Initialize(); |
| 92 } |
| 93 |
| 94 void HomedirMethodsTest::TearDown() { |
| 95 HomedirMethods::Shutdown(); |
| 96 chromeos::DBusThreadManager::Shutdown(); |
| 97 } |
| 98 |
| 99 void HomedirMethodsTest::RunProtobufMethodCallback( |
| 100 const chromeos::CryptohomeClient::ProtobufMethodCallback& callback) { |
| 101 callback.Run(chromeos::DBUS_METHOD_CALL_SUCCESS, |
| 102 true, |
| 103 cryptohome_reply_); |
| 104 } |
| 105 |
| 106 void HomedirMethodsTest::StoreGetKeyDataExResult( |
| 107 bool success, |
| 108 MountError return_code, |
| 109 ScopedVector<RetrievedKeyData> key_data) { |
| 110 success_ = success; |
| 111 return_code_ = return_code; |
| 112 key_data_.swap(key_data); |
| 113 } |
| 114 |
| 115 // Verifies that the result of a GetKeyDataEx() call is correctly parsed. |
| 116 TEST_F(HomedirMethodsTest, GetKeyDataEx) { |
| 117 AccountIdentifier expected_id; |
| 118 expected_id.set_email(kUserID); |
| 119 const cryptohome::AuthorizationRequest expected_auth; |
| 120 cryptohome::GetKeyDataRequest expected_request; |
| 121 expected_request.mutable_key()->mutable_data()->set_label(kKeyLabel); |
| 122 |
| 123 EXPECT_CALL(*cryptohome_client_, |
| 124 GetKeyDataEx(EqualsProto(expected_id), |
| 125 EqualsProto(expected_auth), |
| 126 EqualsProto(expected_request), |
| 127 _)) |
| 128 .Times(1) |
| 129 .WillOnce(WithArg<3>(Invoke( |
| 130 this, |
| 131 &HomedirMethodsTest::RunProtobufMethodCallback))); |
| 132 |
| 133 // Set up the reply that |cryptohome_client_| will make. |
| 134 cryptohome::GetKeyDataReply* reply = |
| 135 cryptohome_reply_.MutableExtension(cryptohome::GetKeyDataReply::reply); |
| 136 KeyData* key_data = reply->add_key_data(); |
| 137 key_data->set_type(KeyData::KEY_TYPE_PASSWORD); |
| 138 key_data->set_label(kKeyLabel); |
| 139 key_data->mutable_privileges()->set_update(false); |
| 140 key_data->set_revision(kKeyRevision); |
| 141 key_data->add_authorization_data()->set_type( |
| 142 KeyAuthorizationData::KEY_AUTHORIZATION_TYPE_HMACSHA256); |
| 143 KeyProviderData* data = key_data->mutable_provider_data(); |
| 144 KeyProviderData::Entry* entry = data->add_entry(); |
| 145 entry->set_name(kProviderData1Name); |
| 146 entry->set_number(kProviderData1Number); |
| 147 entry = data->add_entry(); |
| 148 entry->set_name(kProviderData2Name); |
| 149 entry->set_bytes(kProviderData2Bytes); |
| 150 |
| 151 // Call GetKeyDataEx(). |
| 152 HomedirMethods::GetInstance()->GetKeyDataEx( |
| 153 Identification(kUserID), |
| 154 kKeyLabel, |
| 155 base::Bind(&HomedirMethodsTest::StoreGetKeyDataExResult, |
| 156 base::Unretained(this))); |
| 157 |
| 158 // Verify that the call was successful and the result was correctly parsed. |
| 159 EXPECT_TRUE(success_); |
| 160 EXPECT_EQ(MOUNT_ERROR_NONE, return_code_); |
| 161 ASSERT_EQ(1u, key_data_.size()); |
| 162 const RetrievedKeyData* retrieved_key_data = key_data_.front(); |
| 163 EXPECT_EQ(RetrievedKeyData::TYPE_PASSWORD, retrieved_key_data->type); |
| 164 EXPECT_EQ(PRIV_MOUNT | PRIV_ADD | PRIV_REMOVE, |
| 165 retrieved_key_data->privileges); |
| 166 EXPECT_EQ(kKeyRevision, retrieved_key_data->revision); |
| 167 ASSERT_EQ(1u, retrieved_key_data->authorization_types.size()); |
| 168 EXPECT_EQ(RetrievedKeyData::AUTHORIZATION_TYPE_HMACSHA256, |
| 169 retrieved_key_data->authorization_types.front()); |
| 170 ASSERT_EQ(2u, retrieved_key_data->provider_data.size()); |
| 171 const RetrievedKeyData::ProviderData* provider_data = |
| 172 retrieved_key_data->provider_data[0]; |
| 173 EXPECT_EQ(kProviderData1Name, provider_data->name); |
| 174 ASSERT_TRUE(provider_data->number); |
| 175 EXPECT_EQ(kProviderData1Number, *provider_data->number.get()); |
| 176 EXPECT_FALSE(provider_data->bytes); |
| 177 provider_data = retrieved_key_data->provider_data[1]; |
| 178 EXPECT_EQ(kProviderData2Name, provider_data->name); |
| 179 EXPECT_FALSE(provider_data->number); |
| 180 ASSERT_TRUE(provider_data->bytes); |
| 181 EXPECT_EQ(kProviderData2Bytes, *provider_data->bytes.get()); |
| 182 } |
| 183 |
| 184 } // namespace cryptohome |
OLD | NEW |