lib1156.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "test.h"
  25. /*
  26. Check range/resume returned error codes and data presence.
  27. The input parameters are:
  28. - CURLOPT_RANGE/CURLOPT_RESUME_FROM
  29. - CURLOPT_FAILONERROR
  30. - Returned http code (2xx/416)
  31. - Content-Range header present in reply.
  32. */
  33. #include "memdebug.h"
  34. #define F_RESUME (1 << 0) /* resume/range. */
  35. #define F_HTTP416 (1 << 1) /* Server returns http code 416. */
  36. #define F_FAIL (1 << 2) /* Fail on error. */
  37. #define F_CONTENTRANGE (1 << 3) /* Server sends content-range hdr. */
  38. #define F_IGNOREBODY (1 << 4) /* Body should be ignored. */
  39. struct testparams {
  40. unsigned int flags; /* ORed flags as above. */
  41. CURLcode result; /* Code that should be returned by curl_easy_perform(). */
  42. };
  43. static const struct testparams params[] = {
  44. { 0, CURLE_OK },
  45. { F_CONTENTRANGE, CURLE_OK },
  46. { F_FAIL, CURLE_OK },
  47. { F_FAIL | F_CONTENTRANGE, CURLE_OK },
  48. { F_HTTP416, CURLE_OK },
  49. { F_HTTP416 | F_CONTENTRANGE, CURLE_OK },
  50. { F_HTTP416 | F_FAIL | F_IGNOREBODY,
  51. CURLE_HTTP_RETURNED_ERROR },
  52. { F_HTTP416 | F_FAIL | F_CONTENTRANGE | F_IGNOREBODY,
  53. CURLE_HTTP_RETURNED_ERROR },
  54. { F_RESUME | F_IGNOREBODY,
  55. CURLE_RANGE_ERROR },
  56. { F_RESUME | F_CONTENTRANGE, CURLE_OK },
  57. { F_RESUME | F_FAIL | F_IGNOREBODY,
  58. CURLE_RANGE_ERROR },
  59. { F_RESUME | F_FAIL | F_CONTENTRANGE, CURLE_OK },
  60. { F_RESUME | F_HTTP416 | F_IGNOREBODY, CURLE_OK },
  61. { F_RESUME | F_HTTP416 | F_CONTENTRANGE | F_IGNOREBODY, CURLE_OK },
  62. { F_RESUME | F_HTTP416 | F_FAIL | F_IGNOREBODY, CURLE_OK },
  63. { F_RESUME | F_HTTP416 | F_FAIL | F_CONTENTRANGE | F_IGNOREBODY,
  64. CURLE_OK }
  65. };
  66. static int hasbody;
  67. static size_t writedata(char *data, size_t size, size_t nmemb, void *userdata)
  68. {
  69. (void) data;
  70. (void) userdata;
  71. if(size && nmemb)
  72. hasbody = 1;
  73. return size * nmemb;
  74. }
  75. static int onetest(CURL *curl, const char *url, const struct testparams *p,
  76. size_t num)
  77. {
  78. CURLcode res;
  79. unsigned int replyselector;
  80. char urlbuf[256];
  81. replyselector = (p->flags & F_CONTENTRANGE)? 1: 0;
  82. if(p->flags & F_HTTP416)
  83. replyselector += 2;
  84. msnprintf(urlbuf, sizeof(urlbuf), "%s%04u", url, replyselector);
  85. test_setopt(curl, CURLOPT_URL, urlbuf);
  86. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  87. test_setopt(curl, CURLOPT_RESUME_FROM, (p->flags & F_RESUME)? 3: 0);
  88. test_setopt(curl, CURLOPT_RANGE, !(p->flags & F_RESUME)?
  89. "3-1000000": (char *) NULL);
  90. test_setopt(curl, CURLOPT_FAILONERROR, (p->flags & F_FAIL)? 1: 0);
  91. hasbody = 0;
  92. res = curl_easy_perform(curl);
  93. if(res != p->result) {
  94. printf("%zd: bad error code (%d): resume=%s, fail=%s, http416=%s, "
  95. "content-range=%s, expected=%d\n", num, res,
  96. (p->flags & F_RESUME)? "yes": "no",
  97. (p->flags & F_FAIL)? "yes": "no",
  98. (p->flags & F_HTTP416)? "yes": "no",
  99. (p->flags & F_CONTENTRANGE)? "yes": "no",
  100. p->result);
  101. return 1;
  102. }
  103. if(hasbody && (p->flags & F_IGNOREBODY)) {
  104. printf("body should be ignored and is not: resume=%s, fail=%s, "
  105. "http416=%s, content-range=%s\n",
  106. (p->flags & F_RESUME)? "yes": "no",
  107. (p->flags & F_FAIL)? "yes": "no",
  108. (p->flags & F_HTTP416)? "yes": "no",
  109. (p->flags & F_CONTENTRANGE)? "yes": "no");
  110. return 1;
  111. }
  112. return 0;
  113. test_cleanup:
  114. return 1;
  115. }
  116. /* for debugging: */
  117. /* #define SINGLETEST 9 */
  118. int test(char *URL)
  119. {
  120. CURLcode res;
  121. CURL *curl;
  122. size_t i;
  123. int status = 0;
  124. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  125. fprintf(stderr, "curl_global_init() failed\n");
  126. return TEST_ERR_MAJOR_BAD;
  127. }
  128. for(i = 0; i < sizeof(params) / sizeof(params[0]); i++) {
  129. curl = curl_easy_init();
  130. if(!curl) {
  131. fprintf(stderr, "curl_easy_init() failed\n");
  132. curl_global_cleanup();
  133. return TEST_ERR_MAJOR_BAD;
  134. }
  135. test_setopt(curl, CURLOPT_WRITEFUNCTION, writedata);
  136. #ifdef SINGLETEST
  137. if(SINGLETEST == i)
  138. #endif
  139. status |= onetest(curl, URL, params + i, i);
  140. curl_easy_cleanup(curl);
  141. }
  142. curl_global_cleanup();
  143. printf("%d\n", status);
  144. return status;
  145. test_cleanup:
  146. curl_easy_cleanup(curl);
  147. curl_global_cleanup();
  148. return (int)res;
  149. }