proxygen
FizzUtilTest.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 <gtest/gtest.h>
10 
13 #include <fizz/util/FizzUtil.h>
14 #include <folly/FileUtil.h>
16 
17 using namespace folly;
18 using namespace testing;
19 
20 namespace fizz {
21 namespace test {
22 
23 TEST(UtilTest, GetAlpnFromNpn) {
24  std::list<folly::SSLContext::NextProtocolsItem> npList;
25  std::list<std::string> protocolList1{"test", "test2"};
26  std::list<std::string> protocolList2{"test3", "test4"};
27 
28  npList.push_back(folly::SSLContext::NextProtocolsItem(1, protocolList1));
29  {
30  std::vector<std::string> expectedList{std::begin(protocolList1),
31  std::end(protocolList1)};
32  EXPECT_EQ(FizzUtil::getAlpnsFromNpnList(npList), expectedList);
33  }
34 
35  npList.push_back(folly::SSLContext::NextProtocolsItem(2, protocolList2));
36  {
37  std::vector<std::string> expectedList{std::begin(protocolList2),
38  std::end(protocolList2)};
39  EXPECT_EQ(FizzUtil::getAlpnsFromNpnList(npList), expectedList);
40  }
41 }
42 
43 TEST(UtilTest, CreateTickerCipher) {
44  auto cipher = FizzUtil::createTicketCipher<server::AES128TicketCipher>(
45  std::vector<std::string>(),
46  "fakeSecretttttttttttttttttttttttttt",
47  std::vector<std::string>(),
48  // any number high enough to last the duration of the test should be fine
49  std::chrono::seconds(100),
50  folly::Optional<std::string>("fakeContext"));
51  {
53  auto blob = cipher->encrypt(std::move(state)).get();
54  EXPECT_EQ(
55  std::get<0>(cipher->decrypt(std::move(std::get<0>(*blob))).get()),
56  PskType::Resumption);
57  }
58  {
59  auto newCipher = FizzUtil::createTicketCipher<server::AES128TicketCipher>(
60  std::vector<std::string>(),
61  "fakeSecrettttttttttttttttttttttttt2",
62  std::vector<std::string>(),
63  std::chrono::seconds(100),
64  folly::Optional<std::string>("fakeContext"));
66  auto blob = cipher->encrypt(std::move(state)).get();
67  EXPECT_EQ(
68  std::get<0>(newCipher->decrypt(std::move(std::get<0>(*blob))).get()),
69  PskType::Rejected);
70  }
71 }
72 
73 TEST(UtilTest, ReadPKey) {
74  {
75  folly::test::TemporaryFile testFile("test");
76  folly::writeFileAtomic(testFile.path().native(), kP256Key);
77  FizzUtil::readPrivateKey(testFile.path().native(), "");
78  }
79  {
80  folly::test::TemporaryFile testFile("test");
82  testFile.path().native(), folly::StringPiece("test"));
84  FizzUtil::readPrivateKey(testFile.path().native(), ""),
85  std::runtime_error);
86  }
87 }
88 
89 TEST(UtilTest, ReadChainFile) {
90  {
91  folly::test::TemporaryFile testFile("test");
92  folly::writeFileAtomic(testFile.path().native(), kP256Certificate);
93  EXPECT_EQ(FizzUtil::readChainFile(testFile.path().native()).size(), 1);
94  }
95  {
96  folly::test::TemporaryFile testFile("test");
97  folly::writeFileAtomic(testFile.path().native(), kP384Key);
99  FizzUtil::readChainFile(testFile.path().native()), std::runtime_error);
100  }
101  {
102  EXPECT_THROW(
103  FizzUtil::readChainFile("test_file_does_not_exist"),
104  std::runtime_error);
105  }
106 }
107 
108 } // namespace test
109 } // namespace fizz
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
constexpr folly::StringPiece kP256Key
Definition: TestUtil.h:18
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
CipherSuite cipher
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
Definition: Actions.h:16
const fs::path & path() const
Definition: TestUtil.cpp:85
void writeFileAtomic(StringPiece filename, iovec *iov, int count, mode_t permissions)
Definition: FileUtil.cpp:224
Range< const char * > StringPiece
constexpr folly::StringPiece kP384Key
Definition: TestUtil.h:57
TEST(SequencedExecutor, CPUThreadPoolExecutor)
state
Definition: http_parser.c:272
constexpr folly::StringPiece kP256Certificate
Definition: TestUtil.h:41