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
|
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "../src/rust-url-capi.h"
class StringContainer
{
public:
StringContainer()
{
mBuffer = nullptr;
mLength = 0;
}
~StringContainer()
{
free(mBuffer);
mBuffer = nullptr;
}
void SetSize(size_t size)
{
mLength = size;
if (mBuffer) {
mBuffer = (char *)realloc(mBuffer, size);
return;
}
mBuffer = (char *)malloc(size);
}
char * GetBuffer()
{
return mBuffer;
}
void CheckEquals(const char * ref) {
int32_t refLen = strlen(ref);
printf("CheckEquals: %s (len:%d)\n", ref, refLen);
if (refLen != mLength || strncmp(mBuffer, ref, mLength)) {
printf("\t--- ERROR ---\n");
printf("Got : ");
fwrite(mBuffer, mLength, 1, stdout);
printf(" (len:%d)\n", mLength);
exit(-1);
}
printf("-> OK\n");
}
private:
int32_t mLength;
char * mBuffer;
};
extern "C" int32_t c_fn_set_size(void * container, size_t size)
{
((StringContainer *) container)->SetSize(size);
return 0;
}
extern "C" char * c_fn_get_buffer(void * container)
{
return ((StringContainer *) container)->GetBuffer();
}
#define TEST_CALL(func, expected) \
{ \
int32_t code = func; \
printf("%s -> code %d\n", #func, code); \
assert(code == expected); \
printf("-> OK\n"); \
} \
int main() {
// Create URL
rusturl_ptr url = rusturl_new("http://example.com/path/some/file.txt",
strlen("http://example.com/path/some/file.txt"));
assert(url); // Check we have a URL
StringContainer container;
TEST_CALL(rusturl_get_spec(url, &container), 0);
container.CheckEquals("http://example.com/path/some/file.txt");
TEST_CALL(rusturl_set_host(url, "test.com", strlen("test.com")), 0);
TEST_CALL(rusturl_get_host(url, &container), 0);
container.CheckEquals("test.com");
TEST_CALL(rusturl_get_path(url, &container), 0);
container.CheckEquals("/path/some/file.txt");
TEST_CALL(rusturl_set_path(url, "hello/../else.txt", strlen("hello/../else.txt")), 0);
TEST_CALL(rusturl_get_path(url, &container), 0);
container.CheckEquals("/else.txt");
TEST_CALL(rusturl_resolve(url, "./bla/file.txt", strlen("./bla/file.txt"), &container), 0);
container.CheckEquals("http://test.com/bla/file.txt");
TEST_CALL(rusturl_get_scheme(url, &container), 0);
container.CheckEquals("http");
TEST_CALL(rusturl_set_username(url, "user", strlen("user")), 0);
TEST_CALL(rusturl_get_username(url, &container), 0);
container.CheckEquals("user");
TEST_CALL(rusturl_get_spec(url, &container), 0);
container.CheckEquals("http://user@test.com/else.txt");
TEST_CALL(rusturl_set_password(url, "pass", strlen("pass")), 0);
TEST_CALL(rusturl_get_password(url, &container), 0);
container.CheckEquals("pass");
TEST_CALL(rusturl_get_spec(url, &container), 0);
container.CheckEquals("http://user:pass@test.com/else.txt");
TEST_CALL(rusturl_set_username(url, "", strlen("")), 0);
TEST_CALL(rusturl_set_password(url, "", strlen("")), 0);
TEST_CALL(rusturl_get_spec(url, &container), 0);
container.CheckEquals("http://test.com/else.txt");
TEST_CALL(rusturl_set_host_and_port(url, "example.org:1234", strlen("example.org:1234")), 0);
TEST_CALL(rusturl_get_host(url, &container), 0);
container.CheckEquals("example.org");
assert(rusturl_get_port(url) == 1234);
TEST_CALL(rusturl_set_port(url, "9090", strlen("9090")), 0);
assert(rusturl_get_port(url) == 9090);
TEST_CALL(rusturl_set_query(url, "x=1", strlen("x=1")), 0);
TEST_CALL(rusturl_get_query(url, &container), 0);
container.CheckEquals("x=1");
TEST_CALL(rusturl_set_fragment(url, "fragment", strlen("fragment")), 0);
TEST_CALL(rusturl_get_fragment(url, &container), 0);
container.CheckEquals("fragment");
TEST_CALL(rusturl_get_spec(url, &container), 0);
container.CheckEquals("http://example.org:9090/else.txt?x=1#fragment");
// Free the URL
rusturl_free(url);
url = rusturl_new("http://example.com/#",
strlen("http://example.com/#"));
assert(url); // Check we have a URL
assert(rusturl_has_fragment(url) == 1);
TEST_CALL(rusturl_set_fragment(url, "", 0), 0);
assert(rusturl_has_fragment(url) == 0);
TEST_CALL(rusturl_get_spec(url, &container), 0);
container.CheckEquals("http://example.com/");
rusturl_free(url);
printf("SUCCESS\n");
return 0;
}
|