1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "contrib/libidn2/libidn2_sapi.h"
16
17 #include <optional>
18
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include "sandboxed_api/testing.h"
22 #include "sandboxed_api/util/status_matchers.h"
23
24 using ::sapi::IsOk;
25 using ::testing::Not;
26 using ::testing::StrEq;
27
28 class Idn2SapiSandboxTest : public testing::Test {
29 protected:
SetUpTestSuite()30 static void SetUpTestSuite() {
31 sandbox_ = new Idn2SapiSandbox();
32 ASSERT_THAT(sandbox_->Init(), IsOk());
33 lib_ = new IDN2Lib(sandbox_);
34 }
TearDownTestSuite()35 static void TearDownTestSuite() {
36 delete lib_;
37 delete sandbox_;
38 }
39 static IDN2Lib* lib_;
40
41 private:
42 static Idn2SapiSandbox* sandbox_;
43 };
44
45 IDN2Lib* Idn2SapiSandboxTest::lib_;
46 Idn2SapiSandbox* Idn2SapiSandboxTest::sandbox_;
47
TEST_F(Idn2SapiSandboxTest,WorksOkay)48 TEST_F(Idn2SapiSandboxTest, WorksOkay) {
49 EXPECT_THAT(lib_->idn2_lookup_u8("β").value(), StrEq("xn--nxa"));
50 EXPECT_THAT(lib_->idn2_lookup_u8("ß").value(), StrEq("xn--zca"));
51 EXPECT_THAT(lib_->idn2_lookup_u8("straße.de").value(),
52 StrEq("xn--strae-oqa.de"));
53 EXPECT_THAT(lib_->idn2_to_unicode_8z8z("xn--strae-oqa.de").value(),
54 StrEq("straße.de"));
55 EXPECT_THAT(lib_->idn2_lookup_u8("--- "), Not(IsOk()));
56 }
57
TEST_F(Idn2SapiSandboxTest,RegisterConversion)58 TEST_F(Idn2SapiSandboxTest, RegisterConversion) {
59 // I could not get this to succeed except on ASCII-only strings
60 EXPECT_THAT(lib_->idn2_register_u8("βgr", "xn--gr-e9b").value(),
61 StrEq("xn--gr-e9b"));
62 EXPECT_THAT(lib_->idn2_register_u8("βgr", "xn--gr-e9"), Not(IsOk()));
63 EXPECT_THAT(lib_->idn2_register_u8("β.gr", nullptr), Not(IsOk()));
64 }
65