1 /* Copyright (c) 2017-2018, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "core/or/or.h"
5 #include "lib/crypt_ops/crypto_rand.h"
6 #include "lib/fs/storagedir.h"
7 #include "lib/encoding/confline.h"
15 test_storagedir_empty(void *arg
)
17 char *dirname
= tor_strdup(get_fname_rnd("store_dir"));
18 storage_dir_t
*d
= NULL
;
21 tt_int_op(FN_NOENT
, OP_EQ
, file_status(dirname
));
23 d
= storage_dir_new(dirname
, 10);
26 tt_int_op(FN_DIR
, OP_EQ
, file_status(dirname
));
28 tt_int_op(0, OP_EQ
, smartlist_len(storage_dir_list(d
)));
29 tt_u64_op(0, OP_EQ
, storage_dir_get_usage(d
));
32 d
= storage_dir_new(dirname
, 10);
35 tt_int_op(FN_DIR
, OP_EQ
, file_status(dirname
));
37 tt_int_op(0, OP_EQ
, smartlist_len(storage_dir_list(d
)));
38 tt_u64_op(0, OP_EQ
, storage_dir_get_usage(d
));
46 test_storagedir_basic(void *arg
)
48 char *dirname
= tor_strdup(get_fname_rnd("store_dir"));
49 storage_dir_t
*d
= NULL
;
50 uint8_t *junk
= NULL
, *bytes
= NULL
;
51 const size_t junklen
= 1024;
52 char *fname1
= NULL
, *fname2
= NULL
;
53 const char hello_str
[] = "then what are we but cold, alone ... ?";
54 tor_mmap_t
*mapping
= NULL
;
57 junk
= tor_malloc(junklen
);
58 crypto_rand((void*)junk
, junklen
);
60 d
= storage_dir_new(dirname
, 10);
62 tt_u64_op(0, OP_EQ
, storage_dir_get_usage(d
));
65 r
= storage_dir_save_string_to_file(d
, hello_str
, 1, &fname1
);
66 tt_int_op(r
, OP_EQ
, 0);
67 tt_ptr_op(fname1
, OP_NE
, NULL
);
68 tt_u64_op(strlen(hello_str
), OP_EQ
, storage_dir_get_usage(d
));
70 r
= storage_dir_save_bytes_to_file(d
, junk
, junklen
, 1, &fname2
);
71 tt_int_op(r
, OP_EQ
, 0);
72 tt_ptr_op(fname2
, OP_NE
, NULL
);
74 tt_str_op(fname1
, OP_NE
, fname2
);
76 tt_int_op(2, OP_EQ
, smartlist_len(storage_dir_list(d
)));
77 tt_u64_op(junklen
+ strlen(hello_str
), OP_EQ
, storage_dir_get_usage(d
));
78 tt_assert(smartlist_contains_string(storage_dir_list(d
), fname1
));
79 tt_assert(smartlist_contains_string(storage_dir_list(d
), fname2
));
82 d
= storage_dir_new(dirname
, 10);
84 tt_int_op(2, OP_EQ
, smartlist_len(storage_dir_list(d
)));
85 tt_u64_op(junklen
+ strlen(hello_str
), OP_EQ
, storage_dir_get_usage(d
));
86 tt_assert(smartlist_contains_string(storage_dir_list(d
), fname1
));
87 tt_assert(smartlist_contains_string(storage_dir_list(d
), fname2
));
90 bytes
= storage_dir_read(d
, fname2
, 1, &n
);
92 tt_u64_op(n
, OP_EQ
, junklen
);
93 tt_mem_op(bytes
, OP_EQ
, junk
, junklen
);
95 mapping
= storage_dir_map(d
, fname1
);
97 tt_u64_op(mapping
->size
, OP_EQ
, strlen(hello_str
));
98 tt_mem_op(mapping
->data
, OP_EQ
, hello_str
, strlen(hello_str
));
104 tor_munmap_file(mapping
);
111 test_storagedir_deletion(void *arg
)
114 char *dirname
= tor_strdup(get_fname_rnd("store_dir"));
115 storage_dir_t
*d
= NULL
;
116 char *fn1
= NULL
, *fn2
= NULL
;
119 const char str1
[] = "There are nine and sixty ways to disguise communiques";
120 const char str2
[] = "And rather more than one of them is right";
122 // Make sure the directory is there. */
123 d
= storage_dir_new(dirname
, 10);
127 tor_asprintf(&fn1
, "%s/1007", dirname
);
128 r
= write_str_to_file(fn1
, str1
, 0);
129 tt_int_op(r
, OP_EQ
, 0);
131 tor_asprintf(&fn2
, "%s/1003.tmp", dirname
);
132 r
= write_str_to_file(fn2
, str2
, 0);
133 tt_int_op(r
, OP_EQ
, 0);
135 // The tempfile should be deleted the next time we list the directory.
136 d
= storage_dir_new(dirname
, 10);
137 tt_int_op(1, OP_EQ
, smartlist_len(storage_dir_list(d
)));
138 tt_u64_op(strlen(str1
), OP_EQ
, storage_dir_get_usage(d
));
139 tt_int_op(FN_FILE
, OP_EQ
, file_status(fn1
));
140 tt_int_op(FN_NOENT
, OP_EQ
, file_status(fn2
));
142 bytes
= (char*) storage_dir_read(d
, "1007", 1, NULL
);
143 tt_str_op(bytes
, OP_EQ
, str1
);
145 // Should have no effect; file already gone.
146 storage_dir_remove_file(d
, "1003.tmp");
147 tt_int_op(1, OP_EQ
, smartlist_len(storage_dir_list(d
)));
148 tt_u64_op(strlen(str1
), OP_EQ
, storage_dir_get_usage(d
));
150 // Actually remove a file.
151 storage_dir_remove_file(d
, "1007");
152 tt_int_op(FN_NOENT
, OP_EQ
, file_status(fn1
));
153 tt_int_op(0, OP_EQ
, smartlist_len(storage_dir_list(d
)));
154 tt_u64_op(0, OP_EQ
, storage_dir_get_usage(d
));
165 test_storagedir_full(void *arg
)
169 char *dirname
= tor_strdup(get_fname_rnd("store_dir"));
170 storage_dir_t
*d
= NULL
;
171 const char str
[] = "enemies of the peephole";
174 d
= storage_dir_new(dirname
, 3);
177 r
= storage_dir_save_string_to_file(d
, str
, 1, NULL
);
178 tt_int_op(r
, OP_EQ
, 0);
179 r
= storage_dir_save_string_to_file(d
, str
, 1, NULL
);
180 tt_int_op(r
, OP_EQ
, 0);
181 r
= storage_dir_save_string_to_file(d
, str
, 1, NULL
);
182 tt_int_op(r
, OP_EQ
, 0);
184 // These should fail!
185 r
= storage_dir_save_string_to_file(d
, str
, 1, NULL
);
186 tt_int_op(r
, OP_EQ
, -1);
187 r
= storage_dir_save_string_to_file(d
, str
, 1, NULL
);
188 tt_int_op(r
, OP_EQ
, -1);
190 tt_u64_op(strlen(str
) * 3, OP_EQ
, storage_dir_get_usage(d
));
198 test_storagedir_cleaning(void *arg
)
202 char *dirname
= tor_strdup(get_fname_rnd("store_dir"));
203 storage_dir_t
*d
= NULL
;
205 "On a mountain halfway between Reno and Rome / "
206 "We have a machine in a plexiglass dome / "
207 "Which listens and looks into everyone's home."
212 memset(fns
, 0, sizeof(fns
));
213 d
= storage_dir_new(dirname
, 10);
216 for (i
= 0; i
< 8; ++i
) {
217 r
= storage_dir_save_string_to_file(d
, str
+i
*2, 1, &fns
[i
]);
218 tt_int_op(r
, OP_EQ
, 0);
221 /* Now we're going to make sure all the files have distinct mtimes. */
222 time_t now
= time(NULL
);
225 ub
.modtime
= now
- 1000;
226 for (i
= 0; i
< 8; ++i
) {
228 tor_asprintf(&f
, "%s/%s", dirname
, fns
[i
]);
231 tt_int_op(r
, OP_EQ
, 0);
235 const uint64_t usage_orig
= storage_dir_get_usage(d
);
236 /* No changes needed if we are already under target. */
237 storage_dir_shrink(d
, 1024*1024, 0);
238 tt_u64_op(usage_orig
, OP_EQ
, storage_dir_get_usage(d
));
240 /* Get rid of at least one byte. This will delete fns[0]. */
241 storage_dir_shrink(d
, usage_orig
- 1, 0);
242 tt_u64_op(usage_orig
, OP_GT
, storage_dir_get_usage(d
));
243 tt_u64_op(usage_orig
- strlen(str
), OP_EQ
, storage_dir_get_usage(d
));
245 /* Get rid of at least two files. This will delete fns[1] and fns[2]. */
246 storage_dir_shrink(d
, 1024*1024, 2);
247 tt_u64_op(usage_orig
- strlen(str
)*3 + 6, OP_EQ
, storage_dir_get_usage(d
));
249 /* Get rid of everything. */
250 storage_dir_remove_all(d
);
251 tt_u64_op(0, OP_EQ
, storage_dir_get_usage(d
));
256 for (i
= 0; i
< 8; ++i
) {
262 test_storagedir_save_labeled(void *arg
)
265 char *dirname
= tor_strdup(get_fname_rnd("store_dir"));
266 storage_dir_t
*d
= NULL
;
267 uint8_t *inp
= tor_malloc_zero(8192);
268 config_line_t
*labels
= NULL
;
270 uint8_t *saved
= NULL
;
272 d
= storage_dir_new(dirname
, 10);
275 crypto_rand((char *)inp
, 8192);
277 config_line_append(&labels
, "Foo", "bar baz");
278 config_line_append(&labels
, "quux", "quuzXxz");
279 const char expected
[] =
283 int r
= storage_dir_save_labeled_to_file(d
, labels
, inp
, 8192, &fname
);
284 tt_int_op(r
, OP_EQ
, 0);
287 saved
= storage_dir_read(d
, fname
, 1, &n
);
288 tt_assert(memchr(saved
, '\0', n
));
289 tt_str_op((char*)saved
, OP_EQ
, expected
); /* NUL guarantees strcmp works */
290 tt_mem_op(saved
+strlen(expected
)+1, OP_EQ
, inp
, 8192);
297 config_free_lines(labels
);
302 test_storagedir_read_labeled(void *arg
)
305 char *dirname
= tor_strdup(get_fname_rnd("store_dir"));
306 storage_dir_t
*d
= NULL
;
307 uint8_t *inp
= tor_malloc_zero(8192);
308 config_line_t
*labels
= NULL
, *labels2
= NULL
;
310 tor_mmap_t
*map
= NULL
;
311 uint8_t *as_read
= NULL
;
313 d
= storage_dir_new(dirname
, 10);
316 tor_snprintf((char*)inp
, 8192,
320 size_t bodylen
= 8192 - strlen((char*)inp
) - 1;
321 crypto_rand((char *)inp
+strlen((char*)inp
)+1, bodylen
);
323 int r
= storage_dir_save_bytes_to_file(d
, inp
, 8192, 1, &fname
);
324 tt_int_op(r
, OP_EQ
, 0);
327 const uint8_t *datap
= NULL
;
329 map
= storage_dir_map_labeled(d
, fname
, &labels
, &datap
, &sz
);
332 tt_u64_op(sz
, OP_EQ
, bodylen
);
333 tt_mem_op(datap
, OP_EQ
, inp
+strlen((char*)inp
)+1, bodylen
);
335 tt_str_op(labels
->key
, OP_EQ
, "Hello");
336 tt_str_op(labels
->value
, OP_EQ
, "world");
337 tt_assert(labels
->next
);
338 tt_str_op(labels
->next
->key
, OP_EQ
, "This");
339 tt_str_op(labels
->next
->value
, OP_EQ
, "is a test");
340 tt_assert(labels
->next
->next
);
341 tt_str_op(labels
->next
->next
->key
, OP_EQ
, "Yadda");
342 tt_str_op(labels
->next
->next
->value
, OP_EQ
, "yadda.");
343 tt_ptr_op(labels
->next
->next
->next
, OP_EQ
, NULL
);
345 /* Try reading this time. */
347 as_read
= storage_dir_read_labeled(d
, fname
, &labels2
, &sz
);
349 tt_u64_op(sz
, OP_EQ
, bodylen
);
350 tt_mem_op(as_read
, OP_EQ
, inp
+strlen((char*)inp
)+1, bodylen
);
351 tt_assert(config_lines_eq(labels
, labels2
));
358 config_free_lines(labels
);
359 config_free_lines(labels2
);
360 tor_munmap_file(map
);
365 { #name, test_storagedir_ ## name, TT_FORK, NULL, NULL }
367 struct testcase_t storagedir_tests
[] = {