quickjs-tart

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

lib2023.c (4413B)


      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 /* argv1 = URL
     25  * argv2 = main auth type
     26  * argv3 = second auth type
     27  */
     28 
     29 #include "first.h"
     30 
     31 #include "memdebug.h"
     32 
     33 static CURLcode send_request(CURL *curl, const char *url, int seq,
     34                              long auth_scheme, const char *userpwd)
     35 {
     36   CURLcode res;
     37   size_t len = strlen(url) + 4 + 1;
     38   char *full_url = malloc(len);
     39   if(!full_url) {
     40     curl_mfprintf(stderr, "Not enough memory for full url\n");
     41     return CURLE_OUT_OF_MEMORY;
     42   }
     43 
     44   curl_msnprintf(full_url, len, "%s%04d", url, seq);
     45   curl_mfprintf(stderr, "Sending new request %d to %s with credential %s "
     46                 "(auth %ld)\n", seq, full_url, userpwd, auth_scheme);
     47   test_setopt(curl, CURLOPT_URL, full_url);
     48   test_setopt(curl, CURLOPT_VERBOSE, 1L);
     49   test_setopt(curl, CURLOPT_HEADER, 1L);
     50   test_setopt(curl, CURLOPT_HTTPGET, 1L);
     51   test_setopt(curl, CURLOPT_USERPWD, userpwd);
     52   test_setopt(curl, CURLOPT_HTTPAUTH, auth_scheme);
     53 
     54   res = curl_easy_perform(curl);
     55 
     56 test_cleanup:
     57   free(full_url);
     58   return res;
     59 }
     60 
     61 static CURLcode send_wrong_password(CURL *curl, const char *url, int seq,
     62                                     long auth_scheme)
     63 {
     64   return send_request(curl, url, seq, auth_scheme, "testuser:wrongpass");
     65 }
     66 
     67 static CURLcode send_right_password(CURL *curl, const char *url, int seq,
     68                                     long auth_scheme)
     69 {
     70   return send_request(curl, url, seq, auth_scheme, "testuser:testpass");
     71 }
     72 
     73 static long parse_auth_name(const char *arg)
     74 {
     75   if(!arg)
     76     return CURLAUTH_NONE;
     77   if(curl_strequal(arg, "basic"))
     78     return CURLAUTH_BASIC;
     79   if(curl_strequal(arg, "digest"))
     80     return CURLAUTH_DIGEST;
     81   if(curl_strequal(arg, "ntlm"))
     82     return CURLAUTH_NTLM;
     83   return CURLAUTH_NONE;
     84 }
     85 
     86 static CURLcode test_lib2023(char *URL)  /* libauthretry */
     87 {
     88   CURLcode res;
     89   CURL *curl = NULL;
     90 
     91   long main_auth_scheme = parse_auth_name(libtest_arg2);
     92   long fallback_auth_scheme = parse_auth_name(libtest_arg3);
     93 
     94   if(main_auth_scheme == CURLAUTH_NONE ||
     95       fallback_auth_scheme == CURLAUTH_NONE) {
     96     curl_mfprintf(stderr, "auth schemes not found on commandline\n");
     97     return TEST_ERR_MAJOR_BAD;
     98   }
     99 
    100   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
    101     curl_mfprintf(stderr, "curl_global_init() failed\n");
    102     return TEST_ERR_MAJOR_BAD;
    103   }
    104 
    105   /* Send wrong password, then right password */
    106 
    107   curl = curl_easy_init();
    108   if(!curl) {
    109     curl_mfprintf(stderr, "curl_easy_init() failed\n");
    110     curl_global_cleanup();
    111     return TEST_ERR_MAJOR_BAD;
    112   }
    113 
    114   res = send_wrong_password(curl, URL, 100, main_auth_scheme);
    115   if(res != CURLE_OK)
    116     goto test_cleanup;
    117 
    118   res = send_right_password(curl, URL, 200, fallback_auth_scheme);
    119   if(res != CURLE_OK)
    120     goto test_cleanup;
    121 
    122   curl_easy_cleanup(curl);
    123 
    124   /* Send wrong password twice, then right password */
    125   curl = curl_easy_init();
    126   if(!curl) {
    127     curl_mfprintf(stderr, "curl_easy_init() failed\n");
    128     curl_global_cleanup();
    129     return TEST_ERR_MAJOR_BAD;
    130   }
    131 
    132   res = send_wrong_password(curl, URL, 300, main_auth_scheme);
    133   if(res != CURLE_OK)
    134     goto test_cleanup;
    135 
    136   res = send_wrong_password(curl, URL, 400, fallback_auth_scheme);
    137   if(res != CURLE_OK)
    138     goto test_cleanup;
    139 
    140   res = send_right_password(curl, URL, 500, fallback_auth_scheme);
    141   if(res != CURLE_OK)
    142     goto test_cleanup;
    143 
    144 test_cleanup:
    145 
    146   curl_easy_cleanup(curl);
    147   curl_global_cleanup();
    148 
    149   return res;
    150 }