web_api.c 16 KB

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