1 // This is a unittest for the <gwenhywfar/urlfns.h> function, notably
2 // GWEN_Url_fromString
3
4 #include <stdio.h>
5 #include <string.h>
6 #include <gwenhywfar/debug.h>
7 #include <gwenhywfar/url.h>
8 #include <gwenhywfar/urlfns.h>
9
10 // Global variable for storing the test result
11 int g_testSuccess = 0;
12
13 // Macro to test for one assertion
14 #define test_assert(expr) if (!(expr)) { printf("%s: Failed test in line %d\n", __FILE__, (int)__LINE__); g_testSuccess = -1; }
15
16 // Macro to test for string equality. Since this is C, we also check
17 // for NULL pointers already here.
18 #define test_strcmp(str1, str2) if (!(str1) || !(str2) || strcmp((str1), (str2)) != 0) { printf("%s: Failed strcmp test in line %d, \"%s\" != \"%s\"\n", __FILE__, (int)__LINE__, (str1) ? (str1) : "NULL", (str2) ? (str2) : "NULL"); g_testSuccess = -1; }
19
20 // Convenience function to check the interesting fields in one single
21 // GWEN_URL, which is also free'd here in this function already.
verify_url(GWEN_URL * url,const char * path,int port,const char * protocol,const char * server)22 int verify_url(GWEN_URL *url,
23 const char *path, int port,
24 const char *protocol, const char *server)
25 {
26 int prev_g_testSuccess = g_testSuccess;
27 g_testSuccess = 0;
28
29 if (path) {
30 test_strcmp(GWEN_Url_GetPath(url), path);
31 }
32 else {
33 test_assert(GWEN_Url_GetPath(url) == NULL);
34 }
35
36 test_assert(GWEN_Url_GetPort(url) == port);
37
38 if (protocol) {
39 test_strcmp(GWEN_Url_GetProtocol(url), protocol);
40 }
41 else {
42 test_assert(GWEN_Url_GetProtocol(url) == NULL);
43 }
44
45 if (server) {
46 test_strcmp(GWEN_Url_GetServer(url), server);
47 }
48 else {
49 test_assert(GWEN_Url_GetServer(url) == NULL);
50 }
51
52 GWEN_Url_free(url);
53
54 {
55 int result = !g_testSuccess;
56 g_testSuccess = g_testSuccess || prev_g_testSuccess;
57 return result;
58 }
59 }
60
main(GWEN_UNUSED int argc,GWEN_UNUSED char ** argv)61 int main(GWEN_UNUSED int argc, GWEN_UNUSED char **argv)
62 {
63 GWEN_URL *url = GWEN_Url_fromString("http://www.aqbanking.de/foo/bar");
64 test_assert(GWEN_Url_GetPassword(url) == NULL);
65 test_strcmp(GWEN_Url_GetPath(url), "/foo/bar");
66 test_assert(GWEN_Url_GetPort(url) == 80);
67 test_strcmp(GWEN_Url_GetProtocol(url), "http");
68 test_strcmp(GWEN_Url_GetServer(url), "www.aqbanking.de");
69 test_assert(GWEN_Url_GetUserName(url) == NULL);
70 GWEN_Url_free(url);
71
72 // Check some error conditions: No Server, should return NULL
73 test_assert(GWEN_Url_fromString("http://") == NULL);
74 // No server after user, should return NULL
75 test_assert(GWEN_Url_fromString("http://cs@") == NULL);
76 // No numerical port number, should return NULL
77 test_assert(GWEN_Url_fromString("http://a.b.c:aa/foo") == NULL);
78 DBG_WARN(GWEN_LOGDOMAIN, "The 3 error messages above are just fine - all works as expected.");
79
80 // And some very weird URL
81 url = GWEN_Url_fromString("rsync://foo:bar@a.b.c.d.e.f:4711/some space /in here");
82 test_strcmp(GWEN_Url_GetProtocol(url), "rsync");
83 test_strcmp(GWEN_Url_GetUserName(url), "foo");
84 test_strcmp(GWEN_Url_GetPassword(url), "bar");
85 test_strcmp(GWEN_Url_GetServer(url), "a.b.c.d.e.f");
86 test_assert(GWEN_Url_GetPort(url) == 4711);
87 test_strcmp(GWEN_Url_GetPath(url), "/some space /in here");
88 GWEN_Url_free(url);
89
90 url = GWEN_Url_fromString("file:/home/aquamaniac");
91 test_strcmp(GWEN_Url_GetPath(url), "/home/aquamaniac");
92 test_assert(GWEN_Url_GetPort(url) == 0);
93 test_assert(GWEN_Url_GetProtocol(url) == NULL); // no "file" here? Probably correct because of missing extra slashes.
94 test_strcmp(GWEN_Url_GetServer(url), "file");
95 GWEN_Url_free(url);
96
97 test_assert((verify_url(GWEN_Url_fromString("file:///home/aquamaniac"),
98 /*GetPath*/ "/home/aquamaniac",
99 /*GetPort*/ 0,
100 /*GetProtocol*/ "file", // now the protocol it is here
101 /*GetServer*/ NULL)));
102
103 test_assert((verify_url(GWEN_Url_fromString("/home/aquamaniac"),
104 /*GetPath*/ "/home/aquamaniac",
105 /*GetPort*/ 0,
106 /*GetProtocol*/ NULL,
107 /*GetServer*/ NULL)));
108
109 test_assert((verify_url(GWEN_Url_fromString("dir:///home/aquamaniac/.aqbanking/settings"),
110 /*GetPath*/ "/home/aquamaniac/.aqbanking/settings",
111 /*GetPort*/ 0,
112 /*GetProtocol*/ "dir",
113 /*GetServer*/ NULL)));
114
115 // Also some windows paths
116 test_assert((verify_url(GWEN_Url_fromString("c:/home/aquamaniac"),
117 /*GetPath*/ "c:/home/aquamaniac",
118 /*GetPort*/ 0,
119 /*GetProtocol*/ NULL,
120 /*GetServer*/ NULL)));
121
122 test_assert((verify_url(GWEN_Url_fromString("c:\\home\\aquamaniac"),
123 /*GetPath*/ "c:\\home\\aquamaniac",
124 /*GetPort*/ 0,
125 /*GetProtocol*/ NULL,
126 /*GetServer*/ NULL)));
127
128 // This path caused the crash in the windows gnucash binary; is
129 // fixed now.
130 test_assert((verify_url(GWEN_Url_fromString("dir://c:\\home\\aquamaniac"),
131 /*GetPath*/ "c:\\home\\aquamaniac",
132 /*GetPort*/ 0,
133 /*GetProtocol*/ "dir",
134 /*GetServer*/ NULL)));
135
136 test_assert((verify_url(GWEN_Url_fromString("file://c:\\home\\aquamaniac"),
137 /*GetPath*/ "c:\\home\\aquamaniac",
138 /*GetPort*/ 0,
139 /*GetProtocol*/ "file",
140 /*GetServer*/ NULL)));
141
142 return g_testSuccess;
143 }
144