| 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 { |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 |
| 185 TEST_F(U2fApduTest, TestCreateSign) { |
| 186 std::vector<uint8_t> appid(U2fApduCommand::kAppIdDigestLen, 0x01); |
| 187 std::vector<uint8_t> challenge(U2fApduCommand::kChallengeDigestLen, 0xff); |
| 188 std::vector<uint8_t> key_handle(U2fApduCommand::kMaxKeyHandleLength); |
| 189 |
| 190 scoped_refptr<U2fApduCommand> cmd = |
| 191 U2fApduCommand::CreateSign(appid, challenge, key_handle); |
| 192 ASSERT_NE(nullptr, cmd); |
| 193 EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand()) |
| 194 ->GetEncodedCommand(), |
| 195 testing::ContainerEq(cmd->GetEncodedCommand())); |
| 196 // Expect null result with incorrectly sized key handle |
| 197 key_handle.push_back(0x0f); |
| 198 cmd = U2fApduCommand::CreateSign(appid, challenge, key_handle); |
| 199 EXPECT_EQ(nullptr, cmd); |
| 200 key_handle.pop_back(); |
| 201 // Expect null result with incorrectly sized appid |
| 202 appid.pop_back(); |
| 203 cmd = U2fApduCommand::CreateSign(appid, challenge, key_handle); |
| 204 EXPECT_EQ(nullptr, cmd); |
| 205 appid.push_back(0xff); |
| 206 // Expect null result with incorrectly sized challenge |
| 207 challenge.push_back(0x0); |
| 208 cmd = U2fApduCommand::CreateSign(appid, challenge, key_handle); |
| 209 EXPECT_EQ(nullptr, cmd); |
| 210 } |
| 211 |
| 212 TEST_F(U2fApduTest, TestCreateRegister) { |
| 213 std::vector<uint8_t> appid(U2fApduCommand::kAppIdDigestLen, 0x01); |
| 214 std::vector<uint8_t> challenge(U2fApduCommand::kChallengeDigestLen, 0xff); |
| 215 scoped_refptr<U2fApduCommand> cmd = |
| 216 U2fApduCommand::CreateRegister(appid, challenge); |
| 217 ASSERT_NE(nullptr, cmd); |
| 218 EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand()) |
| 219 ->GetEncodedCommand(), |
| 220 testing::ContainerEq(cmd->GetEncodedCommand())); |
| 221 // Expect null result with incorrectly sized appid |
| 222 appid.push_back(0xff); |
| 223 cmd = U2fApduCommand::CreateRegister(appid, challenge); |
| 224 EXPECT_EQ(nullptr, cmd); |
| 225 appid.pop_back(); |
| 226 // Expect null result with incorrectly sized challenge |
| 227 challenge.push_back(0xff); |
| 228 cmd = U2fApduCommand::CreateRegister(appid, challenge); |
| 229 EXPECT_EQ(nullptr, cmd); |
| 230 } |
| 231 |
| 232 TEST_F(U2fApduTest, TestCreateVersion) { |
| 233 scoped_refptr<U2fApduCommand> cmd = U2fApduCommand::CreateVersion(); |
| 234 std::vector<uint8_t> expected = { |
| 235 0x0, U2fApduCommand::kInsU2fVersion, 0x0, 0x0, 0x0, 0x0, 0x0}; |
| 236 |
| 237 EXPECT_THAT(expected, testing::ContainerEq(cmd->GetEncodedCommand())); |
| 238 EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand()) |
| 239 ->GetEncodedCommand(), |
| 240 testing::ContainerEq(cmd->GetEncodedCommand())); |
| 241 } |
| 242 |
| 243 TEST_F(U2fApduTest, TestCreateLegacyVersion) { |
| 244 scoped_refptr<U2fApduCommand> cmd = U2fApduCommand::CreateLegacyVersion(); |
| 245 // Legacy version command contains 2 extra null bytes compared to ISO 7816-4 |
| 246 // format |
| 247 std::vector<uint8_t> expected = { |
| 248 0x0, U2fApduCommand::kInsU2fVersion, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; |
| 249 |
| 250 EXPECT_THAT(expected, testing::ContainerEq(cmd->GetEncodedCommand())); |
| 251 EXPECT_THAT(U2fApduCommand::CreateFromMessage(cmd->GetEncodedCommand()) |
| 252 ->GetEncodedCommand(), |
| 253 testing::ContainerEq(cmd->GetEncodedCommand())); |
| 254 } |
| 184 } // namespace device | 255 } // namespace device |
| OLD | NEW |