1226031Sstas/*
2226031Sstas * Copyright (c) 2004 Kungliga Tekniska H��gskolan
3226031Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4226031Sstas * All rights reserved.
5226031Sstas *
6226031Sstas * Redistribution and use in source and binary forms, with or without
7226031Sstas * modification, are permitted provided that the following conditions
8226031Sstas * are met:
9226031Sstas *
10226031Sstas * 1. Redistributions of source code must retain the above copyright
11226031Sstas *    notice, this list of conditions and the following disclaimer.
12226031Sstas *
13226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
14226031Sstas *    notice, this list of conditions and the following disclaimer in the
15226031Sstas *    documentation and/or other materials provided with the distribution.
16226031Sstas *
17226031Sstas * 3. Neither the name of the Institute nor the names of its contributors
18226031Sstas *    may be used to endorse or promote products derived from this software
19226031Sstas *    without specific prior written permission.
20226031Sstas *
21226031Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31226031Sstas * SUCH DAMAGE.
32226031Sstas */
33226031Sstas
34226031Sstas#ifdef HAVE_CONFIG_H
35226031Sstas#include <config.h>
36226031Sstas#endif
37226031Sstas#include <stdio.h>
38226031Sstas#include <string.h>
39226031Sstas
40226031Sstas#include "windlocl.h"
41226031Sstas#include "punycode_examples.h"
42226031Sstas
43226031Sstasint
44226031Sstasmain(void)
45226031Sstas{
46226031Sstas    unsigned i;
47226031Sstas    unsigned failures = 0;
48226031Sstas
49226031Sstas    for (i = 0; i < punycode_examples_size; ++i) {
50226031Sstas	char buf[256];
51226031Sstas	int ret;
52226031Sstas	const struct punycode_example *e = &punycode_examples[i];
53226031Sstas	size_t len;
54226031Sstas
55226031Sstas	len = sizeof(buf);
56226031Sstas	ret = wind_punycode_label_toascii(e->val, e->len, buf, &len);
57226031Sstas	if (ret) {
58226031Sstas	    printf("punycode %u (%s) failed: %d\n", i, e->description, ret);
59226031Sstas	    ++failures;
60226031Sstas	    continue;
61226031Sstas	}
62226031Sstas	if (strncmp(buf, "xn--", 4) == 0) {
63226031Sstas	    memmove(buf, buf + 4, len - 4);
64226031Sstas	    len -= 4;
65226031Sstas	}
66226031Sstas	if (len != strlen(e->pc)) {
67226031Sstas	    printf("punycode %u (%s) wrong len, actual: %u, expected: %u\n",
68226031Sstas		   i, e->description,
69226031Sstas		   (unsigned int)len, (unsigned int)strlen(e->pc));
70226031Sstas	    printf("buf %s != pc: %s\n", buf, e->pc);
71226031Sstas	    ++failures;
72226031Sstas	    continue;
73226031Sstas	}
74226031Sstas	if (strncasecmp(buf, e->pc, len) != 0) {
75226031Sstas	    printf("punycode %u (%s) wrong contents, "
76226031Sstas		   "actual: \"%.*s\", expected: \"%s\"\n",
77226031Sstas		   i, e->description, (unsigned int)len, buf, e->pc);
78226031Sstas	    ++failures;
79226031Sstas	    continue;
80226031Sstas	}
81226031Sstas    }
82226031Sstas    return failures != 0;
83226031Sstas}
84