blob: df36564fc226b0a494e6f48ab6daaac8c1223011 [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001// Copyright (C) 2002-2003 Chad C. Yates cyates@uidaho.edu
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation; either version 2 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program; if not, write to the Free Software
15// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16//
17// As a special exception to the GNU General Public License, permission is
18// granted for additional uses of the text contained in its release
19// of Common C++.
20//
21// The exception is that, if you link the Common C++ library with other
22// files to produce an executable, this does not by itself cause the
23// resulting executable to be covered by the GNU General Public License.
24// Your use of that executable is in no way restricted on account of
25// linking the Common C++ library code into it.
26//
27// This exception does not however invalidate any other reasons why
28// the executable file might be covered by the GNU General Public License.
29//
30// This exception applies only to the code released under the
31// name Common C++. If you copy code from other releases into a copy of
32// Common C++, as the General Public License permits, the exception does
33// not apply to the code that you add in this way. To avoid misleading
34// anyone as to the status of such modified files, you must delete
35// this exception notice from them.
36//
37// If you write modifications of your own for Common C++, it is your choice
38// whether to permit this exception to apply to your modifications.
39// If you do not wish that, delete this exception notice.
40
41#include <cppunit/extensions/HelperMacros.h>
42#include <iostream>
43#include <cc++/url.h>
44
45using namespace ost;
46using std::string;
47
48/**
49 * Test Fixture to excercise the Common C++ urlstring functions
50 *
51 * @author Chad C. Yates
52 */
53class URLStringTest : public CppUnit::TestFixture
54{
55 CPPUNIT_TEST_SUITE(URLStringTest);
56 CPPUNIT_TEST(testBinaryBase64EncodeDecode);
57 CPPUNIT_TEST(testStringVersion);
58 CPPUNIT_TEST(testTypicalUrlEncodeDecode);
59 CPPUNIT_TEST(testTypicalTextBase64EncodeDecode);
60 CPPUNIT_TEST(testSimpleTextBase64EncodeDecode);
61 CPPUNIT_TEST(testComplexTextBase64EncodeDecode);
62 CPPUNIT_TEST(testPaddingPerfect);
63 CPPUNIT_TEST(testPadding1Short);
64 CPPUNIT_TEST(testPadding2Short);
65 CPPUNIT_TEST_SUITE_END();
66
67private:
68 /*
69 static const char inputChars1[] = "`1234567890-=\\QWERTYUIOP[]ASDFGHJKL;'ZXCVBNM,./~!@#$%^&*()_+|qwertyuiop{}asdfghjkl:\"zxcvbnm<>?";
70 static const expectedOutputChars1[1000] = "YDEyMzQ1Njc4OTAtPVxRV0VSVFlVSU9QW11BU0RGR0hKS0w7J1pYQ1ZCTk0sLi9+IUAjJCVeJiooKV8rfHF3ZXJ0eXVpb3B7fWFzZGZnaGprbDoienhjdmJubTw+Pw==";
71 static const char inputChars2[] = "This is a test.";
72 static const char expectedOutputChars2[1000] = "VGhpcyBpcyBhIHRlc3Qu";
73 */
74 char actualEncodedChars[1000]; // should be enough for the tests
75 char actualDecodedChars[1000]; // should be enough for the tests
76
77public:
78 void testTypicalUrlEncodeDecode() {
79 char inputChars[] = "`1234567890-=\\QWERTYUIOP[]ASDFGHJKL;'ZXCVBNM,./~!@#$%^&*()_+|qwertyuiop{}asdfghjkl:\"zxcvbnm<>?";
80 char expectedOutputChars[] = "%601234567890-%3d%5cQWERTYUIOP%5b%5dASDFGHJKL;%27ZXCVBNM,./%7e%21%40%23%24%25%5e%26%2a%28%29%5f%2b%7cqwertyuiop%7b%7dasdfghjkl:%22zxcvbnm%3c%3e%3f";
81
82 urlEncode(inputChars, actualEncodedChars, sizeof(actualEncodedChars));
83 CPPUNIT_ASSERT_EQUAL_MESSAGE("String to url encode was: '" + string(inputChars) + string("'"), string(expectedOutputChars), string(actualEncodedChars));
84
85 urlDecode(actualEncodedChars, actualDecodedChars);
86 CPPUNIT_ASSERT_EQUAL_MESSAGE("String to url decode was: '" + string(actualEncodedChars) + string("'"), string(inputChars), string(actualDecodedChars));
87 }
88
89 void testBinaryBase64EncodeDecode() {
90 unsigned char binaryData[256];
91 char encodedBinaryData[sizeof(binaryData)*4];
92 unsigned char decodedBinaryData[sizeof(binaryData)];
93 unsigned int i;
94
95 for(i = 0; i < sizeof(binaryData); ++i)
96 binaryData[i] = i;
97
98 b64Encode(binaryData, sizeof(binaryData), encodedBinaryData, sizeof(encodedBinaryData));
99 b64Decode(encodedBinaryData, decodedBinaryData, sizeof(decodedBinaryData));
100
101 for(i = 0; i < sizeof(binaryData); ++i)
102 if(binaryData[i] != decodedBinaryData[i]) {
103 CPPUNIT_ASSERT_EQUAL(binaryData[i], decodedBinaryData[i]);
104 break;
105 }
106
107 CPPUNIT_ASSERT(true);
108 }
109
110 void testStringVersion() {
111 CPPUNIT_ASSERT_EQUAL(String("VGhpcyBpcyBhIHRlc3Qu"), b64Encode(String("This is a test.")));
112 CPPUNIT_ASSERT_EQUAL(String("This is a test."), b64Decode(String("VGhpcyBpcyBhIHRlc3Qu")));
113 }
114
115 void testTypicalTextBase64EncodeDecode() {
116 char inputChars[] = "`1234567890-=\\QWERTYUIOP[]ASDFGHJKL;'ZXCVBNM,./~!@#$%^&*()_+|qwertyuiop{}asdfghjkl:\"zxcvbnm<>?";
117 char expectedOutputChars[1000] = "YDEyMzQ1Njc4OTAtPVxRV0VSVFlVSU9QW11BU0RGR0hKS0w7J1pYQ1ZCTk0sLi9+IUAjJCVeJiooKV8rfHF3ZXJ0eXVpb3B7fWFzZGZnaGprbDoienhjdmJubTw+Pw==";
118
119 b64Encode(inputChars, actualEncodedChars, sizeof(actualEncodedChars));
120 CPPUNIT_ASSERT_EQUAL_MESSAGE("String to base64 encode was: '" + string(inputChars) + string("'"), string(expectedOutputChars), string(actualEncodedChars));
121
122 b64Decode(actualEncodedChars, actualDecodedChars);
123 CPPUNIT_ASSERT_EQUAL_MESSAGE("String to base64 decode was: '" + string(actualEncodedChars) + string("'"), string(inputChars), string(actualDecodedChars));
124 }
125
126 void testSimpleTextBase64EncodeDecode() {
127 const char inputChars[] = "This is a test.";
128 const char expectedOutputChars[1000] = "VGhpcyBpcyBhIHRlc3Qu";
129
130 b64Encode((const unsigned char *)inputChars, strlen(inputChars), actualEncodedChars, sizeof(actualEncodedChars));
131 CPPUNIT_ASSERT_EQUAL_MESSAGE("String to base64 encode was: '" + string(inputChars) + string("'"), string(expectedOutputChars), string(actualEncodedChars));
132 int size = b64Decode(actualEncodedChars, (unsigned char *)actualDecodedChars, sizeof(actualDecodedChars));
133 actualDecodedChars[size] = '\0';
134 CPPUNIT_ASSERT_EQUAL_MESSAGE("String to base64 decode was: '" + string(actualEncodedChars) + string("'"), string(inputChars), string(actualDecodedChars));
135 }
136
137 void testPaddingPerfect() {
138 const char inputChars[] = "aaabbb123";
139
140 b64Encode((const unsigned char *)inputChars, strlen(inputChars), actualEncodedChars, sizeof(actualEncodedChars));
141 int size = b64Decode(actualEncodedChars, (unsigned char *)actualDecodedChars, sizeof(actualDecodedChars));
142 actualDecodedChars[size] = '\0';
143 CPPUNIT_ASSERT_EQUAL_MESSAGE(
144 "String to base64 decode was: '" + string(actualEncodedChars) + string("' ") +
145 "",
146 string(inputChars), // expected
147 string(actualDecodedChars)); // actual
148 }
149
150 void testPadding1Short() {
151 const char inputChars[] = "aaabbb12";
152
153 b64Encode((const unsigned char *)inputChars, strlen(inputChars), actualEncodedChars, sizeof(actualEncodedChars));
154 int size = b64Decode(actualEncodedChars, (unsigned char *)actualDecodedChars, sizeof(actualDecodedChars));
155 actualDecodedChars[size] = '\0';
156 CPPUNIT_ASSERT_EQUAL_MESSAGE("String to base64 decode was: '" + string(actualEncodedChars) + string("'"), string(inputChars), string(actualDecodedChars));
157 }
158
159 void testPadding2Short() {
160 const char inputChars[] = "aaabbb1";
161
162 b64Encode((const unsigned char *)inputChars, strlen(inputChars), actualEncodedChars, sizeof(actualEncodedChars));
163 int size = b64Decode(actualEncodedChars, (unsigned char *)actualDecodedChars, sizeof(actualDecodedChars));
164 actualDecodedChars[size] = '\0';
165 CPPUNIT_ASSERT_EQUAL_MESSAGE("String to base64 decode was: '" + string(actualEncodedChars) + string("'"), string(inputChars), string(actualDecodedChars));
166 }
167
168 void testComplexTextBase64EncodeDecode() {
169 const char inputChars[] =
170 "This module provides functions to encode and decode\n"
171 "strings into the Base64 encoding specified in RFC 2045 -\n"
172 "MIME (Multipurpose Internet Mail Extensions). The Base64\n"
173 "encoding is designed to represent arbitrary sequences of\n"
174 "octets in a form that need not be humanly readable. A\n"
175 "65-character subset ([A-Za-z0-9+/=]) of US-ASCII is used,\n"
176 "enabling 6 bits to be represented per printable character.";
177 const char expectedOutputChars[1000] =
178 "VGhpcyBtb2R1bGUgcHJvdmlkZXMgZnVuY3Rpb25zIHRvIGVuY29kZSBhbmQgZGVjb2RlCnN0cmlu"
179 "Z3MgaW50byB0aGUgQmFzZTY0IGVuY29kaW5nIHNwZWNpZmllZCBpbiBSRkMgMjA0NSAtCk1JTUUg"
180 "KE11bHRpcHVycG9zZSBJbnRlcm5ldCBNYWlsIEV4dGVuc2lvbnMpLiBUaGUgQmFzZTY0CmVuY29k"
181 "aW5nIGlzIGRlc2lnbmVkIHRvIHJlcHJlc2VudCBhcmJpdHJhcnkgc2VxdWVuY2VzIG9mCm9jdGV0"
182 "cyBpbiBhIGZvcm0gdGhhdCBuZWVkIG5vdCBiZSBodW1hbmx5IHJlYWRhYmxlLiBBCjY1LWNoYXJh"
183 "Y3RlciBzdWJzZXQgKFtBLVphLXowLTkrLz1dKSBvZiBVUy1BU0NJSSBpcyB1c2VkLAplbmFibGlu"
184 "ZyA2IGJpdHMgdG8gYmUgcmVwcmVzZW50ZWQgcGVyIHByaW50YWJsZSBjaGFyYWN0ZXIu";
185
186 b64Encode((const unsigned char *)inputChars, strlen(inputChars), actualEncodedChars, sizeof(actualEncodedChars));
187 CPPUNIT_ASSERT_EQUAL_MESSAGE("String to base64 encode was: '" + string(inputChars) + string("'"), string(expectedOutputChars), string(actualEncodedChars));
188 int size = b64Decode(actualEncodedChars, (unsigned char *)actualDecodedChars, sizeof(actualDecodedChars));
189 actualDecodedChars[size] = '\0';
190 CPPUNIT_ASSERT_EQUAL_MESSAGE("String to base64 decode was: '" + string(actualEncodedChars) + string("'"), string(inputChars), string(actualDecodedChars));
191 }
192};