summaryrefslogtreecommitdiffstats
blob: c5f94d0b78a63ad9d339405bc445b7741edc3629 (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
/*
 * Copyright (c) 2010-2018 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <memory>

#include <gtest/gtest.h>

#include <Swiften/IDN/IDNConverter.h>
#include <Swiften/IDN/PlatformIDNConverter.h>

using namespace Swift;

class IDNConverterTest : public ::testing::Test {

protected:
    virtual void SetUp() {
        testling_ = std::shared_ptr<IDNConverter>(PlatformIDNConverter::create());
    }

    std::shared_ptr<IDNConverter> testling_;
};

TEST_F(IDNConverterTest, testStringPrep) {
    std::string result = testling_->getStringPrepared("tron\xc3\x87on", IDNConverter::NamePrep);

    ASSERT_EQ(std::string("tron\xc3\xa7on"), result);
}

TEST_F(IDNConverterTest, testStringPrep_Empty) {
    ASSERT_EQ(std::string(""), testling_->getStringPrepared("", IDNConverter::NamePrep));
    ASSERT_EQ(std::string(""), testling_->getStringPrepared("", IDNConverter::XMPPNodePrep));
    ASSERT_EQ(std::string(""), testling_->getStringPrepared("", IDNConverter::XMPPResourcePrep));
}

TEST_F(IDNConverterTest, testStringPrep_MaximumOutputSize) {
    const std::string input(1023, 'x');
    ASSERT_EQ(input, testling_->getStringPrepared(input, IDNConverter::NamePrep));
    ASSERT_EQ(input, testling_->getStringPrepared(input, IDNConverter::XMPPNodePrep));
    ASSERT_EQ(input, testling_->getStringPrepared(input, IDNConverter::XMPPResourcePrep));
}

TEST_F(IDNConverterTest, testStringPrep_TooLong) {
    const std::string input(1024, 'x');
    ASSERT_THROW(testling_->getStringPrepared(input, IDNConverter::NamePrep), std::exception);
    ASSERT_THROW(testling_->getStringPrepared(input, IDNConverter::XMPPNodePrep), std::exception);
    ASSERT_THROW(testling_->getStringPrepared(input, IDNConverter::XMPPResourcePrep), std::exception);
}

TEST_F(IDNConverterTest, testStringPrep_ShrinkingBelow1023) {
    std::string input;
    std::string expected;
    // The four byte \u03b1\u0313 UTF-8 string will shrink to the three byte \u1f00
    for (auto i = 0; i < 300; ++i) {
        input +="\xce\xb1\xcc\x93"; // UTF-8 repesentation of U+03B1 U+0313
        expected += "\xe1\xbc\x80"; // UTF-8 representation of U+1F00
    }
    ASSERT_EQ(expected, testling_->getStringPrepared(input, IDNConverter::NamePrep));
    ASSERT_EQ(expected, testling_->getStringPrepared(input, IDNConverter::XMPPNodePrep));
    ASSERT_EQ(expected, testling_->getStringPrepared(input, IDNConverter::XMPPResourcePrep));
}

TEST_F(IDNConverterTest, testGetEncoded) {
    boost::optional<std::string> result = testling_->getIDNAEncoded("www.swift.im");
    ASSERT_TRUE(!!result);
    ASSERT_EQ(std::string("www.swift.im"), *result);
}

TEST_F(IDNConverterTest, testGetEncoded_International) {
    boost::optional<std::string> result = testling_->getIDNAEncoded("www.tron\xc3\x87on.com");
    ASSERT_TRUE(result);
    ASSERT_EQ(std::string("www.xn--tronon-zua.com"), *result);
}

TEST_F(IDNConverterTest, testGetEncoded_Invalid) {
    boost::optional<std::string> result = testling_->getIDNAEncoded("www.foo,bar.com");
    ASSERT_FALSE(result);
}