Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Unified Diff: chromeos/dbus/fake_easy_unlock_client_unittest.cc

Issue 513013003: Add unit tests for easyUnlockPrivate API - crypto methods (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chromeos/dbus/fake_easy_unlock_client_unittest.cc
diff --git a/chromeos/dbus/fake_easy_unlock_client_unittest.cc b/chromeos/dbus/fake_easy_unlock_client_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f2074c6e627925174010bfac951e61e9ee316444
--- /dev/null
+++ b/chromeos/dbus/fake_easy_unlock_client_unittest.cc
@@ -0,0 +1,232 @@
+// 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 "chromeos/dbus/fake_easy_unlock_client.h"
+
+#include <string>
+
+#include "base/bind.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+void RecordKeyPair(std::string* private_key_target,
satorux1 2014/08/29 01:10:50 function comment is missing.
tbarzic 2014/08/29 02:47:05 Done.
+ 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 RecordData(std::string* data_target,
satorux1 2014/08/29 01:10:50 ditto. just a one liner would suffice
tbarzic 2014/08/29 02:47:05 Done.
+ const std::string& data_source) {
+ *data_target = data_source;
+}
+
+TEST(FakeEasyUnlockClientTest, GenerateEcP256KeyPair) {
+ chromeos::FakeEasyUnlockClient client;
+
+ std::string private_key_1;
+ std::string public_key_1;
+ client.GenerateEcP256KeyPair(
+ base::Bind(&RecordKeyPair, &private_key_1, &public_key_1));
+ ASSERT_EQ("{\"ec_p256_private_key\": 1}", private_key_1);
+ ASSERT_EQ("{\"ec_p256_public_key\": 1}", public_key_1);
+
+ std::string private_key_2;
+ std::string public_key_2;
+ client.GenerateEcP256KeyPair(
+ base::Bind(&RecordKeyPair, &private_key_2, &public_key_2));
+ ASSERT_EQ("{\"ec_p256_private_key\": 2}", private_key_2);
+ ASSERT_EQ("{\"ec_p256_public_key\": 2}", public_key_2);
+
+ EXPECT_NE(private_key_1, private_key_2);
+ EXPECT_NE(public_key_1, public_key_2);
+}
+
+TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair) {
+ ASSERT_TRUE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
+ "{\"ec_p256_private_key\": 12}",
+ "{\"ec_p256_public_key\": 12}"));
+}
+
+TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair_KeysFromDiffrentPairs) {
+ ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
+ "{\"ec_p256_private_key\": 12}",
+ "{\"ec_p256_public_key\": 34}"));
+}
+
+TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair_KeyOrderSwitched) {
+ ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
+ "{\"ec_p256_public_key\": 34}",
+ "{\"ec_p256_private_key\": 34}"));
+}
+
+TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair_PrivateKeyInvalidFormat) {
+ ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
+ "\"ec_p256_private_key\": 12",
+ "{\"ec_p256_public_key\": 12}"));
+}
+
+TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair_PublicKeyInvalidFormat) {
+ ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
+ "{\"ec_p256_private_key\": 12}",
+ "\"ec_p256_public_key\": 12"));
+}
+
+TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair_PrivateKeyInvalidDictKey) {
+ ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
+ "{\"invalid\": 12}",
+ "{\"ec_p256_public_key\": 12}"));
+}
+
+TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair_PublicKeyInvalidDictKey) {
+ ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
+ "{\"ec_p256_private_key\": 12}",
+ "{\"invalid\": 12}"));
+}
+
+TEST(FakeEasyUnlockClientTest, IsEcP256KeyPair_InvalidDictValues) {
+ ASSERT_FALSE(chromeos::FakeEasyUnlockClient::IsEcP256KeyPair(
+ "{\"ec_p256_private_key\": \"12\"}",
+ "{\"ec_p256_public_key\": \"12\"}"));
+}
+
+TEST(FakeEasyUnlockClientTest, ECDHKeyAgreementSuccess) {
+ chromeos::FakeEasyUnlockClient client;
+
+ const std::string private_key_1 = "{\"ec_p256_private_key\": 32}";
+ const std::string public_key_1 = "{\"ec_p256_public_key\": 32}";
+ const std::string private_key_2 = "{\"ec_p256_private_key\": 352}";
+ const std::string public_key_2 = "{\"ec_p256_public_key\": 352}";
+ const std::string private_key_3 = "{\"ec_p256_private_key\": 432}";
+ const std::string public_key_3 = "{\"ec_p256_public_key\": 432}";
+
+ std::string shared_key_1;
satorux1 2014/08/29 01:10:50 could you add some comments in the test? it was ha
tbarzic 2014/08/29 02:47:05 Done.
satorux1 2014/08/29 03:48:14 Thank you for adding. These are helpful.
+ client.PerformECDHKeyAgreement(private_key_2,
+ public_key_1,
+ base::Bind(&RecordData, &shared_key_1));
+ EXPECT_FALSE(shared_key_1.empty());
+
+ std::string shared_key_2;
+ client.PerformECDHKeyAgreement(private_key_1,
+ public_key_2,
+ base::Bind(&RecordData, &shared_key_2));
+ EXPECT_FALSE(shared_key_2.empty());
+
+ std::string shared_key_3;
+ client.PerformECDHKeyAgreement(private_key_1,
+ public_key_3,
+ base::Bind(&RecordData, &shared_key_3));
+ EXPECT_FALSE(shared_key_3.empty());
+
+ EXPECT_EQ(shared_key_1, shared_key_2);
+ EXPECT_NE(shared_key_1, shared_key_3);
+ EXPECT_NE(shared_key_2, shared_key_3);
+}
+
+TEST(FakeEasyUnlockClientTest, ECDHKeyAgreementFailsIfKeyOrderSwitched) {
+ chromeos::FakeEasyUnlockClient client;
+
+ const std::string private_key = "{\"ec_p256_private_key\": 415}";
+ const std::string public_key = "{\"ec_p256_public_key\": 345}";
+
+ std::string shared_key;
+ client.PerformECDHKeyAgreement(public_key,
+ private_key,
+ base::Bind(&RecordData, &shared_key));
+ EXPECT_TRUE(shared_key.empty());
+}
+
+TEST(FakeEasyUnlockClientTest, ECDHKeyAgreementFailsIfKeyDictKeyInvalid) {
+ chromeos::FakeEasyUnlockClient client;
+
+ const std::string private_key = "{\"ec_p256_private_key_invalid\": 415}";
+ const std::string public_key = "{\"ec_p256_public_key_invalid\": 345}";
+
+ std::string shared_key;
+ client.PerformECDHKeyAgreement(private_key,
+ public_key,
+ base::Bind(&RecordData, &shared_key));
+ EXPECT_TRUE(shared_key.empty());
+}
+
+TEST(FakeEasyUnlockClientTest, ECDHKeyAgreementFailsIfKeyDictValueInvalid) {
+ chromeos::FakeEasyUnlockClient client;
+
+ const std::string private_key = "{\"ec_p256_private_key\": 415}";
+ const std::string public_key = "{\"ec_p256_public_key\": \"345__\"}";
+
+ std::string shared_key;
+ client.PerformECDHKeyAgreement(private_key,
+ public_key,
+ base::Bind(&RecordData, &shared_key));
+ EXPECT_TRUE(shared_key.empty());
+}
+
+TEST(FakeEasyUnlockClientTest, ECDHKeyAgreementFailsIfKeyFormatInvalid) {
+ chromeos::FakeEasyUnlockClient client;
+
+ const std::string private_key = "invalid";
+ const std::string public_key = "{\"ec_p256_public_key\": 345}";
+
+ std::string shared_key;
+ client.PerformECDHKeyAgreement(private_key,
+ public_key,
+ base::Bind(&RecordData, &shared_key));
+ EXPECT_TRUE(shared_key.empty());
+}
+
+TEST(FakeEasyUnlockClientTest, CreateSecureMessage) {
+ chromeos::FakeEasyUnlockClient client;
+
+ std::string message;
+ client.CreateSecureMessage(
+ "PAYLOAD",
+ "KEY",
+ "ASSOCIATED_DATA",
+ "PUBLIC_METADATA",
+ "VERIFICATION_KEY_ID",
+ "ENCRYPTION_TYPE",
+ "SIGNATURE_TYPE",
+ base::Bind(&RecordData, &message));
+
+ const std::string expected_message(
+ "{\"securemessage\": {"
+ "\"payload\": \"PAYLOAD\","
+ "\"key\": \"KEY\","
+ "\"associated_data\": \"ASSOCIATED_DATA\","
+ "\"public_metadata\": \"PUBLIC_METADATA\","
+ "\"verification_key_id\": \"VERIFICATION_KEY_ID\","
+ "\"encryption_type\": \"ENCRYPTION_TYPE\","
+ "\"signature_type\": \"SIGNATURE_TYPE\"}"
+ "}");
+ ASSERT_EQ(expected_message, message);
+}
+
+TEST(FakeEasyUnlockClientTest, UnwrapSecureMessage) {
+ chromeos::FakeEasyUnlockClient client;
+
+ std::string message;
+ client.UnwrapSecureMessage(
+ "MESSAGE",
+ "KEY",
+ "ASSOCIATED_DATA",
+ "ENCRYPTION_TYPE",
+ "SIGNATURE_TYPE",
+ base::Bind(&RecordData, &message));
+
+ const std::string expected_message(
+ "{\"unwrapped_securemessage\": {"
+ "\"message\": \"MESSAGE\","
+ "\"key\": \"KEY\","
+ "\"associated_data\": \"ASSOCIATED_DATA\","
+ "\"encryption_type\": \"ENCRYPTION_TYPE\","
+ "\"signature_type\": \"SIGNATURE_TYPE\"}"
+ "}");
+ ASSERT_EQ(expected_message, message);
+}
+
+} // namespace
+
« chromeos/dbus/fake_easy_unlock_client.cc ('K') | « chromeos/dbus/fake_easy_unlock_client.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698