src/server/test/uri.c

Sat, 26 Nov 2022 17:07:08 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 26 Nov 2022 17:07:08 +0100
changeset 438
22eca559aded
parent 355
4a7dd7ff92c9
permissions
-rw-r--r--

refactore http listener creation

355
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2022 Olaf Wintermann. All rights reserved.
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "uri.h"
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include "../util/util.h"
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 UCX_TEST(test_util_uri_escape_alphanum) {
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 char *str1 = "/test/path/abc/";
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 char str_enc[512];
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 UCX_TEST_BEGIN;
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 char *test = util_uri_escape(str_enc, str1);
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 UCX_TEST_ASSERT(test, "util_uri_escape returned NULL");
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 UCX_TEST_ASSERT(!strcasecmp(test, str1), "test != str1");
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 UCX_TEST_END;
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 }
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 UCX_TEST(test_util_uri_escape_space) {
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 char *str1 = "/test/space in path/";
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 char *str_enc_expected = "/test/space%20in%20path/";
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 char str_enc[512];
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 UCX_TEST_BEGIN;
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 char *test = util_uri_escape(str_enc, str1);
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 UCX_TEST_ASSERT(test, "util_uri_escape returned NULL");
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 UCX_TEST_ASSERT(!strcasecmp(test, str_enc_expected), "unexpected result");
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 UCX_TEST_END;
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 }
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 UCX_TEST(test_util_uri_escape_latin) {
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 char *str1 = "/test/path/öäütestß/";
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 char *str_enc_expected = "/test/path/%C3%B6%C3%A4%C3%BCtest%C3%9F/";
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 char *str2 = "€";
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 char *str2_enc_expected = "%E2%82%AC";
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 char str_enc[512];
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 UCX_TEST_BEGIN;
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 // test 1
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 char *test = util_uri_escape(str_enc, str1);
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 UCX_TEST_ASSERT(test, "util_uri_escape returned NULL");
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 UCX_TEST_ASSERT(!strcasecmp(test, str_enc_expected), "unexpected result");
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 // test 2
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 test = util_uri_escape(str_enc, str2);
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 UCX_TEST_ASSERT(test, "(2) util_uri_escape returned NULL");
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 UCX_TEST_ASSERT(!strcasecmp(test, str2_enc_expected), "(2) unexpected result");
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 UCX_TEST_END;
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 }
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 UCX_TEST(test_util_uri_escape_kanji) {
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 char *str1 = "漢字";
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 char *str1_enc_expected = "%E6%BC%A2%E5%AD%97";
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 char *str2 = "/test/エンコーディング/漢字/";
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 char *str2_enc_expected = "/test/%E3%82%A8%E3%83%B3%E3%82%B3%E3%83%BC%E3%83%87%E3%82%A3%E3%83%B3%E3%82%B0/%E6%BC%A2%E5%AD%97/";
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 char str_enc[512];
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 UCX_TEST_BEGIN;
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 // test 1
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 char *test = util_uri_escape(str_enc, str1);
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 UCX_TEST_ASSERT(test, "util_uri_escape returned NULL");
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 UCX_TEST_ASSERT(!strcasecmp(test, str1_enc_expected), "unexpected result");
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 // test 2
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 test = util_uri_escape(str_enc, str2);
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 UCX_TEST_ASSERT(test, "(2) util_uri_escape returned NULL");
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 UCX_TEST_ASSERT(!strcasecmp(test, str2_enc_expected), "(2) unexpected result");
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 UCX_TEST_END;
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 }
4a7dd7ff92c9 enable util_uri_escape and util_url_escape and add some tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108

mercurial