VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstBase64.cpp@ 16767

Last change on this file since 16767 was 16767, checked in by vboxsync, 16 years ago

Wrote RTBase64Encode while at it.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 KB
Line 
1/* $Id: tstBase64.cpp 16767 2009-02-14 09:33:15Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Base64.
4 */
5
6/*
7 * Copyright (C) 2009 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/base64.h>
35#include <iprt/string.h>
36#include <iprt/stream.h>
37#include <iprt/err.h>
38#include <iprt/initterm.h>
39
40/*******************************************************************************
41* Defined Constants And Macros *
42*******************************************************************************/
43#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
44# define MY_NL "\r\n"
45#else
46# define MY_NL "\n"
47#endif
48
49
50int main()
51{
52 int cErrors = 0;
53 char szOut[0x10000];
54 size_t cchOut = 0;
55 RTR3Init();
56
57
58 /*
59 * Series of simple tests.
60 */
61 static const struct
62 {
63 const char *pszText;
64 size_t cchText;
65 const char *pszEnc;
66 size_t cchEnc;
67 } g_aTests[] =
68 {
69#define TEST_ENTRY(szText, szEnc) { szText, sizeof(szText) - 1, szEnc, sizeof(szEnc) - 1 }
70 TEST_ENTRY("Hey", "SGV5"),
71 TEST_ENTRY("Base64", "QmFzZTY0"),
72 TEST_ENTRY("Call me Ishmael.", "Q2FsbCBtZSBJc2htYWVsLg=="),
73 TEST_ENTRY(
74 "Man is distinguished, not only by his reason, but by this singular passion "
75 "from other animals, which is a lust of the mind, that by a perseverance of "
76 "delight in the continued and indefatigable generation of knowledge, exceeds "
77 "the short vehemence of any carnal pleasure." /* Thomas Hobbes's Leviathan */,
78 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1" MY_NL
79 "dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3" MY_NL
80 "aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFu" MY_NL
81 "Y2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxl" MY_NL
82 "IGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhl" MY_NL
83 "bWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="
84 )
85#undef TEST_ENTRY
86 };
87
88 for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++)
89 {
90 int rc = RTBase64Decode(g_aTests[i].pszEnc, szOut, g_aTests[i].cchText, &cchOut, NULL);
91 if (RT_FAILURE(rc))
92 {
93 RTPrintf("tstBase64: FAILURE - #%u: RTBase64Decode -> %Rrc\n", i, rc);
94 cErrors++;
95 }
96 else if (cchOut != g_aTests[i].cchText)
97 {
98 RTPrintf("tstBase64: FAILURE - #%u: RTBase64Decode returned %zu bytes, expected %zu.\n",
99 i, cchOut, g_aTests[i].cchText);
100 cErrors++;
101 }
102 else if (memcmp(szOut, g_aTests[i].pszText, cchOut))
103 {
104 RTPrintf("tstBase64: FAILURE - #%u: RTBase64Decode returned:\n%.*s\nexpected:\n%s\n",
105 i, (int)cchOut, szOut, g_aTests[i].pszText);
106 cErrors++;
107 }
108
109 cchOut = RTBase64DecodedSize(g_aTests[i].pszEnc, NULL);
110 if (cchOut != g_aTests[i].cchText)
111 {
112 RTPrintf("tstBase64: FAILURE - #%u: RTBase64DecodedSize returned %zu bytes, expected %zu.\n",
113 i, cchOut, g_aTests[i].cchText);
114 cErrors++;
115 }
116
117 rc = RTBase64Encode(g_aTests[i].pszText, g_aTests[i].cchText, szOut, g_aTests[i].cchEnc + 1, &cchOut);
118 if (RT_FAILURE(rc))
119 {
120 RTPrintf("tstBase64: FAILURE - #%u: RTBase64Encode -> %Rrc\n", i, rc);
121 cErrors++;
122 }
123 else if (cchOut != g_aTests[i].cchEnc)
124 {
125 RTPrintf("tstBase64: FAILURE - #%u: RTBase64Encode returned %zu bytes, expected %zu.\n",
126 i, cchOut, g_aTests[i].cchEnc);
127 cErrors++;
128 }
129 else if (memcmp(szOut, g_aTests[i].pszEnc, cchOut + 1))
130 {
131 RTPrintf("tstBase64: FAILURE - #%u: RTBase64Encode returned:\n%*s\nexpected:\n%s\n",
132 i, szOut, g_aTests[i].pszText);
133 cErrors++;
134 }
135
136 cchOut = RTBase64EncodedLength(g_aTests[i].cchText);
137 if (cchOut != g_aTests[i].cchEnc)
138 {
139 RTPrintf("tstBase64: FAILURE - #%u: RTBase64EncodedLength returned %zu bytes, expected %zu.\n",
140 i, cchOut, g_aTests[i].cchEnc);
141 cErrors++;
142 }
143
144 /** @todo negative testing. */
145 }
146
147 /*
148 * Try with some more junk in the encoding and different line length.
149 */
150 static const char s_szText2[] =
151 "Man is distinguished, not only by his reason, but by this singular passion "
152 "from other animals, which is a lust of the mind, that by a perseverance of "
153 "delight in the continued and indefatigable generation of knowledge, exceeds "
154 "the short vehemence of any carnal pleasure."; /* Thomas Hobbes's Leviathan */
155
156 static const char s_szEnc2[] =
157 " TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\n"
158 " IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\n\r\t\t\t\v"
159 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\n"
160 "\tdWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\n\r"
161 " ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=\n \n \r \n \t";
162
163 int rc = RTBase64Decode(&s_szEnc2[0], szOut, sizeof(s_szText2), &cchOut, NULL);
164 if (RT_FAILURE(rc))
165 {
166 RTPrintf("tstBase64: FAILURE - RTBase64Decode s_szEnc2 -> %Rrc\n", rc);
167 cErrors++;
168 }
169 else if (cchOut != sizeof(s_szText2) - 1)
170 {
171 RTPrintf("tstBase64: FAILURE - RTBase64Decode returned %zu bytes, expected %zu.\n",
172 cchOut, sizeof(s_szText2) - 1);
173 cErrors++;
174 }
175 else if (memcmp(szOut, s_szText2, cchOut))
176 {
177 RTPrintf("tstBase64: FAILURE - RTBase64Decode returned:\n%.*s\nexpected:\n%s\n",
178 (int)cchOut, szOut, s_szText2);
179 cErrors++;
180 }
181
182 cchOut = RTBase64DecodedSize(s_szEnc2, NULL);
183 if (cchOut != sizeof(s_szText2) - 1)
184 {
185 RTPrintf("tstBase64: FAILURE - RTBase64DecodedSize returned %zu bytes, expected %zu.\n",
186 cchOut, sizeof(s_szText2) - 1);
187 cErrors++;
188 }
189
190 /*
191 * Summary.
192 */
193 if (!cErrors)
194 RTPrintf("tstBase64: SUCCESS\n");
195 else
196 RTPrintf("tstBase64: FAILURE - %d errors\n", cErrors);
197 return !!cErrors;
198}
199
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette