test_storagedir.c 10 KB

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