test_storagedir.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* Copyright (c) 2017-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "core/or/or.h"
  4. #include "lib/crypt_ops/crypto_rand.h"
  5. #include "lib/fs/storagedir.h"
  6. #include "lib/encoding/confline.h"
  7. #include "test/test.h"
  8. #ifdef HAVE_UTIME_H
  9. #include <utime.h>
  10. #endif
  11. static void
  12. test_storagedir_empty(void *arg)
  13. {
  14. char *dirname = tor_strdup(get_fname_rnd("store_dir"));
  15. storage_dir_t *d = NULL;
  16. (void)arg;
  17. tt_int_op(FN_NOENT, OP_EQ, file_status(dirname));
  18. d = storage_dir_new(dirname, 10);
  19. tt_assert(d);
  20. tt_int_op(FN_DIR, OP_EQ, file_status(dirname));
  21. tt_int_op(0, OP_EQ, smartlist_len(storage_dir_list(d)));
  22. tt_u64_op(0, OP_EQ, storage_dir_get_usage(d));
  23. storage_dir_free(d);
  24. d = storage_dir_new(dirname, 10);
  25. tt_assert(d);
  26. tt_int_op(FN_DIR, OP_EQ, file_status(dirname));
  27. tt_int_op(0, OP_EQ, smartlist_len(storage_dir_list(d)));
  28. tt_u64_op(0, OP_EQ, storage_dir_get_usage(d));
  29. done:
  30. storage_dir_free(d);
  31. tor_free(dirname);
  32. }
  33. static void
  34. test_storagedir_basic(void *arg)
  35. {
  36. char *dirname = tor_strdup(get_fname_rnd("store_dir"));
  37. storage_dir_t *d = NULL;
  38. uint8_t *junk = NULL, *bytes = NULL;
  39. const size_t junklen = 1024;
  40. char *fname1 = NULL, *fname2 = NULL;
  41. const char hello_str[] = "then what are we but cold, alone ... ?";
  42. tor_mmap_t *mapping = NULL;
  43. (void)arg;
  44. junk = tor_malloc(junklen);
  45. crypto_rand((void*)junk, junklen);
  46. d = storage_dir_new(dirname, 10);
  47. tt_assert(d);
  48. tt_u64_op(0, OP_EQ, storage_dir_get_usage(d));
  49. int r;
  50. r = storage_dir_save_string_to_file(d, hello_str, 1, &fname1);
  51. tt_int_op(r, OP_EQ, 0);
  52. tt_ptr_op(fname1, OP_NE, NULL);
  53. tt_u64_op(strlen(hello_str), OP_EQ, storage_dir_get_usage(d));
  54. r = storage_dir_save_bytes_to_file(d, junk, junklen, 1, &fname2);
  55. tt_int_op(r, OP_EQ, 0);
  56. tt_ptr_op(fname2, OP_NE, NULL);
  57. tt_str_op(fname1, OP_NE, fname2);
  58. tt_int_op(2, OP_EQ, smartlist_len(storage_dir_list(d)));
  59. tt_u64_op(junklen + strlen(hello_str), OP_EQ, storage_dir_get_usage(d));
  60. tt_assert(smartlist_contains_string(storage_dir_list(d), fname1));
  61. tt_assert(smartlist_contains_string(storage_dir_list(d), fname2));
  62. storage_dir_free(d);
  63. d = storage_dir_new(dirname, 10);
  64. tt_assert(d);
  65. tt_int_op(2, OP_EQ, smartlist_len(storage_dir_list(d)));
  66. tt_u64_op(junklen + strlen(hello_str), OP_EQ, storage_dir_get_usage(d));
  67. tt_assert(smartlist_contains_string(storage_dir_list(d), fname1));
  68. tt_assert(smartlist_contains_string(storage_dir_list(d), fname2));
  69. size_t n;
  70. bytes = storage_dir_read(d, fname2, 1, &n);
  71. tt_assert(bytes);
  72. tt_u64_op(n, OP_EQ, junklen);
  73. tt_mem_op(bytes, OP_EQ, junk, junklen);
  74. mapping = storage_dir_map(d, fname1);
  75. tt_assert(mapping);
  76. tt_u64_op(mapping->size, OP_EQ, strlen(hello_str));
  77. tt_mem_op(mapping->data, OP_EQ, hello_str, strlen(hello_str));
  78. done:
  79. tor_free(dirname);
  80. tor_free(junk);
  81. tor_free(bytes);
  82. tor_munmap_file(mapping);
  83. storage_dir_free(d);
  84. tor_free(fname1);
  85. tor_free(fname2);
  86. }
  87. static void
  88. test_storagedir_deletion(void *arg)
  89. {
  90. (void)arg;
  91. char *dirname = tor_strdup(get_fname_rnd("store_dir"));
  92. storage_dir_t *d = NULL;
  93. char *fn1 = NULL, *fn2 = NULL;
  94. char *bytes = NULL;
  95. int r;
  96. const char str1[] = "There are nine and sixty ways to disguise communiques";
  97. const char str2[] = "And rather more than one of them is right";
  98. // Make sure the directory is there. */
  99. d = storage_dir_new(dirname, 10);
  100. storage_dir_free(d);
  101. d = NULL;
  102. tor_asprintf(&fn1, "%s/1007", dirname);
  103. r = write_str_to_file(fn1, str1, 0);
  104. tt_int_op(r, OP_EQ, 0);
  105. tor_asprintf(&fn2, "%s/1003.tmp", dirname);
  106. r = write_str_to_file(fn2, str2, 0);
  107. tt_int_op(r, OP_EQ, 0);
  108. // The tempfile should be deleted the next time we list the directory.
  109. d = storage_dir_new(dirname, 10);
  110. tt_int_op(1, OP_EQ, smartlist_len(storage_dir_list(d)));
  111. tt_u64_op(strlen(str1), OP_EQ, storage_dir_get_usage(d));
  112. tt_int_op(FN_FILE, OP_EQ, file_status(fn1));
  113. tt_int_op(FN_NOENT, OP_EQ, file_status(fn2));
  114. bytes = (char*) storage_dir_read(d, "1007", 1, NULL);
  115. tt_str_op(bytes, OP_EQ, str1);
  116. // Should have no effect; file already gone.
  117. storage_dir_remove_file(d, "1003.tmp");
  118. tt_int_op(1, OP_EQ, smartlist_len(storage_dir_list(d)));
  119. tt_u64_op(strlen(str1), OP_EQ, storage_dir_get_usage(d));
  120. // Actually remove a file.
  121. storage_dir_remove_file(d, "1007");
  122. tt_int_op(FN_NOENT, OP_EQ, file_status(fn1));
  123. tt_int_op(0, OP_EQ, smartlist_len(storage_dir_list(d)));
  124. tt_u64_op(0, OP_EQ, storage_dir_get_usage(d));
  125. done:
  126. tor_free(dirname);
  127. tor_free(fn1);
  128. tor_free(fn2);
  129. storage_dir_free(d);
  130. tor_free(bytes);
  131. }
  132. static void
  133. test_storagedir_full(void *arg)
  134. {
  135. (void)arg;
  136. char *dirname = tor_strdup(get_fname_rnd("store_dir"));
  137. storage_dir_t *d = NULL;
  138. const char str[] = "enemies of the peephole";
  139. int r;
  140. d = storage_dir_new(dirname, 3);
  141. tt_assert(d);
  142. r = storage_dir_save_string_to_file(d, str, 1, NULL);
  143. tt_int_op(r, OP_EQ, 0);
  144. r = storage_dir_save_string_to_file(d, str, 1, NULL);
  145. tt_int_op(r, OP_EQ, 0);
  146. r = storage_dir_save_string_to_file(d, str, 1, NULL);
  147. tt_int_op(r, OP_EQ, 0);
  148. // These should fail!
  149. r = storage_dir_save_string_to_file(d, str, 1, NULL);
  150. tt_int_op(r, OP_EQ, -1);
  151. r = storage_dir_save_string_to_file(d, str, 1, NULL);
  152. tt_int_op(r, OP_EQ, -1);
  153. tt_u64_op(strlen(str) * 3, OP_EQ, storage_dir_get_usage(d));
  154. done:
  155. tor_free(dirname);
  156. storage_dir_free(d);
  157. }
  158. static void
  159. test_storagedir_cleaning(void *arg)
  160. {
  161. (void)arg;
  162. char *dirname = tor_strdup(get_fname_rnd("store_dir"));
  163. storage_dir_t *d = NULL;
  164. const char str[] =
  165. "On a mountain halfway between Reno and Rome / "
  166. "We have a machine in a plexiglass dome / "
  167. "Which listens and looks into everyone's home."
  168. " -- Dr. Seuss";
  169. char *fns[8];
  170. int r, i;
  171. memset(fns, 0, sizeof(fns));
  172. d = storage_dir_new(dirname, 10);
  173. tt_assert(d);
  174. for (i = 0; i < 8; ++i) {
  175. r = storage_dir_save_string_to_file(d, str+i*2, 1, &fns[i]);
  176. tt_int_op(r, OP_EQ, 0);
  177. }
  178. /* Now we're going to make sure all the files have distinct mtimes. */
  179. time_t now = time(NULL);
  180. struct utimbuf ub;
  181. ub.actime = now;
  182. ub.modtime = now - 1000;
  183. for (i = 0; i < 8; ++i) {
  184. char *f = NULL;
  185. tor_asprintf(&f, "%s/%s", dirname, fns[i]);
  186. r = utime(f, &ub);
  187. tor_free(f);
  188. tt_int_op(r, OP_EQ, 0);
  189. ub.modtime += 5;
  190. }
  191. const uint64_t usage_orig = storage_dir_get_usage(d);
  192. /* No changes needed if we are already under target. */
  193. storage_dir_shrink(d, 1024*1024, 0);
  194. tt_u64_op(usage_orig, OP_EQ, storage_dir_get_usage(d));
  195. /* Get rid of at least one byte. This will delete fns[0]. */
  196. storage_dir_shrink(d, usage_orig - 1, 0);
  197. tt_u64_op(usage_orig, OP_GT, storage_dir_get_usage(d));
  198. tt_u64_op(usage_orig - strlen(str), OP_EQ, storage_dir_get_usage(d));
  199. /* Get rid of at least two files. This will delete fns[1] and fns[2]. */
  200. storage_dir_shrink(d, 1024*1024, 2);
  201. tt_u64_op(usage_orig - strlen(str)*3 + 6, OP_EQ, storage_dir_get_usage(d));
  202. /* Get rid of everything. */
  203. storage_dir_remove_all(d);
  204. tt_u64_op(0, OP_EQ, storage_dir_get_usage(d));
  205. done:
  206. tor_free(dirname);
  207. storage_dir_free(d);
  208. for (i = 0; i < 8; ++i) {
  209. tor_free(fns[i]);
  210. }
  211. }
  212. static void
  213. test_storagedir_save_labeled(void *arg)
  214. {
  215. (void)arg;
  216. char *dirname = tor_strdup(get_fname_rnd("store_dir"));
  217. storage_dir_t *d = NULL;
  218. uint8_t *inp = tor_malloc_zero(8192);
  219. config_line_t *labels = NULL;
  220. char *fname = NULL;
  221. uint8_t *saved = NULL;
  222. d = storage_dir_new(dirname, 10);
  223. tt_assert(d);
  224. crypto_rand((char *)inp, 8192);
  225. config_line_append(&labels, "Foo", "bar baz");
  226. config_line_append(&labels, "quux", "quuzXxz");
  227. const char expected[] =
  228. "Foo bar baz\n"
  229. "quux quuzXxz\n";
  230. int r = storage_dir_save_labeled_to_file(d, labels, inp, 8192, &fname);
  231. tt_int_op(r, OP_EQ, 0);
  232. size_t n = 0;
  233. saved = storage_dir_read(d, fname, 1, &n);
  234. tt_assert(memchr(saved, '\0', n));
  235. tt_str_op((char*)saved, OP_EQ, expected); /* NUL guarantees strcmp works */
  236. tt_mem_op(saved+strlen(expected)+1, OP_EQ, inp, 8192);
  237. done:
  238. storage_dir_free(d);
  239. tor_free(dirname);
  240. tor_free(inp);
  241. tor_free(fname);
  242. config_free_lines(labels);
  243. tor_free(saved);
  244. }
  245. static void
  246. test_storagedir_read_labeled(void *arg)
  247. {
  248. (void)arg;
  249. char *dirname = tor_strdup(get_fname_rnd("store_dir"));
  250. storage_dir_t *d = NULL;
  251. uint8_t *inp = tor_malloc_zero(8192);
  252. config_line_t *labels = NULL, *labels2 = NULL;
  253. char *fname = NULL;
  254. tor_mmap_t *map = NULL;
  255. uint8_t *as_read = NULL;
  256. d = storage_dir_new(dirname, 10);
  257. tt_assert(d);
  258. tor_snprintf((char*)inp, 8192,
  259. "Hello world\n"
  260. "This is a test\n"
  261. "Yadda yadda.\n");
  262. size_t bodylen = 8192 - strlen((char*)inp) - 1;
  263. crypto_rand((char *)inp+strlen((char*)inp)+1, bodylen);
  264. int r = storage_dir_save_bytes_to_file(d, inp, 8192, 1, &fname);
  265. tt_int_op(r, OP_EQ, 0);
  266. /* Try mapping */
  267. const uint8_t *datap = NULL;
  268. size_t sz = 0;
  269. map = storage_dir_map_labeled(d, fname, &labels, &datap, &sz);
  270. tt_assert(map);
  271. tt_assert(datap);
  272. tt_u64_op(sz, OP_EQ, bodylen);
  273. tt_mem_op(datap, OP_EQ, inp+strlen((char*)inp)+1, bodylen);
  274. tt_assert(labels);
  275. tt_str_op(labels->key, OP_EQ, "Hello");
  276. tt_str_op(labels->value, OP_EQ, "world");
  277. tt_assert(labels->next);
  278. tt_str_op(labels->next->key, OP_EQ, "This");
  279. tt_str_op(labels->next->value, OP_EQ, "is a test");
  280. tt_assert(labels->next->next);
  281. tt_str_op(labels->next->next->key, OP_EQ, "Yadda");
  282. tt_str_op(labels->next->next->value, OP_EQ, "yadda.");
  283. tt_ptr_op(labels->next->next->next, OP_EQ, NULL);
  284. /* Try reading this time. */
  285. sz = 0;
  286. as_read = storage_dir_read_labeled(d, fname, &labels2, &sz);
  287. tt_assert(as_read);
  288. tt_u64_op(sz, OP_EQ, bodylen);
  289. tt_mem_op(as_read, OP_EQ, inp+strlen((char*)inp)+1, bodylen);
  290. tt_assert(config_lines_eq(labels, labels2));
  291. done:
  292. storage_dir_free(d);
  293. tor_free(dirname);
  294. tor_free(inp);
  295. tor_free(fname);
  296. config_free_lines(labels);
  297. config_free_lines(labels2);
  298. tor_munmap_file(map);
  299. tor_free(as_read);
  300. }
  301. #define ENT(name) \
  302. { #name, test_storagedir_ ## name, TT_FORK, NULL, NULL }
  303. struct testcase_t storagedir_tests[] = {
  304. ENT(empty),
  305. ENT(basic),
  306. ENT(deletion),
  307. ENT(full),
  308. ENT(cleaning),
  309. ENT(save_labeled),
  310. ENT(read_labeled),
  311. END_OF_TESTCASES
  312. };