113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci#include "curlcheck.h"
2513498266Sopenharmony_ci
2613498266Sopenharmony_ci#include "urldata.h"
2713498266Sopenharmony_ci#include "dynbuf.h"
2813498266Sopenharmony_ci#include "dynhds.h"
2913498266Sopenharmony_ci#include "curl_trc.h"
3013498266Sopenharmony_ci
3113498266Sopenharmony_cistatic CURLcode unit_setup(void)
3213498266Sopenharmony_ci{
3313498266Sopenharmony_ci  return CURLE_OK;
3413498266Sopenharmony_ci}
3513498266Sopenharmony_ci
3613498266Sopenharmony_cistatic void unit_stop(void)
3713498266Sopenharmony_ci{
3813498266Sopenharmony_ci}
3913498266Sopenharmony_ci
4013498266Sopenharmony_ci
4113498266Sopenharmony_ciUNITTEST_START
4213498266Sopenharmony_ci
4313498266Sopenharmony_ci  struct dynhds hds;
4413498266Sopenharmony_ci  struct dynbuf dbuf;
4513498266Sopenharmony_ci  CURLcode result;
4613498266Sopenharmony_ci  size_t i;
4713498266Sopenharmony_ci
4813498266Sopenharmony_ci  /* add 1 more header than allowed */
4913498266Sopenharmony_ci  Curl_dynhds_init(&hds, 2, 128);
5013498266Sopenharmony_ci  fail_if(Curl_dynhds_count(&hds), "should be empty");
5113498266Sopenharmony_ci  fail_if(Curl_dynhds_add(&hds, "test1", 5, "123", 3), "add failed");
5213498266Sopenharmony_ci  fail_if(Curl_dynhds_add(&hds, "test2", 5, "456", 3), "add failed");
5313498266Sopenharmony_ci  /* remove and add without exceeding limits */
5413498266Sopenharmony_ci  for(i = 0; i < 100; ++i) {
5513498266Sopenharmony_ci    if(Curl_dynhds_remove(&hds, "test2", 5) != 1) {
5613498266Sopenharmony_ci      fail_if(TRUE, "should");
5713498266Sopenharmony_ci      break;
5813498266Sopenharmony_ci    }
5913498266Sopenharmony_ci    if(Curl_dynhds_add(&hds, "test2", 5, "456", 3)) {
6013498266Sopenharmony_ci      fail_if(TRUE, "add failed");
6113498266Sopenharmony_ci      break;
6213498266Sopenharmony_ci    }
6313498266Sopenharmony_ci  }
6413498266Sopenharmony_ci  fail_unless(Curl_dynhds_count(&hds) == 2, "should hold 2");
6513498266Sopenharmony_ci  /* set, replacing previous entry without exceeding limits */
6613498266Sopenharmony_ci  for(i = 0; i < 100; ++i) {
6713498266Sopenharmony_ci    if(Curl_dynhds_set(&hds, "test2", 5, "456", 3)) {
6813498266Sopenharmony_ci      fail_if(TRUE, "add failed");
6913498266Sopenharmony_ci      break;
7013498266Sopenharmony_ci    }
7113498266Sopenharmony_ci  }
7213498266Sopenharmony_ci  fail_unless(Curl_dynhds_count(&hds) == 2, "should hold 2");
7313498266Sopenharmony_ci  /* exceed limit on # of entries */
7413498266Sopenharmony_ci  result = Curl_dynhds_add(&hds, "test3", 5, "789", 3);
7513498266Sopenharmony_ci  fail_unless(result, "add should have failed");
7613498266Sopenharmony_ci
7713498266Sopenharmony_ci  fail_unless(Curl_dynhds_count_name(&hds, "test", 4) == 0, "false positive");
7813498266Sopenharmony_ci  fail_unless(Curl_dynhds_count_name(&hds, "test1", 4) == 0, "false positive");
7913498266Sopenharmony_ci  fail_if(Curl_dynhds_get(&hds, "test1", 4), "false positive");
8013498266Sopenharmony_ci  fail_unless(Curl_dynhds_get(&hds, "test1", 5), "false negative");
8113498266Sopenharmony_ci  fail_unless(Curl_dynhds_count_name(&hds, "test1", 5) == 1, "should");
8213498266Sopenharmony_ci  fail_unless(Curl_dynhds_ccount_name(&hds, "test2") == 1, "should");
8313498266Sopenharmony_ci  fail_unless(Curl_dynhds_cget(&hds, "test2"), "should");
8413498266Sopenharmony_ci  fail_unless(Curl_dynhds_ccount_name(&hds, "TEST2") == 1, "should");
8513498266Sopenharmony_ci  fail_unless(Curl_dynhds_ccontains(&hds, "TesT2"), "should");
8613498266Sopenharmony_ci  fail_unless(Curl_dynhds_contains(&hds, "TeSt2", 5), "should");
8713498266Sopenharmony_ci  Curl_dynhds_free(&hds);
8813498266Sopenharmony_ci
8913498266Sopenharmony_ci  /* add header exceeding max overall length */
9013498266Sopenharmony_ci  Curl_dynhds_init(&hds, 128, 10);
9113498266Sopenharmony_ci  fail_if(Curl_dynhds_add(&hds, "test1", 5, "123", 3), "add failed");
9213498266Sopenharmony_ci  fail_unless(Curl_dynhds_add(&hds, "test2", 5, "456", 3), "should fail");
9313498266Sopenharmony_ci  fail_if(Curl_dynhds_add(&hds, "t", 1, "1", 1), "add failed");
9413498266Sopenharmony_ci  Curl_dynhds_reset(&hds);
9513498266Sopenharmony_ci  Curl_dynhds_free(&hds);
9613498266Sopenharmony_ci
9713498266Sopenharmony_ci  Curl_dynhds_init(&hds, 128, 4*1024);
9813498266Sopenharmony_ci  fail_if(Curl_dynhds_add(&hds, "test1", 5, "123", 3), "add failed");
9913498266Sopenharmony_ci  fail_if(Curl_dynhds_add(&hds, "test1", 5, "123", 3), "add failed");
10013498266Sopenharmony_ci  fail_if(Curl_dynhds_cadd(&hds, "blablabla", "thingies"), "add failed");
10113498266Sopenharmony_ci  fail_if(Curl_dynhds_h1_cadd_line(&hds, "blablabla: thingies"), "add failed");
10213498266Sopenharmony_ci  fail_unless(Curl_dynhds_ccount_name(&hds, "blablabla") == 2, "should");
10313498266Sopenharmony_ci  fail_unless(Curl_dynhds_cremove(&hds, "blablabla") == 2, "should");
10413498266Sopenharmony_ci  fail_if(Curl_dynhds_ccontains(&hds, "blablabla"), "should not");
10513498266Sopenharmony_ci
10613498266Sopenharmony_ci  result = Curl_dynhds_h1_cadd_line(&hds, "blablabla thingies");
10713498266Sopenharmony_ci  fail_unless(result, "add should have failed");
10813498266Sopenharmony_ci  if(!result) {
10913498266Sopenharmony_ci    fail_unless(Curl_dynhds_ccount_name(&hds, "bLABlaBlA") == 0, "should");
11013498266Sopenharmony_ci    fail_if(Curl_dynhds_cadd(&hds, "Bla-Bla", "thingies"), "add failed");
11113498266Sopenharmony_ci
11213498266Sopenharmony_ci    Curl_dyn_init(&dbuf, 32*1024);
11313498266Sopenharmony_ci    fail_if(Curl_dynhds_h1_dprint(&hds, &dbuf), "h1 print failed");
11413498266Sopenharmony_ci    if(Curl_dyn_ptr(&dbuf)) {
11513498266Sopenharmony_ci      fail_if(strcmp(Curl_dyn_ptr(&dbuf),
11613498266Sopenharmony_ci                     "test1: 123\r\ntest1: 123\r\nBla-Bla: thingies\r\n"),
11713498266Sopenharmony_ci                     "h1 format differs");
11813498266Sopenharmony_ci    }
11913498266Sopenharmony_ci    Curl_dyn_free(&dbuf);
12013498266Sopenharmony_ci  }
12113498266Sopenharmony_ci
12213498266Sopenharmony_ci  Curl_dynhds_free(&hds);
12313498266Sopenharmony_ci  Curl_dynhds_init(&hds, 128, 4*1024);
12413498266Sopenharmony_ci  /* continuation without previous header fails */
12513498266Sopenharmony_ci  result = Curl_dynhds_h1_cadd_line(&hds, " indented value");
12613498266Sopenharmony_ci  fail_unless(result, "add should have failed");
12713498266Sopenharmony_ci
12813498266Sopenharmony_ci  /* continuation with previous header must succeed */
12913498266Sopenharmony_ci  fail_if(Curl_dynhds_h1_cadd_line(&hds, "ti1: val1"), "add");
13013498266Sopenharmony_ci  fail_if(Curl_dynhds_h1_cadd_line(&hds, " val2"), "add indent");
13113498266Sopenharmony_ci  fail_if(Curl_dynhds_h1_cadd_line(&hds, "ti2: val1"), "add");
13213498266Sopenharmony_ci  fail_if(Curl_dynhds_h1_cadd_line(&hds, "\tval2"), "add indent");
13313498266Sopenharmony_ci  fail_if(Curl_dynhds_h1_cadd_line(&hds, "ti3: val1"), "add");
13413498266Sopenharmony_ci  fail_if(Curl_dynhds_h1_cadd_line(&hds, "     val2"), "add indent");
13513498266Sopenharmony_ci
13613498266Sopenharmony_ci  Curl_dyn_init(&dbuf, 32*1024);
13713498266Sopenharmony_ci  fail_if(Curl_dynhds_h1_dprint(&hds, &dbuf), "h1 print failed");
13813498266Sopenharmony_ci  if(Curl_dyn_ptr(&dbuf)) {
13913498266Sopenharmony_ci    fprintf(stderr, "indent concat: %s\n", Curl_dyn_ptr(&dbuf));
14013498266Sopenharmony_ci    fail_if(strcmp(Curl_dyn_ptr(&dbuf),
14113498266Sopenharmony_ci                   "ti1: val1 val2\r\nti2: val1 val2\r\nti3: val1 val2\r\n"),
14213498266Sopenharmony_ci                   "wrong format");
14313498266Sopenharmony_ci  }
14413498266Sopenharmony_ci  Curl_dyn_free(&dbuf);
14513498266Sopenharmony_ci
14613498266Sopenharmony_ci  Curl_dynhds_free(&hds);
14713498266Sopenharmony_ci
14813498266Sopenharmony_ciUNITTEST_STOP
149