web_api.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "libnetdata/libnetdata.h"
  3. #include "libnetdata/required_dummies.h"
  4. #include "database/rrd.h"
  5. #include "web/server/web_client.h"
  6. #include <setjmp.h>
  7. #include <cmocka.h>
  8. #include <stdbool.h>
  9. void free_temporary_host(RRDHOST *host)
  10. {
  11. (void) host;
  12. }
  13. void *__wrap_free_temporary_host(RRDHOST *host)
  14. {
  15. (void) host;
  16. return NULL;
  17. }
  18. void repr(char *result, int result_size, char const *buf, int size)
  19. {
  20. int n;
  21. char *end = result + result_size - 1;
  22. unsigned char const *ubuf = (unsigned char const *)buf;
  23. while (size && result_size > 0) {
  24. if (*ubuf <= 0x20 || *ubuf >= 0x80) {
  25. n = snprintf(result, result_size, "\\%02X", *ubuf);
  26. } else {
  27. *result = *ubuf;
  28. n = 1;
  29. }
  30. result += n;
  31. result_size -= n;
  32. ubuf++;
  33. size--;
  34. }
  35. if (result_size > 0)
  36. *(result++) = 0;
  37. else
  38. *end = 0;
  39. }
  40. // ---------------------------------- Mocking accesses from web_client ------------------------------------------------
  41. ssize_t send(int sockfd, const void *buf, size_t len, int flags)
  42. {
  43. netdata_log_info("Mocking send: %zu bytes\n", len);
  44. (void)sockfd;
  45. (void)buf;
  46. (void)flags;
  47. return len;
  48. }
  49. RRDHOST *__wrap_rrdhost_find_by_hostname(const char *hostname, uint32_t hash)
  50. {
  51. (void)hostname;
  52. (void)hash;
  53. return NULL;
  54. }
  55. /* Note: we've got some intricate code inside the global statistics module, might be useful to pull it inside the
  56. test set instead of mocking it. */
  57. void __wrap_finished_web_request_statistics(
  58. uint64_t dt, uint64_t bytes_received, uint64_t bytes_sent, uint64_t content_size, uint64_t compressed_content_size)
  59. {
  60. (void)dt;
  61. (void)bytes_received;
  62. (void)bytes_sent;
  63. (void)content_size;
  64. (void)compressed_content_size;
  65. }
  66. char *__wrap_config_get(struct config *root, const char *section, const char *name, const char *default_value)
  67. {
  68. (void)root;
  69. (void)section;
  70. (void)name;
  71. (void)default_value;
  72. return "UNKNOWN FIX ME";
  73. }
  74. int __wrap_web_client_api_request_v1(RRDHOST *host, struct web_client *w, char *url)
  75. {
  76. char url_repr[160];
  77. repr(url_repr, sizeof(url_repr), url, strlen(url));
  78. netdata_log_info("web_client_api_request_v1(url=\"%s\")\n", url_repr);
  79. check_expected_ptr(host);
  80. check_expected_ptr(w);
  81. check_expected_ptr(url_repr);
  82. return HTTP_RESP_OK;
  83. }
  84. int __wrap_rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url)
  85. {
  86. (void)host;
  87. (void)w;
  88. (void)url;
  89. return 0;
  90. }
  91. RRDHOST *__wrap_rrdhost_find_by_guid(const char *guid, uint32_t hash)
  92. {
  93. (void)guid;
  94. (void)hash;
  95. printf("FIXME: rrdset_find_guid\n");
  96. return NULL;
  97. }
  98. RRDSET *__wrap_rrdset_find_byname(RRDHOST *host, const char *name)
  99. {
  100. (void)host;
  101. (void)name;
  102. printf("FIXME: rrdset_find_byname\n");
  103. return NULL;
  104. }
  105. RRDSET *__wrap_rrdset_find(RRDHOST *host, const char *id)
  106. {
  107. (void)host;
  108. (void)id;
  109. printf("FIXME: rrdset_find\n");
  110. return NULL;
  111. }
  112. // -------------------------------- Mocking the log - capture per-test ------------------------------------------------
  113. char log_buffer[10240] = { 0 };
  114. void __wrap_debug_int(const char *file, const char *function, const unsigned long line, const char *fmt, ...)
  115. {
  116. (void)file;
  117. (void)function;
  118. (void)line;
  119. va_list args;
  120. va_start(args, fmt);
  121. size_t cur = strlen(log_buffer);
  122. snprintf(log_buffer + cur, sizeof(log_buffer) - cur, " DEBUG: ");
  123. cur = strlen(log_buffer);
  124. vsnprintf(log_buffer + cur, sizeof(log_buffer) - cur, fmt, args);
  125. cur = strlen(log_buffer);
  126. snprintf(log_buffer + cur, sizeof(log_buffer) - cur, "\n");
  127. va_end(args);
  128. }
  129. void __wrap_info_int(const char *file, const char *function, const unsigned long line, const char *fmt, ...)
  130. {
  131. (void)file;
  132. (void)function;
  133. (void)line;
  134. va_list args;
  135. va_start(args, fmt);
  136. size_t cur = strlen(log_buffer);
  137. snprintf(log_buffer + cur, sizeof(log_buffer) - cur, " INFO: ");
  138. cur = strlen(log_buffer);
  139. vsnprintf(log_buffer + cur, sizeof(log_buffer) - cur, fmt, args);
  140. cur = strlen(log_buffer);
  141. snprintf(log_buffer + cur, sizeof(log_buffer) - cur, "\n");
  142. va_end(args);
  143. }
  144. void __wrap_error_int(
  145. const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ...)
  146. {
  147. (void)prefix;
  148. (void)file;
  149. (void)function;
  150. (void)line;
  151. va_list args;
  152. va_start(args, fmt);
  153. size_t cur = strlen(log_buffer);
  154. snprintf(log_buffer + cur, sizeof(log_buffer) - cur, " ERROR: ");
  155. cur = strlen(log_buffer);
  156. vsnprintf(log_buffer + cur, sizeof(log_buffer) - cur, fmt, args);
  157. cur = strlen(log_buffer);
  158. snprintf(log_buffer + cur, sizeof(log_buffer) - cur, "\n");
  159. va_end(args);
  160. }
  161. void __wrap_fatal_int(const char *file, const char *function, const unsigned long line, const char *fmt, ...)
  162. {
  163. (void)file;
  164. (void)function;
  165. (void)line;
  166. va_list args;
  167. va_start(args, fmt);
  168. printf("FATAL: ");
  169. vprintf(fmt, args);
  170. printf("\n");
  171. va_end(args);
  172. fail();
  173. }
  174. WEB_SERVER_MODE web_server_mode = WEB_SERVER_MODE_STATIC_THREADED;
  175. char *netdata_configured_web_dir = "UNKNOWN FIXME";
  176. RRDHOST *localhost = NULL;
  177. struct config netdata_config = { .first_section = NULL,
  178. .last_section = NULL,
  179. .mutex = NETDATA_MUTEX_INITIALIZER,
  180. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  181. .rwlock = AVL_LOCK_INITIALIZER } };
  182. const char *http_headers[] = { "Host: 254.254.0.1",
  183. "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_" // No ,
  184. "0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36",
  185. "Connection: keep-alive",
  186. "X-Forwarded-For: 1.254.1.251",
  187. "Cookie: _ga=GA1.1.1227576758.1571113676; _gid=GA1.2.1222321739.1573628979",
  188. "X-Requested-With: XMLHttpRequest",
  189. "Accept-Encoding: gzip, deflate",
  190. "Cache-Control: no-cache, no-store" };
  191. #define MAX_HEADERS (sizeof(http_headers) / (sizeof(const char *)))
  192. static void build_request(struct web_buffer *wb, const char *url, bool use_cr, size_t num_headers)
  193. {
  194. buffer_reset(wb);
  195. buffer_strcat(wb, "GET ");
  196. buffer_strcat(wb, url);
  197. buffer_strcat(wb, " HTTP/1.1");
  198. if (use_cr)
  199. buffer_strcat(wb, "\r");
  200. buffer_strcat(wb, "\n");
  201. for (size_t i = 0; i < num_headers && i < MAX_HEADERS; i++) {
  202. buffer_strcat(wb, http_headers[i]);
  203. if (use_cr)
  204. buffer_strcat(wb, "\r");
  205. buffer_strcat(wb, "\n");
  206. }
  207. if (use_cr)
  208. buffer_strcat(wb, "\r");
  209. buffer_strcat(wb, "\n");
  210. }
  211. /* Note: this is not a CMocka group_test_setup/teardown pair. This is performed per-test.
  212. */
  213. static struct web_client *setup_fresh_web_client()
  214. {
  215. struct web_client *w = (struct web_client *)malloc(sizeof(struct web_client));
  216. memset(w, 0, sizeof(struct web_client));
  217. w->response.data = buffer_create(NETDATA_WEB_RESPONSE_INITIAL_SIZE);
  218. w->response.data->date = 0; // Valgrind uninitialised value
  219. w->response.data->expires = 0; // Valgrind uninitialised value
  220. w->response.data->options = 0; // Valgrind uninitialised value
  221. w->response.header = buffer_create(NETDATA_WEB_RESPONSE_HEADER_SIZE);
  222. w->response.header_output = buffer_create(NETDATA_WEB_RESPONSE_HEADER_SIZE);
  223. strcpy(w->origin, "*"); // Simulate web_client_create_on_fd()
  224. w->cookie1[0] = 0; // Simulate web_client_create_on_fd()
  225. w->cookie2[0] = 0; // Simulate web_client_create_on_fd()
  226. w->acl = 0x1f; // Everything on
  227. return w;
  228. }
  229. static void destroy_web_client(struct web_client *w)
  230. {
  231. buffer_free(w->response.data);
  232. buffer_free(w->response.header);
  233. buffer_free(w->response.header_output);
  234. free(w);
  235. }
  236. // ---------------------------------- Parameterized test-families -----------------------------------------------------
  237. // There is no way to pass a parameter block into the setup fixture, we would have to patch CMocka and maintain it
  238. // locally. (The void **current_state in _run_group_tests would be set from a parameter). This is unfortunate as a
  239. // parametric unit-tester needs to be to pass parameters to the fixtures. We are faking this by calculating the
  240. // space of tests in the launcher, passing an array of identical unit-tests to CMocka and then counting through the
  241. // parameters in the shared state passed between tests. To initialise this counter structure we use this global to
  242. // pass from the launcher (test-builder) to the setup-fixture.
  243. void *shared_test_state = NULL;
  244. // -------------------------------- Test family for /api/v1/info ------------------------------------------------------
  245. struct test_def {
  246. size_t num_headers; // Index coordinate
  247. size_t prefix_len; // Index coordinate
  248. char name[80];
  249. size_t full_len;
  250. struct web_client *instance; // Used within this single test
  251. bool completed, use_cr;
  252. struct test_def *next, *prev;
  253. };
  254. static void api_info(void **state)
  255. {
  256. (void)state;
  257. struct test_def *def = (struct test_def *)shared_test_state;
  258. shared_test_state = def->next;
  259. if (def->prev != NULL && !def->prev->completed && strlen(log_buffer) > 0) {
  260. printf("Log of failing case %s:\n", def->prev->name);
  261. puts(log_buffer);
  262. }
  263. log_buffer[0] = 0;
  264. if (localhost != NULL)
  265. free(localhost);
  266. localhost = calloc(1,sizeof(RRDHOST));
  267. def->instance = setup_fresh_web_client();
  268. build_request(def->instance->response.data, "/api/v1/info", def->use_cr, def->num_headers);
  269. def->instance->response.data->len = def->prefix_len;
  270. char buffer_repr[1024];
  271. repr(buffer_repr, sizeof(buffer_repr), def->instance->response.data->buffer,def->prefix_len);
  272. netdata_log_info("Buffer contains: %s [first %zu]", buffer_repr,def->prefix_len);
  273. if (def->prefix_len == def->full_len) {
  274. expect_value(__wrap_web_client_api_request_v1, host, localhost);
  275. expect_value(__wrap_web_client_api_request_v1, w, def->instance);
  276. expect_string(__wrap_web_client_api_request_v1, url_repr, "info");
  277. }
  278. web_client_process_request(def->instance);
  279. if (def->prefix_len == def->full_len)
  280. assert_int_equal(def->instance->flags & WEB_CLIENT_FLAG_WAIT_RECEIVE, 0);
  281. else
  282. assert_int_equal(def->instance->flags & WEB_CLIENT_FLAG_WAIT_RECEIVE, WEB_CLIENT_FLAG_WAIT_RECEIVE);
  283. assert_int_equal(def->instance->mode, WEB_CLIENT_MODE_NORMAL);
  284. def->completed = true;
  285. log_buffer[0] = 0;
  286. }
  287. static int api_info_launcher()
  288. {
  289. size_t num_tests = 0;
  290. struct web_client *template = setup_fresh_web_client();
  291. struct test_def *current, *head = NULL;
  292. struct test_def *prev = NULL;
  293. for (size_t i = 0; i < MAX_HEADERS; i++) {
  294. build_request(template->response.data, "/api/v1/info", true, i);
  295. for (size_t j = 0; j <= template->response.data->len; j++) {
  296. if (j == 0 && i > 0)
  297. continue; // All zero-length prefixes are identical, skip after first time
  298. current = malloc(sizeof(struct test_def));
  299. if (prev != NULL)
  300. prev->next = current;
  301. else
  302. head = current;
  303. current->prev = prev;
  304. prev = current;
  305. current->num_headers = i;
  306. current->prefix_len = j;
  307. current->full_len = template->response.data->len;
  308. current->instance = NULL;
  309. current->next = NULL;
  310. current->use_cr = true;
  311. current->completed = false;
  312. sprintf(
  313. current->name, "/api/v1/info@%zu,%zu/%zu+%d", current->num_headers, current->prefix_len,
  314. current->full_len,true);
  315. num_tests++;
  316. }
  317. }
  318. for (size_t i = 0; i < MAX_HEADERS; i++) {
  319. build_request(template->response.data, "/api/v1/info", false, i);
  320. for (size_t j = 0; j <= template->response.data->len; j++) {
  321. if (j == 0 && i > 0)
  322. continue; // All zero-length prefixes are identical, skip after first time
  323. current = malloc(sizeof(struct test_def));
  324. if (prev != NULL)
  325. prev->next = current;
  326. else
  327. head = current;
  328. current->prev = prev;
  329. prev = current;
  330. current->num_headers = i;
  331. current->prefix_len = j;
  332. current->full_len = template->response.data->len;
  333. current->instance = NULL;
  334. current->next = NULL;
  335. current->use_cr = false;
  336. current->completed = false;
  337. sprintf(
  338. current->name, "/api/v1/info@%zu,%zu/%zu+%d", current->num_headers, current->prefix_len,
  339. current->full_len,false);
  340. num_tests++;
  341. }
  342. }
  343. struct CMUnitTest *tests = calloc(num_tests, sizeof(struct CMUnitTest));
  344. current = head;
  345. for (size_t i = 0; i < num_tests; i++) {
  346. tests[i].name = current->name;
  347. tests[i].test_func = api_info;
  348. tests[i].setup_func = NULL;
  349. tests[i].teardown_func = NULL;
  350. tests[i].initial_state = NULL;
  351. current = current->next;
  352. }
  353. printf("Setup %zu tests in %p\n", num_tests, head);
  354. shared_test_state = head;
  355. int fails = _cmocka_run_group_tests("web_api", tests, num_tests, NULL, NULL);
  356. free(tests);
  357. destroy_web_client(template);
  358. current = head;
  359. while (current != NULL) {
  360. struct test_def *c = current;
  361. current = current->next;
  362. if (c->instance != NULL) // Clean up resources from tests that failed
  363. destroy_web_client(c->instance);
  364. free(c);
  365. }
  366. if (localhost!=NULL)
  367. free(localhost);
  368. return fails;
  369. }
  370. /* Raw notes for the cases that we did not use in the unit testing suite.
  371. Leaving them here instead of deleting them in-case we expand the suite during the
  372. work on the URL parser.
  373. Any ' ' in the URI -> invalid response (Description in 5.1 of RFC2616)
  374. Characters that can't be in paths #;?
  375. "GET /apb/../api/v1/info" HTTP/1.1\r\n"
  376. https://github.com/uriparser/uriparser/blob/uriparser-0.9.3/test/FourSuite.cpp
  377. Not clear why some of these are illegal -> reserved chars?
  378. ASSERT_TRUE(testBadUri("beepbeep\x07\x07", 8));
  379. ASSERT_TRUE(testBadUri("\n", 0));
  380. ASSERT_TRUE(testBadUri("::", 0)); // not OK, per Roy Fielding on the W3C uri list on 2004-04-01
  381. // the following test cases are from a Perl script by David A. Wheeler
  382. // at http://www.dwheeler.com/secure-programs/url.pl
  383. ASSERT_TRUE(testBadUri("http://www yahoo.com", 10));
  384. ASSERT_TRUE(testBadUri("http://www.yahoo.com/hello world/", 26));
  385. ASSERT_TRUE(testBadUri("http://www.yahoo.com/yelp.html#\"", 31));
  386. // the following test cases are from a Haskell program by Graham Klyne
  387. // at http://www.ninebynine.org/Software/HaskellUtils/Network/URITest.hs
  388. ASSERT_TRUE(testBadUri("[2010:836B:4179::836B:4179]", 0));
  389. ASSERT_TRUE(testBadUri(" ", 0));
  390. ASSERT_TRUE(testBadUri("%", 1));
  391. ASSERT_TRUE(testBadUri("A%Z", 2));
  392. ASSERT_TRUE(testBadUri("%ZZ", 1));
  393. ASSERT_TRUE(testBadUri("%AZ", 2));
  394. ASSERT_TRUE(testBadUri("A C", 1));
  395. ASSERT_TRUE(testBadUri("A\\'C", 1)); // r"A\'C"
  396. ASSERT_TRUE(testBadUri("A`C", 1));
  397. ASSERT_TRUE(testBadUri("A<C", 1));
  398. ASSERT_TRUE(testBadUri("A>C", 1));
  399. ASSERT_TRUE(testBadUri("A^C", 1));
  400. ASSERT_TRUE(testBadUri("A\\\\C", 1)); // r'A\\C'
  401. ASSERT_TRUE(testBadUri("A{C", 1));
  402. ASSERT_TRUE(testBadUri("A|C", 1));
  403. ASSERT_TRUE(testBadUri("A}C", 1));
  404. ASSERT_TRUE(testBadUri("A[C", 1));
  405. ASSERT_TRUE(testBadUri("A]C", 1));
  406. ASSERT_TRUE(testBadUri("A[**]C", 1));
  407. ASSERT_TRUE(testBadUri("http://[xyz]/", 8));
  408. ASSERT_TRUE(testBadUri("http://]/", 7));
  409. ASSERT_TRUE(testBadUri("http://example.org/[2010:836B:4179::836B:4179]", 19));
  410. ASSERT_TRUE(testBadUri("http://example.org/abc#[2010:836B:4179::836B:4179]", 23));
  411. ASSERT_TRUE(testBadUri("http://example.org/xxx/[qwerty]#a[b]", 23));
  412. // from a post to the W3C uri list on 2004-02-17
  413. // breaks at 22 instead of 17 because everything up to that point is a valid userinfo
  414. ASSERT_TRUE(testBadUri("http://w3c.org:80path1/path2", 22));
  415. */
  416. int main(void)
  417. {
  418. debug_flags = 0xffffffffffff;
  419. int fails = 0;
  420. fails += api_info_launcher();
  421. return fails;
  422. }