blob: 4748c25712b7bcaad64845d47fa41e54d05e5af4 [file] [log] [blame]
Ed Tanous04e438c2020-10-03 08:06:26 -07001#include "utility.hpp"
Jonathan Doman5beaf842020-08-14 11:23:33 -07002
3#include "gmock/gmock.h"
4
5TEST(Utility, Base64DecodeAuthString)
6{
7 std::string authString("dXNlcm40bWU6cGFzc3cwcmQ=");
8 std::string result;
9 EXPECT_TRUE(crow::utility::base64Decode(authString, result));
10 EXPECT_EQ(result, "usern4me:passw0rd");
11}
12
13TEST(Utility, Base64DecodeNonAscii)
14{
15 std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33");
16 std::string result;
17 EXPECT_FALSE(crow::utility::base64Decode(junkString, result));
18}
Adriana Kobylakd830ff52021-01-27 14:15:27 -060019
20TEST(Utility, Base64EncodeString)
21{
22 using namespace std::string_literals;
23 std::string encoded;
24
25 encoded = crow::utility::base64encode("");
26 EXPECT_EQ(encoded, "");
27
28 encoded = crow::utility::base64encode("f");
29 EXPECT_EQ(encoded, "Zg==");
30
31 encoded = crow::utility::base64encode("f0");
32 EXPECT_EQ(encoded, "ZjA=");
33
34 encoded = crow::utility::base64encode("f0\0"s);
35 EXPECT_EQ(encoded, "ZjAA");
36
37 encoded = crow::utility::base64encode("f0\0 "s);
38 EXPECT_EQ(encoded, "ZjAAIA==");
39
40 encoded = crow::utility::base64encode("f0\0 B"s);
41 EXPECT_EQ(encoded, "ZjAAIEI=");
42
43 encoded = crow::utility::base64encode("f0\0 Ba"s);
44 EXPECT_EQ(encoded, "ZjAAIEJh");
45
46 encoded = crow::utility::base64encode("f0\0 Bar"s);
47 EXPECT_EQ(encoded, "ZjAAIEJhcg==");
48}
49
50TEST(Utility, Base64EncodeDecodeString)
51{
52 using namespace std::string_literals;
53 std::string data("Data fr\0m 90 reading a \nFile"s);
54 std::string encoded = crow::utility::base64encode(data);
55 std::string decoded;
56 EXPECT_TRUE(crow::utility::base64Decode(encoded, decoded));
57 EXPECT_EQ(data, decoded);
58}
Nan Zhou1d8782e2021-11-29 22:23:18 -080059
Ed Tanous8d4c4872022-01-11 10:50:23 -080060TEST(Utility, GetDateTimeStdtime)
Nan Zhou1d8782e2021-11-29 22:23:18 -080061{
Ed Tanous8d4c4872022-01-11 10:50:23 -080062 using crow::utility::getDateTimeStdtime;
63
Nan Zhou1d8782e2021-11-29 22:23:18 -080064 // some time before the epoch
Ed Tanous8d4c4872022-01-11 10:50:23 -080065 EXPECT_EQ(getDateTimeStdtime(std::time_t{-1234567}),
Nan Zhou5ae4b692021-12-14 13:30:37 -080066 "1969-12-17T17:03:53+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080067
Nan Zhou1d8782e2021-11-29 22:23:18 -080068 // epoch
Ed Tanous8d4c4872022-01-11 10:50:23 -080069 EXPECT_EQ(getDateTimeStdtime(std::time_t{0}), "1970-01-01T00:00:00+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -080070 // some time in the past after the epoch
Ed Tanous8d4c4872022-01-11 10:50:23 -080071
72 // Limits
73 EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::max()),
74 "1969-12-31T23:59:59+00:00");
75 EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::min()),
76 "1970-01-01T00:00:00+00:00");
77}
78
79TEST(Utility, getDateTimeUint)
80{
81 using crow::utility::getDateTimeUint;
82
83 EXPECT_EQ(getDateTimeUint(uint64_t{1638312095}),
Nan Zhou5ae4b692021-12-14 13:30:37 -080084 "2021-11-30T22:41:35+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -080085 // some time in the future, beyond 2038
Ed Tanous8d4c4872022-01-11 10:50:23 -080086 EXPECT_EQ(getDateTimeUint(uint64_t{41638312095}),
Nan Zhou5ae4b692021-12-14 13:30:37 -080087 "3289-06-18T21:48:15+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -080088 // the maximum time we support
Ed Tanous8d4c4872022-01-11 10:50:23 -080089 EXPECT_EQ(getDateTimeUint(uint64_t{253402300799}),
Nan Zhou5ae4b692021-12-14 13:30:37 -080090 "9999-12-31T23:59:59+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080091
92 // This test currently throws an exception
93 // EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::max()), "");
94
95 EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::min()),
96 "1970-01-01T00:00:00+00:00");
97}
98
99TEST(Utility, getDateTimeUintMs)
100{
101 using crow::utility::getDateTimeUintMs;
102
103 // Note, this is the wrong result, but is here to make sure that we don't
104 // have worse behavior (ie seg faults, exceptions) when this happens
105 EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::max()),
106 "1969-12-31T23:59:59.999000+00:00");
107
108 EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::min()),
109 "1970-01-01T00:00:00+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -0800110}