run-capabilities.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include <ccan/failtest/failtest_override.h>
  2. #include "ntdb-source.h"
  3. #include "tap-interface.h"
  4. #include "logging.h"
  5. #include "layout.h"
  6. #include "failtest_helper.h"
  7. #include <stdarg.h>
  8. #include "helprun-external-agent.h"
  9. static size_t len_of(bool breaks_check, bool breaks_write, bool breaks_open)
  10. {
  11. size_t len = 0;
  12. if (breaks_check)
  13. len += 8;
  14. if (breaks_write)
  15. len += 16;
  16. if (breaks_open)
  17. len += 32;
  18. return len;
  19. }
  20. /* Creates a NTDB with various capabilities. */
  21. static void create_ntdb(const char *name,
  22. unsigned int cap,
  23. bool breaks_check,
  24. bool breaks_write,
  25. bool breaks_open, ...)
  26. {
  27. NTDB_DATA key, data;
  28. va_list ap;
  29. struct ntdb_layout *layout;
  30. struct ntdb_context *ntdb;
  31. int fd, clen;
  32. union ntdb_attribute seed_attr;
  33. /* Force a seed which doesn't allow records to clash! */
  34. seed_attr.base.attr = NTDB_ATTRIBUTE_SEED;
  35. seed_attr.base.next = &tap_log_attr;
  36. seed_attr.seed.seed = 0;
  37. key = ntdb_mkdata("Hello", 5);
  38. data = ntdb_mkdata("world", 5);
  39. /* Create a NTDB with some data, and some capabilities */
  40. layout = new_ntdb_layout();
  41. ntdb_layout_add_freetable(layout);
  42. ntdb_layout_add_used(layout, key, data, 6);
  43. clen = len_of(breaks_check, breaks_write, breaks_open);
  44. ntdb_layout_add_free(layout, 15496 - clen, 0);
  45. ntdb_layout_add_capability(layout, cap,
  46. breaks_write, breaks_check, breaks_open,
  47. clen);
  48. va_start(ap, breaks_open);
  49. while ((cap = va_arg(ap, int)) != 0) {
  50. breaks_check = va_arg(ap, int);
  51. breaks_write = va_arg(ap, int);
  52. breaks_open = va_arg(ap, int);
  53. key.dsize--;
  54. ntdb_layout_add_used(layout, key, data, 11 - key.dsize);
  55. clen = len_of(breaks_check, breaks_write, breaks_open);
  56. ntdb_layout_add_free(layout, 16304 - clen, 0);
  57. ntdb_layout_add_capability(layout, cap,
  58. breaks_write, breaks_check,
  59. breaks_open, clen);
  60. }
  61. va_end(ap);
  62. /* We open-code this, because we need to use the failtest write. */
  63. ntdb = ntdb_layout_get(layout, failtest_free, &seed_attr);
  64. fd = open(name, O_RDWR|O_TRUNC|O_CREAT, 0600);
  65. if (fd < 0)
  66. err(1, "opening %s for writing", name);
  67. if (write(fd, ntdb->file->map_ptr, ntdb->file->map_size)
  68. != ntdb->file->map_size)
  69. err(1, "writing %s", name);
  70. close(fd);
  71. ntdb_close(ntdb);
  72. ntdb_layout_free(layout);
  73. }
  74. /* Note all the "goto out" early exits: they're to shorten failtest time. */
  75. int main(int argc, char *argv[])
  76. {
  77. struct ntdb_context *ntdb;
  78. char *summary;
  79. failtest_init(argc, argv);
  80. failtest_hook = block_repeat_failures;
  81. failtest_exit_check = exit_check_log;
  82. plan_tests(60);
  83. failtest_suppress = true;
  84. /* Capability says you can ignore it? */
  85. create_ntdb("run-capabilities.ntdb", 1, false, false, false, 0);
  86. failtest_suppress = false;
  87. ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDWR, 0,
  88. &tap_log_attr);
  89. failtest_suppress = true;
  90. if (!ok1(ntdb))
  91. goto out;
  92. ok1(tap_log_messages == 0);
  93. ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
  94. ok1(tap_log_messages == 0);
  95. ntdb_close(ntdb);
  96. /* Two capabilitues say you can ignore them? */
  97. create_ntdb("run-capabilities.ntdb",
  98. 1, false, false, false,
  99. 2, false, false, false, 0);
  100. failtest_suppress = false;
  101. ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDWR, 0,
  102. &tap_log_attr);
  103. failtest_suppress = true;
  104. if (!ok1(ntdb))
  105. goto out;
  106. ok1(tap_log_messages == 0);
  107. ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
  108. ok1(tap_log_messages == 0);
  109. ok1(ntdb_summary(ntdb, 0, &summary) == NTDB_SUCCESS);
  110. ok1(strstr(summary, "Capability 1\n"));
  111. free(summary);
  112. ntdb_close(ntdb);
  113. /* Capability says you can't check. */
  114. create_ntdb("run-capabilities.ntdb",
  115. 1, false, false, false,
  116. 2, true, false, false, 0);
  117. failtest_suppress = false;
  118. ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDWR, 0,
  119. &tap_log_attr);
  120. failtest_suppress = true;
  121. if (!ok1(ntdb))
  122. goto out;
  123. ok1(tap_log_messages == 0);
  124. ok1(ntdb_get_flags(ntdb) & NTDB_CANT_CHECK);
  125. ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
  126. /* We expect a warning! */
  127. ok1(tap_log_messages == 1);
  128. ok1(strstr(log_last, "capabilit"));
  129. ok1(ntdb_summary(ntdb, 0, &summary) == NTDB_SUCCESS);
  130. ok1(strstr(summary, "Capability 1\n"));
  131. ok1(strstr(summary, "Capability 2 (uncheckable)\n"));
  132. free(summary);
  133. ntdb_close(ntdb);
  134. /* Capability says you can't write. */
  135. create_ntdb("run-capabilities.ntdb",
  136. 1, false, false, false,
  137. 2, false, true, false, 0);
  138. failtest_suppress = false;
  139. ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDWR, 0,
  140. &tap_log_attr);
  141. failtest_suppress = true;
  142. /* We expect a message. */
  143. ok1(!ntdb);
  144. if (!ok1(tap_log_messages == 2))
  145. goto out;
  146. if (!ok1(strstr(log_last, "unknown")))
  147. goto out;
  148. ok1(strstr(log_last, "write"));
  149. /* We can open it read-only though! */
  150. failtest_suppress = false;
  151. ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDONLY, 0,
  152. &tap_log_attr);
  153. failtest_suppress = true;
  154. if (!ok1(ntdb))
  155. goto out;
  156. ok1(tap_log_messages == 2);
  157. ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
  158. ok1(tap_log_messages == 2);
  159. ok1(ntdb_summary(ntdb, 0, &summary) == NTDB_SUCCESS);
  160. ok1(strstr(summary, "Capability 1\n"));
  161. ok1(strstr(summary, "Capability 2 (read-only)\n"));
  162. free(summary);
  163. ntdb_close(ntdb);
  164. /* Capability says you can't open. */
  165. create_ntdb("run-capabilities.ntdb",
  166. 1, false, false, false,
  167. 2, false, false, true, 0);
  168. failtest_suppress = false;
  169. ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDWR, 0,
  170. &tap_log_attr);
  171. failtest_suppress = true;
  172. /* We expect a message. */
  173. ok1(!ntdb);
  174. if (!ok1(tap_log_messages == 3))
  175. goto out;
  176. if (!ok1(strstr(log_last, "unknown")))
  177. goto out;
  178. /* Combine capabilities correctly. */
  179. create_ntdb("run-capabilities.ntdb",
  180. 1, false, false, false,
  181. 2, true, false, false,
  182. 3, false, true, false, 0);
  183. failtest_suppress = false;
  184. ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDWR, 0,
  185. &tap_log_attr);
  186. failtest_suppress = true;
  187. /* We expect a message. */
  188. ok1(!ntdb);
  189. if (!ok1(tap_log_messages == 4))
  190. goto out;
  191. if (!ok1(strstr(log_last, "unknown")))
  192. goto out;
  193. ok1(strstr(log_last, "write"));
  194. /* We can open it read-only though! */
  195. failtest_suppress = false;
  196. ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDONLY, 0,
  197. &tap_log_attr);
  198. failtest_suppress = true;
  199. if (!ok1(ntdb))
  200. goto out;
  201. ok1(tap_log_messages == 4);
  202. ok1(ntdb_get_flags(ntdb) & NTDB_CANT_CHECK);
  203. ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
  204. /* We expect a warning! */
  205. ok1(tap_log_messages == 5);
  206. ok1(strstr(log_last, "unknown"));
  207. ok1(ntdb_summary(ntdb, 0, &summary) == NTDB_SUCCESS);
  208. ok1(strstr(summary, "Capability 1\n"));
  209. ok1(strstr(summary, "Capability 2 (uncheckable)\n"));
  210. ok1(strstr(summary, "Capability 3 (read-only)\n"));
  211. free(summary);
  212. ntdb_close(ntdb);
  213. /* Two capability flags in one. */
  214. create_ntdb("run-capabilities.ntdb",
  215. 1, false, false, false,
  216. 2, true, true, false,
  217. 0);
  218. failtest_suppress = false;
  219. ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDWR, 0,
  220. &tap_log_attr);
  221. failtest_suppress = true;
  222. /* We expect a message. */
  223. ok1(!ntdb);
  224. if (!ok1(tap_log_messages == 6))
  225. goto out;
  226. if (!ok1(strstr(log_last, "unknown")))
  227. goto out;
  228. ok1(strstr(log_last, "write"));
  229. /* We can open it read-only though! */
  230. failtest_suppress = false;
  231. ntdb = ntdb_open("run-capabilities.ntdb", MAYBE_NOSYNC, O_RDONLY, 0,
  232. &tap_log_attr);
  233. failtest_suppress = true;
  234. if (!ok1(ntdb))
  235. goto out;
  236. ok1(tap_log_messages == 6);
  237. ok1(ntdb_get_flags(ntdb) & NTDB_CANT_CHECK);
  238. ok1(ntdb_check(ntdb, NULL, NULL) == NTDB_SUCCESS);
  239. /* We expect a warning! */
  240. ok1(tap_log_messages == 7);
  241. ok1(strstr(log_last, "unknown"));
  242. ok1(ntdb_summary(ntdb, 0, &summary) == NTDB_SUCCESS);
  243. ok1(strstr(summary, "Capability 1\n"));
  244. ok1(strstr(summary, "Capability 2 (uncheckable,read-only)\n"));
  245. free(summary);
  246. ntdb_close(ntdb);
  247. out:
  248. failtest_exit(exit_status());
  249. /*
  250. * We will never reach this but the compiler complains if we do not
  251. * return in this function.
  252. */
  253. return EFAULT;
  254. }