quickjs-tart

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

unit2602.c (5640B)


      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 "dynhds.h"
     28 #include "curl_trc.h"
     29 
     30 static CURLcode test_unit2602(char *arg)
     31 {
     32   UNITTEST_BEGIN_SIMPLE
     33 
     34   struct dynhds hds;
     35   struct dynbuf dbuf;
     36   CURLcode result;
     37   size_t i;
     38 
     39   /* add 1 more header than allowed */
     40   Curl_dynhds_init(&hds, 2, 128);
     41   fail_if(Curl_dynhds_count(&hds), "should be empty");
     42   fail_if(Curl_dynhds_add(&hds, "test1", 5, "123", 3), "add failed");
     43   fail_if(Curl_dynhds_add(&hds, "test2", 5, "456", 3), "add failed");
     44   /* remove and add without exceeding limits */
     45   for(i = 0; i < 100; ++i) {
     46     if(Curl_dynhds_remove(&hds, "test2", 5) != 1) {
     47       fail_if(TRUE, "should");
     48       break;
     49     }
     50     if(Curl_dynhds_add(&hds, "test2", 5, "456", 3)) {
     51       fail_if(TRUE, "add failed");
     52       break;
     53     }
     54   }
     55   fail_unless(Curl_dynhds_count(&hds) == 2, "should hold 2");
     56   /* set, replacing previous entry without exceeding limits */
     57   for(i = 0; i < 100; ++i) {
     58     if(Curl_dynhds_set(&hds, "test2", 5, "456", 3)) {
     59       fail_if(TRUE, "add failed");
     60       break;
     61     }
     62   }
     63   fail_unless(Curl_dynhds_count(&hds) == 2, "should hold 2");
     64   /* exceed limit on # of entries */
     65   result = Curl_dynhds_add(&hds, "test3", 5, "789", 3);
     66   fail_unless(result, "add should have failed");
     67 
     68   fail_unless(Curl_dynhds_count_name(&hds, "test", 4) == 0, "false positive");
     69   fail_unless(Curl_dynhds_count_name(&hds, "test1", 4) == 0, "false positive");
     70   fail_if(Curl_dynhds_get(&hds, "test1", 4), "false positive");
     71   fail_unless(Curl_dynhds_get(&hds, "test1", 5), "false negative");
     72   fail_unless(Curl_dynhds_count_name(&hds, "test1", 5) == 1, "should");
     73   fail_unless(Curl_dynhds_ccount_name(&hds, "test2") == 1, "should");
     74   fail_unless(Curl_dynhds_cget(&hds, "test2"), "should");
     75   fail_unless(Curl_dynhds_ccount_name(&hds, "TEST2") == 1, "should");
     76   fail_unless(Curl_dynhds_ccontains(&hds, "TesT2"), "should");
     77   fail_unless(Curl_dynhds_contains(&hds, "TeSt2", 5), "should");
     78   Curl_dynhds_free(&hds);
     79 
     80   /* add header exceeding max overall length */
     81   Curl_dynhds_init(&hds, 128, 10);
     82   fail_if(Curl_dynhds_add(&hds, "test1", 5, "123", 3), "add failed");
     83   fail_unless(Curl_dynhds_add(&hds, "test2", 5, "456", 3), "should fail");
     84   fail_if(Curl_dynhds_add(&hds, "t", 1, "1", 1), "add failed");
     85   Curl_dynhds_reset(&hds);
     86   Curl_dynhds_free(&hds);
     87 
     88   Curl_dynhds_init(&hds, 128, 4*1024);
     89   fail_if(Curl_dynhds_add(&hds, "test1", 5, "123", 3), "add failed");
     90   fail_if(Curl_dynhds_add(&hds, "test1", 5, "123", 3), "add failed");
     91   fail_if(Curl_dynhds_cadd(&hds, "blablabla", "thingies"), "add failed");
     92   fail_if(Curl_dynhds_h1_cadd_line(&hds, "blablabla: thingies"), "add failed");
     93   fail_unless(Curl_dynhds_ccount_name(&hds, "blablabla") == 2, "should");
     94   fail_unless(Curl_dynhds_cremove(&hds, "blablabla") == 2, "should");
     95   fail_if(Curl_dynhds_ccontains(&hds, "blablabla"), "should not");
     96 
     97   result = Curl_dynhds_h1_cadd_line(&hds, "blablabla thingies");
     98   fail_unless(result, "add should have failed");
     99   if(!result) {
    100     fail_unless(Curl_dynhds_ccount_name(&hds, "bLABlaBlA") == 0, "should");
    101     fail_if(Curl_dynhds_cadd(&hds, "Bla-Bla", "thingies"), "add failed");
    102 
    103     curlx_dyn_init(&dbuf, 32*1024);
    104     fail_if(Curl_dynhds_h1_dprint(&hds, &dbuf), "h1 print failed");
    105     if(curlx_dyn_ptr(&dbuf)) {
    106       fail_if(strcmp(curlx_dyn_ptr(&dbuf),
    107                      "test1: 123\r\ntest1: 123\r\nBla-Bla: thingies\r\n"),
    108                      "h1 format differs");
    109     }
    110     curlx_dyn_free(&dbuf);
    111   }
    112 
    113   Curl_dynhds_free(&hds);
    114   Curl_dynhds_init(&hds, 128, 4*1024);
    115   /* continuation without previous header fails */
    116   result = Curl_dynhds_h1_cadd_line(&hds, " indented value");
    117   fail_unless(result, "add should have failed");
    118 
    119   /* continuation with previous header must succeed */
    120   fail_if(Curl_dynhds_h1_cadd_line(&hds, "ti1: val1"), "add");
    121   fail_if(Curl_dynhds_h1_cadd_line(&hds, " val2"), "add indent");
    122   fail_if(Curl_dynhds_h1_cadd_line(&hds, "ti2: val1"), "add");
    123   fail_if(Curl_dynhds_h1_cadd_line(&hds, "\tval2"), "add indent");
    124   fail_if(Curl_dynhds_h1_cadd_line(&hds, "ti3: val1"), "add");
    125   fail_if(Curl_dynhds_h1_cadd_line(&hds, "     val2"), "add indent");
    126 
    127   curlx_dyn_init(&dbuf, 32*1024);
    128   fail_if(Curl_dynhds_h1_dprint(&hds, &dbuf), "h1 print failed");
    129   if(curlx_dyn_ptr(&dbuf)) {
    130     curl_mfprintf(stderr, "indent concat: %s\n", curlx_dyn_ptr(&dbuf));
    131     fail_if(strcmp(curlx_dyn_ptr(&dbuf),
    132                    "ti1: val1 val2\r\nti2: val1 val2\r\nti3: val1 val2\r\n"),
    133                    "wrong format");
    134   }
    135   curlx_dyn_free(&dbuf);
    136 
    137   Curl_dynhds_free(&hds);
    138 
    139   UNITTEST_END_SIMPLE
    140 }