1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
// Copyright 2017 The Chromium Authors. All rights reserved.
// Copyright (C) 2018 Apple Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "config.h"
#if ENABLE(WEB_AUTHN)
#include <WebCore/FidoConstants.h>
#include <WebCore/FidoHidMessage.h>
#include <WebCore/FidoHidPacket.h>
#include <wtf/Deque.h>
#include <wtf/Vector.h>
namespace TestWebKitAPI {
using namespace fido;
// Packets should be 64 bytes + 1 report ID byte.
TEST(FidoHidMessageTest, TestPacketSize)
{
uint32_t channelId = 0x05060708;
Vector<uint8_t> data;
auto initPacket = makeUnique<FidoHidInitPacket>(channelId, FidoHidDeviceCommand::kInit, Vector<uint8_t>(data), data.size());
EXPECT_EQ(64u, initPacket->getSerializedData().size());
auto continuationPacket = makeUnique<FidoHidContinuationPacket>(channelId, 0, WTFMove(data));
EXPECT_EQ(64u, continuationPacket->getSerializedData().size());
}
/*
* CTAP HID Init Packets are of the format:
* Byte 0-3: Channel ID
* Byte 4: Command byte
* Byte 5-6: Big Endian size of data
* Byte 7-n: Data block
*
* Remaining buffer is padded with 0
*/
TEST(FidoHidMessageTest, TestPacketData1)
{
uint32_t channelId = 0xF5060708;
Vector<uint8_t> data {10, 11};
FidoHidDeviceCommand cmd = FidoHidDeviceCommand::kWink;
auto initPacket = makeUnique<FidoHidInitPacket>(channelId, cmd, Vector<uint8_t>(data), data.size());
size_t index = 0;
Vector<uint8_t> serialized = initPacket->getSerializedData();
EXPECT_EQ((channelId >> 24) & 0xff, serialized[index++]);
EXPECT_EQ((channelId >> 16) & 0xff, serialized[index++]);
EXPECT_EQ((channelId >> 8) & 0xff, serialized[index++]);
EXPECT_EQ(channelId & 0xff, serialized[index++]);
EXPECT_EQ(static_cast<uint8_t>(cmd), serialized[index++] & 0x7f);
EXPECT_EQ(data.size() >> 8, serialized[index++]);
EXPECT_EQ(data.size() & 0xff, serialized[index++]);
EXPECT_EQ(data[0], serialized[index++]);
EXPECT_EQ(data[1], serialized[index++]);
for (; index < serialized.size(); index++)
EXPECT_EQ(0, serialized[index]) << "mismatch at index " << index;
}
/*
* CTAP HID Continuation Packets are of the format:
* Byte 0-3: Channel ID
* Byte 4: SEQ
* Byte 5-n: Data block
*
* Remaining buffer is padded with 0
*/
TEST(FidoHidMessageTest, TestPacketData2)
{
uint32_t channelId = 0xF5060708;
Vector<uint8_t> data {10, 11};
auto initPacket = makeUnique<FidoHidContinuationPacket>(channelId, 0, Vector<uint8_t>(data));
size_t index = 0;
Vector<uint8_t> serialized = initPacket->getSerializedData();
EXPECT_EQ((channelId >> 24) & 0xff, serialized[index++]);
EXPECT_EQ((channelId >> 16) & 0xff, serialized[index++]);
EXPECT_EQ((channelId >> 8) & 0xff, serialized[index++]);
EXPECT_EQ(channelId & 0xff, serialized[index++]);
EXPECT_EQ(0, serialized[index++]);
EXPECT_EQ(data[0], serialized[index++]);
EXPECT_EQ(data[1], serialized[index++]);
for (; index < serialized.size(); index++)
EXPECT_EQ(0, serialized[index]) << "mismatch at index " << index;
}
TEST(FidoHidMessageTest, TestPacketConstructors)
{
uint32_t channelId = 0x05060708;
Vector<uint8_t> data {10, 11};
FidoHidDeviceCommand cmd = FidoHidDeviceCommand::kWink;
size_t length = data.size();
auto origPacket = makeUnique<FidoHidInitPacket>(channelId, cmd, WTFMove(data), length);
size_t payloadLength = static_cast<size_t>(origPacket->payloadLength());
Vector<uint8_t> origData = origPacket->getSerializedData();
auto reconstructedPacket = FidoHidInitPacket::createFromSerializedData(origData, &payloadLength);
EXPECT_EQ(origPacket->command(), reconstructedPacket->command());
EXPECT_EQ(origPacket->payloadLength(), reconstructedPacket->payloadLength());
EXPECT_EQ(origPacket->getPacketPayload(), reconstructedPacket->getPacketPayload());
EXPECT_EQ(channelId, reconstructedPacket->channelId());
ASSERT_EQ(origPacket->getSerializedData().size(), reconstructedPacket->getSerializedData().size());
for (size_t index = 0; index < origPacket->getSerializedData().size(); ++index)
EXPECT_EQ(origPacket->getSerializedData()[index], reconstructedPacket->getSerializedData()[index]) << "mismatch at index " << index;
}
TEST(FidoHidMessageTest, TestMaxLengthPacketConstructors)
{
uint32_t channelId = 0xAAABACAD;
Vector<uint8_t> data;
for (size_t i = 0; i < kHidMaxMessageSize; ++i)
data.append(static_cast<uint8_t>(i % 0xff));
auto origMsg = FidoHidMessage::create(channelId, FidoHidDeviceCommand::kMsg, data);
ASSERT_TRUE(origMsg);
const auto& originalMsgPackets = origMsg->getPacketsForTesting();
auto it = originalMsgPackets.begin();
auto msgData = (*it)->getSerializedData();
auto newMsg = FidoHidMessage::createFromSerializedData(msgData);
++it;
for (; it != originalMsgPackets.end(); ++it) {
msgData = (*it)->getSerializedData();
EXPECT_TRUE(newMsg->addContinuationPacket(msgData));
}
auto origIt = originalMsgPackets.begin();
const auto& newMsgPackets = newMsg->getPacketsForTesting();
auto newMsgIt = newMsgPackets.begin();
EXPECT_EQ(origMsg->numPackets(), newMsg->numPackets());
for (; origIt != originalMsgPackets.end() || newMsgIt != newMsgPackets.end(); ++origIt, ++newMsgIt) {
EXPECT_EQ((*origIt)->getPacketPayload(), (*newMsgIt)->getPacketPayload());
EXPECT_EQ((*origIt)->channelId(), (*newMsgIt)->channelId());
ASSERT_EQ((*origIt)->getSerializedData().size(), (*newMsgIt)->getSerializedData().size());
for (size_t index = 0; index < (*origIt)->getSerializedData().size(); ++index)
EXPECT_EQ((*origIt)->getSerializedData()[index], (*newMsgIt)->getSerializedData()[index]) << "mismatch at index " << index;
}
}
TEST(FidoHidMessageTest, TestMessagePartitoning)
{
uint32_t channelId = 0x01010203;
Vector<uint8_t> data(kHidInitPacketDataSize + 1);
auto twoPacketMessage = FidoHidMessage::create(channelId, FidoHidDeviceCommand::kPing, data);
ASSERT_TRUE(twoPacketMessage);
EXPECT_EQ(2U, twoPacketMessage->numPackets());
data.resize(kHidInitPacketDataSize);
auto onePacketMessage = FidoHidMessage::create(channelId, FidoHidDeviceCommand::kPing, data);
ASSERT_TRUE(onePacketMessage);
EXPECT_EQ(1U, onePacketMessage->numPackets());
data.resize(kHidInitPacketDataSize + kHidContinuationPacketDataSize + 1);
auto threePacketMessage = FidoHidMessage::create(channelId, FidoHidDeviceCommand::kPing, data);
ASSERT_TRUE(threePacketMessage);
EXPECT_EQ(3U, threePacketMessage->numPackets());
}
TEST(FidoHidMessageTest, TestMaxSize)
{
uint32_t channelId = 0x00010203;
Vector<uint8_t> data(kHidMaxMessageSize + 1);
auto oversizeMessage = FidoHidMessage::create(channelId, FidoHidDeviceCommand::kPing, data);
EXPECT_FALSE(oversizeMessage);
}
TEST(FidoHidMessageTest, TestDeconstruct)
{
uint32_t channelId = 0x0A0B0C0D;
Vector<uint8_t> data(kHidMaxMessageSize, 0x7F);
auto filledMessage = FidoHidMessage::create(channelId, FidoHidDeviceCommand::kPing, data);
ASSERT_TRUE(filledMessage);
EXPECT_EQ(data, filledMessage->getMessagePayload());
}
TEST(FidoHidMessageTest, TestDeserialize)
{
uint32_t channelId = 0x0A0B0C0D;
Vector<uint8_t> data(kHidMaxMessageSize);
auto origMessage = FidoHidMessage::create(channelId, FidoHidDeviceCommand::kPing, data);
ASSERT_TRUE(origMessage);
Deque<Vector<uint8_t>> origList;
auto buf = origMessage->popNextPacket();
origList.append(buf);
auto newMessage = FidoHidMessage::createFromSerializedData(buf);
while (!newMessage->messageComplete()) {
buf = origMessage->popNextPacket();
origList.append(buf);
newMessage->addContinuationPacket(buf);
}
while (!(buf = newMessage->popNextPacket()).isEmpty()) {
EXPECT_EQ(buf, origList.first());
origList.removeFirst();
}
}
TEST(FidoHidMessageTest, TestProperties)
{
uint32_t channelId = 0x05060708;
Vector<uint8_t> data;
auto message = FidoHidMessage::create(channelId, FidoHidDeviceCommand::kCancel, data);
EXPECT_EQ(channelId, message->channelId());
EXPECT_EQ(FidoHidDeviceCommand::kCancel, message->cmd());
}
} // namespace TestWebKitAPI
#endif // ENABLE(WEB_AUTHN)
|