lib1156.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.haxx.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. ***************************************************************************/
  22. #include "test.h"
  23. /*
  24. Check range/resume returned error codes and data presence.
  25. The input parameters are:
  26. - CURLOPT_RANGE/CURLOPT_RESUME_FROM
  27. - CURLOPT_FAILONERROR
  28. - Returned http code (2xx/416)
  29. - Content-Range header present in reply.
  30. */
  31. #include "memdebug.h"
  32. #define F_RESUME (1 << 0) /* resume/range. */
  33. #define F_HTTP416 (1 << 1) /* Server returns http code 416. */
  34. #define F_FAIL (1 << 2) /* Fail on error. */
  35. #define F_CONTENTRANGE (1 << 3) /* Server sends content-range hdr. */
  36. #define F_IGNOREBODY (1 << 4) /* Body should be ignored. */
  37. struct testparams {
  38. unsigned int flags; /* ORed flags as above. */
  39. CURLcode result; /* Code that should be returned by curl_easy_perform(). */
  40. };
  41. static const struct testparams params[] = {
  42. { 0, CURLE_OK },
  43. { F_CONTENTRANGE, CURLE_OK },
  44. { F_FAIL, CURLE_OK },
  45. { F_FAIL | F_CONTENTRANGE, CURLE_OK },
  46. { F_HTTP416, CURLE_OK },
  47. { F_HTTP416 | F_CONTENTRANGE, CURLE_OK },
  48. { F_HTTP416 | F_FAIL | F_IGNOREBODY,
  49. CURLE_HTTP_RETURNED_ERROR },
  50. { F_HTTP416 | F_FAIL | F_CONTENTRANGE | F_IGNOREBODY,
  51. CURLE_HTTP_RETURNED_ERROR },
  52. { F_RESUME | F_IGNOREBODY,
  53. CURLE_RANGE_ERROR },
  54. { F_RESUME | F_CONTENTRANGE, CURLE_OK },
  55. { F_RESUME | F_FAIL | F_IGNOREBODY,
  56. CURLE_RANGE_ERROR },
  57. { F_RESUME | F_FAIL | F_CONTENTRANGE, CURLE_OK },
  58. { F_RESUME | F_HTTP416 | F_IGNOREBODY, CURLE_OK },
  59. { F_RESUME | F_HTTP416 | F_CONTENTRANGE | F_IGNOREBODY, CURLE_OK },
  60. { F_RESUME | F_HTTP416 | F_FAIL | F_IGNOREBODY,
  61. CURLE_HTTP_RETURNED_ERROR },
  62. { F_RESUME | F_HTTP416 | F_FAIL | F_CONTENTRANGE | F_IGNOREBODY,
  63. CURLE_HTTP_RETURNED_ERROR }
  64. };
  65. static int hasbody;
  66. static size_t writedata(char *data, size_t size, size_t nmemb, void *userdata)
  67. {
  68. (void) data;
  69. (void) userdata;
  70. if(size && nmemb)
  71. hasbody = 1;
  72. return size * nmemb;
  73. }
  74. static int onetest(CURL *curl, const char *url, const struct testparams *p)
  75. {
  76. CURLcode res;
  77. unsigned int replyselector;
  78. char urlbuf[256];
  79. replyselector = (p->flags & F_CONTENTRANGE)? 1: 0;
  80. if(p->flags & F_HTTP416)
  81. replyselector += 2;
  82. msnprintf(urlbuf, sizeof(urlbuf), "%s%04u", url, replyselector);
  83. test_setopt(curl, CURLOPT_URL, urlbuf);
  84. test_setopt(curl, CURLOPT_RESUME_FROM, (p->flags & F_RESUME)? 3: 0);
  85. test_setopt(curl, CURLOPT_RANGE, !(p->flags & F_RESUME)?
  86. "3-1000000": (char *) NULL);
  87. test_setopt(curl, CURLOPT_FAILONERROR, (p->flags & F_FAIL)? 1: 0);
  88. hasbody = 0;
  89. res = curl_easy_perform(curl);
  90. if(res != p->result) {
  91. fprintf(stderr, "bad error code (%d): resume=%s, fail=%s, http416=%s, "
  92. "content-range=%s, expected=%d\n", res,
  93. (p->flags & F_RESUME)? "yes": "no",
  94. (p->flags & F_FAIL)? "yes": "no",
  95. (p->flags & F_HTTP416)? "yes": "no",
  96. (p->flags & F_CONTENTRANGE)? "yes": "no",
  97. p->result);
  98. return 1;
  99. }
  100. if(hasbody && (p->flags & F_IGNOREBODY)) {
  101. fprintf(stderr, "body should be ignored and is not: resume=%s, fail=%s, "
  102. "http416=%s, content-range=%s\n",
  103. (p->flags & F_RESUME)? "yes": "no",
  104. (p->flags & F_FAIL)? "yes": "no",
  105. (p->flags & F_HTTP416)? "yes": "no",
  106. (p->flags & F_CONTENTRANGE)? "yes": "no");
  107. return 1;
  108. }
  109. return 0;
  110. test_cleanup:
  111. return 1;
  112. }
  113. int test(char *URL)
  114. {
  115. CURLcode res;
  116. CURL *curl;
  117. size_t i;
  118. int status = 0;
  119. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  120. fprintf(stderr, "curl_global_init() failed\n");
  121. return TEST_ERR_MAJOR_BAD;
  122. }
  123. curl = curl_easy_init();
  124. if(!curl) {
  125. fprintf(stderr, "curl_easy_init() failed\n");
  126. curl_global_cleanup();
  127. return TEST_ERR_MAJOR_BAD;
  128. }
  129. test_setopt(curl, CURLOPT_WRITEFUNCTION, writedata);
  130. for(i = 0; i < sizeof(params) / sizeof(params[0]); i++)
  131. status |= onetest(curl, URL, params + i);
  132. curl_easy_cleanup(curl);
  133. curl_global_cleanup();
  134. return status;
  135. test_cleanup:
  136. curl_easy_cleanup(curl);
  137. curl_global_cleanup();
  138. return (int)res;
  139. }