proxygen
TLSCredProcessorTest.cpp File Reference
#include <boost/filesystem.hpp>
#include <folly/portability/GTest.h>
#include <folly/synchronization/Baton.h>
#include <folly/File.h>
#include <folly/FileUtil.h>
#include <folly/Range.h>
#include <wangle/ssl/TLSCredProcessor.h>
#include <wangle/ssl/test/TicketUtil.h>

Go to the source code of this file.

Classes

class  ProcessTicketTest
 

Functions

void expectValidData (folly::Optional< wangle::TLSTicketKeySeeds > seeds)
 
 TEST_F (ProcessTicketTest, ParseTicketFile)
 
 TEST_F (ProcessTicketTest, ParseInvalidFile)
 
 TEST_F (ProcessTicketTest, handleAbsentFile)
 
void updateModifiedTime (const std::string &fileName, int elapsed)
 
 TEST_F (ProcessTicketTest, TestUpdateTicketFile)
 
 TEST_F (ProcessTicketTest, TestMultipleCerts)
 
 TEST_F (ProcessTicketTest, TestSetPullInterval)
 

Function Documentation

void expectValidData ( folly::Optional< wangle::TLSTicketKeySeeds seeds)

Definition at line 51 of file TLSCredProcessorTest.cpp.

References ASSERT_EQ, ASSERT_TRUE, wangle::TLSTicketKeySeeds::currentSeeds, wangle::TLSTicketKeySeeds::newSeeds, and wangle::TLSTicketKeySeeds::oldSeeds.

Referenced by TEST_F().

51  {
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 }
std::vector< std::string > newSeeds
std::vector< std::string > currentSeeds
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
std::vector< std::string > oldSeeds
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
TEST_F ( ProcessTicketTest  ,
ParseTicketFile   
)

Definition at line 60 of file TLSCredProcessorTest.cpp.

References expectValidData(), validTicketData, and folly::writeFile().

60  {
61  CHECK(writeFile(validTicketData, ticketFile.c_str()));
62  auto seeds = TLSCredProcessor::processTLSTickets(ticketFile);
63  expectValidData(seeds);
64 }
void expectValidData(folly::Optional< wangle::TLSTicketKeySeeds > seeds)
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
TEST_F ( ProcessTicketTest  ,
ParseInvalidFile   
)

Definition at line 66 of file TLSCredProcessorTest.cpp.

References ASSERT_FALSE, invalidTicketData, and folly::writeFile().

66  {
67  CHECK(writeFile(invalidTicketData, ticketFile.c_str()));
68  auto seeds = TLSCredProcessor::processTLSTickets(ticketFile);
69  ASSERT_FALSE(seeds);
70 }
constexpr folly::StringPiece invalidTicketData
Definition: TicketUtil.h:32
bool writeFile(const Container &data, const char *filename, int flags=O_WRONLY|O_CREAT|O_TRUNC, mode_t mode=0666)
Definition: FileUtil.h:211
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
TEST_F ( ProcessTicketTest  ,
handleAbsentFile   
)

Definition at line 72 of file TLSCredProcessorTest.cpp.

References ASSERT_FALSE.

72  {
73  auto seeds = TLSCredProcessor::processTLSTickets("/path/does/not/exist");
74  ASSERT_FALSE(seeds);
75 }
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
TEST_F ( ProcessTicketTest  ,
TestUpdateTicketFile   
)

Definition at line 85 of file TLSCredProcessorTest.cpp.

References wangle::TLSCredProcessor::addCertCallback(), wangle::TLSCredProcessor::addTicketCallback(), ASSERT_FALSE, ASSERT_TRUE, EXPECT_TRUE, folly::Baton< MayBlock, Atom >::post(), wangle::TLSCredProcessor::setCertPathsToWatch(), wangle::TLSCredProcessor::setTicketPathToWatch(), folly::Baton< MayBlock, Atom >::try_wait_for(), updateModifiedTime(), validTicketData, and folly::writeFile().

85  {
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 }
void setTicketPathToWatch(const std::string &ticketFile)
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 addCertCallback(std::function< void()> callback)
void post() noexcept
Definition: Baton.h:123
void setCertPathsToWatch(std::set< std::string > certFiles)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
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
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
void addTicketCallback(std::function< void(wangle::TLSTicketKeySeeds)> callback)
TEST_F ( ProcessTicketTest  ,
TestMultipleCerts   
)

Definition at line 114 of file TLSCredProcessorTest.cpp.

References wangle::TLSCredProcessor::addCertCallback(), EXPECT_TRUE, folly::Baton< MayBlock, Atom >::post(), folly::Baton< MayBlock, Atom >::reset(), wangle::TLSCredProcessor::setCertPathsToWatch(), folly::Baton< MayBlock, Atom >::try_wait_for(), updateModifiedTime(), validTicketData, and folly::writeFile().

114  {
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 }
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 addCertCallback(std::function< void()> callback)
void post() noexcept
Definition: Baton.h:123
void setCertPathsToWatch(std::set< std::string > certFiles)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
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
void reset() noexcept
Definition: Baton.h:96
TEST_F ( ProcessTicketTest  ,
TestSetPullInterval   
)

Definition at line 130 of file TLSCredProcessorTest.cpp.

References wangle::TLSCredProcessor::addCertCallback(), wangle::TLSCredProcessor::addTicketCallback(), ASSERT_FALSE, ASSERT_TRUE, EXPECT_TRUE, folly::Baton< MayBlock, Atom >::post(), wangle::TLSCredProcessor::setCertPathsToWatch(), wangle::TLSCredProcessor::setPollInterval(), wangle::TLSCredProcessor::setTicketPathToWatch(), folly::Baton< MayBlock, Atom >::try_wait_for(), updateModifiedTime(), validTicketData, and folly::writeFile().

130  {
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 }
void setTicketPathToWatch(const std::string &ticketFile)
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)
void post() noexcept
Definition: Baton.h:123
void setCertPathsToWatch(std::set< std::string > certFiles)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
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
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
void addTicketCallback(std::function< void(wangle::TLSTicketKeySeeds)> callback)
void updateModifiedTime ( const std::string fileName,
int  elapsed 
)

Definition at line 77 of file TLSCredProcessorTest.cpp.

Referenced by TEST_F().

77  {
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 }