summaryrefslogtreecommitdiffstats
path: root/intl/components/gtest/TestIDNA.cpp
blob: e1c7a501bc326912aef34da9f54f31e9f8139970 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"

#include "mozilla/intl/IDNA.h"
#include "mozilla/Span.h"
#include "TestBuffer.h"

namespace mozilla::intl {

TEST(IntlIDNA, LabelToUnicodeBasic)
{
  auto createResult = IDNA::TryCreate(IDNA::ProcessingType::NonTransitional);
  ASSERT_TRUE(createResult.isOk());
  auto idna = createResult.unwrap();

  // 'A' and 'ª' are mapped to 'a'.
  TestBuffer<char16_t> buf16;
  auto convertResult = idna->LabelToUnicode(MakeStringSpan(u"Aa\u00aa"), buf16);
  ASSERT_TRUE(convertResult.isOk());
  intl::IDNA::Info info = convertResult.unwrap();
  ASSERT_TRUE(!info.HasErrors());
  ASSERT_EQ(buf16.get_string_view(), u"aaa");

  buf16.clear();
  // For nontransitional processing 'ß' is still mapped to 'ß'.
  convertResult = idna->LabelToUnicode(MakeStringSpan(u"Faß"), buf16);
  ASSERT_TRUE(convertResult.isOk());
  info = convertResult.unwrap();
  ASSERT_TRUE(!info.HasErrors());
  ASSERT_EQ(buf16.get_string_view(), u"faß");
}

TEST(IntlIDNA, LabelToUnicodeBasicTransitional)
{
  auto createResult = IDNA::TryCreate(IDNA::ProcessingType::Transitional);
  ASSERT_TRUE(createResult.isOk());
  auto idna = createResult.unwrap();

  TestBuffer<char16_t> buf16;
  // For transitional processing 'ß' will be mapped to 'ss'.
  auto convertResult = idna->LabelToUnicode(MakeStringSpan(u"Faß"), buf16);
  ASSERT_TRUE(convertResult.isOk());
  intl::IDNA::Info info = convertResult.unwrap();
  ASSERT_TRUE(!info.HasErrors());
  ASSERT_EQ(buf16.get_string_view(), u"fass");
}

TEST(IntlIDNA, LabelToUnicodeHasErrors)
{
  auto createResult = IDNA::TryCreate(IDNA::ProcessingType::NonTransitional);
  ASSERT_TRUE(createResult.isOk());
  auto idna = createResult.unwrap();
  TestBuffer<char16_t> buf16;
  // \u0378 is a reserved charactor, conversion should be disallowed.
  auto convertResult = idna->LabelToUnicode(MakeStringSpan(u"\u0378"), buf16);
  ASSERT_TRUE(convertResult.isOk());
  intl::IDNA::Info info = convertResult.unwrap();
  ASSERT_TRUE(info.HasErrors());

  buf16.clear();
  // FULL STOP '.' is not allowed.
  convertResult = idna->LabelToUnicode(MakeStringSpan(u"a.b"), buf16);
  ASSERT_TRUE(convertResult.isOk());
  info = convertResult.unwrap();
  ASSERT_TRUE(info.HasErrors());
}

TEST(IntlIDNA, LabelToUnicodeHasInvalidPunycode)
{
  auto createResult = IDNA::TryCreate(IDNA::ProcessingType::NonTransitional);
  ASSERT_TRUE(createResult.isOk());
  auto idna = createResult.unwrap();
  TestBuffer<char16_t> buf16;
  auto convertResult =
      idna->LabelToUnicode(MakeStringSpan(u"xn--a-ecp.ru"), buf16);
  ASSERT_TRUE(convertResult.isOk());
  intl::IDNA::Info info = convertResult.unwrap();
  ASSERT_TRUE(info.HasInvalidPunycode());

  buf16.clear();
  convertResult = idna->LabelToUnicode(MakeStringSpan(u"xn--0.pt"), buf16);
  ASSERT_TRUE(convertResult.isOk());
  info = convertResult.unwrap();
  ASSERT_TRUE(info.HasInvalidPunycode());
}

TEST(IntlIDNA, LabelToUnicodeHasInvalidHyphen)
{
  auto createResult = IDNA::TryCreate(IDNA::ProcessingType::NonTransitional);
  ASSERT_TRUE(createResult.isOk());
  auto idna = createResult.unwrap();
  TestBuffer<char16_t> buf16;

  // Leading hyphen.
  auto convertResult = idna->LabelToUnicode(MakeStringSpan(u"-a"), buf16);
  ASSERT_TRUE(convertResult.isOk());
  intl::IDNA::Info info = convertResult.unwrap();
  ASSERT_TRUE(info.HasErrors());
  ASSERT_TRUE(info.HasInvalidHyphen());

  buf16.clear();
  // Trailing hyphen.
  convertResult = idna->LabelToUnicode(MakeStringSpan(u"a-"), buf16);
  ASSERT_TRUE(convertResult.isOk());
  info = convertResult.unwrap();
  ASSERT_TRUE(info.HasInvalidHyphen());

  buf16.clear();
  // Contains hyphens in both 3rd and 4th positions.
  convertResult = idna->LabelToUnicode(MakeStringSpan(u"ab--c"), buf16);
  ASSERT_TRUE(convertResult.isOk());
  info = convertResult.unwrap();
  ASSERT_TRUE(info.HasInvalidHyphen());
}

}  // namespace mozilla::intl