1
0
mirror of https://github.com/facebookincubator/mvfst.git synced 2025-11-10 21:22:20 +03:00
Files
mvfst/quic/codec/test/QuicConnectionIdTest.cpp
Subodh Iyengar e3845d7562 Use array instead of vector for connid
Summary:
In perf traces we see that the allocations of the vector memory shows up.

The maximum connid size is 20 bytes. We previously used a vector because
we thought that it might save some space, however the default size of a vector
is actually 24 bytes.

This diff replaces the vector with an array of size Maximum connid size. This
should keep most code the same, and now will only consume 20 + 1 byte + some alignment bytes.
This should make all the vector memory allocations go away during parsing.

Reviewed By: yangchi

Differential Revision: D17806689

fbshipit-source-id: 041d5f18624fb34ce53534c75b2bdca3513fa107
2019-10-08 13:06:00 -07:00

114 lines
3.3 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
#include <quic/codec/QuicConnectionId.h>
#include <iterator>
#include <folly/portability/GTest.h>
using namespace testing;
namespace quic {
namespace test {
TEST(ConnectionIdTest, TestConnidLen) {
std::string out = folly::unhexlify("ffaabbee00");
folly::IOBuf buf = folly::IOBuf::wrapBufferAsValue(out.data(), out.size());
folly::io::Cursor cursor(&buf);
ConnectionId connid(cursor, out.size());
EXPECT_EQ(static_cast<size_t>(connid.size()), out.size());
for (size_t i = 0; i < connid.size(); ++i) {
EXPECT_EQ(*(connid.data() + i), static_cast<uint8_t>(out[i]));
}
std::string hexconnid = folly::hexlify(out);
EXPECT_EQ(connid.hex(), hexconnid);
}
TEST(ConnectionIdTest, TestZeroLenConnid) {
std::string out = "";
folly::IOBuf buf = folly::IOBuf::wrapBufferAsValue(out.data(), out.size());
folly::io::Cursor cursor(&buf);
ConnectionId connid(cursor, out.size());
EXPECT_EQ(static_cast<size_t>(connid.size()), out.size());
}
TEST(ConnectionIdTest, CompareConnId) {
ConnectionId connid1(std::vector<uint8_t>{});
ConnectionId connid2(std::vector<uint8_t>{});
EXPECT_EQ(connid1, connid2);
ConnectionId connid3(std::vector<uint8_t>{0x00, 0x01, 0x02, 0x03});
ConnectionId connid4(std::vector<uint8_t>{0x00, 0x01, 0x02, 0x03});
EXPECT_NE(connid3, connid1);
EXPECT_NE(connid1, connid3);
EXPECT_EQ(connid3, connid4);
EXPECT_EQ(connid4, connid3);
}
TEST(ConnectionIdTest, ConnIdSize) {
std::vector<uint8_t> testconnid;
for (size_t i = 0; i < kMaxConnectionIdSize + 2; ++i) {
testconnid.push_back(0);
}
EXPECT_THROW(ConnectionId{testconnid}, std::runtime_error);
testconnid.clear();
for (size_t i = 0; i < kMinConnectionIdSize - 1; ++i) {
testconnid.push_back(0);
}
EXPECT_THROW(ConnectionId{testconnid}, std::runtime_error);
}
struct ConnectionIdLengthParams {
uint8_t dcidLen;
uint8_t scidLen;
uint8_t lengthByte;
};
class ConnectionIdLengthTest : public TestWithParam<ConnectionIdLengthParams> {
};
TEST_P(ConnectionIdLengthTest, TestDecode) {
auto decoded = decodeConnectionIdLengths(GetParam().lengthByte);
EXPECT_EQ(decoded.first, GetParam().dcidLen);
EXPECT_EQ(decoded.second, GetParam().scidLen);
}
TEST_P(ConnectionIdLengthTest, TestEncode) {
auto length =
encodeConnectionIdLengths(GetParam().dcidLen, GetParam().scidLen);
EXPECT_EQ(length, GetParam().lengthByte);
}
TEST_P(ConnectionIdLengthTest, DecodeEncode) {
auto length =
encodeConnectionIdLengths(GetParam().dcidLen, GetParam().scidLen);
auto decoded = decodeConnectionIdLengths(length);
EXPECT_EQ(decoded.first, GetParam().dcidLen);
EXPECT_EQ(decoded.second, GetParam().scidLen);
}
TEST_P(ConnectionIdLengthTest, EncodeDecode) {
auto decoded = decodeConnectionIdLengths(GetParam().lengthByte);
auto length = encodeConnectionIdLengths(decoded.first, decoded.second);
EXPECT_EQ(length, GetParam().lengthByte);
}
INSTANTIATE_TEST_CASE_P(
ConnectionIdLengthTests,
ConnectionIdLengthTest,
testing::Values(
(ConnectionIdLengthParams){0, 0, 0},
(ConnectionIdLengthParams){0, 4, 0x01},
(ConnectionIdLengthParams){4, 18, 0x1F},
(ConnectionIdLengthParams){18, 18, 0xFF}));
} // namespace test
} // namespace quic