blob: 5dafdce4467d0b74525b68c7b2b6d9e52adb1e4e [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
Ed Tanous22ce5452022-01-11 10:50:23 -08005namespace crow::utility
6{
7namespace
8{
9
Jonathan Doman5beaf842020-08-14 11:23:33 -070010TEST(Utility, Base64DecodeAuthString)
11{
12 std::string authString("dXNlcm40bWU6cGFzc3cwcmQ=");
13 std::string result;
Ed Tanous22ce5452022-01-11 10:50:23 -080014 EXPECT_TRUE(base64Decode(authString, result));
Jonathan Doman5beaf842020-08-14 11:23:33 -070015 EXPECT_EQ(result, "usern4me:passw0rd");
16}
17
18TEST(Utility, Base64DecodeNonAscii)
19{
20 std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33");
21 std::string result;
Ed Tanous22ce5452022-01-11 10:50:23 -080022 EXPECT_FALSE(base64Decode(junkString, result));
Jonathan Doman5beaf842020-08-14 11:23:33 -070023}
Adriana Kobylakd830ff52021-01-27 14:15:27 -060024
25TEST(Utility, Base64EncodeString)
26{
27 using namespace std::string_literals;
28 std::string encoded;
29
Ed Tanous22ce5452022-01-11 10:50:23 -080030 encoded = base64encode("");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060031 EXPECT_EQ(encoded, "");
32
Ed Tanous22ce5452022-01-11 10:50:23 -080033 encoded = base64encode("f");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060034 EXPECT_EQ(encoded, "Zg==");
35
Ed Tanous22ce5452022-01-11 10:50:23 -080036 encoded = base64encode("f0");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060037 EXPECT_EQ(encoded, "ZjA=");
38
Ed Tanous22ce5452022-01-11 10:50:23 -080039 encoded = base64encode("f0\0"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060040 EXPECT_EQ(encoded, "ZjAA");
41
Ed Tanous22ce5452022-01-11 10:50:23 -080042 encoded = base64encode("f0\0 "s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060043 EXPECT_EQ(encoded, "ZjAAIA==");
44
Ed Tanous22ce5452022-01-11 10:50:23 -080045 encoded = base64encode("f0\0 B"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060046 EXPECT_EQ(encoded, "ZjAAIEI=");
47
Ed Tanous22ce5452022-01-11 10:50:23 -080048 encoded = base64encode("f0\0 Ba"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060049 EXPECT_EQ(encoded, "ZjAAIEJh");
50
Ed Tanous22ce5452022-01-11 10:50:23 -080051 encoded = base64encode("f0\0 Bar"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060052 EXPECT_EQ(encoded, "ZjAAIEJhcg==");
53}
54
55TEST(Utility, Base64EncodeDecodeString)
56{
57 using namespace std::string_literals;
58 std::string data("Data fr\0m 90 reading a \nFile"s);
Ed Tanous22ce5452022-01-11 10:50:23 -080059 std::string encoded = base64encode(data);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060060 std::string decoded;
Ed Tanous22ce5452022-01-11 10:50:23 -080061 EXPECT_TRUE(base64Decode(encoded, decoded));
Adriana Kobylakd830ff52021-01-27 14:15:27 -060062 EXPECT_EQ(data, decoded);
63}
Nan Zhou1d8782e2021-11-29 22:23:18 -080064
Ed Tanous8d4c4872022-01-11 10:50:23 -080065TEST(Utility, GetDateTimeStdtime)
Nan Zhou1d8782e2021-11-29 22:23:18 -080066{
Ed Tanous8d4c4872022-01-11 10:50:23 -080067 using crow::utility::getDateTimeStdtime;
68
Nan Zhou1d8782e2021-11-29 22:23:18 -080069 // some time before the epoch
Ed Tanous8d4c4872022-01-11 10:50:23 -080070 EXPECT_EQ(getDateTimeStdtime(std::time_t{-1234567}),
Nan Zhou5ae4b692021-12-14 13:30:37 -080071 "1969-12-17T17:03:53+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080072
Nan Zhou1d8782e2021-11-29 22:23:18 -080073 // epoch
Ed Tanous8d4c4872022-01-11 10:50:23 -080074 EXPECT_EQ(getDateTimeStdtime(std::time_t{0}), "1970-01-01T00:00:00+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080075
76 // Limits
77 EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::max()),
Ed Tanous22ce5452022-01-11 10:50:23 -080078 "9999-12-31T23:59:59+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080079 EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::min()),
80 "1970-01-01T00:00:00+00:00");
81}
82
Ed Tanous22ce5452022-01-11 10:50:23 -080083TEST(Utility, GetDateTimeUint)
Ed Tanous8d4c4872022-01-11 10:50:23 -080084{
Ed Tanous8d4c4872022-01-11 10:50:23 -080085 EXPECT_EQ(getDateTimeUint(uint64_t{1638312095}),
Nan Zhou5ae4b692021-12-14 13:30:37 -080086 "2021-11-30T22:41:35+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -080087 // some time in the future, beyond 2038
Ed Tanous8d4c4872022-01-11 10:50:23 -080088 EXPECT_EQ(getDateTimeUint(uint64_t{41638312095}),
Nan Zhou5ae4b692021-12-14 13:30:37 -080089 "3289-06-18T21:48:15+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -080090 // the maximum time we support
Ed Tanous8d4c4872022-01-11 10:50:23 -080091 EXPECT_EQ(getDateTimeUint(uint64_t{253402300799}),
Nan Zhou5ae4b692021-12-14 13:30:37 -080092 "9999-12-31T23:59:59+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080093
Ed Tanous22ce5452022-01-11 10:50:23 -080094 // returns the maximum Redfish date
95 EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::max()),
96 "9999-12-31T23:59:59+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080097
98 EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::min()),
99 "1970-01-01T00:00:00+00:00");
100}
101
Ed Tanous22ce5452022-01-11 10:50:23 -0800102TEST(Utility, GetDateTimeUintMs)
Ed Tanous8d4c4872022-01-11 10:50:23 -0800103{
Ed Tanous22ce5452022-01-11 10:50:23 -0800104 // returns the maximum Redfish date
Ed Tanous8d4c4872022-01-11 10:50:23 -0800105 EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::max()),
Ed Tanous22ce5452022-01-11 10:50:23 -0800106 "9999-12-31T23:59:59.999000+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -0800107 EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::min()),
108 "1970-01-01T00:00:00+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -0800109}
Ed Tanous22ce5452022-01-11 10:50:23 -0800110} // namespace
111} // namespace crow::utility