Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <stdio.h>
18 #include <string.h>
20 #include "../gmid.h"
22 struct suite {
23 const char *src;
24 const char *res;
25 } t[] = {
26 {"foo", "foo"},
27 {"xn-invalid", "xn-invalid"},
28 {"naïve", "naïve"},
29 {"xn--8ca", "è"},
30 {"xn--caff-8oa", "caffè"},
31 {"xn--nave-6pa", "naïve"},
32 {"xn--e-0mbbc", "τeστ"},
33 {"xn--8ca67lbac", "τèστ"},
34 {"xn--28j2a3ar1p", "こんにちは"},
35 {"xn--hello--ur7iy09x", "hello-世界"},
36 {"xn--hi--hi-rr7iy09x", "hi-世界-hi"},
37 {"xn--caf-8la.foo.org", "cafè.foo.org"},
38 /* 3 bytes */
39 {"xn--j6h", "♨"},
40 /* 4 bytes */
41 {"xn--x73l", "𩸽"},
42 {"xn--x73laaa", "𩸽𩸽𩸽𩸽"},
43 {NULL, NULL}
44 };
46 int
47 main(int argc, char **argv)
48 {
49 struct suite *i;
50 int failed;
51 char *hostname;
52 char buf[64]; /* name len */
54 failed = 0;
55 for (i = t; i->src != NULL; ++i) {
56 if ((hostname = strdup(i->src)) == NULL)
57 return 0;
59 memset(buf, 0, sizeof(buf));
60 if (!puny_decode(hostname, buf, sizeof(buf))) {
61 printf("decode: failure with %s\n", i->src);
62 failed = 1;
63 continue;
64 }
66 if (strcmp(buf, i->res)) {
67 printf("ERR: expected \"%s\", got \"%s\"\n",
68 i->res, buf);
69 failed = 1;
70 continue;
71 } else
72 printf("OK: %s => %s\n", i->src, buf);
74 free(hostname);
75 }
77 return failed;
78 }