Browse code
Test to make sure we handle a too-short file
Ed Langley authored on 12/06/2017 22:44:56
Showing 2 changed files
Showing 2 changed files
... | ... |
@@ -37,9 +37,9 @@ public: |
37 | 37 |
const std::string file_path (user.home_directory() + "/.dual_control"); |
38 | 38 |
fstreams::pstream stream (fstreams_.open_fstream (file_path)); |
39 | 39 |
|
40 |
- // TODO: decode key |
|
41 |
- std::vector<char> line_v(40); |
|
42 |
- stream->getline(line_v.data(), line_v.size()); |
|
40 |
+ // TODO: ignore newlines |
|
41 |
+ std::vector<char> line_v(64); |
|
42 |
+ stream->read(line_v.data(), line_v.size()); |
|
43 | 43 |
|
44 | 44 |
if (stream->fail()) { |
45 | 45 |
return ""; |
... | ... |
@@ -103,7 +103,7 @@ int reads_from_the_right_file () |
103 | 103 |
std::string home_directory = "/somedir"; |
104 | 104 |
// hardcoded file name is .dual_control in the user's home directory |
105 | 105 |
std::string token_file = home_directory + "/.dual_control"; |
106 |
- std::string token ("AAAAAAAA"); |
|
106 |
+ std::string token ("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); |
|
107 | 107 |
|
108 | 108 |
fstreams test_streams (fstreams::delegate (new fake_fstreams (token_file, token))); |
109 | 109 |
totp_generator generator (totp_generator::delegate (new fake_totp_generator (token))); |
... | ... |
@@ -141,6 +141,30 @@ int returns_empty_string_if_file_open_fail() |
141 | 141 |
succeed(); |
142 | 142 |
} |
143 | 143 |
|
144 |
+int returns_empty_string_if_file_too_short() |
|
145 |
+{ |
|
146 |
+ //given |
|
147 |
+ std::string home_directory = "/somedir"; |
|
148 |
+ // hardcoded file name is .dual_control in the user's home directory |
|
149 |
+ std::string token_file = home_directory + "/.dual_control"; |
|
150 |
+ // we want a 40-byte key, so we need a 64-byte base32-encoded file. |
|
151 |
+ std::string token ("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); |
|
152 |
+ |
|
153 |
+ fstreams test_streams (fstreams::delegate (new fake_fstreams (token_file, token))); |
|
154 |
+ totp_generator generator (totp_generator::delegate (new fake_totp_generator (token))); |
|
155 |
+ |
|
156 |
+ //file_reader test_file_reader (file_reader::delegate (new fake_file_reader)); |
|
157 |
+ user test_user (user::delegate (new fake_user (home_directory))); |
|
158 |
+ tokens supplier (tokens::create (test_streams, generator)); |
|
159 |
+ |
|
160 |
+ //when |
|
161 |
+ std::string actual = supplier.token (test_user); |
|
162 |
+ |
|
163 |
+ //then |
|
164 |
+ check (actual == "", "should have returned empty string"); |
|
165 |
+ succeed(); |
|
166 |
+} |
|
167 |
+ |
|
144 | 168 |
int writes_the_token () |
145 | 169 |
{ |
146 | 170 |
// given |
... | ... |
@@ -169,6 +193,7 @@ int run_tests() |
169 | 193 |
{ |
170 | 194 |
test (reads_from_the_right_file); |
171 | 195 |
test (returns_empty_string_if_file_open_fail); |
196 |
+ test (returns_empty_string_if_file_too_short); |
|
172 | 197 |
test (writes_the_token); |
173 | 198 |
succeed(); |
174 | 199 |
} |