Index: chrome/browser/extensions/api/easy_unlock_private/easy_unlock_private_api_chromeos_unittest.cc |
diff --git a/chrome/browser/extensions/api/easy_unlock_private/easy_unlock_private_api_chromeos_unittest.cc b/chrome/browser/extensions/api/easy_unlock_private/easy_unlock_private_api_chromeos_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5561a1d6d62b829fcebe7f1d57bac70d2dae1248 |
--- /dev/null |
+++ b/chrome/browser/extensions/api/easy_unlock_private/easy_unlock_private_api_chromeos_unittest.cc |
@@ -0,0 +1,373 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/bind.h" |
+#include "base/strings/stringprintf.h" |
+#include "base/values.h" |
+#include "chrome/browser/extensions/api/easy_unlock_private/easy_unlock_private_api.h" |
+#include "chrome/browser/extensions/extension_api_unittest.h" |
+#include "chrome/browser/extensions/extension_function_test_utils.h" |
+#include "chrome/common/extensions/api/easy_unlock_private.h" |
+#include "chromeos/dbus/dbus_thread_manager.h" |
+#include "chromeos/dbus/fake_easy_unlock_client.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "third_party/cros_system_api/dbus/service_constants.h" |
+ |
+namespace { |
+ |
+namespace api = extensions::api::easy_unlock_private; |
+ |
+using extensions::api::EasyUnlockPrivateGenerateEcP256KeyPairFunction; |
+using extensions::api::EasyUnlockPrivatePerformECDHKeyAgreementFunction; |
+using extensions::api::EasyUnlockPrivateCreateSecureMessageFunction; |
+using extensions::api::EasyUnlockPrivateUnwrapSecureMessageFunction; |
+ |
+base::BinaryValue* StringToBinaryValue(const std::string value) { |
satorux1
2014/08/29 01:10:49
function comment is missing.
tbarzic
2014/08/29 02:47:04
Done.
|
+ // NOTE: Trailing '\0' is intentionally not included. |
satorux1
2014/08/29 01:10:50
you can make it a part of the function comment.
tbarzic
2014/08/29 02:47:04
Done.
|
+ return base::BinaryValue::CreateWithCopiedBuffer(value.c_str(), |
+ value.length()); |
+} |
+ |
+void CopyKeyPair(std::string* private_key_target, |
+ std::string* public_key_target, |
+ const std::string& private_key_source, |
+ const std::string& public_key_source) { |
+ *private_key_target = private_key_source; |
+ *public_key_target = public_key_source; |
+} |
+ |
+void CopyData(std::string* data_target, const std::string& data_source) { |
+ *data_target = data_source; |
+} |
+ |
+class EasyUnlockPrivateApiTest : public extensions::ExtensionApiUnittest { |
+ public: |
+ EasyUnlockPrivateApiTest() {} |
+ virtual ~EasyUnlockPrivateApiTest() {} |
+ |
+ protected: |
+ virtual void SetUp() OVERRIDE { |
+ chromeos::DBusThreadManager::Initialize(); |
+ client_ = chromeos::DBusThreadManager::Get()->GetEasyUnlockClient(); |
+ |
+ extensions::ExtensionApiUnittest::SetUp(); |
+ } |
+ |
+ virtual void TearDown() OVERRIDE { |
+ extensions::ExtensionApiUnittest::TearDown(); |
+ |
+ chromeos::DBusThreadManager::Shutdown(); |
+ } |
+ |
+ void GetSingleBinaryResultAsString(UIThreadExtensionFunction* function, |
satorux1
2014/08/29 01:10:50
function comment is missing.
tbarzic
2014/08/29 02:47:04
Done.
|
+ std::string* result) { |
+ const base::ListValue* result_list = function->GetResultList(); |
+ ASSERT_TRUE(result_list); |
+ ASSERT_EQ(1u, result_list->GetSize()); |
+ |
+ const base::BinaryValue* result_binary_value; |
+ ASSERT_TRUE(result_list->GetBinary(0, &result_binary_value)); |
+ ASSERT_TRUE(result); |
+ |
+ *result = std::string(result_binary_value->GetBuffer(), |
+ result_binary_value->GetSize()); |
satorux1
2014/08/29 01:10:49
I think you can just return the result string.
tbarzic
2014/08/29 02:47:04
The reason I didn't return result is to be able t
satorux1
2014/08/29 03:48:14
Oh I see. I generally prefer not to have ASSERT/EX
tbarzic
2014/08/29 04:30:27
Yes, I agree with everything you said. I generally
|
+ } |
+ |
+ chromeos::EasyUnlockClient* client_; |
+}; |
+ |
+TEST_F(EasyUnlockPrivateApiTest, GenerateEcP256KeyPair) { |
+ scoped_refptr<EasyUnlockPrivateGenerateEcP256KeyPairFunction> function( |
+ new EasyUnlockPrivateGenerateEcP256KeyPairFunction()); |
+ function->set_has_callback(true); |
+ |
+ ASSERT_TRUE(extension_function_test_utils::RunFunction( |
+ function.get(), |
+ "[]", |
+ browser(), |
+ extension_function_test_utils::NONE)); |
+ |
+ const base::ListValue* result_list = function->GetResultList(); |
+ ASSERT_TRUE(result_list); |
+ ASSERT_EQ(2u, result_list->GetSize()); |
+ |
+ const base::BinaryValue* public_key; |
+ ASSERT_TRUE(result_list->GetBinary(0, &public_key)); |
+ ASSERT_TRUE(public_key); |
+ |
+ const base::BinaryValue* private_key; |
+ ASSERT_TRUE(result_list->GetBinary(1, &private_key)); |
+ ASSERT_TRUE(private_key); |
+ |
+ EXPECT_TRUE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair( |
+ std::string(private_key->GetBuffer(), private_key->GetSize()), |
+ std::string(public_key->GetBuffer(), public_key->GetSize()))); |
+} |
+ |
+TEST_F(EasyUnlockPrivateApiTest, PerformECDHKeyAgreement) { |
+ scoped_refptr<EasyUnlockPrivatePerformECDHKeyAgreementFunction> function( |
+ new EasyUnlockPrivatePerformECDHKeyAgreementFunction()); |
+ function->set_has_callback(true); |
+ |
+ std::string private_key_1; |
+ std::string public_key_1_unused; |
+ client_->GenerateEcP256KeyPair( |
+ base::Bind(&CopyKeyPair, &private_key_1, &public_key_1_unused)); |
+ |
+ std::string private_key_2_unused; |
+ std::string public_key_2; |
+ client_->GenerateEcP256KeyPair( |
+ base::Bind(&CopyKeyPair, &private_key_2_unused, &public_key_2)); |
+ |
+ std::string expected_result; |
+ client_->PerformECDHKeyAgreement( |
+ private_key_1, |
+ public_key_2, |
+ base::Bind(&CopyData, &expected_result)); |
+ ASSERT_GT(expected_result.length(), 0u); |
+ |
+ scoped_ptr<base::ListValue> args(new base::ListValue); |
+ args->Append(StringToBinaryValue(private_key_1)); |
+ args->Append(StringToBinaryValue(public_key_2)); |
+ |
+ ASSERT_TRUE(extension_function_test_utils::RunFunction( |
+ function.get(), |
+ args.Pass(), |
+ browser(), |
+ extension_function_test_utils::NONE)); |
+ |
+ std::string result; |
+ GetSingleBinaryResultAsString(function, &result); |
+ EXPECT_EQ(expected_result, result); |
+} |
+ |
+TEST_F(EasyUnlockPrivateApiTest, CreateSecureMessage) { |
+ scoped_refptr<EasyUnlockPrivateCreateSecureMessageFunction> function( |
+ new EasyUnlockPrivateCreateSecureMessageFunction()); |
+ function->set_has_callback(true); |
+ |
+ std::string expected_result; |
+ client_->CreateSecureMessage( |
+ "PAYLOAD", |
+ "KEY", |
+ "ASSOCIATED_DATA", |
+ "PUBLIC_METADATA", |
+ "VERIFICATION_KEY_ID", |
+ easy_unlock::kEncryptionTypeAES256CBC, |
+ easy_unlock::kSignatureTypeHMACSHA256, |
+ base::Bind(&CopyData, &expected_result)); |
+ ASSERT_GT(expected_result.length(), 0u); |
+ |
+ scoped_ptr<base::ListValue> args(new base::ListValue); |
+ args->Append(StringToBinaryValue("PAYLOAD")); |
+ args->Append(StringToBinaryValue("KEY")); |
+ base::DictionaryValue* options = new base::DictionaryValue(); |
+ args->Append(options); |
+ options->Set("associatedData", StringToBinaryValue("ASSOCIATED_DATA")); |
+ options->Set("publicMetadata", StringToBinaryValue("PUBLIC_METADATA")); |
+ options->Set("verificationKeyId", |
+ StringToBinaryValue("VERIFICATION_KEY_ID")); |
+ options->SetString( |
+ "encryptType", |
+ api::ToString(api::ENCRYPTION_TYPE_AES_256_CBC)); |
+ options->SetString( |
+ "signType", |
+ api::ToString(api::SIGNATURE_TYPE_HMAC_SHA256)); |
+ |
+ ASSERT_TRUE(extension_function_test_utils::RunFunction( |
+ function.get(), |
+ args.Pass(), |
+ browser(), |
+ extension_function_test_utils::NONE)); |
+ |
+ std::string result; |
+ GetSingleBinaryResultAsString(function, &result); |
+ EXPECT_EQ(expected_result, result); |
+} |
+ |
+TEST_F(EasyUnlockPrivateApiTest, CreateSecureMessage_EmptyOptions) { |
+ scoped_refptr<EasyUnlockPrivateCreateSecureMessageFunction> function( |
+ new EasyUnlockPrivateCreateSecureMessageFunction()); |
+ function->set_has_callback(true); |
+ |
+ std::string expected_result; |
+ client_->CreateSecureMessage( |
+ "PAYLOAD", |
+ "KEY", |
+ "", |
+ "", |
+ "", |
+ easy_unlock::kEncryptionTypeNone, |
+ easy_unlock::kSignatureTypeHMACSHA256, |
+ base::Bind(&CopyData, &expected_result)); |
+ ASSERT_GT(expected_result.length(), 0u); |
+ |
+ scoped_ptr<base::ListValue> args(new base::ListValue); |
+ args->Append(StringToBinaryValue("PAYLOAD")); |
+ args->Append(StringToBinaryValue("KEY")); |
+ base::DictionaryValue* options = new base::DictionaryValue(); |
+ args->Append(options); |
+ |
+ ASSERT_TRUE(extension_function_test_utils::RunFunction( |
+ function.get(), |
+ args.Pass(), |
+ browser(), |
+ extension_function_test_utils::NONE)); |
+ |
+ std::string result; |
+ GetSingleBinaryResultAsString(function, &result); |
+ EXPECT_EQ(expected_result, result); |
+} |
+ |
+TEST_F(EasyUnlockPrivateApiTest, CreateSecureMessage_AsymmetricSign) { |
+ scoped_refptr<EasyUnlockPrivateCreateSecureMessageFunction> function( |
+ new EasyUnlockPrivateCreateSecureMessageFunction()); |
+ function->set_has_callback(true); |
+ |
+ std::string expected_result; |
+ client_->CreateSecureMessage( |
+ "PAYLOAD", |
+ "KEY", |
+ "ASSOCIATED_DATA", |
+ "", |
+ "VERIFICATION_KEY_ID", |
+ easy_unlock::kEncryptionTypeNone, |
+ easy_unlock::kSignatureTypeECDSAP256SHA256, |
+ base::Bind(&CopyData, &expected_result)); |
+ ASSERT_GT(expected_result.length(), 0u); |
+ |
+ scoped_ptr<base::ListValue> args(new base::ListValue); |
+ args->Append(StringToBinaryValue("PAYLOAD")); |
+ args->Append(StringToBinaryValue("KEY")); |
+ base::DictionaryValue* options = new base::DictionaryValue(); |
+ args->Append(options); |
+ options->Set("associatedData", |
+ StringToBinaryValue("ASSOCIATED_DATA")); |
+ options->Set("verificationKeyId", |
+ StringToBinaryValue("VERIFICATION_KEY_ID")); |
+ options->SetString( |
+ "signType", |
+ api::ToString(api::SIGNATURE_TYPE_ECDSA_P256_SHA256)); |
+ |
+ ASSERT_TRUE(extension_function_test_utils::RunFunction( |
+ function.get(), |
+ args.Pass(), |
+ browser(), |
+ extension_function_test_utils::NONE)); |
+ |
+ std::string result; |
+ GetSingleBinaryResultAsString(function, &result); |
+ EXPECT_EQ(expected_result, result); |
+} |
+ |
+TEST_F(EasyUnlockPrivateApiTest, UnwrapSecureMessage) { |
+ scoped_refptr<EasyUnlockPrivateUnwrapSecureMessageFunction> function( |
+ new EasyUnlockPrivateUnwrapSecureMessageFunction()); |
+ function->set_has_callback(true); |
+ |
+ std::string expected_result; |
+ client_->UnwrapSecureMessage( |
+ "PAYLOAD", |
+ "KEY", |
+ "ASSOCIATED_DATA", |
+ easy_unlock::kEncryptionTypeAES256CBC, |
+ easy_unlock::kSignatureTypeHMACSHA256, |
+ base::Bind(&CopyData, &expected_result)); |
+ ASSERT_GT(expected_result.length(), 0u); |
+ |
+ scoped_ptr<base::ListValue> args(new base::ListValue); |
+ args->Append(StringToBinaryValue("PAYLOAD")); |
+ args->Append(StringToBinaryValue("KEY")); |
+ base::DictionaryValue* options = new base::DictionaryValue(); |
+ args->Append(options); |
+ options->Set("associatedData", StringToBinaryValue("ASSOCIATED_DATA")); |
+ options->SetString( |
+ "encryptType", |
+ api::ToString(api::ENCRYPTION_TYPE_AES_256_CBC)); |
+ options->SetString( |
+ "signType", |
+ api::ToString(api::SIGNATURE_TYPE_HMAC_SHA256)); |
+ |
+ ASSERT_TRUE(extension_function_test_utils::RunFunction( |
+ function.get(), |
+ args.Pass(), |
+ browser(), |
+ extension_function_test_utils::NONE)); |
+ |
+ std::string result; |
+ GetSingleBinaryResultAsString(function, &result); |
+ EXPECT_EQ(expected_result, result); |
+} |
+ |
+TEST_F(EasyUnlockPrivateApiTest, UnwrapSecureMessage_EmptyOptions) { |
+ scoped_refptr<EasyUnlockPrivateUnwrapSecureMessageFunction> function( |
+ new EasyUnlockPrivateUnwrapSecureMessageFunction()); |
+ function->set_has_callback(true); |
+ |
+ std::string expected_result; |
+ client_->UnwrapSecureMessage( |
+ "MESSAGE", |
+ "KEY", |
+ "", |
+ easy_unlock::kEncryptionTypeNone, |
+ easy_unlock::kSignatureTypeHMACSHA256, |
+ base::Bind(&CopyData, &expected_result)); |
+ ASSERT_GT(expected_result.length(), 0u); |
+ |
+ scoped_ptr<base::ListValue> args(new base::ListValue); |
+ args->Append(StringToBinaryValue("MESSAGE")); |
+ args->Append(StringToBinaryValue("KEY")); |
+ base::DictionaryValue* options = new base::DictionaryValue(); |
+ args->Append(options); |
+ |
+ ASSERT_TRUE(extension_function_test_utils::RunFunction( |
+ function.get(), |
+ args.Pass(), |
+ browser(), |
+ extension_function_test_utils::NONE)); |
+ |
+ std::string result; |
+ GetSingleBinaryResultAsString(function, &result); |
+ EXPECT_EQ(expected_result, result); |
+} |
+ |
+TEST_F(EasyUnlockPrivateApiTest, UnwrapSecureMessage_AsymmetricSign) { |
+ scoped_refptr<EasyUnlockPrivateUnwrapSecureMessageFunction> function( |
+ new EasyUnlockPrivateUnwrapSecureMessageFunction()); |
+ function->set_has_callback(true); |
+ |
+ std::string expected_result; |
+ client_->UnwrapSecureMessage( |
+ "MESSAGE", |
+ "KEY", |
+ "ASSOCIATED_DATA", |
+ easy_unlock::kEncryptionTypeNone, |
+ easy_unlock::kSignatureTypeECDSAP256SHA256, |
+ base::Bind(&CopyData, &expected_result)); |
+ ASSERT_GT(expected_result.length(), 0u); |
+ |
+ scoped_ptr<base::ListValue> args(new base::ListValue); |
+ args->Append(StringToBinaryValue("MESSAGE")); |
+ args->Append(StringToBinaryValue("KEY")); |
+ base::DictionaryValue* options = new base::DictionaryValue(); |
+ args->Append(options); |
+ options->Set("associatedData", |
+ StringToBinaryValue("ASSOCIATED_DATA")); |
+ options->SetString( |
+ "signType", |
+ api::ToString(api::SIGNATURE_TYPE_ECDSA_P256_SHA256)); |
+ |
+ ASSERT_TRUE(extension_function_test_utils::RunFunction( |
+ function.get(), |
+ args.Pass(), |
+ browser(), |
+ extension_function_test_utils::NONE)); |
+ |
+ std::string result; |
+ GetSingleBinaryResultAsString(function, &result); |
+ EXPECT_EQ(expected_result, result); |
+} |
+ |
+} // namespace |
+ |