Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "testing/gmock/include/gmock/gmock.h" | 5 #include "testing/gmock/include/gmock/gmock.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 #include "u2f_apdu_command.h" | 7 #include "u2f_apdu_command.h" |
| 8 #include "u2f_apdu_response.h" | 8 #include "u2f_apdu_response.h" |
| 9 | 9 |
| 10 namespace device { | 10 namespace device { |
| 11 | 11 |
| 12 class U2fApduTest : public testing::Test {}; | 12 class U2fApduTest : public testing::Test {}; |
| 13 | 13 |
| 14 TEST_F(U2fApduTest, TestDeserializeBasic) { | 14 TEST_F(U2fApduTest, TestDeserializeBasic) { |
| 15 uint8_t cla = 0xAA; | 15 uint8_t cla = 0xAA; |
| 16 uint8_t ins = 0xAB; | 16 uint8_t ins = 0xAB; |
| 17 uint8_t p1 = 0xAC; | 17 uint8_t p1 = 0xAC; |
| 18 uint8_t p2 = 0xAD; | 18 uint8_t p2 = 0xAD; |
| 19 std::vector<uint8_t> message = {cla, ins, p1, p2}; | 19 std::vector<uint8_t> message = {cla, ins, p1, p2}; |
| 20 scoped_refptr<U2fApduCommand> cmd = | 20 std::unique_ptr<U2fApduCommand> cmd = |
| 21 U2fApduCommand::CreateFromMessage(message); | 21 U2fApduCommand::CreateFromMessage(message); |
| 22 | 22 |
| 23 EXPECT_EQ(static_cast<size_t>(0), cmd->response_length_); | 23 EXPECT_EQ(static_cast<size_t>(0), cmd->response_length_); |
| 24 EXPECT_THAT(cmd->data_, testing::ContainerEq(std::vector<uint8_t>())); | 24 EXPECT_THAT(cmd->data_, testing::ContainerEq(std::vector<uint8_t>())); |
| 25 EXPECT_EQ(cmd->cla_, cla); | 25 EXPECT_EQ(cmd->cla_, cla); |
| 26 EXPECT_EQ(cmd->ins_, ins); | 26 EXPECT_EQ(cmd->ins_, ins); |
| 27 EXPECT_EQ(cmd->p1_, p1); | 27 EXPECT_EQ(cmd->p1_, p1); |
| 28 EXPECT_EQ(cmd->p2_, p2); | 28 EXPECT_EQ(cmd->p2_, p2); |
| 29 | 29 |
| 30 // Invalid length | 30 // Invalid length |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 50 uint8_t p2 = 0xAD; | 50 uint8_t p2 = 0xAD; |
| 51 std::vector<uint8_t> data( | 51 std::vector<uint8_t> data( |
| 52 U2fApduCommand::kApduMaxDataLength - U2fApduCommand::kApduMaxHeader - 2, | 52 U2fApduCommand::kApduMaxDataLength - U2fApduCommand::kApduMaxHeader - 2, |
| 53 0x7F); | 53 0x7F); |
| 54 std::vector<uint8_t> message = {cla, ins, p1, p2, 0}; | 54 std::vector<uint8_t> message = {cla, ins, p1, p2, 0}; |
| 55 message.push_back((data.size() >> 8) & 0xff); | 55 message.push_back((data.size() >> 8) & 0xff); |
| 56 message.push_back(data.size() & 0xff); | 56 message.push_back(data.size() & 0xff); |
| 57 message.insert(message.end(), data.begin(), data.end()); | 57 message.insert(message.end(), data.begin(), data.end()); |
| 58 | 58 |
| 59 // Create a message with no response expected | 59 // Create a message with no response expected |
| 60 scoped_refptr<U2fApduCommand> cmd_no_response = | 60 std::unique_ptr<U2fApduCommand> cmd_no_response = |
| 61 U2fApduCommand::CreateFromMessage(message); | 61 U2fApduCommand::CreateFromMessage(message); |
| 62 EXPECT_EQ(static_cast<size_t>(0), cmd_no_response->response_length_); | 62 EXPECT_EQ(static_cast<size_t>(0), cmd_no_response->response_length_); |
| 63 EXPECT_THAT(data, testing::ContainerEq(cmd_no_response->data_)); | 63 EXPECT_THAT(data, testing::ContainerEq(cmd_no_response->data_)); |
| 64 EXPECT_EQ(cmd_no_response->cla_, cla); | 64 EXPECT_EQ(cmd_no_response->cla_, cla); |
| 65 EXPECT_EQ(cmd_no_response->ins_, ins); | 65 EXPECT_EQ(cmd_no_response->ins_, ins); |
| 66 EXPECT_EQ(cmd_no_response->p1_, p1); | 66 EXPECT_EQ(cmd_no_response->p1_, p1); |
| 67 EXPECT_EQ(cmd_no_response->p2_, p2); | 67 EXPECT_EQ(cmd_no_response->p2_, p2); |
| 68 | 68 |
| 69 // Add response length to message | 69 // Add response length to message |
| 70 message.push_back(0xF1); | 70 message.push_back(0xF1); |
| 71 message.push_back(0xD0); | 71 message.push_back(0xD0); |
| 72 scoped_refptr<U2fApduCommand> cmd = | 72 std::unique_ptr<U2fApduCommand> cmd = |
| 73 U2fApduCommand::CreateFromMessage(message); | 73 U2fApduCommand::CreateFromMessage(message); |
| 74 EXPECT_THAT(data, testing::ContainerEq(cmd->data_)); | 74 EXPECT_THAT(data, testing::ContainerEq(cmd->data_)); |
| 75 EXPECT_EQ(cmd->cla_, cla); | 75 EXPECT_EQ(cmd->cla_, cla); |
| 76 EXPECT_EQ(cmd->ins_, ins); | 76 EXPECT_EQ(cmd->ins_, ins); |
| 77 EXPECT_EQ(cmd->p1_, p1); | 77 EXPECT_EQ(cmd->p1_, p1); |
| 78 EXPECT_EQ(cmd->p2_, p2); | 78 EXPECT_EQ(cmd->p2_, p2); |
| 79 EXPECT_EQ(static_cast<size_t>(0xF1D0), cmd->response_length_); | 79 EXPECT_EQ(static_cast<size_t>(0xF1D0), cmd->response_length_); |
| 80 } | 80 } |
| 81 | 81 |
| 82 TEST_F(U2fApduTest, TestDeserializeResponse) { | 82 TEST_F(U2fApduTest, TestDeserializeResponse) { |
| 83 U2fApduResponse::Status status; | 83 U2fApduResponse::Status status; |
| 84 scoped_refptr<U2fApduResponse> response; | 84 std::unique_ptr<U2fApduResponse> response; |
| 85 std::vector<uint8_t> test_vector; | 85 std::vector<uint8_t> test_vector; |
| 86 | 86 |
| 87 // Invalid length | 87 // Invalid length |
| 88 std::vector<uint8_t> message = {0xAA}; | 88 std::vector<uint8_t> message = {0xAA}; |
| 89 EXPECT_EQ(nullptr, U2fApduResponse::CreateFromMessage(message)); | 89 EXPECT_EQ(nullptr, U2fApduResponse::CreateFromMessage(message)); |
| 90 | 90 |
| 91 // Valid length and status | 91 // Valid length and status |
| 92 status = U2fApduResponse::Status::SW_CONDITIONS_NOT_SATISFIED; | 92 status = U2fApduResponse::Status::SW_CONDITIONS_NOT_SATISFIED; |
| 93 message = {static_cast<uint8_t>(static_cast<uint16_t>(status) >> 8), | 93 message = {static_cast<uint8_t>(static_cast<uint16_t>(status) >> 8), |
| 94 static_cast<uint8_t>(status)}; | 94 static_cast<uint8_t>(status)}; |
| 95 response = U2fApduResponse::CreateFromMessage(message); | 95 response = U2fApduResponse::CreateFromMessage(message); |
| 96 ASSERT_NE(nullptr, response); | 96 ASSERT_NE(nullptr, response); |
| 97 EXPECT_EQ(U2fApduResponse::Status::SW_CONDITIONS_NOT_SATISFIED, | 97 EXPECT_EQ(U2fApduResponse::Status::SW_CONDITIONS_NOT_SATISFIED, |
| 98 response->response_status_); | 98 response->response_status_); |
| 99 EXPECT_THAT(response->data_, testing::ContainerEq(std::vector<uint8_t>())); | 99 EXPECT_THAT(response->data_, testing::ContainerEq(std::vector<uint8_t>())); |
| 100 | 100 |
| 101 // Valid length and status | 101 // Valid length and status |
| 102 status = U2fApduResponse::Status::SW_NO_ERROR; | 102 status = U2fApduResponse::Status::SW_NO_ERROR; |
| 103 message = {static_cast<uint8_t>(static_cast<uint16_t>(status) >> 8), | 103 message = {static_cast<uint8_t>(static_cast<uint16_t>(status) >> 8), |
| 104 static_cast<uint8_t>(status)}; | 104 static_cast<uint8_t>(status)}; |
| 105 test_vector = {0x01, 0x02, 0xEF, 0xFF}; | 105 test_vector = {0x01, 0x02, 0xEF, 0xFF}; |
| 106 message.insert(message.begin(), test_vector.begin(), test_vector.end()); | 106 message.insert(message.begin(), test_vector.begin(), test_vector.end()); |
| 107 response = U2fApduResponse::CreateFromMessage(message); | 107 response = U2fApduResponse::CreateFromMessage(message); |
| 108 ASSERT_NE(nullptr, response); | 108 ASSERT_NE(nullptr, response); |
| 109 EXPECT_EQ(U2fApduResponse::Status::SW_NO_ERROR, response->response_status_); | 109 EXPECT_EQ(U2fApduResponse::Status::SW_NO_ERROR, response->response_status_); |
| 110 EXPECT_THAT(response->data_, testing::ContainerEq(test_vector)); | 110 EXPECT_THAT(response->data_, testing::ContainerEq(test_vector)); |
| 111 } | 111 } |
| 112 | 112 |
| 113 TEST_F(U2fApduTest, TestSerializeCommand) { | 113 TEST_F(U2fApduTest, TestSerializeCommand) { |
| 114 scoped_refptr<U2fApduCommand> cmd = U2fApduCommand::Create(); | 114 std::unique_ptr<U2fApduCommand> cmd(new U2fApduCommand()); |
|
Reilly Grant (use Gerrit)
2017/03/20 17:44:37
Use base::MakeUnique here and below.
| |
| 115 | 115 |
| 116 cmd->set_cla(0xA); | 116 cmd->set_cla(0xA); |
| 117 cmd->set_ins(0xB); | 117 cmd->set_ins(0xB); |
| 118 cmd->set_p1(0xC); | 118 cmd->set_p1(0xC); |
| 119 cmd->set_p2(0xD); | 119 cmd->set_p2(0xD); |
| 120 | 120 |
| 121 // No data, no response expected | 121 // No data, no response expected |
| 122 std::vector<uint8_t> expected({0xA, 0xB, 0xC, 0xD}); | 122 std::vector<uint8_t> expected({0xA, 0xB, 0xC, 0xD}); |
| 123 ASSERT_THAT(expected, testing::ContainerEq(cmd->GetEncodedCommand())); | 123 ASSERT_THAT(expected, testing::ContainerEq(cmd->GetEncodedCommand())); |
| 124 EXPECT_THAT( | 124 EXPECT_THAT( |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 150 cmd->set_response_length(0); | 150 cmd->set_response_length(0); |
| 151 expected = {0xA, 0xB, 0xC, 0xD, 0x0, 0x0, 0x4, 0x1, 0x2, 0x3, 0x4}; | 151 expected = {0xA, 0xB, 0xC, 0xD, 0x0, 0x0, 0x4, 0x1, 0x2, 0x3, 0x4}; |
| 152 EXPECT_THAT(expected, testing::ContainerEq(cmd->GetEncodedCommand())); | 152 EXPECT_THAT(expected, testing::ContainerEq(cmd->GetEncodedCommand())); |
| 153 EXPECT_THAT( | 153 EXPECT_THAT( |
| 154 expected, | 154 expected, |
| 155 testing::ContainerEq( | 155 testing::ContainerEq( |
| 156 U2fApduCommand::CreateFromMessage(expected)->GetEncodedCommand())); | 156 U2fApduCommand::CreateFromMessage(expected)->GetEncodedCommand())); |
| 157 } | 157 } |
| 158 | 158 |
| 159 TEST_F(U2fApduTest, TestSerializeEdgeCases) { | 159 TEST_F(U2fApduTest, TestSerializeEdgeCases) { |
| 160 scoped_refptr<U2fApduCommand> cmd = U2fApduCommand::Create(); | 160 std::unique_ptr<U2fApduCommand> cmd(new U2fApduCommand()); |
| 161 | 161 |
| 162 cmd->set_cla(0xA); | 162 cmd->set_cla(0xA); |
| 163 cmd->set_ins(0xB); | 163 cmd->set_ins(0xB); |
| 164 cmd->set_p1(0xC); | 164 cmd->set_p1(0xC); |
| 165 cmd->set_p2(0xD); | 165 cmd->set_p2(0xD); |
| 166 | 166 |
| 167 // Set response length to maximum, which should serialize to 0x0000 | 167 // Set response length to maximum, which should serialize to 0x0000 |
| 168 cmd->set_response_length(U2fApduCommand::kApduMaxResponseLength); | 168 cmd->set_response_length(U2fApduCommand::kApduMaxResponseLength); |
| 169 std::vector<uint8_t> expected = {0xA, 0xB, 0xC, 0xD, 0x0, 0x0, 0x0}; | 169 std::vector<uint8_t> expected = {0xA, 0xB, 0xC, 0xD, 0x0, 0x0, 0x0}; |
| 170 EXPECT_THAT(expected, testing::ContainerEq(cmd->GetEncodedCommand())); | 170 EXPECT_THAT(expected, testing::ContainerEq(cmd->GetEncodedCommand())); |
| 171 EXPECT_THAT( | 171 EXPECT_THAT( |
| 172 expected, | 172 expected, |
| 173 testing::ContainerEq( | 173 testing::ContainerEq( |
| 174 U2fApduCommand::CreateFromMessage(expected)->GetEncodedCommand())); | 174 U2fApduCommand::CreateFromMessage(expected)->GetEncodedCommand())); |
| 175 | 175 |
| 176 // Maximum data size | 176 // Maximum data size |
| 177 std::vector<uint8_t> oversized(U2fApduCommand::kApduMaxDataLength); | 177 std::vector<uint8_t> oversized(U2fApduCommand::kApduMaxDataLength); |
| 178 cmd->set_data(oversized); | 178 cmd->set_data(oversized); |
| 179 EXPECT_THAT(cmd->GetEncodedCommand(), | 179 EXPECT_THAT(cmd->GetEncodedCommand(), |
| 180 testing::ContainerEq( | 180 testing::ContainerEq( |
| 181 U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand()) | 181 U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand()) |
| 182 ->GetEncodedCommand())); | 182 ->GetEncodedCommand())); |
| 183 } | 183 } |
| 184 | 184 |
| 185 TEST_F(U2fApduTest, TestCreateSign) { | 185 TEST_F(U2fApduTest, TestCreateSign) { |
| 186 std::vector<uint8_t> appid(U2fApduCommand::kAppIdDigestLen, 0x01); | 186 std::vector<uint8_t> appid(U2fApduCommand::kAppIdDigestLen, 0x01); |
| 187 std::vector<uint8_t> challenge(U2fApduCommand::kChallengeDigestLen, 0xff); | 187 std::vector<uint8_t> challenge(U2fApduCommand::kChallengeDigestLen, 0xff); |
| 188 std::vector<uint8_t> key_handle(U2fApduCommand::kMaxKeyHandleLength); | 188 std::vector<uint8_t> key_handle(U2fApduCommand::kMaxKeyHandleLength); |
| 189 | 189 |
| 190 scoped_refptr<U2fApduCommand> cmd = | 190 std::unique_ptr<U2fApduCommand> cmd = |
| 191 U2fApduCommand::CreateSign(appid, challenge, key_handle); | 191 U2fApduCommand::CreateSign(appid, challenge, key_handle); |
| 192 ASSERT_NE(nullptr, cmd); | 192 ASSERT_NE(nullptr, cmd); |
| 193 EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand()) | 193 EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand()) |
| 194 ->GetEncodedCommand(), | 194 ->GetEncodedCommand(), |
| 195 testing::ContainerEq(cmd->GetEncodedCommand())); | 195 testing::ContainerEq(cmd->GetEncodedCommand())); |
| 196 // Expect null result with incorrectly sized key handle | 196 // Expect null result with incorrectly sized key handle |
| 197 key_handle.push_back(0x0f); | 197 key_handle.push_back(0x0f); |
| 198 cmd = U2fApduCommand::CreateSign(appid, challenge, key_handle); | 198 cmd = U2fApduCommand::CreateSign(appid, challenge, key_handle); |
| 199 EXPECT_EQ(nullptr, cmd); | 199 EXPECT_EQ(nullptr, cmd); |
| 200 key_handle.pop_back(); | 200 key_handle.pop_back(); |
| 201 // Expect null result with incorrectly sized appid | 201 // Expect null result with incorrectly sized appid |
| 202 appid.pop_back(); | 202 appid.pop_back(); |
| 203 cmd = U2fApduCommand::CreateSign(appid, challenge, key_handle); | 203 cmd = U2fApduCommand::CreateSign(appid, challenge, key_handle); |
| 204 EXPECT_EQ(nullptr, cmd); | 204 EXPECT_EQ(nullptr, cmd); |
| 205 appid.push_back(0xff); | 205 appid.push_back(0xff); |
| 206 // Expect null result with incorrectly sized challenge | 206 // Expect null result with incorrectly sized challenge |
| 207 challenge.push_back(0x0); | 207 challenge.push_back(0x0); |
| 208 cmd = U2fApduCommand::CreateSign(appid, challenge, key_handle); | 208 cmd = U2fApduCommand::CreateSign(appid, challenge, key_handle); |
| 209 EXPECT_EQ(nullptr, cmd); | 209 EXPECT_EQ(nullptr, cmd); |
| 210 } | 210 } |
| 211 | 211 |
| 212 TEST_F(U2fApduTest, TestCreateRegister) { | 212 TEST_F(U2fApduTest, TestCreateRegister) { |
| 213 std::vector<uint8_t> appid(U2fApduCommand::kAppIdDigestLen, 0x01); | 213 std::vector<uint8_t> appid(U2fApduCommand::kAppIdDigestLen, 0x01); |
| 214 std::vector<uint8_t> challenge(U2fApduCommand::kChallengeDigestLen, 0xff); | 214 std::vector<uint8_t> challenge(U2fApduCommand::kChallengeDigestLen, 0xff); |
| 215 scoped_refptr<U2fApduCommand> cmd = | 215 std::unique_ptr<U2fApduCommand> cmd = |
| 216 U2fApduCommand::CreateRegister(appid, challenge); | 216 U2fApduCommand::CreateRegister(appid, challenge); |
| 217 ASSERT_NE(nullptr, cmd); | 217 ASSERT_NE(nullptr, cmd); |
| 218 EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand()) | 218 EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand()) |
| 219 ->GetEncodedCommand(), | 219 ->GetEncodedCommand(), |
| 220 testing::ContainerEq(cmd->GetEncodedCommand())); | 220 testing::ContainerEq(cmd->GetEncodedCommand())); |
| 221 // Expect null result with incorrectly sized appid | 221 // Expect null result with incorrectly sized appid |
| 222 appid.push_back(0xff); | 222 appid.push_back(0xff); |
| 223 cmd = U2fApduCommand::CreateRegister(appid, challenge); | 223 cmd = U2fApduCommand::CreateRegister(appid, challenge); |
| 224 EXPECT_EQ(nullptr, cmd); | 224 EXPECT_EQ(nullptr, cmd); |
| 225 appid.pop_back(); | 225 appid.pop_back(); |
| 226 // Expect null result with incorrectly sized challenge | 226 // Expect null result with incorrectly sized challenge |
| 227 challenge.push_back(0xff); | 227 challenge.push_back(0xff); |
| 228 cmd = U2fApduCommand::CreateRegister(appid, challenge); | 228 cmd = U2fApduCommand::CreateRegister(appid, challenge); |
| 229 EXPECT_EQ(nullptr, cmd); | 229 EXPECT_EQ(nullptr, cmd); |
| 230 } | 230 } |
| 231 | 231 |
| 232 TEST_F(U2fApduTest, TestCreateVersion) { | 232 TEST_F(U2fApduTest, TestCreateVersion) { |
| 233 scoped_refptr<U2fApduCommand> cmd = U2fApduCommand::CreateVersion(); | 233 std::unique_ptr<U2fApduCommand> cmd = U2fApduCommand::CreateVersion(); |
| 234 std::vector<uint8_t> expected = { | 234 std::vector<uint8_t> expected = { |
| 235 0x0, U2fApduCommand::kInsU2fVersion, 0x0, 0x0, 0x0, 0x0, 0x0}; | 235 0x0, U2fApduCommand::kInsU2fVersion, 0x0, 0x0, 0x0, 0x0, 0x0}; |
| 236 | 236 |
| 237 EXPECT_THAT(expected, testing::ContainerEq(cmd->GetEncodedCommand())); | 237 EXPECT_THAT(expected, testing::ContainerEq(cmd->GetEncodedCommand())); |
| 238 EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand()) | 238 EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand()) |
| 239 ->GetEncodedCommand(), | 239 ->GetEncodedCommand(), |
| 240 testing::ContainerEq(cmd->GetEncodedCommand())); | 240 testing::ContainerEq(cmd->GetEncodedCommand())); |
| 241 } | 241 } |
| 242 | 242 |
| 243 TEST_F(U2fApduTest, TestCreateLegacyVersion) { | 243 TEST_F(U2fApduTest, TestCreateLegacyVersion) { |
| 244 scoped_refptr<U2fApduCommand> cmd = U2fApduCommand::CreateLegacyVersion(); | 244 std::unique_ptr<U2fApduCommand> cmd = U2fApduCommand::CreateLegacyVersion(); |
| 245 // Legacy version command contains 2 extra null bytes compared to ISO 7816-4 | 245 // Legacy version command contains 2 extra null bytes compared to ISO 7816-4 |
| 246 // format | 246 // format |
| 247 std::vector<uint8_t> expected = { | 247 std::vector<uint8_t> expected = { |
| 248 0x0, U2fApduCommand::kInsU2fVersion, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; | 248 0x0, U2fApduCommand::kInsU2fVersion, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; |
| 249 | 249 |
| 250 EXPECT_THAT(expected, testing::ContainerEq(cmd->GetEncodedCommand())); | 250 EXPECT_THAT(expected, testing::ContainerEq(cmd->GetEncodedCommand())); |
| 251 EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand()) | 251 EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand()) |
| 252 ->GetEncodedCommand(), | 252 ->GetEncodedCommand(), |
| 253 testing::ContainerEq(cmd->GetEncodedCommand())); | 253 testing::ContainerEq(cmd->GetEncodedCommand())); |
| 254 } | 254 } |
| 255 } // namespace device | 255 } // namespace device |
| OLD | NEW |