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