lib655.c 3.1 KB

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