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