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