Source code: org/bouncycastle/util/encoders/Base64Test.java
1 /*
2
3 $Id: Base64Test.java,v 1.6 2002/08/09 15:16:33 stefano_fornari Exp $
4
5 Copyright (c) 2001, 2002 sync4j project
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
10 are met:
11
12 1. Redistributions of source code must retain the above copyright
13 notice, this list of conditions, and the following disclaimer.
14
15 2. Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions, and the disclaimer that follows
17 these conditions in the documentation and/or other materials
18 provided with the distribution.
19
20 3. The name "sync4j" must not be used to endorse or promote products
21 derived from this software without prior written permission.
22
23 4. Products derived from this software may not be called "sync4j", nor
24 may "sync4j" appear in their name, without prior written permission.
25
26 In addition, we request (but do not require) that you include in the
27 end-user documentation provided with the redistribution and/or in the
28 software itself an acknowledgement equivalent to the following:
29
30 "This product includes software developed by the
31 sync4j project."
32
33 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED. IN NO EVENT SHALL THE SYNC4J AUTHORS OR THE PROJECT
37 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 SUCH DAMAGE.
45
46 */
47
48
49 package org.bouncycastle.util.encoders;
50
51 /* Use readily made Bounty Castle Crypto API instead...
52 import sync4j.framework.core.Util;
53 */
54 import org.bouncycastle.util.encoders.Base64;
55
56 import junit.framework.*;
57
58 import java.util.Random;
59 import java.util.Arrays;
60
61 /**
62 *
63 * Unit test code for Base64 encoding and decoding.
64 * Test code inherited from Bounty Castle Crypto API.
65 *
66 * @author Sean C. Sullivan
67 *
68 */
69 public class Base64Test extends junit.framework.TestCase
70 {
71 private Random _r = null;
72
73 public Base64Test(String strName)
74 {
75 super(strName);
76 }
77
78 protected void setUp() {
79 _r = new Random();
80 }
81
82 /*
83 Copyright (c) 2000 The Legion Of The Bouncy Castle
84 (http://www.bouncycastle.org)
85
86 Permission is hereby granted, free of charge, to any person obtaining
87 a copy of this software and associated documentation files (the "Software"),
88 to deal in the Software without restriction, including without limitation
89 the rights to use, copy, modify, merge, publish, distribute, sublicense,
90 and/or sell copies of the Software, and to permit persons to whom the Software
91 is furnished to do so, subject to the following conditions:
92
93 The above copyright notice and this permission notice shall be included in all
94 copies or substantial portions of the Software.
95
96 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
97 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
98 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
99 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
100 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
101 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
102 THE SOFTWARE.
103 */
104 public void testPowerOf2()
105 {
106 byte[] _orig1024 = new byte[1024];
107 _r.nextBytes(_orig1024);
108
109 byte[] _orig2048 = new byte[2048];
110 _r.nextBytes(_orig2048);
111
112 byte[] _orig4096 = new byte[4096];
113 _r.nextBytes(_orig4096);
114
115 byte[] _orig8192 = new byte[8192];
116 _r.nextBytes(_orig8192);
117
118 byte[] _enc1024 = Base64.encode(_orig1024);
119 byte[] _enc2048 = Base64.encode(_orig2048);
120 byte[] _enc4096 = Base64.encode(_orig4096);
121 byte[] _enc8192 = Base64.encode(_orig8192);
122
123 byte[] _dec1024 = Base64.decode(_enc1024);
124 byte[] _dec2048 = Base64.decode(_enc2048);
125 byte[] _dec4096 = Base64.decode(_enc4096);
126 byte[] _dec8192 = Base64.decode(_enc8192);
127
128 assertTrue(Arrays.equals(_orig1024, _dec1024));
129
130 assertTrue(Arrays.equals(_orig2048, _dec2048));
131
132 assertTrue(Arrays.equals(_orig4096, _dec4096));
133
134 assertTrue(Arrays.equals(_orig8192, _dec8192));
135 }
136
137 public void testPowerOf2Plus1() {
138 byte[] _orig1025 = new byte[1025];
139 _r.nextBytes(_orig1025);
140
141 byte[] _orig2049 = new byte[2049];
142 _r.nextBytes(_orig2049);
143
144 byte[] _orig4097 = new byte[4097];
145 _r.nextBytes(_orig4097);
146
147 byte[] _orig8193 = new byte[8193];
148 _r.nextBytes(_orig8193);
149
150 byte[] _enc1025 = Base64.encode(_orig1025);
151 byte[] _enc2049 = Base64.encode(_orig2049);
152 byte[] _enc4097 = Base64.encode(_orig4097);
153 byte[] _enc8193 = Base64.encode(_orig8193);
154
155 byte[] _dec1025 = Base64.decode(_enc1025);
156 byte[] _dec2049 = Base64.decode(_enc2049);
157 byte[] _dec4097 = Base64.decode(_enc4097);
158 byte[] _dec8193 = Base64.decode(_enc8193);
159
160 assertTrue(Arrays.equals(_orig1025, _dec1025));
161
162 assertTrue(Arrays.equals(_orig2049, _dec2049));
163
164 assertTrue(Arrays.equals(_orig4097, _dec4097));
165
166 assertTrue(Arrays.equals(_orig8193, _dec8193));
167 }
168 }