proxygen
TLSCredProcessorTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <boost/filesystem.hpp>
19 #include <folly/File.h>
20 #include <folly/FileUtil.h>
21 #include <folly/Range.h>
24 
25 using namespace testing;
26 using namespace folly;
27 using namespace wangle;
28 
29 namespace fs = boost::filesystem;
30 
32  public:
33  void SetUp() override {
34  char ticketTemp[] = {"/tmp/ticketFile-XXXXXX"};
35  File(mkstemp(ticketTemp), true);
36  ticketFile = ticketTemp;
37  char certTemp[] = {"/tmp/certFile-XXXXXX"};
38  File(mkstemp(certTemp), true);
39  certFile = certTemp;
40  }
41 
42  void TearDown() override {
43  remove(ticketFile.c_str());
44  remove(certFile.c_str());
45  }
46 
49 };
50 
52  ASSERT_TRUE(seeds);
53  ASSERT_EQ(2, seeds->newSeeds.size());
54  ASSERT_EQ(1, seeds->currentSeeds.size());
55  ASSERT_EQ(0, seeds->oldSeeds.size());
56  ASSERT_EQ("0000111122223333444455556666777788889999aaaabbbbccccddddeeeeffff", seeds->newSeeds[0]);
57  ASSERT_EQ("111122223333444455556666777788889999aaaabbbbccccddddeeeeffff0000", seeds->newSeeds[1]);
58 }
59 
60 TEST_F(ProcessTicketTest, ParseTicketFile) {
61  CHECK(writeFile(validTicketData, ticketFile.c_str()));
62  auto seeds = TLSCredProcessor::processTLSTickets(ticketFile);
63  expectValidData(seeds);
64 }
65 
66 TEST_F(ProcessTicketTest, ParseInvalidFile) {
67  CHECK(writeFile(invalidTicketData, ticketFile.c_str()));
68  auto seeds = TLSCredProcessor::processTLSTickets(ticketFile);
69  ASSERT_FALSE(seeds);
70 }
71 
72 TEST_F(ProcessTicketTest, handleAbsentFile) {
73  auto seeds = TLSCredProcessor::processTLSTickets("/path/does/not/exist");
74  ASSERT_FALSE(seeds);
75 }
76 
77 void updateModifiedTime(const std::string& fileName, int elapsed) {
78  auto previous = fs::last_write_time(fileName);
79  auto newTime = std::chrono::system_clock::to_time_t(
80  std::chrono::system_clock::from_time_t(previous) +
81  std::chrono::seconds(elapsed));
82  fs::last_write_time(fileName, newTime);
83 }
84 
85 TEST_F(ProcessTicketTest, TestUpdateTicketFile) {
86  Baton<> ticketBaton;
87  Baton<> certBaton;
88  TLSCredProcessor processor;
89  processor.setTicketPathToWatch(ticketFile);
90  processor.setCertPathsToWatch({certFile});
91  bool ticketUpdated = false;
92  bool certUpdated = false;
93  processor.addTicketCallback([&](TLSTicketKeySeeds) {
94  ticketUpdated = true;
95  ticketBaton.post();
96  });
97  processor.addCertCallback([&]() {
98  certUpdated = true;
99  certBaton.post();
100  });
101  CHECK(writeFile(validTicketData, ticketFile.c_str()));
102  updateModifiedTime(ticketFile,10);
103  EXPECT_TRUE(ticketBaton.try_wait_for(std::chrono::seconds(30)));
104  ASSERT_TRUE(ticketUpdated);
105  ASSERT_FALSE(certUpdated);
106  ticketUpdated = false;
107  CHECK(writeFile(validTicketData, certFile.c_str()));
108  updateModifiedTime(certFile,10);
109  EXPECT_TRUE(certBaton.try_wait_for(std::chrono::seconds(30)));
110  ASSERT_TRUE(certUpdated);
111  ASSERT_FALSE(ticketUpdated);
112 }
113 
114 TEST_F(ProcessTicketTest, TestMultipleCerts) {
115  Baton<> certBaton;
116  TLSCredProcessor processor;
117  processor.setCertPathsToWatch({certFile, ticketFile});
118  processor.addCertCallback([&]() {
119  certBaton.post();
120  });
121  CHECK(writeFile(validTicketData, ticketFile.c_str()));
122  updateModifiedTime(ticketFile,10);
123  EXPECT_TRUE(certBaton.try_wait_for(std::chrono::seconds(30)));
124  certBaton.reset();
125  CHECK(writeFile(validTicketData, certFile.c_str()));
126  updateModifiedTime(certFile,10);
127  EXPECT_TRUE(certBaton.try_wait_for(std::chrono::seconds(30)));
128 }
129 
130 TEST_F(ProcessTicketTest, TestSetPullInterval) {
131  Baton<> ticketBaton;
132  Baton<> certBaton;
133  TLSCredProcessor processor;
134  processor.setTicketPathToWatch(ticketFile);
135  processor.setCertPathsToWatch({certFile});
136  processor.setPollInterval(std::chrono::seconds(3));
137  bool ticketUpdated = false;
138  bool certUpdated = false;
139  processor.addTicketCallback([&](TLSTicketKeySeeds) {
140  ticketUpdated = true;
141  ticketBaton.post();
142  });
143  processor.addCertCallback([&]() {
144  certUpdated = true;
145  certBaton.post();
146  });
147  CHECK(writeFile(validTicketData, ticketFile.c_str()));
148  updateModifiedTime(ticketFile,3);
149  EXPECT_TRUE(ticketBaton.try_wait_for(std::chrono::seconds(5)));
150  ASSERT_TRUE(ticketUpdated);
151  ASSERT_FALSE(certUpdated);
152  ticketUpdated = false;
153  CHECK(writeFile(validTicketData, certFile.c_str()));
154  updateModifiedTime(certFile,3);
155  EXPECT_TRUE(certBaton.try_wait_for(std::chrono::seconds(5)));
156  ASSERT_TRUE(certUpdated);
157  ASSERT_FALSE(ticketUpdated);
158 }
std::vector< std::string > newSeeds
void setTicketPathToWatch(const std::string &ticketFile)
std::vector< std::string > currentSeeds
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
TEST_F(TestInfoTest, Names)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void expectValidData(folly::Optional< wangle::TLSTicketKeySeeds > seeds)
void TearDown() override
FOLLY_ALWAYS_INLINE bool try_wait_for(const std::chrono::duration< Rep, Period > &timeout, const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:206
void setPollInterval(std::chrono::milliseconds pollInterval)
void addCertCallback(std::function< void()> callback)
filesystem
Definition: CMakeCache.txt:563
void post() noexcept
Definition: Baton.h:123
void setCertPathsToWatch(std::set< std::string > certFiles)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
constexpr folly::StringPiece invalidTicketData
Definition: TicketUtil.h:32
const char * string
Definition: Conv.cpp:212
void updateModifiedTime(const std::string &fileName, int elapsed)
bool writeFile(const Container &data, const char *filename, int flags=O_WRONLY|O_CREAT|O_TRUNC, mode_t mode=0666)
Definition: FileUtil.h:211
constexpr folly::StringPiece validTicketData
Definition: TicketUtil.h:20
std::vector< std::string > oldSeeds
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
void reset() noexcept
Definition: Baton.h:96
void addTicketCallback(std::function< void(wangle::TLSTicketKeySeeds)> callback)