Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
to_unicode_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/to_ascii.h"
10#include "ada/idna/to_unicode.h"
11
12// Global variables for file paths that can be set via environment variables
13std::string GetUnicodeFilename() {
14 const char* env_file = std::getenv("IDNA_UNICODE_FILE");
15 return env_file ? std::string(env_file)
16 : "fixtures/to_unicode_alternating.txt";
17}
18
19class IdnaToUnicodeTest : 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// Labels whose punycode decodes to pure ASCII (e.g. "xn--abc-" -> "abc") must
47// NOT be emitted as the decoded form — they should stay in their original
48// xn-- form. Per UTS #46, the decoded label must round-trip through mapping,
49// normalization, and validity; an ASCII-only decoding implies a malformed
50// xn-- label.
51TEST(IdnaToUnicodeValidation, PunycodeDecodingToAsciiIsRejected) {
52 EXPECT_EQ(ada::idna::to_unicode("xn--abc-"), "xn--abc-");
53}
54
55// A genuine non-ASCII punycode label should still decode normally.
56TEST(IdnaToUnicodeValidation, ValidPunycodeStillDecodes) {
57 // xn--maana-pta -> mañana
58 EXPECT_EQ(ada::idna::to_unicode("xn--maana-pta"), "mañana");
59}
60
61// Test for Unicode conversions from file
62TEST_F(IdnaToUnicodeTest, AlternatingFileConversions) {
63 std::string filename = GetUnicodeFilename();
64
65 // Skip test if file doesn't exist
66 if (!std::filesystem::exists(filename)) {
67 GTEST_SKIP() << "Test file not found: " << filename;
68 }
69
70 std::string buffer = ReadFile(filename);
71 std::vector<std::string> lines = SplitString(buffer);
72
73 ASSERT_GE(lines.size(), 2) << "File must contain at least 2 lines";
74
75 for (size_t i = 0; i + 1 < lines.size(); i += 2) {
76 const std::string& input = lines[i];
77 const std::string& output = lines[i + 1];
78
79 SCOPED_TRACE("Line " + std::to_string(i) + ": " + input);
80 ASSERT_EQ(ada::idna::to_unicode(input), output);
81 }
82}
83
84TEST_F(IdnaToUnicodeTest, BidiDomainKeepsOriginalWhenAsciiLabelFails) {
85 // "1.xn--mgb" decodes to "1." + U+0627. The digit label violates RFC 5893
86 // rule 1 because the domain is a Bidi domain, so ToUnicode keeps the input.
87 EXPECT_EQ(ada::idna::to_unicode("1.xn--mgb"), "1.xn--mgb");
88 EXPECT_EQ(ada::idna::to_unicode("xn--mgb.1a"), "xn--mgb.1a");
89 // "example" satisfies the LTR Bidi rules, so the RTL label is converted.
90 EXPECT_EQ(ada::idna::to_unicode("example.xn--mgb"), "example.\u0627");
91 EXPECT_EQ(ada::idna::to_unicode("xn--mgb.example"), "\u0627.example");
92}
static std::string ReadFile(const std::string &filename)
static std::vector< std::string > SplitString(const std::string &str)
std::string to_unicode(std::string_view input)
TEST_F(IdnaToUnicodeTest, AlternatingFileConversions)
TEST(IdnaToUnicodeValidation, PunycodeDecodingToAsciiIsRejected)
std::string GetUnicodeFilename()