lib578.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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. #include "memdebug.h"
  24. /* The size of data should be kept below MAX_INITIAL_POST_SIZE! */
  25. static char data[]="this is a short string.\n";
  26. static size_t data_size = sizeof(data) / sizeof(char);
  27. static int progress_callback(void *clientp, double dltotal, double dlnow,
  28. double ultotal, double ulnow)
  29. {
  30. FILE *moo = fopen(libtest_arg2, "wb");
  31. (void)clientp; /* UNUSED */
  32. (void)dltotal; /* UNUSED */
  33. (void)dlnow; /* UNUSED */
  34. if(moo) {
  35. if((size_t)ultotal == data_size && (size_t)ulnow == data_size)
  36. fprintf(moo, "PASSED, UL data matched data size\n");
  37. else
  38. fprintf(moo, "Progress callback called with UL %f out of %f\n",
  39. ulnow, ultotal);
  40. fclose(moo);
  41. }
  42. return 0;
  43. }
  44. int test(char *URL)
  45. {
  46. CURL *curl;
  47. CURLcode res=CURLE_OK;
  48. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  49. fprintf(stderr, "curl_global_init() failed\n");
  50. return TEST_ERR_MAJOR_BAD;
  51. }
  52. curl = curl_easy_init();
  53. if(!curl) {
  54. fprintf(stderr, "curl_easy_init() failed\n");
  55. curl_global_cleanup();
  56. return TEST_ERR_MAJOR_BAD;
  57. }
  58. /* First set the URL that is about to receive our POST. */
  59. test_setopt(curl, CURLOPT_URL, URL);
  60. /* Now specify we want to POST data */
  61. test_setopt(curl, CURLOPT_POST, 1L);
  62. #ifdef CURL_DOES_CONVERSIONS
  63. /* Convert the POST data to ASCII */
  64. test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
  65. #endif
  66. /* Set the expected POST size */
  67. test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)data_size);
  68. test_setopt(curl, CURLOPT_POSTFIELDS, data);
  69. /* we want to use our own progress function */
  70. test_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  71. test_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
  72. /* pointer to pass to our read function */
  73. /* get verbose debug output please */
  74. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  75. /* include headers in the output */
  76. test_setopt(curl, CURLOPT_HEADER, 1L);
  77. /* Perform the request, res will get the return code */
  78. res = curl_easy_perform(curl);
  79. test_cleanup:
  80. /* always cleanup */
  81. curl_easy_cleanup(curl);
  82. curl_global_cleanup();
  83. return res;
  84. }