scclib
Stable Cloud Computing C++ Library
base64.cc
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <gtest/gtest.h>
32 #include <stdint.h>
33 #include <iostream>
34 #include <string>
35 #include <vector>
36 #include <encode/base64.h>
37 
45 using std::string;
46 using std::cout;
47 using std::endl;
49 
50 static string b64_allchar_enc =
51 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4"
52 "OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx"
53 "cnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmq"
54 "q6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj"
55 "5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==";
56 
57 static string b64_test1 = "This is a test with some space";
58 static string b64_test1_enc = "VGhpcyBpcyBhIHRlc3QgICAgIHdpdGggc29tZSBzcGFjZQ==";
59 static string b64_test2 = " ";
60 static string b64_test2_enc = "ICAgICA=";
61 static string b64_test3 = "hit";
62 static string b64_test3_enc = "aGl0";
63 
64 TEST(base64, zero)
65 {
66  string z;
67  ASSERT_EQ(Base64::str_to_base64(z).size(), 0);
68  ASSERT_EQ(Base64::base64_to_str(z).size(), 0);
69 }
70 
71 TEST(base64, teststrings)
72 {
73  ASSERT_EQ(Base64::str_to_base64(b64_test1), b64_test1_enc);
74  ASSERT_EQ(Base64::base64_to_str(b64_test1_enc), b64_test1);
75  ASSERT_EQ(Base64::str_to_base64(b64_test2), b64_test2_enc);
76  ASSERT_EQ(Base64::base64_to_str(b64_test2_enc), b64_test2);
77  ASSERT_EQ(Base64::str_to_base64(b64_test3), b64_test3_enc);
78  ASSERT_EQ(Base64::base64_to_str(b64_test3_enc), b64_test3);
79 }
80 
81 TEST(base64, allchars)
82 {
83  std::vector<char> allvect;
84  std::string allstr;
85 
86  for (int i = 0; i < 256; i++)
87  {
88  allvect.push_back(i);
89  allstr.push_back(i);
90  }
91 
92  std::string s;
93  Base64::base64_encode(allvect, s);
94  cout << "allvect encoded: " << s << " size: " << s.size() << endl;
95  ASSERT_EQ(s, b64_allchar_enc);
96 
97  s = Base64::str_to_base64(allstr);
98  cout << "allstr encoded : " << s << " size: " << s.size() << endl;
99  ASSERT_EQ(s, b64_allchar_enc);
100 
101  s = Base64::base64_to_str(b64_allchar_enc);
102  ASSERT_EQ(s, allstr);
103 
104  std::vector<char> v;
105  Base64::base64_decode(b64_allchar_enc, v);
106  ASSERT_EQ(v, allvect);
107 }
108 
109 TEST(base64, teststrings_url)
110 {
111 
112  auto urltest = [] (const string& encs)
113  {
114  return encs.find_first_of("+/=") == string::npos;
115  };
116 
117  auto urlall= Base64::base64_to_base64url(b64_allchar_enc);
118  cout << "all char base64:" << endl << b64_allchar_enc << endl;
119  cout << "all char base64url:" << endl << urlall << endl;
120  ASSERT_TRUE(urltest(urlall));
121  auto baseall = Base64::base64url_to_base64(urlall);
122  ASSERT_EQ(baseall, b64_allchar_enc);
123 
124  auto url1 = Base64::base64_to_base64url(b64_test1_enc);
125  ASSERT_TRUE(urltest(url1));
126  auto base1 = Base64::base64url_to_base64(url1);
127  ASSERT_EQ(base1, b64_test1_enc);
128 
129  auto url2 = Base64::base64_to_base64url(b64_test2_enc);
130  ASSERT_TRUE(urltest(url2));
131  auto base2 = Base64::base64url_to_base64(url2);
132  ASSERT_EQ(base2, b64_test2_enc);
133 
134  auto url3 = Base64::base64_to_base64url(b64_test3_enc);
135  ASSERT_TRUE(urltest(url3));
136  auto base3 = Base64::base64url_to_base64(url3);
137  ASSERT_EQ(base3, b64_test3_enc);
138 }
TEST(inet_example, client_server_stream_test)
[Inet client server]
Definition: inet.cc:521