quickjs-tart

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

unit1620.c (4541B)


      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 "unitcheck.h"
     25 
     26 #include "urldata.h"
     27 #include "url.h"
     28 
     29 #include "memdebug.h" /* LAST include file */
     30 
     31 static CURLcode t1620_setup(void)
     32 {
     33   CURLcode res = CURLE_OK;
     34   global_init(CURL_GLOBAL_ALL);
     35   return res;
     36 }
     37 
     38 static void t1620_parse(
     39   const char *input,
     40   const char *exp_username,
     41   const char *exp_password,
     42   const char *exp_options)
     43 {
     44   char *userstr = NULL;
     45   char *passwdstr = NULL;
     46   char *options = NULL;
     47   CURLcode rc = Curl_parse_login_details(input, strlen(input),
     48                                 &userstr, &passwdstr, &options);
     49   fail_unless(rc == CURLE_OK, "Curl_parse_login_details() failed");
     50 
     51   fail_unless(!!exp_username == !!userstr, "username expectation failed");
     52   fail_unless(!!exp_password == !!passwdstr, "password expectation failed");
     53   fail_unless(!!exp_options == !!options, "options expectation failed");
     54 
     55   if(!unitfail) {
     56     fail_unless(!userstr || !exp_username ||
     57                 strcmp(userstr, exp_username) == 0,
     58                 "userstr should be equal to exp_username");
     59     fail_unless(!passwdstr || !exp_password ||
     60                 strcmp(passwdstr, exp_password) == 0,
     61                 "passwdstr should be equal to exp_password");
     62     fail_unless(!options || !exp_options ||
     63                 strcmp(options, exp_options) == 0,
     64                 "options should be equal to exp_options");
     65   }
     66 
     67   free(userstr);
     68   free(passwdstr);
     69   free(options);
     70 }
     71 
     72 static CURLcode test_unit1620(char *arg)
     73 {
     74   UNITTEST_BEGIN(t1620_setup())
     75 
     76   CURLcode rc;
     77   struct Curl_easy *empty;
     78   enum dupstring i;
     79 
     80   bool async = FALSE;
     81   bool protocol_connect = FALSE;
     82 
     83   rc = Curl_open(&empty);
     84   if(rc)
     85     goto unit_test_abort;
     86   fail_unless(rc == CURLE_OK, "Curl_open() failed");
     87 
     88   rc = Curl_connect(empty, &async, &protocol_connect);
     89   fail_unless(rc == CURLE_URL_MALFORMAT,
     90               "Curl_connect() failed to return CURLE_URL_MALFORMAT");
     91 
     92   fail_unless(empty->magic == CURLEASY_MAGIC_NUMBER,
     93               "empty->magic should be equal to CURLEASY_MAGIC_NUMBER");
     94 
     95   /* double invoke to ensure no dependency on internal state */
     96   rc = Curl_connect(empty, &async, &protocol_connect);
     97   fail_unless(rc == CURLE_URL_MALFORMAT,
     98               "Curl_connect() failed to return CURLE_URL_MALFORMAT");
     99 
    100   rc = Curl_init_userdefined(empty);
    101   fail_unless(rc == CURLE_OK, "Curl_userdefined() failed");
    102 
    103   rc = Curl_init_do(empty, empty->conn);
    104   fail_unless(rc == CURLE_OK, "Curl_init_do() failed");
    105 
    106   t1620_parse("hostname", "hostname", NULL, NULL);
    107   t1620_parse("user:password", "user", "password", NULL);
    108   t1620_parse("user:password;options", "user", "password", "options");
    109   t1620_parse("user:password;options;more", "user", "password",
    110               "options;more");
    111   t1620_parse("", "", NULL, NULL);
    112   t1620_parse(":", "", "", NULL);
    113   t1620_parse(":;", "", "", NULL);
    114   t1620_parse(":password", "", "password", NULL);
    115   t1620_parse(":password;", "", "password", NULL);
    116   t1620_parse(";options", "", NULL, "options");
    117   t1620_parse("user;options", "user", NULL, "options");
    118   t1620_parse("user:;options", "user", "", "options");
    119   t1620_parse("user;options:password", "user", "password", "options");
    120   t1620_parse("user;options:", "user", "", "options");
    121 
    122   Curl_freeset(empty);
    123   for(i = (enum dupstring)0; i < STRING_LAST; i++) {
    124     fail_unless(empty->set.str[i] == NULL,
    125                 "Curl_free() did not set to NULL");
    126   }
    127 
    128   rc = Curl_close(&empty);
    129   fail_unless(rc == CURLE_OK, "Curl_close() failed");
    130 
    131   UNITTEST_END(curl_global_cleanup())
    132 }