1 // Copyright (C) 2020 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <util/encode/utf8.h>
10 
11 #include <gtest/gtest.h>
12 
13 using namespace isc::util;
14 using namespace isc::util::encode;
15 using namespace std;
16 
17 namespace {
18 
19 // Verify it does nothing for ASCII.
TEST(Utf8Test,foobar)20 TEST(Utf8Test, foobar) {
21     string str("foobar");
22     vector<uint8_t> vec8 = encodeUtf8(str);
23     ASSERT_FALSE(vec8.empty());
24     const char* start = reinterpret_cast<const char*>(&vec8[0]);
25     string str8(start, start + vec8.size());
26     EXPECT_EQ(str, str8);
27 }
28 
29 // Verify it encodes not ASCII as expected.
TEST(Utf8Test,eightc)30 TEST(Utf8Test, eightc) {
31     string str("-\x8c-");
32     vector<uint8_t> vec8 = encodeUtf8(str);
33     ASSERT_FALSE(vec8.empty());
34     const char* start = reinterpret_cast<const char*>(&vec8[0]);
35     string str8(start, start + vec8.size());
36     string expected("-\xc2\x8c-");
37     EXPECT_EQ(expected, str8);
38 }
39 
40 // Verify it handles correctly control characters.
TEST(Utf8Test,control)41 TEST(Utf8Test, control) {
42     string str("fo\x00\n\bar");
43     vector<uint8_t> vec8 = encodeUtf8(str);
44     ASSERT_FALSE(vec8.empty());
45     const char* start = reinterpret_cast<const char*>(&vec8[0]);
46     string str8(start, start + vec8.size());
47     EXPECT_EQ(str, str8);
48 }
49 
50 }
51