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

Unified Diff: device/u2f/u2f_apdu_unittest.cc

Issue 2665493002: Add builders to APDU command class (Closed)
Patch Set: Add suffix for legacy version case and add additional unittests Created 3 years, 11 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
« no previous file with comments | « device/u2f/u2f_apdu_command.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: device/u2f/u2f_apdu_unittest.cc
diff --git a/device/u2f/u2f_apdu_unittest.cc b/device/u2f/u2f_apdu_unittest.cc
index a0435eb0d97ef560663e540fb9fd0d7262d97986..366031965af20071aa9d2a5f2b83c3e6bce56e86 100644
--- a/device/u2f/u2f_apdu_unittest.cc
+++ b/device/u2f/u2f_apdu_unittest.cc
@@ -181,4 +181,75 @@ TEST_F(U2fApduTest, TestSerializeEdgeCases) {
U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand())
->GetEncodedCommand()));
}
+
+TEST_F(U2fApduTest, TestCreateSign) {
+ std::vector<uint8_t> appid(U2fApduCommand::kAppIdDigestLen, 0x01);
+ std::vector<uint8_t> challenge(U2fApduCommand::kChallengeDigestLen, 0xff);
+ std::vector<uint8_t> key_handle(U2fApduCommand::kMaxKeyHandleLength);
+
+ scoped_refptr<U2fApduCommand> cmd =
+ U2fApduCommand::CreateSign(appid, challenge, key_handle);
+ ASSERT_NE(nullptr, cmd);
+ EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand())
+ ->GetEncodedCommand(),
+ testing::ContainerEq(cmd->GetEncodedCommand()));
+ // Expect null result with incorrectly sized key handle
+ key_handle.push_back(0x0f);
+ cmd = U2fApduCommand::CreateSign(appid, challenge, key_handle);
+ EXPECT_EQ(nullptr, cmd);
+ key_handle.pop_back();
+ // Expect null result with incorrectly sized appid
+ appid.pop_back();
+ cmd = U2fApduCommand::CreateSign(appid, challenge, key_handle);
+ EXPECT_EQ(nullptr, cmd);
+ appid.push_back(0xff);
+ // Expect null result with incorrectly sized challenge
+ challenge.push_back(0x0);
+ cmd = U2fApduCommand::CreateSign(appid, challenge, key_handle);
+ EXPECT_EQ(nullptr, cmd);
+}
+
+TEST_F(U2fApduTest, TestCreateRegister) {
+ std::vector<uint8_t> appid(U2fApduCommand::kAppIdDigestLen, 0x01);
+ std::vector<uint8_t> challenge(U2fApduCommand::kChallengeDigestLen, 0xff);
+ scoped_refptr<U2fApduCommand> cmd =
+ U2fApduCommand::CreateRegister(appid, challenge);
+ ASSERT_NE(nullptr, cmd);
+ EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand())
+ ->GetEncodedCommand(),
+ testing::ContainerEq(cmd->GetEncodedCommand()));
+ // Expect null result with incorrectly sized appid
+ appid.push_back(0xff);
+ cmd = U2fApduCommand::CreateRegister(appid, challenge);
+ EXPECT_EQ(nullptr, cmd);
+ appid.pop_back();
+ // Expect null result with incorrectly sized challenge
+ challenge.push_back(0xff);
+ cmd = U2fApduCommand::CreateRegister(appid, challenge);
+ EXPECT_EQ(nullptr, cmd);
+}
+
+TEST_F(U2fApduTest, TestCreateVersion) {
+ scoped_refptr<U2fApduCommand> cmd = U2fApduCommand::CreateVersion();
+ std::vector<uint8_t> expected = {
+ 0x0, U2fApduCommand::kInsU2fVersion, 0x0, 0x0, 0x0, 0x0, 0x0};
+
+ EXPECT_THAT(expected, testing::ContainerEq(cmd->GetEncodedCommand()));
+ EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand())
+ ->GetEncodedCommand(),
+ testing::ContainerEq(cmd->GetEncodedCommand()));
+}
+
+TEST_F(U2fApduTest, TestCreateLegacyVersion) {
+ scoped_refptr<U2fApduCommand> cmd = U2fApduCommand::CreateLegacyVersion();
+ // Legacy version command contains 2 extra null bytes compared to ISO 7816-4
+ // format
+ std::vector<uint8_t> expected = {
+ 0x0, U2fApduCommand::kInsU2fVersion, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
+
+ EXPECT_THAT(expected, testing::ContainerEq(cmd->GetEncodedCommand()));
+ EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand())
+ ->GetEncodedCommand(),
+ testing::ContainerEq(cmd->GetEncodedCommand()));
+}
} // namespace device
« no previous file with comments | « device/u2f/u2f_apdu_command.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698