proxygen
TokenBindingTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #include <gmock/gmock.h>
10 #include <gtest/gtest.h>
11 
14 #include <fizz/record/Extensions.h>
15 #include <fizz/record/Types.h>
17 
18 using namespace fizz::extensions;
19 
20 using namespace folly;
21 using namespace folly::io;
22 
23 using testing::_;
24 using namespace testing;
25 
26 StringPiece tokenBinding{"00180004000d0102"};
27 
28 // Received from chrome using ServerSocket.cpp
30  "00890002004140dd2fa2430a0f54ca96454bdf23c264353a252812bc5fe7b851a6fe9d620424be43f20e50a4ca0a1769f4024db346ca5075eecdb7f62d0018cf1642b75f679d98004089915dea6f6d4d46db349993c5194e709fd22e10bc63ed2b1dfc1f58f300abc13d28c4ecb4dc1dadc8597f813d5f129a58181d3489db69766d832919fbe38a940000"};
31 
32 namespace fizz {
33 namespace test {
34 
36  auto exts = getExtensions(tokenBinding);
37  auto ext = getExtension<TokenBindingParameters>(exts);
38 
40  EXPECT_EQ(ext->key_parameters_list.size(), 1);
41  EXPECT_EQ(ext->key_parameters_list[0], TokenBindingKeyParameters::ecdsap256);
42 
43  checkEncode(std::move(*ext), tokenBinding);
44 }
45 
46 TEST_F(ExtensionsTest, TokenBindingMessageFromChrome) {
47  auto buf = getBuf(tokenBindingMessage);
48  auto message = decode<TokenBindingMessage>(std::move(buf));
49  EXPECT_EQ(message.tokenbindings.size(), 1);
50  auto& tokenbinding = message.tokenbindings.front();
51  EXPECT_EQ(
52  tokenbinding.tokenbinding_type, TokenBindingType::provided_token_binding);
53  auto& id = tokenbinding.tokenbindingid;
55 
56  // 32 * 2 for the key + 1 for size of point
57  EXPECT_EQ(id.key->computeChainDataLength(), 65);
58 
59  // No extensions
60  EXPECT_EQ(tokenbinding.extensions->computeChainDataLength(), 0);
61 
62  // 32 * 2 for the signature with this algorithm
63  EXPECT_EQ(tokenbinding.signature->computeChainDataLength(), 64);
64  auto encodedBuf = encode(std::move(message));
65  EXPECT_TRUE(folly::IOBufEqualTo()(encodedBuf, buf));
66 }
67 
68 TEST_F(ExtensionsTest, TokenBindingMessageSelfCreated) {
72  TokenBindingID id;
74  id.key = folly::IOBuf::create(10);
75  tokenBinding.tokenbindingid = std::move(id);
76  tokenBinding.signature = folly::IOBuf::create(10);
77  tokenBinding.extensions = folly::IOBuf::create(10);
78  message.tokenbindings.push_back(std::move(tokenBinding));
79 
80  auto encoded = encode(std::move(message));
81  auto decoded = decode<TokenBindingMessage>(std::move(encoded));
82 
83  EXPECT_EQ(decoded.tokenbindings.size(), 1);
84  auto& decodedTokenBinding = message.tokenbindings.front();
85  auto& decodedId = decodedTokenBinding.tokenbindingid;
86  EXPECT_EQ(
87  decodedTokenBinding.tokenbinding_type,
89  EXPECT_EQ(
90  decodedId.key_parameters, TokenBindingKeyParameters::rsa2048_pkcs1_5);
91 }
92 
93 TEST(ContextTest, TestAscendingVersionsNotSet) {
94  std::vector<TokenBindingProtocolVersion> ascending{
99  auto before = ctx.getSupportedVersions();
100  EXPECT_THROW(ctx.setSupportedVersions(ascending), std::runtime_error);
101  auto after = ctx.getSupportedVersions();
102  EXPECT_EQ(before, after);
103 }
104 
105 TEST(ContextTest, TestShuffledNotSet) {
106  std::vector<TokenBindingProtocolVersion> shuffled{
111  auto before = ctx.getSupportedVersions();
112  EXPECT_THROW(ctx.setSupportedVersions(shuffled), std::runtime_error);
113  auto after = ctx.getSupportedVersions();
114  EXPECT_EQ(before, after);
115 }
116 
117 TEST(ContextTest, TestDoubleNotSet) {
118  std::vector<TokenBindingProtocolVersion> two{
122  auto before = ctx.getSupportedVersions();
123  EXPECT_THROW(ctx.setSupportedVersions(two), std::runtime_error);
124  auto after = ctx.getSupportedVersions();
125  EXPECT_EQ(before, after);
126 }
127 
128 TEST(ContextTest, TestEmptySet) {
129  std::vector<TokenBindingProtocolVersion> empty;
131  ctx.setSupportedVersions(empty);
132  auto after = ctx.getSupportedVersions();
133  EXPECT_EQ(empty, after);
134 }
135 
136 TEST(ContextTest, TestSingleSet) {
137  std::vector<TokenBindingProtocolVersion> single{
141  auto after = ctx.getSupportedVersions();
142  EXPECT_EQ(single, after);
143 }
144 
145 TEST(ContextTest, TestDescendingVersionsSet) {
146  std::vector<TokenBindingProtocolVersion> descending{
151  ctx.setSupportedVersions(descending);
152  auto after = ctx.getSupportedVersions();
153  EXPECT_EQ(descending, after);
154 }
155 
156 TEST(ContextTest, TestDoubleSet) {
157  std::vector<TokenBindingProtocolVersion> two{
161  ctx.setSupportedVersions(two);
162  auto after = ctx.getSupportedVersions();
163  EXPECT_EQ(two, after);
164 }
165 
166 TEST(ContextTest, TestDescendingDuplicatesNotSet) {
167  std::vector<TokenBindingProtocolVersion> duplicate{
170  TokenBindingProtocolVersion::token_binding_0_11};
172  auto before = ctx.getSupportedVersions();
173  EXPECT_THROW(ctx.setSupportedVersions(duplicate), std::runtime_error);
174  auto after = ctx.getSupportedVersions();
175  EXPECT_EQ(before, after);
176 }
177 } // namespace test
178 } // namespace fizz
Definition: test.c:42
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST_F(ExtensionsTest, TokenBindingMessageSelfCreated)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void setSupportedVersions(std::vector< TokenBindingProtocolVersion > versions)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
TokenBindingType tokenbinding_type
Definition: Types.h:57
TEST(ContextTest, TestDescendingDuplicatesNotSet)
std::vector< TokenBinding > tokenbindings
Definition: Types.h:66
StringPiece tokenBindingMessage
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
Definition: Actions.h:16
std::string message
Definition: SPDYCodec.cpp:133
StringPiece tokenBinding
TokenBindingKeyParameters key_parameters
Definition: Types.h:47
TokenBindingID tokenbindingid
Definition: Types.h:58
countdownsingle single
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
Buf encode(TokenBindingMessage &&message)
Definition: Types.cpp:124
const internal::AnythingMatcher _