Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
punycode_tests.cpp
Go to the documentation of this file.
1#include "gtest/gtest.h"
2#include <filesystem>
3#include <fstream>
4#include <iostream>
5#include <sstream>
6#include <string>
7#include <vector>
8
9#include "ada/idna/punycode.h"
11
12// Global function to get filename from environment or use default
13std::string GetPunycodeFilename() {
14 const char* env_file = std::getenv("IDNA_PUNYCODE_FILE");
15 return env_file ? std::string(env_file)
16 : "fixtures/utf8_punycode_alternating.txt";
17}
18
19class PunycodeTest : public ::testing::Test {
20 protected:
21 // Helper function to read a file
22 static std::string ReadFile(const std::string& filename) {
23 constexpr auto read_size = std::size_t(4096);
24 auto stream = std::ifstream(filename.c_str());
25 stream.exceptions(std::ios_base::badbit);
26 auto out = std::string();
27 auto buf = std::string(read_size, '\0');
28 while (stream.read(&buf[0], read_size)) {
29 out.append(buf, 0, size_t(stream.gcount()));
30 }
31 out.append(buf, 0, size_t(stream.gcount()));
32 return out;
33 }
34
35 // Helper function to split a string by newlines
36 static std::vector<std::string> SplitString(const std::string& str) {
37 auto result = std::vector<std::string>{};
38 auto ss = std::stringstream{str};
39 for (std::string line; std::getline(ss, line, '\n');) {
40 result.push_back(line);
41 }
42 return result;
43 }
44};
45
46// Test for Punycode transcoding from file
47TEST_F(PunycodeTest, AlternatingFileTranscoding) {
48 std::string filename = GetPunycodeFilename();
49
50 // Skip test if file doesn't exist
51 if (!std::filesystem::exists(filename)) {
52 GTEST_SKIP() << "Test file not found: " << filename;
53 return;
54 }
55
56 std::string buffer = ReadFile(filename);
57 std::vector<std::string> lines = SplitString(buffer);
58
59 ASSERT_GE(lines.size(), 2) << "File must contain at least 2 lines";
60
61 for (size_t i = 0; i + 1 < lines.size(); i += 2) {
62 std::string utf8_string = lines[i];
63 std::string puny_string = lines[i + 1];
64
65 SCOPED_TRACE("Line " + std::to_string(i) + ": UTF-8 = '" + utf8_string +
66 "', Punycode = '" + puny_string + "'");
67
68 std::cout << "processing " << puny_string << std::endl;
69
70 // Test UTF-8 <=> UTF-32 roundtrip
71 size_t utf32_length = ada::idna::utf32_length_from_utf8(utf8_string.data(),
72 utf8_string.size());
73 std::u32string utf32(utf32_length, '\0');
74 ada::idna::utf8_to_utf32(utf8_string.data(), utf8_string.size(),
75 utf32.data());
76
77 std::string tmp(utf8_string.size(), '\0');
78 ada::idna::utf32_to_utf8(utf32.data(), utf32_length, tmp.data());
79
80 EXPECT_EQ(tmp, utf8_string) << "bad utf-8 <==> utf-32 transcoding";
81
82 // Test UTF-32 => Punycode
83 std::string puny;
84 bool is_ok1 = ada::idna::utf32_to_punycode(utf32, puny);
85
86 EXPECT_TRUE(is_ok1) << "bad utf-32 => punycode transcoding";
87 EXPECT_EQ(puny, puny_string) << "punycode mismatch";
88
89 // Test Punycode => UTF-32
90 std::u32string utf32back;
91 bool is_ok2 = ada::idna::punycode_to_utf32(puny, utf32back);
92
93 EXPECT_TRUE(is_ok2) << "bad punycode => utf-32 transcoding";
94
95 // Test roundtrip Punycode
96 std::string punyback;
97 bool is_ok3 = ada::idna::utf32_to_punycode(utf32back, punyback);
98
99 EXPECT_TRUE(is_ok3) << "bad utf-32 => punycode transcoding (second time)";
100 EXPECT_EQ(punyback, puny)
101 << "bad punycode => utf-32 => punycode transcoding";
102
103 // Test full roundtrip back to UTF-8
104 size_t utf8_length =
105 ada::idna::utf8_length_from_utf32(utf32back.data(), utf32back.size());
106 std::string finalutf8(utf8_length, '\0');
107 ada::idna::utf32_to_utf8(utf32back.data(), utf32back.size(),
108 finalutf8.data());
109
110 EXPECT_EQ(finalutf8, utf8_string)
111 << "bad roundtrip utf8 => utf8 transcoding";
112 }
113}
114
115TEST(punycode_tests, leading_delimiter_is_invalid) {
116 // RFC 3492 section 6.2 / appendix C: the delimiter is only consumed if at
117 // least one basic code point precedes it. A leading "-" therefore starts
118 // the encoded part, is not a digit, and the input is invalid.
119 std::u32string out;
120 EXPECT_FALSE(ada::idna::punycode_to_utf32("-dddddddd", out));
121 EXPECT_FALSE(ada::idna::punycode_to_utf32("-a", out));
122 EXPECT_FALSE(ada::idna::punycode_to_utf32("-", out));
123 // Without the leading delimiter these are valid.
124 out.clear();
125 EXPECT_TRUE(ada::idna::punycode_to_utf32("dddddddd", out));
126 EXPECT_EQ(out, std::u32string(U"\u0F4C\u0F49\u0F4B\u0F4A\u0F4C\u0F47"));
127 out.clear();
128 EXPECT_TRUE(ada::idna::punycode_to_utf32("a", out));
129 EXPECT_EQ(out, std::u32string(U"\u0080"));
130 // Basic code points, then delimiter, then encoded part.
131 out.clear();
132 EXPECT_TRUE(ada::idna::punycode_to_utf32("-q----jra", out));
133 EXPECT_EQ(out, std::u32string(U"-q--\u00E4-"));
134}
static std::vector< std::string > SplitString(const std::string &str)
static std::string ReadFile(const std::string &filename)
bool punycode_to_utf32(std::string_view input, std::u32string &out)
size_t utf32_length_from_utf8(const char *buf, size_t len)
Definition ada_idna.cpp:142
size_t utf32_to_utf8(const char32_t *buf, size_t len, char *utf8_output)
Definition ada_idna.cpp:151
bool utf32_to_punycode(std::u32string_view input, std::string &out)
size_t utf8_length_from_utf32(const char32_t *buf, size_t len)
Definition ada_idna.cpp:129
size_t utf8_to_utf32(const char *buf, size_t len, char32_t *utf32_output)
Definition ada_idna.cpp:32
TEST_F(PunycodeTest, AlternatingFileTranscoding)
std::string GetPunycodeFilename()
TEST(punycode_tests, leading_delimiter_is_invalid)