summaryrefslogtreecommitdiff
path: root/http/ut/utility_test.cpp
blob: fc8b90e43b77b54b0f124852b2b91331014f6bc0 (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
#include "utility.hpp"

#include "gmock/gmock.h"

TEST(Utility, Base64DecodeAuthString)
{
    std::string authString("dXNlcm40bWU6cGFzc3cwcmQ=");
    std::string result;
    EXPECT_TRUE(crow::utility::base64Decode(authString, result));
    EXPECT_EQ(result, "usern4me:passw0rd");
}

TEST(Utility, Base64DecodeNonAscii)
{
    std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33");
    std::string result;
    EXPECT_FALSE(crow::utility::base64Decode(junkString, result));
}

TEST(Utility, Base64EncodeString)
{
    using namespace std::string_literals;
    std::string encoded;

    encoded = crow::utility::base64encode("");
    EXPECT_EQ(encoded, "");

    encoded = crow::utility::base64encode("f");
    EXPECT_EQ(encoded, "Zg==");

    encoded = crow::utility::base64encode("f0");
    EXPECT_EQ(encoded, "ZjA=");

    encoded = crow::utility::base64encode("f0\0"s);
    EXPECT_EQ(encoded, "ZjAA");

    encoded = crow::utility::base64encode("f0\0 "s);
    EXPECT_EQ(encoded, "ZjAAIA==");

    encoded = crow::utility::base64encode("f0\0 B"s);
    EXPECT_EQ(encoded, "ZjAAIEI=");

    encoded = crow::utility::base64encode("f0\0 Ba"s);
    EXPECT_EQ(encoded, "ZjAAIEJh");

    encoded = crow::utility::base64encode("f0\0 Bar"s);
    EXPECT_EQ(encoded, "ZjAAIEJhcg==");
}

TEST(Utility, Base64EncodeDecodeString)
{
    using namespace std::string_literals;
    std::string data("Data fr\0m 90 reading a \nFile"s);
    std::string encoded = crow::utility::base64encode(data);
    std::string decoded;
    EXPECT_TRUE(crow::utility::base64Decode(encoded, decoded));
    EXPECT_EQ(data, decoded);
}

TEST(Utility, GetDateTime)
{
    // some time before the epoch
    EXPECT_EQ(crow::utility::getDateTimeStdtime(std::time_t{-1234567}),
              "1969-12-17T17:03:53Z");
    // epoch
    EXPECT_EQ(crow::utility::getDateTimeStdtime(std::time_t{0}),
              "1970-01-01T00:00:00Z");
    // some time in the past after the epoch
    EXPECT_EQ(crow::utility::getDateTimeUint(uint64_t{1638312095}),
              "2021-11-30T22:41:35Z");
    // some time in the future, beyond 2038
    EXPECT_EQ(crow::utility::getDateTimeUint(uint64_t{41638312095}),
              "3289-06-18T21:48:15Z");
    // the maximum time we support
    EXPECT_EQ(crow::utility::getDateTimeUint(uint64_t{253402300799}),
              "9999-12-31T23:59:59Z");
}