lib655.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.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. static const char *TEST_DATA_STRING = "Test data";
  25. static int cb_count = 0;
  26. static int
  27. resolver_alloc_cb_fail(void *resolver_state, void *reserved, void *userdata)
  28. {
  29. (void)resolver_state;
  30. (void)reserved;
  31. cb_count++;
  32. if(strcmp(userdata, TEST_DATA_STRING)) {
  33. fprintf(stderr, "Invalid test data received");
  34. exit(1);
  35. }
  36. return 1;
  37. }
  38. static int
  39. resolver_alloc_cb_pass(void *resolver_state, void *reserved, void *userdata)
  40. {
  41. (void)resolver_state;
  42. (void)reserved;
  43. cb_count++;
  44. if(strcmp(userdata, TEST_DATA_STRING)) {
  45. fprintf(stderr, "Invalid test data received");
  46. exit(1);
  47. }
  48. return 0;
  49. }
  50. int test(char *URL)
  51. {
  52. CURL *curl;
  53. CURLcode res = CURLE_OK;
  54. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  55. fprintf(stderr, "curl_global_init() failed\n");
  56. return TEST_ERR_MAJOR_BAD;
  57. }
  58. curl = curl_easy_init();
  59. if(!curl) {
  60. fprintf(stderr, "curl_easy_init() failed\n");
  61. res = TEST_ERR_MAJOR_BAD;
  62. goto test_cleanup;
  63. }
  64. /* First set the URL that is about to receive our request. */
  65. test_setopt(curl, CURLOPT_URL, URL);
  66. test_setopt(curl, CURLOPT_RESOLVER_START_DATA, TEST_DATA_STRING);
  67. test_setopt(curl, CURLOPT_RESOLVER_START_FUNCTION, resolver_alloc_cb_fail);
  68. /* this should fail */
  69. res = curl_easy_perform(curl);
  70. if(res != CURLE_COULDNT_RESOLVE_HOST) {
  71. fprintf(stderr, "curl_easy_perform should have returned "
  72. "CURLE_COULDNT_RESOLVE_HOST but instead returned error %d\n", res);
  73. if(res == CURLE_OK)
  74. res = TEST_ERR_FAILURE;
  75. goto test_cleanup;
  76. }
  77. test_setopt(curl, CURLOPT_RESOLVER_START_FUNCTION, resolver_alloc_cb_pass);
  78. /* this should succeed */
  79. res = curl_easy_perform(curl);
  80. if(res) {
  81. fprintf(stderr, "curl_easy_perform failed.\n");
  82. goto test_cleanup;
  83. }
  84. if(cb_count != 2) {
  85. fprintf(stderr, "Unexpected number of callbacks: %d\n", cb_count);
  86. res = TEST_ERR_FAILURE;
  87. goto test_cleanup;
  88. }
  89. test_cleanup:
  90. /* always cleanup */
  91. curl_easy_cleanup(curl);
  92. curl_global_cleanup();
  93. return (int)res;
  94. }