00001 #include <u/libu.h>
00002
00003 int test_suite_uri_register (u_test_t *t);
00004
00005 static int test_uri_parser (u_test_case_t *tc);
00006 static int test_uri_builder (u_test_case_t *tc);
00007
00008
00009 typedef struct
00010 {
00011 unsigned int flags;
00012 const char *scheme, *user, *pwd, *host, *port, *path, *query, *fragment;
00013 } u_uri_atoms_t;
00014
00015 static int test_uri_parser (u_test_case_t *tc)
00016 {
00017 struct vt_s
00018 {
00019 const char *in;
00020 u_uri_atoms_t ex;
00021 } vt[] = {
00022 {
00023 "tcp4://www.kame.net:http/index.html",
00024 {
00025 .flags = U_URI_FLAGS_NONE,
00026 .scheme = "tcp4",
00027 .user = NULL,
00028 .pwd = NULL,
00029 .host = "www.kame.net",
00030 .port = "http",
00031 .path = "/index.html"
00032 }
00033 },
00034 {
00035 "http://wiki.koanlogic.com/doku.php?id=libu",
00036 {
00037 .flags = U_URI_FLAGS_NONE,
00038 .scheme = "http",
00039 .user = NULL,
00040 .pwd = NULL,
00041 .host = "wiki.koanlogic.com",
00042 .port = NULL,
00043 .path = "/doku.php",
00044 .query = "id=libu"
00045 }
00046 },
00047 {
00048 "http://[2001:200::8002:203:47ff:fea5:3085]:80/index.html",
00049 {
00050 .flags = U_URI_FLAGS_HOST_IS_IPADDRESS |
00051 U_URI_FLAGS_HOST_IS_IPLITERAL,
00052 .scheme = "http",
00053 .user = NULL,
00054 .pwd = NULL,
00055 .host = "2001:200::8002:203:47ff:fea5:3085",
00056 .port = "80",
00057 .path = "/index.html"
00058 }
00059 },
00060 {
00061 "coap://[::1]/.well-known/core",
00062 {
00063 .flags = U_URI_FLAGS_HOST_IS_IPADDRESS |
00064 U_URI_FLAGS_HOST_IS_IPLITERAL,
00065 .scheme = "coap",
00066 .user = NULL,
00067 .pwd = NULL,
00068 .host = "::1",
00069 .port = NULL,
00070 .path = "/.well-known/core"
00071 }
00072 },
00073 {
00074 "coaps://[::1]",
00075 {
00076 .flags = U_URI_FLAGS_HOST_IS_IPADDRESS |
00077 U_URI_FLAGS_HOST_IS_IPLITERAL,
00078 .scheme = "coaps",
00079 .user = NULL,
00080 .pwd = NULL,
00081 .host = "::1",
00082 .port = NULL,
00083 .path = NULL
00084 }
00085 },
00086 {
00087 NULL,
00088 {
00089 .scheme = NULL,
00090 .user = NULL,
00091 .pwd = NULL,
00092 .host = NULL,
00093 .port = NULL,
00094 .path = NULL
00095 }
00096 }
00097 };
00098
00099 int i;
00100 u_uri_t *u = NULL;
00101
00102 #define CHECK_EXP_MSG(field) do { \
00103 if ((vt[i].ex.field == NULL) ? \
00104 (strlen(u_uri_get_##field(u)) > 0) : \
00105 (strcmp(u_uri_get_##field(u), vt[i].ex.field) != 0)) \
00106 { \
00107 u_test_case_printf(tc, "%s != %s at idx %d", \
00108 u_uri_get_##field(u), vt[i].ex.field, i); \
00109 goto err; \
00110 } \
00111 } while (0)
00112
00113 for (i = 0; vt[i].in; i++)
00114 {
00115 u_test_err_if (u_uri_crumble(vt[i].in, U_URI_OPT_NONE, &u));
00116
00117 CHECK_EXP_MSG(scheme);
00118 CHECK_EXP_MSG(user);
00119 CHECK_EXP_MSG(pwd);
00120 CHECK_EXP_MSG(host);
00121 CHECK_EXP_MSG(port);
00122 CHECK_EXP_MSG(path);
00123 u_test_err_if (u_uri_get_flags(u) != vt[i].ex.flags);
00124
00125 u_uri_free(u), u = NULL;
00126 }
00127
00128 #undef CHECK_EXP_MSG
00129
00130 return U_TEST_SUCCESS;
00131 err:
00132 u_uri_free(u);
00133 return U_TEST_FAILURE;
00134 }
00135
00136 static int test_uri_builder (u_test_case_t *tc)
00137 {
00138 struct vt_s
00139 {
00140 u_uri_atoms_t in;
00141 const char *ex;
00142 } vt[] = {
00143 {
00144 .in = {
00145 .scheme = "tcp4",
00146 .user = NULL,
00147 .pwd = NULL,
00148 .host = "www.kame.net",
00149 .port = "http",
00150 .path = "/index.html",
00151 .fragment = "overview"
00152 },
00153 .ex = "tcp4://www.kame.net:http/index.html#overview"
00154 },
00155 {
00156 .in = {
00157 .scheme = "coap",
00158 .user = NULL,
00159 .pwd = NULL,
00160 .host = "::1",
00161 .port = NULL,
00162 .path = "/.well-known/core"
00163 },
00164 .ex = "coap://[::1]/.well-known/core"
00165 },
00166 { .ex = NULL }
00167 };
00168
00169 int i;
00170 u_uri_t *u = NULL;
00171
00172 #define SET_URI_ATOM(field) do { \
00173 if (vt[i].in.field != NULL) \
00174 u_test_err_if (u_uri_set_##field(u, vt[i].in.field)); \
00175 } while (0)
00176
00177 for (i = 0; vt[i].ex; i++)
00178 {
00179 char s[U_URI_STRMAX];
00180
00181 u_test_err_if (u_uri_new(0, &u));
00182
00183 SET_URI_ATOM(scheme);
00184 SET_URI_ATOM(user);
00185 SET_URI_ATOM(pwd);
00186 SET_URI_ATOM(host);
00187 SET_URI_ATOM(port);
00188 SET_URI_ATOM(path);
00189 SET_URI_ATOM(query);
00190 SET_URI_ATOM(fragment);
00191
00192 u_test_err_if (u_uri_knead(u, s));
00193 u_test_err_ifm (strcasecmp(s, vt[i].ex), "%s != %s", s, vt[i].ex);
00194
00195 u_uri_free(u), u = NULL;
00196 }
00197
00198 #undef SET_URI_ATOM
00199
00200 return U_TEST_SUCCESS;
00201 err:
00202 if (u)
00203 u_uri_free(u);
00204 return U_TEST_FAILURE;
00205 }
00206
00207 int test_suite_uri_register (u_test_t *t)
00208 {
00209 u_test_suite_t *ts = NULL;
00210
00211 con_err_if (u_test_suite_new("URI", &ts));
00212
00213 con_err_if (u_test_case_register("u_uri_crumble", test_uri_parser, ts));
00214 con_err_if (u_test_case_register("u_uri_knead", test_uri_builder, ts));
00215
00216
00217 con_err_if (u_test_suite_dep_register("Lexer", ts));
00218
00219 return u_test_suite_add(ts, t);
00220 err:
00221 u_test_suite_free(ts);
00222 return ~0;
00223 }