quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

lib655.c (3281B)


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