VirtualBox

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

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

RTBase64Decode testcase and bugfixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1/* $Id: tstBase64.cpp 16762 2009-02-14 08:21:28Z 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
40struct TstI64
41{
42 const char *psz;
43 unsigned uBase;
44 int rc;
45 int64_t Result;
46};
47
48struct TstU64
49{
50 const char *psz;
51 unsigned uBase;
52 int rc;
53 uint64_t Result;
54};
55
56struct TstI32
57{
58 const char *psz;
59 unsigned uBase;
60 int rc;
61 int32_t Result;
62};
63
64struct TstU32
65{
66 const char *psz;
67 unsigned uBase;
68 int rc;
69 uint32_t Result;
70};
71
72
73#define TEST(Test, Type, Fmt, Fun, iTest) \
74 do \
75 { \
76 Type Result; \
77 int rc = Fun(Test.psz, NULL, Test.uBase, &Result); \
78 if (Result != Test.Result) \
79 { \
80 RTPrintf("failure: '%s' -> " Fmt " expected " Fmt ". (%s/%u)\n", Test.psz, Result, Test.Result, #Fun, iTest); \
81 cErrors++; \
82 } \
83 else if (rc != Test.rc) \
84 { \
85 RTPrintf("failure: '%s' -> rc=%Rrc expected %Rrc. (%s/%u)\n", Test.psz, rc, Test.rc, #Fun, iTest); \
86 cErrors++; \
87 } \
88 } while (0)
89
90
91int main()
92{
93 int cErrors =0;
94 RTR3Init();
95
96 /*
97 * Test 0.
98 */
99 static const char s_szText0[] = "Hey";
100 static const char s_szEnc0[] = "SGV5";
101 size_t cchOut0 = 0;
102 char szOut0[sizeof(s_szText0)];
103 int rc = RTBase64Decode(&s_szEnc0[0], szOut0, sizeof(szOut0), &cchOut0, NULL);
104 if (RT_FAILURE(rc))
105 {
106 RTPrintf("tstBase64: FAILURE - RTBase64Decode s_szEnc0 -> %Rrc\n", rc);
107 cErrors++;
108 }
109 else if (cchOut0 != sizeof(s_szText0) - 1)
110 {
111 RTPrintf("tstBase64: FAILURE - RTBase64Decode returned %zu bytes, expected %zu.\n",
112 cchOut0, sizeof(s_szText0) - 1);
113 cErrors++;
114 }
115 else if (memcmp(szOut0, s_szText0, cchOut0))
116 {
117 RTPrintf("tstBase64: FAILURE - RTBase64Decode returned:\n%.*s\nexpected:\n%s\n",
118 (int)cchOut0, szOut0, s_szText0);
119 cErrors++;
120 }
121
122 /*
123 * Test 1.
124 */
125 static const char s_szText1[] = "Call me Ishmael.";
126 static const char s_szEnc1[] = "Q2FsbCBtZSBJc2htYWVsLg==";
127 size_t cchOut1 = 0;
128 char szOut1[sizeof(s_szText1)];
129 rc = RTBase64Decode(&s_szEnc1[0], szOut1, sizeof(szOut1), &cchOut1, NULL);
130 if (RT_FAILURE(rc))
131 {
132 RTPrintf("tstBase64: FAILURE - RTBase64Decode s_szEnc1 -> %Rrc\n", rc);
133 cErrors++;
134 }
135 else if (cchOut1 != sizeof(s_szText1) - 1)
136 {
137 RTPrintf("tstBase64: FAILURE - RTBase64Decode returned %zu bytes, expected %zu.\n",
138 cchOut1, sizeof(s_szText1) - 1);
139 cErrors++;
140 }
141 else if (memcmp(szOut1, s_szText1, cchOut1))
142 {
143 RTPrintf("tstBase64: FAILURE - RTBase64Decode returned:\n%.*s\nexpected:\n%s\n",
144 (int)cchOut1, szOut1, s_szText1);
145 cErrors++;
146 }
147
148 /*
149 * Test 2.
150 */
151 static const char s_szText2[] =
152 "Man is distinguished, not only by his reason, but by this singular passion "
153 "from other animals, which is a lust of the mind, that by a perseverance of "
154 "delight in the continued and indefatigable generation of knowledge, exceeds "
155 "the short vehemence of any carnal pleasure."; /* Thomas Hobbes's Leviathan */
156
157 static const char s_szEnc2[] =
158 " TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\n"
159 " IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\n\r"
160 " dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\n"
161 " dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\n\r"
162 " ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=\n \n";
163
164 size_t cchOut2 = 0;
165 char szOut2[sizeof(s_szText2)];
166 rc = RTBase64Decode(&s_szEnc2[0], szOut2, sizeof(szOut2), &cchOut2, NULL);
167 if (RT_FAILURE(rc))
168 {
169 RTPrintf("tstBase64: FAILURE - RTBase64Decode s_szEnc2 -> %Rrc\n", rc);
170 cErrors++;
171 }
172 else if (cchOut2 != sizeof(s_szText2) - 1)
173 {
174 RTPrintf("tstBase64: FAILURE - RTBase64Decode returned %zu bytes, expected %zu.\n",
175 cchOut2, sizeof(s_szText2) - 1);
176 cErrors++;
177 }
178 else if (memcmp(szOut2, s_szText2, cchOut2))
179 {
180 RTPrintf("tstBase64: FAILURE - RTBase64Decode returned:\n%.*s\nexpected:\n%s\n",
181 (int)cchOut2, szOut2, s_szText2);
182 cErrors++;
183 }
184
185 /*
186 * Summary.
187 */
188 if (!cErrors)
189 RTPrintf("tstBase64: SUCCESS\n");
190 else
191 RTPrintf("tstBase64: FAILURE - %d errors\n", cErrors);
192 return !!cErrors;
193}
194
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