void doit (void) { const char *p; /* Test success. */ p = idna_strerror (0); if (strcmp (p, SUCCESS) != 0) fail ("idna_strerror (0) failed: %s\n", p); if (debug) printf ("idna_strerror (0) OK\n"); p = pr29_strerror (0); if (strcmp (p, SUCCESS) != 0) fail ("pr29_strerror (0) failed: %s\n", p); if (debug) printf ("pr29_strerror (0) OK\n"); p = punycode_strerror (0); if (strcmp (p, SUCCESS) != 0) fail ("punycode_strerror (0) failed: %s\n", p); if (debug) printf ("punycode_strerror (0) OK\n"); p = stringprep_strerror (0); if (strcmp (p, SUCCESS) != 0) fail ("stringprep_strerror (0) failed: %s\n", p); if (debug) printf ("stringprep_strerror (0) OK\n"); p = tld_strerror (0); if (strcmp (p, SUCCESS) != 0) fail ("tld_strerror (0) failed: %s\n", p); if (debug) printf ("tld_strerror (0) OK\n"); /* Test unknown error. */ p = idna_strerror (42); if (strcmp (p, UNKNOWN) != 0) fail ("idna_strerror (42) failed: %s\n", p); if (debug) printf ("idna_strerror (42) OK\n"); p = pr29_strerror (42); if (strcmp (p, UNKNOWN) != 0) fail ("pr29_strerror (42) failed: %s\n", p); if (debug) printf ("pr29_strerror (42) OK\n"); p = punycode_strerror (42); if (strcmp (p, UNKNOWN) != 0) fail ("punycode_strerror (42) failed: %s\n", p); if (debug) printf ("punycode_strerror (42) OK\n"); p = stringprep_strerror (42); if (strcmp (p, UNKNOWN) != 0) fail ("stringprep_strerror (42) failed: %s\n", p); if (debug) printf ("stringprep_strerror (42) OK\n"); p = tld_strerror (42); if (strcmp (p, UNKNOWN) != 0) fail ("tld_strerror (42) failed: %s\n", p); if (debug) printf ("tld_strerror (42) OK\n"); /* Iterate through all error codes. */ { size_t i; const char *last_p = NULL; for (i = 0;; i++) { p = idna_strerror (i); if (p == last_p) { if (i == 11) { i = 200; continue; } break; } if (debug) printf ("idna %ld: %s\n", i, p); last_p = p; } } { size_t i; const char *last_p = NULL; for (i = 0;; i++) { p = pr29_strerror (i); if (p == last_p) break; if (debug) printf ("pr29 %ld: %s\n", i, p); last_p = p; } } { size_t i; const char *last_p = NULL; for (i = 0;; i++) { p = punycode_strerror (i); if (p == last_p) break; if (debug) printf ("punycode %ld: %s\n", i, p); last_p = p; } } { size_t i; const char *last_p = NULL; for (i = 0;; i++) { p = stringprep_strerror (i); if (p == last_p) { if (i == 7) { i = 99; continue; } else if (i == 105) { i = 199; continue; } break; } if (debug) printf ("stringprep %ld: %s\n", i, p); last_p = p; } } { size_t i; const char *last_p = NULL; for (i = 0;; i++) { p = tld_strerror (i); if (p == last_p) break; if (debug) printf ("tld %ld: %s\n", i, p); last_p = p; } } }
int main (int argc, char *argv[]) { char buf[BUFSIZ]; char *p; uint32_t *r; int rc; size_t errpos, i; printf ("Input domain encoded as `%s': ", stringprep_locale_charset ()); fflush (stdout); fgets (buf, BUFSIZ, stdin); buf[strlen (buf) - 1] = '\0'; printf ("Read string (length %d): ", strlen (buf)); for (i = 0; i < strlen (buf); i++) printf ("%02x ", buf[i] & 0xFF); printf ("\n"); p = stringprep_locale_to_utf8 (buf); if (p) { strcpy (buf, p); free (p); } else printf ("Could not convert string to UTF-8, continuing anyway...\n"); rc = idna_to_ascii_8z (buf, &p, 0); if (rc != IDNA_SUCCESS) { printf ("idna_to_ascii_8z failed (%d): %s\n", rc, idna_strerror (rc)); return 2; } printf ("ToASCII string (length %d): %s\n", strlen (p), p); rc = idna_to_unicode_8z4z (p, &r, 0); free (p); if (rc != IDNA_SUCCESS) { printf ("idna_to_unicode_8z4z failed (%d): %s\n", rc, idna_strerror (rc)); return 2; } printf ("ToUnicode string: "); for (i = 0; r[i]; i++) printf ("U+%04x ", r[i]); printf ("\n"); rc = tld_check_4z (r, &errpos, NULL); free (r); if (rc == TLD_INVALID) { printf ("Domain rejected by TLD check, Unicode position %d\n", errpos); return 1; } else if (rc != TLD_SUCCESS) { printf ("tld_check_4z() failed (%d): %s\n", rc, tld_strerror (rc)); return 2; } printf ("Domain accepted by TLD check\n"); return 0; }