token_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 #include <gmock/gmock.h>
18 
19 #include <aws/core/utils/logging/LogMacros.h>
20 #include <aws/core/utils/logging/ConsoleLogSystem.h>
21 
23 
24 using namespace Aws::FileManagement;
25 
26 static const FileTokenInfo kTestToken1("fake_file", 0, false);
27 static const FileTokenInfo kTestToken2("fake_file", 10, true);
28 
29 class TokenTest : public ::testing::Test {
30 public:
31  void SetUp() override
32  {
33  }
34 
35  void TearDown() override
36  {
37  std::experimental::filesystem::path test_folder{"token_backups/"};
38  if (std::experimental::filesystem::exists(test_folder)) {
39  std::experimental::filesystem::remove_all(test_folder);
40  }
41  }
42 
43 protected:
44  std::string backup_folder = "token_backups/";
45  std::string kBackupFilename = "token_store.info";
46  TokenStoreOptions options{backup_folder};
47 };
48 
49 TEST(token_test, fail_unknown_token) {
50  TokenStore token_store;
51  EXPECT_THROW(token_store.fail(0), std::runtime_error);
52 }
53 
54 TEST(token_test, fail_token_twice) {
55  TokenStore token_store;
56  FileTokenInfo kTestToken1("fake_file", 0, false);
57  auto token = token_store.createToken(kTestToken1.file_path_, kTestToken1.position_, kTestToken1.eof_);
58  token_store.fail(token);
59  EXPECT_THROW(token_store.fail(token), std::runtime_error);
60 }
61 
62 TEST(token_test, resolve_then_fail_token) {
63  TokenStore token_store;
65  token_store.resolve(token);
66  EXPECT_THROW(token_store.fail(token), std::runtime_error);
67 }
68 
69 TEST(token_test, fail_then_recover_token) {
70  TokenStore token_store;
72  token_store.fail(token);
73  EXPECT_TRUE(token_store.isTokenAvailable(kTestToken1.file_path_));
74  auto popped_token = token_store.popAvailableToken(kTestToken1.file_path_);
75  EXPECT_EQ(kTestToken1, popped_token);
76 }
77 
78 TEST(token_test, resolve_unknown_token) {
79  TokenStore token_store;
80  EXPECT_THROW(token_store.resolve(0), std::runtime_error);
81 }
82 
83 TEST(token_test, resolve_token) {
84  TokenStore token_store;
86  auto resolved_token = token_store.resolve(token);
87  EXPECT_EQ(kTestToken1, resolved_token);
88 }
89 
90 TEST(token_test, resolve_token_twice) {
91  TokenStore token_store;
93  token_store.resolve(token);
94  EXPECT_THROW(token_store.resolve(token), std::runtime_error);
95 }
96 
97 TEST(token_test, test_backup) {
98  TokenStore token_store;
101  auto backup = token_store.backup();
102 
103  EXPECT_THAT(backup, testing::ElementsAre(kTestToken1));
104 }
105 
106 TEST(token_test, test_backup_failed_file) {
107  TokenStore token_store;
108 
111  token_store.fail(token1);
112  auto backup = token_store.backup();
113 
114  EXPECT_THAT(backup, testing::ElementsAre(kTestToken1));
115 }
116 
117 TEST(token_test, test_token_restore) {
118  std::vector<FileTokenInfo> backup;
119  {
120  TokenStore token_store;
122  backup = token_store.backup();
123  EXPECT_THAT(backup, testing::ElementsAre(kTestToken1));
124  }
125  {
126  TokenStore token_store;
127  token_store.restore(backup);
128  EXPECT_TRUE(token_store.isTokenAvailable(kTestToken1.file_path_));
129  EXPECT_EQ(kTestToken1, token_store.popAvailableToken(kTestToken1.file_path_));
130  }
131 }
132 
133 TEST(token_test, test_backup_two_files) {
134  TokenStore token_store;
135  FileTokenInfo test_token_2("different_file", 10, true);
137  token_store.createToken(test_token_2.file_path_, test_token_2.position_, test_token_2.eof_);
138  auto backup = token_store.backup();
139  EXPECT_THAT(backup, testing::UnorderedElementsAre(kTestToken1, test_token_2));
140 }
141 
142 TEST_F(TokenTest, test_backup_to_disk) {
143  TokenStore token_store(options);
144  FileTokenInfo test_token_2("different_file", 10, true);
146  token_store.createToken(test_token_2.file_path_, test_token_2.position_, test_token_2.eof_);
147  EXPECT_NO_THROW(token_store.backupToDisk());
148  std::string backup_file = options.backup_directory + kBackupFilename;
149  EXPECT_TRUE(std::experimental::filesystem::exists(backup_file));
150 }
151 
152 TEST_F(TokenTest, test_restore_from_disk) {
153  FileTokenInfo test_token_2("different_file", 10, true);
154  std::string backup_file = options.backup_directory + kBackupFilename;
155  {
156  TokenStore token_store(options);
158  token_store.createToken(test_token_2.file_path_, test_token_2.position_, test_token_2.eof_);
159  EXPECT_NO_THROW(token_store.backupToDisk());
160  EXPECT_TRUE(std::experimental::filesystem::exists(backup_file));
161  }
162  {
163  TokenStore token_store(options);
164  token_store.restoreFromDisk();
165  EXPECT_THAT(token_store.backup(), testing::UnorderedElementsAre(kTestToken1, test_token_2));
166  }
167 }
168 
169 TEST_F(TokenTest, test_restoring_from_disk_with_invalid_json) {
170  FileTokenInfo test_token_2("different_file", 10, true);
171  std::string backup_file = options.backup_directory + kBackupFilename;
172  {
173  TokenStore token_store(options);
175  token_store.createToken(test_token_2.file_path_, test_token_2.position_, test_token_2.eof_);
176  EXPECT_NO_THROW(token_store.backupToDisk());
177  EXPECT_TRUE(std::experimental::filesystem::exists(backup_file));
178  }
179  {
180  std::ofstream token_store_file(backup_file, std::ios_base::app);
181  token_store_file << "some other stuff" << std::endl;
182  token_store_file.close();
183  }
184  {
185  TokenStore token_store(options);
186  EXPECT_NO_THROW(token_store.restoreFromDisk());
187  EXPECT_THAT(token_store.backup(), testing::UnorderedElementsAre(kTestToken1, test_token_2));
188  }
189 }
190 
191 
192 
193 int main(int argc, char** argv)
194 {
195  Aws::Utils::Logging::InitializeAWSLogging(
196  Aws::MakeShared<Aws::Utils::Logging::ConsoleLogSystem>(
197  "RunUnitTests", Aws::Utils::Logging::LogLevel::Trace));
198  ::testing::InitGoogleMock(&argc, argv);
199  int exitCode = RUN_ALL_TESTS();
200  Aws::Utils::Logging::ShutdownAWSLogging();
201  return exitCode;
202 }
TEST_F(TokenTest, test_backup_to_disk)
Definition: token_test.cpp:142
void restore(const std::vector< FileTokenInfo > &file_tokens)
TEST(token_test, fail_unknown_token)
Definition: token_test.cpp:49
bool isTokenAvailable(const std::string &file_name) const
void SetUp() override
Definition: token_test.cpp:31
DataToken createToken(const std::string &file_name, const long streampos, bool is_eof)
static const FileTokenInfo kTestToken2("fake_file", 10, true)
FileTokenInfo popAvailableToken(const std::string &file_name)
static const FileTokenInfo kTestToken1("fake_file", 0, false)
void TearDown() override
Definition: token_test.cpp:35
FileTokenInfo fail(const DataToken &token)
int main(int argc, char **argv)
Definition: token_test.cpp:193
std::vector< FileTokenInfo > backup()
FileTokenInfo resolve(const DataToken &token)


file_management
Author(s): AWS RoboMaker
autogenerated on Fri May 7 2021 02:18:23