summaryrefslogtreecommitdiff
path: root/test/SetFragment.cpp
blob: 71e53437daf17d9570a9acf232524f645900fb95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * uriparser - RFC 3986 URI parsing library
 *
 * Copyright (C) 2025, Sebastian Pipping <sebastian@pipping.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#undef NDEBUG  // because we rely on assert(3) further down

#include <cassert>

#include <gtest/gtest.h>

#include <uriparser/Uri.h>

namespace {

static void testIsWellFormedFragment(const char * candidate, bool expectedWellFormed) {
    const char * const first = candidate;
    const char * const afterLast =
        (candidate == NULL) ? NULL : (candidate + strlen(candidate));

    const UriBool actualWellFormed = uriIsWellFormedFragmentA(first, afterLast);

    ASSERT_EQ(actualWellFormed, expectedWellFormed);
}

static UriUriA parseWellFormedUri(const char * text) {
    UriUriA uri;
    const int error = uriParseSingleUriA(&uri, text, NULL);
    // NOTE: we cannot use ASSERT_EQ here because of the outer non-void return type
    assert(error == URI_SUCCESS);
    return uri;
}

static void assertUriEqual(const UriUriA * uri, const char * expected) {
    int charsRequired = -1;
    ASSERT_EQ(uriToStringCharsRequiredA(uri, &charsRequired), URI_SUCCESS);
    ASSERT_TRUE(charsRequired >= 0);

    char * const buffer = (char *)malloc(charsRequired + 1);
    ASSERT_TRUE(buffer != NULL);

    ASSERT_EQ(uriToStringA(buffer, uri, charsRequired + 1, NULL), URI_SUCCESS);

    EXPECT_STREQ(buffer, expected);

    free(buffer);
}

}  // namespace

TEST(IsWellFormedFragment, Null) {
    testIsWellFormedFragment(NULL, false);
}

TEST(IsWellFormedFragment, Empty) {
    testIsWellFormedFragment("", true);
}

TEST(IsWellFormedFragment, AllowedCharacters) {
    // The related grammar subset is this:
    //
    //   fragment    = *( pchar / "/" / "?" )
    //   pchar       = unreserved / pct-encoded / sub-delims / ":" / "@"
    //   unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
    //   pct-encoded = "%" HEXDIG HEXDIG
    //   sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"
    //               / "*" / "+" / "," / ";" / "="
    //
    // NOTE: Percent encoding has dedicated tests further down
    testIsWellFormedFragment("0123456789"
                             "ABCDEF"
                             "abcdef"
                             "gGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"
                             "-._~"
                             "!$&'()*+,;="
                             ":@"
                             "/?",
                             true);
}

TEST(IsWellFormedFragment, ForbiddenCharacters) {
    testIsWellFormedFragment(" ", false);
}

TEST(IsWellFormedFragment, PercentEncodingWellFormed) {
    testIsWellFormedFragment("%"
                             "aa"
                             "%"
                             "AA",
                             true);
}

TEST(IsWellFormedFragment, PercentEncodingMalformedCutOff1) {
    testIsWellFormedFragment("%", false);
}

TEST(IsWellFormedFragment, PercentEncodingMalformedCutOff2) {
    testIsWellFormedFragment("%"
                             "a",
                             false);
}

TEST(IsWellFormedFragment, PercentEncodingMalformedForbiddenCharacter1) {
    testIsWellFormedFragment("%"
                             "ga",
                             false);
}

TEST(IsWellFormedFragment, PercentEncodingMalformedForbiddenCharacter2) {
    testIsWellFormedFragment("%"
                             "ag",
                             false);
}

TEST(SetFragment, NullUriOnly) {
    UriUriA * const uri = NULL;
    const char * const first = "toc";
    const char * const afterLast = first + strlen(first);
    ASSERT_EQ(uriSetFragmentA(uri, first, afterLast), URI_ERROR_NULL);
}

TEST(SetFragment, NullFirstOnly) {
    UriUriA uri = {};
    const char * const fragment = "toc";
    const char * const first = NULL;
    const char * const afterLast = fragment + strlen(fragment);
    ASSERT_EQ(uriSetFragmentA(&uri, first, afterLast), URI_ERROR_NULL);
}

TEST(SetFragment, NullAfterLastOnly) {
    UriUriA uri = {};
    const char * const first = "toc";
    const char * const afterLast = NULL;
    ASSERT_EQ(uriSetFragmentA(&uri, first, afterLast), URI_ERROR_NULL);
}

TEST(SetFragment, NullValueLeavesOwnerAtFalse) {
    UriUriA uri = parseWellFormedUri("scheme://host/#fragment");
    EXPECT_EQ(uri.owner, URI_FALSE);  // self-test

    EXPECT_EQ(uriSetFragmentA(&uri, NULL, NULL), URI_SUCCESS);

    EXPECT_EQ(uri.owner, URI_FALSE);  // i.e. still false

    uriFreeUriMembersA(&uri);
}

TEST(SetFragment, NonNullValueMakesOwner) {
    UriUriA uri = parseWellFormedUri("scheme://host/#old");
    const char * const first = "new";
    const char * const afterLast = first + strlen(first);
    EXPECT_EQ(uri.owner, URI_FALSE);  // self-test

    EXPECT_EQ(uriSetFragmentA(&uri, first, afterLast), URI_SUCCESS);

    EXPECT_EQ(uri.owner, URI_TRUE);  // i.e. now owned

    uriFreeUriMembersA(&uri);
}

TEST(SetFragment, NullValueApplied) {
    UriUriA uri = parseWellFormedUri("scheme://host/#fragment");

    EXPECT_EQ(uriSetFragmentA(&uri, NULL, NULL), URI_SUCCESS);

    assertUriEqual(&uri, "scheme://host/");

    uriFreeUriMembersA(&uri);
}

TEST(SetFragment, NonNullValueAppliedEmpty) {
    UriUriA uri = parseWellFormedUri("scheme://host/#fragment");
    const char * const empty = "";

    EXPECT_EQ(uriSetFragmentA(&uri, empty, empty), URI_SUCCESS);

    assertUriEqual(&uri, "scheme://host/#");

    uriFreeUriMembersA(&uri);
}

TEST(SetFragment, NonNullValueAppliedNonEmpty) {
    UriUriA uri = parseWellFormedUri("scheme://host/#old");
    const char * const first = "new";
    const char * const afterLast = first + strlen(first);

    EXPECT_EQ(uriSetFragmentA(&uri, first, afterLast), URI_SUCCESS);

    assertUriEqual(&uri, "scheme://host/#new");

    uriFreeUriMembersA(&uri);
}

TEST(SetFragment, MalformedValueRejected) {
    UriUriA uri = parseWellFormedUri("scheme://host/#fragment");
    const char * const first = "not well-formed";
    const char * const afterLast = first + strlen(first);

    EXPECT_EQ(uriSetFragmentA(&uri, first, afterLast), URI_ERROR_SYNTAX);

    uriFreeUriMembersA(&uri);
}

TEST(SetFragment, UriWithoutHostTolerated) {
    UriUriA uri = parseWellFormedUri("/no/host/here");
    const char * const first = "toc";
    const char * const afterLast = first + strlen(first);
    EXPECT_TRUE(uri.hostText.first == NULL);  // self-test

    EXPECT_EQ(uriSetFragmentA(&uri, first, afterLast), URI_SUCCESS);

    assertUriEqual(&uri, "/no/host/here#toc");

    uriFreeUriMembersA(&uri);
}