File: test-compat-punycode.c

package info (click to toggle)
libidn2 2.3.8-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,768 kB
  • sloc: ansic: 6,049; sh: 1,480; makefile: 499; xml: 50; perl: 15
file content (95 lines) | stat: -rw-r--r-- 2,684 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* test-compat-punycode.c --- Self test Libidn2 punycode internal symbols.
   Copyright (C) 2002-2025 Simon Josefsson

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#include <idn2.h>

extern int _IDN2_API _idn2_punycode_decode (size_t input_length,
					    const char input[],
					    size_t *output_length,
					    uint32_t output[]);
extern int _IDN2_API _idn2_punycode_encode (size_t input_length,
					    const uint32_t input[],
					    size_t *output_length,
					    char output[]);

int
main (void)
{
  int rc;
  const uint32_t ucs4_input[] = { 0x00DF, 0x00E5 };	/* ßå */
  size_t output_length = 2;
  uint32_t ucs4_output[2];
  char ascii[4];

  output_length = 4;
  rc = _idn2_punycode_encode (2, ucs4_input, &output_length, ascii);
  if (rc != IDN2_OK)
    {
      printf ("FAIL: encode: %d: %s: %s\n",
	      rc, idn2_strerror_name (rc), idn2_strerror (rc));
      return EXIT_FAILURE;
    }

  if (output_length != 4)
    {
      printf ("FAIL: encode 4 != %lu\n", (unsigned long) output_length);
      return EXIT_FAILURE;
    }

  if (memcmp (ascii, "zcam", 4) != 0)
    {
      printf ("FAIL: encode: memcmp: %s\n", ascii);
      return EXIT_FAILURE;
    }

  printf ("PASS: ßå => zcam\n");

  rc = _idn2_punycode_decode (4, ascii, &output_length, ucs4_output);
  if (rc != IDN2_OK)
    {
      printf ("FAIL: decode: %d: %s: %s\n",
	      rc, idn2_strerror_name (rc), idn2_strerror (rc));
      return EXIT_FAILURE;
    }

  if (output_length != 2)
    {
      printf ("FAIL: decode 2 != %lu\n", (unsigned long) output_length);
      return EXIT_FAILURE;
    }

  if (memcmp (ucs4_output, ucs4_input, 2 * sizeof (uint32_t)) != 0)
    {
      printf ("FAIL: decode: memcmp: %s\n", ascii);
      printf ("ucs4_output[0] = %lX\n", (unsigned long) ucs4_output[0]);
      printf ("ucs4_output[1] = %lX\n", (unsigned long) ucs4_output[1]);
      return EXIT_FAILURE;
    }

  printf ("PASS: zcam => ßå\n");

  return EXIT_SUCCESS;
}