blob: fc8b90e43b77b54b0f124852b2b91331014f6bc0 [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
60TEST(Utility, GetDateTime)
61{
62 // some time before the epoch
63 EXPECT_EQ(crow::utility::getDateTimeStdtime(std::time_t{-1234567}),
64 "1969-12-17T17:03:53Z");
65 // epoch
66 EXPECT_EQ(crow::utility::getDateTimeStdtime(std::time_t{0}),
67 "1970-01-01T00:00:00Z");
68 // some time in the past after the epoch
69 EXPECT_EQ(crow::utility::getDateTimeUint(uint64_t{1638312095}),
70 "2021-11-30T22:41:35Z");
71 // some time in the future, beyond 2038
72 EXPECT_EQ(crow::utility::getDateTimeUint(uint64_t{41638312095}),
73 "3289-06-18T21:48:15Z");
74 // the maximum time we support
75 EXPECT_EQ(crow::utility::getDateTimeUint(uint64_t{253402300799}),
76 "9999-12-31T23:59:59Z");
77}