aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/string_utils/base32/ut/base32_ut.cpp
blob: fdd2be41b1b94c13321e40cee6582ef0b8876829 (plain) (blame)
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
#include <library/cpp/string_utils/base32/base32.h>

#include <library/cpp/testing/gtest/gtest.h>

#include <util/generic/yexception.h>

#include <vector>

namespace {

    static const std::vector<std::string> TEST_DATA = {
        {},
        "\x00",
        "\x01\x02",
        "\x01\x02\x03",
        "\x03\x02\x01",
        "\x10\x20\x30\x40",
        "\x10\x20\x30\x40\x50",
        "\xFF\xFF\xFF",
        "\xFF\xFE\xFD\xFC\xFB\xFA",
    };

} // namespace

TEST(base32, encode)
{
    EXPECT_EQ(Base32Encode({}), "");
    EXPECT_EQ(Base32Encode({"\x00\x40", 2}), "ABAA====");
    EXPECT_EQ(Base32Encode("apple"), "MFYHA3DF");
    EXPECT_EQ(Base32Encode("TestTest"), "KRSXG5CUMVZXI===");
    EXPECT_EQ(Base32Encode("1234567890"), "GEZDGNBVGY3TQOJQ");
}

TEST(base32, decode_strict)
{
    EXPECT_EQ(Base32StrictDecode(""), "");
    EXPECT_EQ(Base32StrictDecode("MFYHA3DF"), "apple");

    EXPECT_EQ(Base32StrictDecode("KRSXG5CUMVZXI"), "TestTest");
    EXPECT_EQ(Base32StrictDecode("KRSXG5CUMVZXI==="), "TestTest");
    EXPECT_THROW(Base32StrictDecode("KRSXG5CUMVZXI=======A"), yexception);

    EXPECT_EQ(Base32StrictDecode("AA======"), std::string(1, '\x00'));
    EXPECT_THROW(Base32StrictDecode("AAA="), yexception);

    EXPECT_EQ(Base32StrictDecode("AE======"), "\x01");
    EXPECT_THROW(Base32StrictDecode("AB======"), yexception); // "\x00\x40"

    EXPECT_THROW(Base32StrictDecode("invalid"), yexception);
    EXPECT_THROW(Base32StrictDecode("\xFF\xFF"), yexception);
    EXPECT_THROW(Base32StrictDecode(std::string_view{"A\0", 2}), yexception);
}

TEST(base32, decode)
{
    EXPECT_EQ(Base32Decode(""), "");
    EXPECT_EQ(Base32Decode("MFYHA3DF"), "apple");

    EXPECT_EQ(Base32Decode("KRSXG5CUMVZXI"), "TestTest");
    EXPECT_EQ(Base32Decode("KRSXG5CUMVZXI==="), "TestTest");
    EXPECT_NO_THROW(Base32Decode("KRSXG5CUMVZXI=======A"));

    EXPECT_EQ(Base32Decode("AA======"), std::string(1, '\x00'));
    EXPECT_NO_THROW(Base32Decode("AAA="));

    EXPECT_EQ(Base32Decode("AE======"), "\x01");
    EXPECT_NO_THROW(Base32Decode("AB======")); // "\x00\x40"

    EXPECT_NO_THROW(Base32Decode("invalid"));
    EXPECT_NO_THROW(Base32Decode("\xFF\xFF"));
    EXPECT_NO_THROW(Base32Decode(std::string_view{"A\0", 2}));
}

TEST(base32, encode_decode)
{
    for (const auto& data : TEST_DATA) {
        EXPECT_THAT(Base32StrictDecode(Base32Encode(data)), ::testing::ContainerEq(data));
    }
}