web_api.c 16 KB

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