| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/http/private/strutil.h> |
| 6 |
|
| 7 |
#include <aws/testing/aws_test_harness.h> |
| 8 |
|
| 9 |
#define TEST_CASE(NAME) \ |
| 10 |
AWS_TEST_CASE(NAME, s_test_##NAME); \ |
| 11 |
static int s_test_##NAME(struct aws_allocator *allocator, void *ctx) |
| 12 |
|
| 13 |
AWS_TEST_CASE(strutil_trim_http_whitespace, s_strutil_trim_http_whitespace); |
| 14 |
static int s_strutil_trim_http_whitespace(struct aws_allocator *allocator, void *ctx) { |
| 15 |
(void)allocator; |
| 16 |
(void)ctx; |
| 17 |
|
| 18 |
struct test { |
| 19 |
const char *input; |
| 20 |
const char *expected; |
| 21 |
}; |
| 22 |
|
| 23 |
struct test tests[] = { |
| 24 |
{"a", "a"}, |
| 25 |
{" a", "a"}, |
| 26 |
{"a ", "a"}, |
| 27 |
{" a ", "a"}, |
| 28 |
{"", ""}, |
| 29 |
{" ", ""}, |
| 30 |
{" ", ""}, |
| 31 |
{"a", "a"}, |
| 32 |
{"\t", ""}, |
| 33 |
{"\ta", "a"}, |
| 34 |
{"a\t", "a"}, |
| 35 |
{"\t a \t", "a"}, |
| 36 |
}; |
| 37 |
|
| 38 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(tests); ++i) { |
| 39 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str(tests[i].input); |
| 40 |
struct aws_byte_cursor expected = aws_byte_cursor_from_c_str(tests[i].expected); |
| 41 |
struct aws_byte_cursor trimmed = aws_strutil_trim_http_whitespace(input); |
| 42 |
ASSERT_TRUE(aws_byte_cursor_eq(&expected, &trimmed)); |
| 43 |
} |
| 44 |
|
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
AWS_TEST_CASE(strutil_is_http_token, s_strutil_is_http_token); |
| 49 |
static int s_strutil_is_http_token(struct aws_allocator *allocator, void *ctx) { |
| 50 |
(void)allocator; |
| 51 |
(void)ctx; |
| 52 |
|
| 53 |
|
| 54 |
ASSERT_TRUE(aws_strutil_is_http_token(aws_byte_cursor_from_c_str("A"))); |
| 55 |
ASSERT_TRUE(aws_strutil_is_http_token(aws_byte_cursor_from_c_str("Host"))); |
| 56 |
|
| 57 |
|
| 58 |
ASSERT_FALSE(aws_strutil_is_http_token(aws_byte_cursor_from_c_str(""))); |
| 59 |
|
| 60 |
|
| 61 |
const char *all_acceptable = "!#$%&'*+-.^_`|~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 62 |
const size_t all_acceptable_strlen = strlen(all_acceptable); |
| 63 |
ASSERT_TRUE(aws_strutil_is_http_token(aws_byte_cursor_from_c_str(all_acceptable))); |
| 64 |
|
| 65 |
|
| 66 |
for (size_t i = 0; i < 256; ++i) { |
| 67 |
uint8_t c = (uint8_t)i; |
| 68 |
bool is_acceptable = memchr(all_acceptable, c, all_acceptable_strlen) != NULL; |
| 69 |
ASSERT_UINT_EQUALS(is_acceptable, aws_strutil_is_http_token(aws_byte_cursor_from_array(&c, 1))); |
| 70 |
} |
| 71 |
|
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
AWS_TEST_CASE(strutil_is_lowercase_http_token, s_strutil_is_lowercase_http_token); |
| 76 |
static int s_strutil_is_lowercase_http_token(struct aws_allocator *allocator, void *ctx) { |
| 77 |
(void)allocator; |
| 78 |
(void)ctx; |
| 79 |
|
| 80 |
|
| 81 |
ASSERT_TRUE(aws_strutil_is_lowercase_http_token(aws_byte_cursor_from_c_str("a"))); |
| 82 |
ASSERT_TRUE(aws_strutil_is_lowercase_http_token(aws_byte_cursor_from_c_str("host"))); |
| 83 |
|
| 84 |
|
| 85 |
ASSERT_FALSE(aws_strutil_is_lowercase_http_token(aws_byte_cursor_from_c_str(""))); |
| 86 |
|
| 87 |
|
| 88 |
ASSERT_FALSE(aws_strutil_is_lowercase_http_token(aws_byte_cursor_from_c_str("Host"))); |
| 89 |
ASSERT_FALSE(aws_strutil_is_lowercase_http_token(aws_byte_cursor_from_c_str(":\""))); |
| 90 |
|
| 91 |
|
| 92 |
const char *all_acceptable = "!#$%&'*+-.^_`|~0123456789abcdefghijklmnopqrstuvwxyz"; |
| 93 |
const size_t all_acceptable_strlen = strlen(all_acceptable); |
| 94 |
ASSERT_TRUE(aws_strutil_is_lowercase_http_token(aws_byte_cursor_from_c_str(all_acceptable))); |
| 95 |
|
| 96 |
|
| 97 |
for (size_t i = 0; i < 256; ++i) { |
| 98 |
uint8_t c = (uint8_t)i; |
| 99 |
bool is_acceptable = memchr(all_acceptable, c, all_acceptable_strlen) != NULL; |
| 100 |
ASSERT_UINT_EQUALS(is_acceptable, aws_strutil_is_lowercase_http_token(aws_byte_cursor_from_array(&c, 1))); |
| 101 |
} |
| 102 |
|
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
AWS_TEST_CASE(strutil_is_http_field_value, s_strutil_is_http_field_value); |
| 107 |
static int s_strutil_is_http_field_value(struct aws_allocator *allocator, void *ctx) { |
| 108 |
(void)allocator; |
| 109 |
(void)ctx; |
| 110 |
|
| 111 |
|
| 112 |
ASSERT_TRUE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("0"))); |
| 113 |
ASSERT_TRUE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("en"))); |
| 114 |
|
| 115 |
|
| 116 |
ASSERT_TRUE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str(""))); |
| 117 |
|
| 118 |
|
| 119 |
ASSERT_TRUE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("a b"))); |
| 120 |
ASSERT_TRUE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("a\tb"))); |
| 121 |
ASSERT_TRUE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("a\t\t \t\t b"))); |
| 122 |
|
| 123 |
|
| 124 |
ASSERT_FALSE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str(" 999"))); |
| 125 |
ASSERT_FALSE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("999 "))); |
| 126 |
ASSERT_FALSE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("\t999"))); |
| 127 |
ASSERT_FALSE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("999\t"))); |
| 128 |
|
| 129 |
|
| 130 |
ASSERT_TRUE(aws_strutil_is_http_field_value( |
| 131 |
aws_byte_cursor_from_c_str("\xF0\x9F\x91\x81\xF0\x9F\x91\x84\xF0\x9F\x91\x81"))); |
| 132 |
|
| 133 |
|
| 134 |
ASSERT_FALSE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("item1\r\n item2"))); |
| 135 |
ASSERT_FALSE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("item1\r item2"))); |
| 136 |
ASSERT_FALSE(aws_strutil_is_http_field_value(aws_byte_cursor_from_c_str("item1\n item2"))); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
char mutable_str[] = {'a', 'b', 'c'}; |
| 142 |
for (size_t i = 0; i < 256; ++i) { |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
bool allowed_in_grammar = ( i == ' ') || ( i == '\t') || ( i >= 0x21 && i <= 0x7E) || |
| 151 |
( i >= 0x80 && i <= 0xFF); |
| 152 |
|
| 153 |
mutable_str[1] = (char)i; |
| 154 |
struct aws_byte_cursor cursor = aws_byte_cursor_from_array(mutable_str, AWS_ARRAY_SIZE(mutable_str)); |
| 155 |
bool passes = aws_strutil_is_http_field_value(cursor); |
| 156 |
ASSERT_INT_EQUALS(allowed_in_grammar, passes, "failed at character 0x%02X", i); |
| 157 |
} |
| 158 |
|
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
AWS_TEST_CASE(strutil_is_http_reason_phrase, s_strutil_is_http_reason_phrase); |
| 163 |
static int s_strutil_is_http_reason_phrase(struct aws_allocator *allocator, void *ctx) { |
| 164 |
(void)allocator; |
| 165 |
(void)ctx; |
| 166 |
|
| 167 |
|
| 168 |
ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("OK"))); |
| 169 |
|
| 170 |
|
| 171 |
ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str(""))); |
| 172 |
|
| 173 |
|
| 174 |
ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("Not Found"))); |
| 175 |
ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("Not\tFound"))); |
| 176 |
ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str(" Not Found"))); |
| 177 |
ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("Not Found "))); |
| 178 |
ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("\t Not\t\t Found \t"))); |
| 179 |
ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str(" "))); |
| 180 |
ASSERT_TRUE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("\t"))); |
| 181 |
|
| 182 |
|
| 183 |
ASSERT_TRUE(aws_strutil_is_http_reason_phrase( |
| 184 |
aws_byte_cursor_from_c_str("\xF0\x9F\x91\x81\xF0\x9F\x91\x84\xF0\x9F\x91\x81"))); |
| 185 |
|
| 186 |
|
| 187 |
ASSERT_FALSE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("Line\r\nFolds"))); |
| 188 |
ASSERT_FALSE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("Line\rFeed"))); |
| 189 |
ASSERT_FALSE(aws_strutil_is_http_reason_phrase(aws_byte_cursor_from_c_str("New\nLine"))); |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
char mutable_str[] = {'a', 'b', 'c'}; |
| 195 |
for (size_t i = 0; i < 256; ++i) { |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
bool allowed_in_grammar = ( i == ' ') || ( i == '\t') || ( i >= 0x21 && i <= 0x7E) || |
| 202 |
( i >= 0x80 && i <= 0xFF); |
| 203 |
|
| 204 |
mutable_str[1] = (char)i; |
| 205 |
struct aws_byte_cursor cursor = aws_byte_cursor_from_array(mutable_str, AWS_ARRAY_SIZE(mutable_str)); |
| 206 |
bool passes = aws_strutil_is_http_reason_phrase(cursor); |
| 207 |
ASSERT_INT_EQUALS(allowed_in_grammar, passes, "failed at character 0x%02X", i); |
| 208 |
} |
| 209 |
|
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
AWS_TEST_CASE(strutil_is_http_request_target, s_strutil_is_http_request_target); |
| 214 |
static int s_strutil_is_http_request_target(struct aws_allocator *allocator, void *ctx) { |
| 215 |
(void)allocator; |
| 216 |
(void)ctx; |
| 217 |
|
| 218 |
|
| 219 |
ASSERT_TRUE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("/"))); |
| 220 |
|
| 221 |
|
| 222 |
ASSERT_FALSE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str(""))); |
| 223 |
|
| 224 |
|
| 225 |
ASSERT_FALSE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str(" "))); |
| 226 |
ASSERT_FALSE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("/spaces-are-bad .html"))); |
| 227 |
ASSERT_FALSE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("/tabs-are-bad\t.html"))); |
| 228 |
ASSERT_FALSE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("/crlf-is-really-bad\r\n.html"))); |
| 229 |
ASSERT_FALSE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("/newline-is-bad\n.html"))); |
| 230 |
ASSERT_FALSE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("/linefeed-is-bad\r.html"))); |
| 231 |
|
| 232 |
|
| 233 |
ASSERT_TRUE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("/where?q=now"))); |
| 234 |
|
| 235 |
|
| 236 |
ASSERT_TRUE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("http://www.amazon.com/index.html"))); |
| 237 |
|
| 238 |
|
| 239 |
ASSERT_TRUE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("www.example.com:80"))); |
| 240 |
|
| 241 |
|
| 242 |
ASSERT_TRUE(aws_strutil_is_http_request_target(aws_byte_cursor_from_c_str("*"))); |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
AWS_TEST_CASE(strutil_is_http_pseudo_header_name, s_strutil_is_http_pseudo_header_name); |
| 252 |
static int s_strutil_is_http_pseudo_header_name(struct aws_allocator *allocator, void *ctx) { |
| 253 |
(void)allocator; |
| 254 |
(void)ctx; |
| 255 |
|
| 256 |
|
| 257 |
ASSERT_TRUE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str(":method"))); |
| 258 |
ASSERT_TRUE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str(":scheme"))); |
| 259 |
ASSERT_TRUE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str(":authority"))); |
| 260 |
ASSERT_TRUE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str(":path"))); |
| 261 |
ASSERT_TRUE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str(":status"))); |
| 262 |
|
| 263 |
|
| 264 |
ASSERT_FALSE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str(""))); |
| 265 |
|
| 266 |
|
| 267 |
ASSERT_FALSE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str("connect"))); |
| 268 |
ASSERT_FALSE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str("Method"))); |
| 269 |
ASSERT_FALSE(aws_strutil_is_http_pseudo_header_name(aws_byte_cursor_from_c_str("httpCRT"))); |
| 270 |
return 0; |
| 271 |
} |