test_httpuplds.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /* _
  2. * ___ __ _ __ _ _ _(_)
  3. * / __|/ _` |/ _` | | | | |
  4. * \__ \ (_| | (_| | |_| | |
  5. * |___/\__,_|\__, |\__,_|_|
  6. * |___/
  7. *
  8. * Cross-platform library which helps to develop web servers or frameworks.
  9. *
  10. * Copyright (C) 2016-2020 Silvio Clecio <[email protected]>
  11. *
  12. * Sagui library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * Sagui library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with Sagui library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #define SG_EXTERN
  27. #include "sg_assert.h"
  28. #include <string.h>
  29. #include <fcntl.h>
  30. #include <sagui.h>
  31. #include "sg_utils.h"
  32. #include "sg_httpuplds.c"
  33. #ifndef TEST_HTTPUPLDS_BASE_PATH
  34. #ifdef __ANDROID__
  35. #define TEST_HTTPUPLDS_BASE_PATH SG_ANDROID_TESTS_DEST_DIR "/"
  36. #else /* __ANDROID__ */
  37. #ifdef _WIN32
  38. #define TEST_HTTPUPLDS_BASE_PATH BINARY_DIR "/"
  39. #else /* _WIN32 */
  40. #define TEST_HTTPUPLDS_BASE_PATH "/tmp/"
  41. #endif /* _WIN32 */
  42. #endif /* __ANDROID__ */
  43. #endif /* TEST_HTTPUPLDS_BASE_PATH */
  44. #ifndef TEST_HTTPUPLDS_OPEN_MODE
  45. #define TEST_HTTPUPLDS_OPEN_MODE \
  46. S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
  47. #endif /* TEST_HTTPUPLDS_OPEN_MODE */
  48. #ifndef TEST_HTTPUPLDS_OPEN_WFLAGS
  49. #define TEST_HTTPUPLDS_OPEN_WFLAGS O_RDWR | O_CREAT | O_TRUNC
  50. #endif /* TEST_HTTPUPLDS_OPEN_WFLAGS */
  51. static int dummy_httpuplds_save_cb(void *handle, bool overwritten) {
  52. (void) handle;
  53. (void) overwritten;
  54. return 123;
  55. }
  56. static int dummy_httpuplds_save_as_cb(void *handle, const char *path,
  57. bool overwritten) {
  58. (void) handle;
  59. (void) path;
  60. (void) overwritten;
  61. return 123;
  62. }
  63. static int dummy_httpuplds_iter_cb(void *cls, struct sg_httpupld *upld) {
  64. const char *str = cls;
  65. if (strcmp(upld->name, "foo") == 0 || strcmp(upld->name, "bar") == 0) {
  66. sprintf(cls, "%s%s", str, upld->name);
  67. return 0;
  68. }
  69. return 123;
  70. }
  71. static void dummy_httpreq_cb(void *cls, struct sg_httpreq *req,
  72. struct sg_httpres *res) {
  73. (void) cls;
  74. (void) req;
  75. (void) res;
  76. }
  77. static void dummy_err_cb(void *cls, const char *err) {
  78. strcpy(cls, err);
  79. }
  80. static int empty_httpupld_cb(void *cls, void **handle, const char *dir,
  81. const char *field, const char *name,
  82. const char *mime, const char *encoding) {
  83. (void) cls;
  84. (void) handle;
  85. (void) dir;
  86. (void) field;
  87. (void) name;
  88. (void) mime;
  89. (void) encoding;
  90. return -1;
  91. }
  92. static ssize_t empty_httpupld_write_cb(void *handle, uint64_t offset,
  93. const char *buf, size_t size) {
  94. (void) handle;
  95. (void) offset;
  96. (void) buf;
  97. (void) size;
  98. return -1;
  99. }
  100. static void test__httpuplds_add(void) {
  101. char err[256];
  102. struct sg_httpsrv *srv =
  103. sg_httpsrv_new2(NULL, dummy_httpreq_cb, dummy_err_cb, err);
  104. struct sg_httpreq *req = sg__httpreq_new(srv, NULL, "", "", "");
  105. ASSERT(sg_httpuplds_count(sg_httpreq_uploads(req)) == 0);
  106. sg__httpuplds_add(srv, req, "abc", "def", "ghi", "jkl");
  107. ASSERT(strcmp(sg_httpupld_field(req->curr_upld), "abc") == 0);
  108. ASSERT(strcmp(sg_httpupld_name(req->curr_upld), "def") == 0);
  109. ASSERT(strcmp(sg_httpupld_mime(req->curr_upld), "ghi") == 0);
  110. ASSERT(strcmp(sg_httpupld_encoding(req->curr_upld), "jkl") == 0);
  111. ASSERT(sg_httpuplds_count(sg_httpreq_uploads(req)) == 1);
  112. sg__httpuplds_free(srv, req);
  113. sg__httpreq_free(req);
  114. sg_httpsrv_free(srv);
  115. }
  116. static void test__httpuplds_free(void) {
  117. sg__httpuplds_free(NULL, NULL);
  118. }
  119. static void test__httpuplds_iter(void) {
  120. const char *filename = "foo.txt";
  121. const size_t len = 3;
  122. char err[256], str[256];
  123. struct sg_httpsrv *srv =
  124. sg_httpsrv_new2(NULL, dummy_httpreq_cb, dummy_err_cb, err);
  125. struct sg_httpreq *req = sg__httpreq_new(srv, NULL, "", "", "");
  126. struct sg__httpupld_holder holder = {srv, req};
  127. struct sg_strmap **fields;
  128. sg_httpupld_cb saved_upld_cb;
  129. sg_write_cb saved_upld_write_cb;
  130. char *dir, *dest_path;
  131. ASSERT(sg__httpuplds_iter(NULL, MHD_POSTDATA_KIND, NULL, NULL, NULL, NULL,
  132. NULL, 0, 0) == MHD_YES);
  133. saved_upld_cb = srv->upld_cb;
  134. srv->upld_cb = empty_httpupld_cb;
  135. ASSERT(sg__httpuplds_iter(&holder, MHD_POSTDATA_KIND, NULL, filename, NULL,
  136. NULL, "foo", 0, len) == MHD_NO);
  137. srv->upld_cb = saved_upld_cb;
  138. saved_upld_write_cb = srv->upld_write_cb;
  139. srv->upld_write_cb = empty_httpupld_write_cb;
  140. ASSERT(sg__httpuplds_iter(&holder, MHD_POSTDATA_KIND, NULL, filename, NULL,
  141. NULL, "foo", 0, len) == MHD_NO);
  142. srv->upld_write_cb = saved_upld_write_cb;
  143. dir = sg_tmpdir();
  144. ASSERT(dir);
  145. dest_path = sg__strjoin(PATH_SEP, dir, filename);
  146. sg_free(dir);
  147. ASSERT(dest_path);
  148. unlink(dest_path);
  149. ASSERT(access(dest_path, F_OK) == -1);
  150. ASSERT(sg__httpuplds_iter(&holder, MHD_POSTDATA_KIND, NULL, filename, NULL,
  151. NULL, "foo", 0, len) == MHD_YES);
  152. ASSERT(sg_httpupld_save(holder.req->curr_upld, true) == 0);
  153. ASSERT(access(dest_path, F_OK) == 0);
  154. sg_free(dest_path);
  155. ASSERT(sg_httpsrv_set_uplds_limit(srv, 1) == 0);
  156. memset(err, 0, sizeof(err));
  157. ASSERT(sg__httpuplds_iter(&holder, MHD_POSTDATA_KIND, NULL, filename, NULL,
  158. NULL, "foo", 0, len) == MHD_NO);
  159. memset(str, 0, sizeof(str));
  160. snprintf(str, sizeof(str), _("Upload too large.\n"));
  161. ASSERT(strcmp(err, str) == 0);
  162. fields = sg_httpreq_fields(req);
  163. ASSERT(!sg_strmap_get(*fields, "foo"));
  164. ASSERT(sg__httpuplds_iter(&holder, MHD_POSTDATA_KIND, "foo", NULL, NULL, NULL,
  165. "bar", 0, len) == MHD_YES);
  166. ASSERT(strcmp(sg_strmap_get(*fields, "foo"), "bar") == 0);
  167. ASSERT(sg_httpsrv_set_payld_limit(srv, 1) == 0);
  168. memset(err, 0, sizeof(err));
  169. ASSERT(sg__httpuplds_iter(&holder, MHD_POSTDATA_KIND, "foo", NULL, NULL, NULL,
  170. "bar", 0, len) == MHD_NO);
  171. memset(str, 0, sizeof(str));
  172. snprintf(str, sizeof(str), _("Payload too large.\n"));
  173. ASSERT(strcmp(err, str) == 0);
  174. sg__httpuplds_cleanup(srv, req);
  175. sg__httpreq_free(req);
  176. sg_httpsrv_free(srv);
  177. }
  178. static void test__httpuplds_process(void) {
  179. const size_t len = 3;
  180. char err[256], str[256];
  181. struct MHD_Connection *con = sg_alloc(64);
  182. struct sg_httpsrv *srv =
  183. sg_httpsrv_new2(NULL, dummy_httpreq_cb, dummy_err_cb, err);
  184. struct sg_httpreq *req = sg__httpreq_new(srv, NULL, "", "", "");
  185. int ret = 0;
  186. size_t size = 0;
  187. ASSERT(!sg__httpuplds_process(NULL, NULL, NULL, NULL, &size, &ret));
  188. ASSERT(sg_httpsrv_set_payld_limit(srv, 1) == 0);
  189. size = len;
  190. ret = MHD_YES;
  191. memset(err, 0, sizeof(err));
  192. ASSERT(sg__httpuplds_process(srv, req, con, "foo", &size, &ret));
  193. ASSERT(ret == MHD_NO);
  194. memset(str, 0, sizeof(str));
  195. snprintf(str, sizeof(str), _("Payload too large.\n"));
  196. ASSERT(strcmp(err, str) == 0);
  197. ASSERT(sg_httpsrv_set_payld_limit(srv, len) == 0);
  198. ret = MHD_NO;
  199. ASSERT(sg__httpuplds_process(srv, req, con, "foo", &size, &ret));
  200. ASSERT(ret == MHD_YES);
  201. ASSERT(strcmp(sg_str_content(req->payload), "foo") == 0);
  202. sg__httpreq_free(req);
  203. sg_httpsrv_free(srv);
  204. sg_free(con);
  205. }
  206. static void test__httpuplds_cleanup(void) {
  207. struct sg_httpsrv *srv = sg_httpsrv_new(dummy_httpreq_cb, NULL);
  208. struct sg_httpreq *req = sg__httpreq_new(srv, NULL, "", "", "");
  209. struct sg_httpupld *tmp;
  210. size_t count = 0;
  211. ASSERT(srv);
  212. ASSERT(req);
  213. sg__httpuplds_cleanup(srv, req);
  214. LL_COUNT(req->uplds, tmp, count);
  215. ASSERT(count == 0);
  216. req->curr_upld = sg_alloc(sizeof(struct sg_httpupld));
  217. LL_APPEND(req->uplds, req->curr_upld);
  218. req->curr_upld = NULL; /* fix PVS-Studio analysis */
  219. req->curr_upld = sg_alloc(sizeof(struct sg_httpupld));
  220. LL_APPEND(req->uplds, req->curr_upld);
  221. LL_COUNT(req->uplds, tmp, count);
  222. ASSERT(count == 2);
  223. sg__httpuplds_cleanup(srv, req);
  224. LL_COUNT(req->uplds, tmp, count);
  225. ASSERT(count == 0);
  226. sg__httpreq_free(req);
  227. sg_httpsrv_free(srv);
  228. }
  229. static void test__httpupld_cb(void) {
  230. const char *dummy_path = TEST_HTTPUPLDS_BASE_PATH "foo.txt",
  231. *filename = "foo.txt";
  232. const ssize_t len = 3;
  233. char err[256], str[256];
  234. void *handle = NULL;
  235. struct sg__httpupld *h;
  236. struct sg_httpsrv *srv;
  237. char *dir, *dest_path;
  238. int fd;
  239. memset(err, 0, sizeof(err));
  240. srv = sg_httpsrv_new2(NULL, dummy_httpreq_cb, dummy_err_cb, err);
  241. ASSERT(sg__httpupld_cb(srv, &handle, "", "", "", "", "") == ENOENT);
  242. ASSERT(!handle);
  243. snprintf(str, sizeof(str), _("Cannot find uploads directory \"%s\": %s.\n"),
  244. "", strerror(ENOENT));
  245. ASSERT(strcmp(err, str) == 0);
  246. unlink(dummy_path);
  247. fd = open(dummy_path, TEST_HTTPUPLDS_OPEN_WFLAGS, TEST_HTTPUPLDS_OPEN_MODE);
  248. ASSERT(fd > -1);
  249. ASSERT(close(fd) == 0);
  250. ASSERT(access(dummy_path, F_OK) == 0);
  251. memset(err, 0, sizeof(err));
  252. ASSERT(sg__httpupld_cb(srv, &handle, dummy_path, "", "", "", "") == ENOTDIR);
  253. unlink(dummy_path);
  254. ASSERT(!handle);
  255. memset(str, 0, sizeof(str));
  256. snprintf(str, sizeof(str), _("Cannot access uploads directory \"%s\": %s.\n"),
  257. dummy_path, strerror(ENOTDIR));
  258. ASSERT(strcmp(err, str) == 0);
  259. #if defined(__linux__) && !defined(__ANDROID__)
  260. memset(err, 0, sizeof(err));
  261. ASSERT(sg__httpupld_cb(srv, &handle, "/", "", "", "", "") == EACCES);
  262. ASSERT(!handle);
  263. memset(str, 0, sizeof(str));
  264. snprintf(str, sizeof(str),
  265. _("Cannot create temporary upload file in \"%s\": %s.\n"), "/",
  266. strerror(EACCES));
  267. ASSERT(strcmp(err, str) == 0);
  268. #endif /* __linux__ && !__ANDROID__ */
  269. dir = sg_tmpdir();
  270. dest_path = sg__strjoin(PATH_SEP, dir, filename);
  271. ASSERT(dest_path);
  272. unlink(dest_path);
  273. ASSERT(access(dest_path, F_OK) == -1);
  274. ASSERT(sg__httpupld_cb(srv, &handle, dir, "foo", "foo.txt", "", "") == 0);
  275. sg_free(dir);
  276. h = handle;
  277. ASSERT(access(h->path, F_OK) == 0);
  278. ASSERT(sg__httpupld_write_cb(handle, 0, "foo", len) == len);
  279. ASSERT(sg__httpupld_save_cb(handle, true) == 0);
  280. sg__httpupld_free_cb(handle);
  281. ASSERT(access(dest_path, F_OK) == 0);
  282. fd = open(dest_path, O_RDONLY);
  283. sg_free(dest_path);
  284. ASSERT(fd > -1);
  285. memset(str, 0, sizeof(str));
  286. ASSERT(read(fd, str, len) == len);
  287. ASSERT(close(fd) == 0);
  288. ASSERT(strcmp(str, "foo") == 0);
  289. sg_httpsrv_free(srv);
  290. }
  291. static void test__httpupld_write_cb(void) {
  292. const ssize_t len = 3;
  293. char str[4];
  294. struct sg__httpupld *handle = sg_alloc(sizeof(struct sg__httpupld));
  295. handle->path = TEST_HTTPUPLDS_BASE_PATH "foo.txt";
  296. unlink(handle->path);
  297. ASSERT(access(handle->path, F_OK) == -1);
  298. handle->fd =
  299. open(handle->path, TEST_HTTPUPLDS_OPEN_WFLAGS, TEST_HTTPUPLDS_OPEN_MODE);
  300. ASSERT(handle->fd > -1);
  301. ASSERT(sg__httpupld_write_cb(handle, 0, "foo", len - 1) == (len - 1));
  302. ASSERT(close(handle->fd) == 0);
  303. ASSERT(access(handle->path, F_OK) == 0);
  304. handle->fd = open(handle->path, O_RDONLY);
  305. ASSERT(handle->fd);
  306. memset(str, 0, sizeof(str));
  307. ASSERT(read(handle->fd, str, len) == (len - 1));
  308. ASSERT(close(handle->fd) == 0);
  309. ASSERT(strcmp(str, "fo") == 0);
  310. sg_free(handle);
  311. }
  312. static void test__httpupld_free_cb(void) {
  313. const char *path = TEST_HTTPUPLDS_BASE_PATH "foo.txt";
  314. char err[256];
  315. struct sg_httpsrv *saved_srv;
  316. struct sg__httpupld *handle = sg_alloc(sizeof(struct sg__httpupld));
  317. memset(err, 0, sizeof(err));
  318. handle->srv = sg_httpsrv_new2(NULL, dummy_httpreq_cb, dummy_err_cb, err);
  319. handle->path = sg__strdup(path);
  320. handle->dest = NULL;
  321. unlink(handle->path);
  322. handle->fd =
  323. open(handle->path, TEST_HTTPUPLDS_OPEN_WFLAGS, TEST_HTTPUPLDS_OPEN_MODE);
  324. ASSERT(handle->fd > -1);
  325. #ifdef _WIN32
  326. ASSERT(close(handle->fd) == 0);
  327. #endif /* _WIN32 */
  328. ASSERT(unlink(handle->path) == 0);
  329. ASSERT(strcmp(err, "") == 0);
  330. saved_srv = handle->srv;
  331. sg__httpupld_free_cb(handle);
  332. sg_httpsrv_free(saved_srv);
  333. }
  334. static void test__httpupld_save_cb(void) {
  335. const ssize_t len = 3;
  336. char str[4];
  337. struct sg__httpupld *handle = sg_alloc(sizeof(struct sg__httpupld));
  338. handle->path = TEST_HTTPUPLDS_BASE_PATH "foo.txt";
  339. handle->dest = TEST_HTTPUPLDS_BASE_PATH "bar.txt";
  340. unlink(handle->dest);
  341. unlink(handle->path);
  342. ASSERT(sg__httpupld_save_cb(NULL, false) == EINVAL);
  343. handle->fd =
  344. open(handle->path, TEST_HTTPUPLDS_OPEN_WFLAGS, TEST_HTTPUPLDS_OPEN_MODE);
  345. ASSERT(handle->fd > -1);
  346. ASSERT(write(handle->fd, "foo", len) == len);
  347. ASSERT(access(handle->dest, F_OK) == -1);
  348. ASSERT(sg__httpupld_save_cb(handle, true) == 0);
  349. ASSERT(access(handle->dest, F_OK) == 0);
  350. handle->fd = open(handle->dest, O_RDONLY);
  351. ASSERT(handle->fd > -1);
  352. memset(str, 0, sizeof(str));
  353. ASSERT(read(handle->fd, str, len) == len);
  354. ASSERT(close(handle->fd) == 0);
  355. ASSERT(strcmp(str, "foo") == 0);
  356. sg_free(handle);
  357. }
  358. static void test__httpupld_save_as_cb(void) {
  359. const ssize_t len = 3;
  360. const char *bar_path = TEST_HTTPUPLDS_BASE_PATH "bar.txt";
  361. char str[4];
  362. char *dir;
  363. struct sg__httpupld *handle = sg_alloc(sizeof(struct sg__httpupld));
  364. handle->path = TEST_HTTPUPLDS_BASE_PATH "foo.txt";
  365. handle->fd = -1;
  366. unlink(bar_path);
  367. unlink(handle->path);
  368. ASSERT(sg__httpupld_save_as_cb(NULL, "foo", false) == EINVAL);
  369. ASSERT(sg__httpupld_save_as_cb(handle, "foo", false) == EINVAL);
  370. handle->fd =
  371. open(handle->path, TEST_HTTPUPLDS_OPEN_WFLAGS, TEST_HTTPUPLDS_OPEN_MODE);
  372. ASSERT(handle->fd > -1);
  373. ASSERT(sg__httpupld_save_as_cb(handle, NULL, false) == EINVAL);
  374. close(handle->fd);
  375. handle->fd =
  376. open(bar_path, TEST_HTTPUPLDS_OPEN_WFLAGS, TEST_HTTPUPLDS_OPEN_MODE);
  377. ASSERT(handle->fd > -1);
  378. ASSERT(write(handle->fd, "bar", len) == len);
  379. ASSERT(close(handle->fd) == 0);
  380. ASSERT(access(bar_path, F_OK) == 0);
  381. handle->fd = open(bar_path, O_RDONLY);
  382. ASSERT(handle->fd > -1);
  383. memset(str, 0, sizeof(str));
  384. ASSERT(read(handle->fd, str, len) == len);
  385. ASSERT(close(handle->fd) == 0);
  386. ASSERT(strcmp(str, "bar") == 0);
  387. handle->fd =
  388. open(handle->path, TEST_HTTPUPLDS_OPEN_WFLAGS, TEST_HTTPUPLDS_OPEN_MODE);
  389. ASSERT(handle->fd > -1);
  390. ASSERT(write(handle->fd, "foo", len) == len);
  391. ASSERT(sg__httpupld_save_as_cb(handle, bar_path, false) == EEXIST);
  392. ASSERT(access(bar_path, F_OK) == 0);
  393. handle->fd = open(bar_path, O_RDONLY);
  394. ASSERT(handle->fd > -1);
  395. memset(str, 0, sizeof(str));
  396. ASSERT(read(handle->fd, str, len) == len);
  397. ASSERT(close(handle->fd) == 0);
  398. ASSERT(strcmp(str, "bar") == 0);
  399. dir = sg_tmpdir();
  400. ASSERT(dir);
  401. ASSERT(access(dir, F_OK) == 0);
  402. handle->fd =
  403. open(handle->path, TEST_HTTPUPLDS_OPEN_WFLAGS, TEST_HTTPUPLDS_OPEN_MODE);
  404. ASSERT(handle->fd > -1);
  405. ASSERT(write(handle->fd, "foo", len) == len);
  406. ASSERT(sg__httpupld_save_as_cb(handle, dir, false) == EISDIR);
  407. sg_free(dir);
  408. handle->fd =
  409. open(handle->path, TEST_HTTPUPLDS_OPEN_WFLAGS, TEST_HTTPUPLDS_OPEN_MODE);
  410. ASSERT(handle->fd > -1);
  411. ASSERT(write(handle->fd, "foo", len) == len);
  412. ASSERT(sg__httpupld_save_as_cb(handle, "", false) == ENOENT);
  413. handle->fd =
  414. open(handle->path, TEST_HTTPUPLDS_OPEN_WFLAGS, TEST_HTTPUPLDS_OPEN_MODE);
  415. ASSERT(handle->fd > -1);
  416. ASSERT(write(handle->fd, "foo", len) == len);
  417. ASSERT(sg__httpupld_save_as_cb(handle, bar_path, true) == 0);
  418. ASSERT(access(bar_path, F_OK) == 0);
  419. handle->fd = open(bar_path, O_RDONLY);
  420. ASSERT(handle->fd > -1);
  421. memset(str, 0, sizeof(str));
  422. ASSERT(read(handle->fd, str, len) == len);
  423. ASSERT(close(handle->fd) == 0);
  424. ASSERT(strcmp(str, "foo") == 0);
  425. unlink(bar_path);
  426. ASSERT(access(bar_path, F_OK) == -1);
  427. handle->fd =
  428. open(handle->path, TEST_HTTPUPLDS_OPEN_WFLAGS, TEST_HTTPUPLDS_OPEN_MODE);
  429. ASSERT(handle->fd > -1);
  430. ASSERT(write(handle->fd, "foo", len) == len);
  431. ASSERT(sg__httpupld_save_as_cb(handle, bar_path, false) == 0);
  432. ASSERT(access(bar_path, F_OK) == 0);
  433. handle->fd = open(bar_path, O_RDONLY);
  434. ASSERT(handle->fd > -1);
  435. memset(str, 0, sizeof(str));
  436. ASSERT(read(handle->fd, str, len) == len);
  437. ASSERT(close(handle->fd) == 0);
  438. ASSERT(strcmp(str, "foo") == 0);
  439. sg_free(handle);
  440. unlink(bar_path);
  441. }
  442. static void test_httpuplds_iter(void) {
  443. struct sg_httpupld *tmp, *upld, *uplds = NULL;
  444. char str[100];
  445. ASSERT(sg_httpuplds_iter(uplds, NULL, str) == EINVAL);
  446. ASSERT(sg_httpuplds_iter(NULL, dummy_httpuplds_iter_cb, str) == 0);
  447. upld = sg_alloc(sizeof(struct sg_httpupld));
  448. upld->name = "foo";
  449. LL_APPEND(uplds, upld);
  450. upld = sg_alloc(sizeof(struct sg_httpupld));
  451. upld->name = "bar";
  452. LL_APPEND(uplds, upld);
  453. memset(str, 0, sizeof(str));
  454. ASSERT(sg_httpuplds_iter(uplds, dummy_httpuplds_iter_cb, str) == 0);
  455. ASSERT(strcmp(str, "foobar") == 0);
  456. upld = sg_alloc(sizeof(struct sg_httpupld));
  457. upld->name = "xxx";
  458. LL_APPEND(uplds, upld);
  459. ASSERT(sg_httpuplds_iter(uplds, dummy_httpuplds_iter_cb, str) == 123);
  460. LL_FOREACH_SAFE(uplds, upld, tmp) {
  461. LL_DELETE(uplds, upld);
  462. sg_free(upld);
  463. }
  464. }
  465. static void test_httpuplds_next(void) {
  466. struct sg_httpupld *tmp, *upld, *uplds = NULL;
  467. ASSERT(sg_httpuplds_next(NULL) == EINVAL);
  468. upld = sg_alloc(sizeof(struct sg_httpupld));
  469. upld->name = "abc123";
  470. LL_APPEND(uplds, upld);
  471. upld = sg_alloc(sizeof(struct sg_httpupld));
  472. upld->name = "def456";
  473. LL_APPEND(uplds, upld);
  474. upld = sg_alloc(sizeof(struct sg_httpupld));
  475. upld->name = "ghi789";
  476. LL_APPEND(uplds, upld);
  477. upld = uplds;
  478. ASSERT(strcmp(sg_httpupld_name(upld), "abc123") == 0);
  479. ASSERT(sg_httpuplds_next(&upld) == 0);
  480. ASSERT(strcmp(sg_httpupld_name(upld), "def456") == 0);
  481. ASSERT(sg_httpuplds_next(&upld) == 0);
  482. ASSERT(strcmp(sg_httpupld_name(upld), "ghi789") == 0);
  483. LL_FOREACH_SAFE(uplds, upld, tmp) {
  484. LL_DELETE(uplds, upld);
  485. sg_free(upld);
  486. }
  487. }
  488. static void test_httpuplds_count(void) {
  489. struct sg_httpupld *tmp, *upld, *uplds = NULL;
  490. errno = 0;
  491. ASSERT(sg_httpuplds_count(NULL) == 0);
  492. ASSERT(errno == 0);
  493. upld = sg_alloc(sizeof(struct sg_httpupld));
  494. LL_APPEND(uplds, upld);
  495. ASSERT(sg_httpuplds_count(uplds) == 1);
  496. upld = sg_alloc(sizeof(struct sg_httpupld));
  497. LL_APPEND(uplds, upld);
  498. ASSERT(sg_httpuplds_count(uplds) == 2);
  499. LL_FOREACH_SAFE(uplds, upld, tmp) {
  500. LL_DELETE(uplds, upld);
  501. sg_free(upld);
  502. }
  503. }
  504. static void test_httpupld_handle(struct sg_httpupld *upld) {
  505. errno = 0;
  506. ASSERT(sg_httpupld_handle(NULL) == 0);
  507. ASSERT(errno == EINVAL);
  508. upld->handle = NULL;
  509. errno = 0;
  510. ASSERT(!sg_httpupld_handle(upld));
  511. ASSERT(errno == 0);
  512. upld->handle = upld;
  513. ASSERT(sg_httpupld_handle(upld) == upld);
  514. }
  515. static void test_httpupld_dir(struct sg_httpupld *upld) {
  516. errno = 0;
  517. ASSERT(sg_httpupld_dir(NULL) == 0);
  518. ASSERT(errno == EINVAL);
  519. upld->dir = NULL;
  520. errno = 0;
  521. ASSERT(!sg_httpupld_dir(upld));
  522. ASSERT(errno == 0);
  523. upld->dir = "K7eFIFhFJmwSI601";
  524. ASSERT(strcmp(sg_httpupld_dir(upld), "K7eFIFhFJmwSI601") == 0);
  525. }
  526. static void test_httpupld_field(struct sg_httpupld *upld) {
  527. errno = 0;
  528. ASSERT(sg_httpupld_field(NULL) == 0);
  529. ASSERT(errno == EINVAL);
  530. upld->field = NULL;
  531. errno = 0;
  532. ASSERT(!sg_httpupld_field(upld));
  533. ASSERT(errno == 0);
  534. upld->field = "ZgJwfUrXVAHSV4pb";
  535. ASSERT(strcmp(sg_httpupld_field(upld), "ZgJwfUrXVAHSV4pb") == 0);
  536. }
  537. static void test_httpupld_name(struct sg_httpupld *upld) {
  538. errno = 0;
  539. ASSERT(sg_httpupld_name(NULL) == 0);
  540. ASSERT(errno == EINVAL);
  541. upld->name = NULL;
  542. errno = 0;
  543. ASSERT(!sg_httpupld_name(upld));
  544. ASSERT(errno == 0);
  545. upld->name = "04wSe5er0FCCI1JG";
  546. ASSERT(strcmp(sg_httpupld_name(upld), "04wSe5er0FCCI1JG") == 0);
  547. }
  548. static void test_httpupld_mime(struct sg_httpupld *upld) {
  549. errno = 0;
  550. ASSERT(sg_httpupld_mime(NULL) == 0);
  551. ASSERT(errno == EINVAL);
  552. upld->mime = NULL;
  553. errno = 0;
  554. ASSERT(!sg_httpupld_mime(upld));
  555. ASSERT(errno == 0);
  556. upld->mime = "diOx11XHmluaYegV";
  557. ASSERT(strcmp(sg_httpupld_mime(upld), "diOx11XHmluaYegV") == 0);
  558. }
  559. static void test_httpupld_encoding(struct sg_httpupld *upld) {
  560. errno = 0;
  561. ASSERT(sg_httpupld_encoding(NULL) == 0);
  562. ASSERT(errno == EINVAL);
  563. upld->encoding = NULL;
  564. errno = 0;
  565. ASSERT(!sg_httpupld_encoding(upld));
  566. ASSERT(errno == 0);
  567. upld->encoding = "uwJtxi8hSMIDMdO7";
  568. ASSERT(strcmp(sg_httpupld_encoding(upld), "uwJtxi8hSMIDMdO7") == 0);
  569. }
  570. static void test_httpupld_size(struct sg_httpupld *upld) {
  571. errno = 0;
  572. ASSERT(sg_httpupld_size(NULL) == 0);
  573. ASSERT(errno == EINVAL);
  574. upld->size = 0;
  575. errno = 0;
  576. ASSERT(sg_httpupld_size(upld) == 0);
  577. ASSERT(errno == 0);
  578. upld->size = 123;
  579. ASSERT(sg_httpupld_size(upld) == 123);
  580. }
  581. static void test_httpupld_save(struct sg_httpupld *upld) {
  582. ASSERT(sg_httpupld_save(NULL, false) == EINVAL);
  583. upld->save_cb = dummy_httpuplds_save_cb;
  584. ASSERT(sg_httpupld_save(upld, true) == 123);
  585. }
  586. static void test_httpupld_save_as(struct sg_httpupld *upld) {
  587. ASSERT(sg_httpupld_save_as(NULL, "", false) == EINVAL);
  588. ASSERT(sg_httpupld_save_as(upld, NULL, false) == EINVAL);
  589. upld->save_as_cb = dummy_httpuplds_save_as_cb;
  590. ASSERT(sg_httpupld_save_as(upld, "abc", true) == 123);
  591. }
  592. int main(void) {
  593. struct sg_httpupld *upld = sg_alloc(sizeof(struct sg_httpupld));
  594. test__httpuplds_add();
  595. test__httpuplds_free();
  596. test__httpuplds_iter();
  597. test__httpuplds_process();
  598. test__httpuplds_cleanup();
  599. test__httpupld_cb();
  600. test__httpupld_write_cb();
  601. test__httpupld_free_cb();
  602. test__httpupld_save_cb();
  603. test__httpupld_save_as_cb();
  604. test_httpuplds_iter();
  605. test_httpuplds_next();
  606. test_httpuplds_count();
  607. test_httpupld_handle(upld);
  608. test_httpupld_dir(upld);
  609. test_httpupld_field(upld);
  610. test_httpupld_name(upld);
  611. test_httpupld_mime(upld);
  612. test_httpupld_encoding(upld);
  613. test_httpupld_size(upld);
  614. test_httpupld_save(upld);
  615. test_httpupld_save_as(upld);
  616. sg_free(upld);
  617. return EXIT_SUCCESS;
  618. }