001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.util.crypt;
018:
019: import java.io.IOException;
020: import java.nio.ByteBuffer;
021: import java.util.Random;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.wicket.settings.ISecuritySettings;
026:
027: /**
028: *
029: * @author Juergen Donnerstag
030: */
031: public class Base64Test extends TestCase {
032: /**
033: * Construct.
034: *
035: * @param name
036: */
037: public Base64Test(String name) {
038: super (name);
039: }
040:
041: /**
042: * @throws IOException
043: */
044: public void test_1() throws IOException {
045: for (int i = 0; i < 200; i++) {
046: byte bytes1[] = new byte[200];
047: new Random().nextBytes(bytes1);
048:
049: byte[] s = new Base64().encode(bytes1);
050:
051: byte[] bytes2 = new Base64().decode(s);
052: boolean isEqual = ByteBuffer.wrap(bytes1).equals(
053: ByteBuffer.wrap(bytes2));
054: assertEquals(true, isEqual);
055: }
056: }
057:
058: /**
059: * @throws IOException
060: */
061: public void test_1a() throws IOException {
062: String input = "wicket:interface=:2:entityTree:node:node:0:node:nodeLink::IBehaviorListener";
063:
064: byte[] s = new Base64().encode(input.getBytes());
065:
066: byte[] bytes2 = new Base64().decode(s);
067: String output = new String(bytes2);
068: boolean isEqual = input.equals(output);
069: assertEquals(true, isEqual);
070: }
071:
072: /**
073: * @throws IOException
074: */
075: public void test_1b() throws IOException {
076: String input = "wicket:interface=:2:entityTree:node:node:0:node:nodeLink::IBehaviorListenerA";
077:
078: byte[] s = new Base64().encode(input.getBytes());
079:
080: byte[] bytes2 = new Base64().decode(s);
081: String output = new String(bytes2);
082: boolean isEqual = input.equals(output);
083: assertEquals(true, isEqual);
084: }
085:
086: /**
087: * @throws IOException
088: */
089: public void test_1c() throws IOException {
090: String input = "wicket:interface=:2:entityTree:node:node:0:node:nodeLink::IBehaviorListenerAB";
091:
092: byte[] s = new Base64().encode(input.getBytes());
093:
094: byte[] bytes2 = new Base64().decode(s);
095: String output = new String(bytes2);
096: boolean isEqual = input.equals(output);
097: assertEquals(true, isEqual);
098: }
099:
100: /**
101: * @throws IOException
102: */
103: public void test_1d() throws IOException {
104: String input = "wicket:interface=:2:entityTree:node:node:0:node:nodeLink::IBehaviorListenerABC";
105:
106: byte[] s = new Base64().encode(input.getBytes());
107:
108: byte[] bytes2 = new Base64().decode(s);
109: String output = new String(bytes2);
110: boolean isEqual = input.equals(output);
111: assertEquals(true, isEqual);
112: }
113:
114: /**
115: * @throws IOException
116: */
117: public void test_2() throws IOException {
118: String input = "This is a text which is longer than 76 character and which contains some none-ascii chars like these: �����?�`=";
119: String s = new NoCrypt().encryptUrlSafe(input);
120:
121: String output = new NoCrypt().decryptUrlSafe(s);
122: assertEquals(input, output);
123: }
124:
125: /**
126: * @throws IOException
127: */
128: public void test_3() throws IOException {
129: String input = "wicket:interface=:2:entityTree:node:node:0:node:nodeLink::IBehaviorListener";
130: ICrypt crypt = new CachingSunJceCryptFactory(
131: ISecuritySettings.DEFAULT_ENCRYPTION_KEY).newCrypt();
132: String s = crypt.encryptUrlSafe(input);
133:
134: String output = crypt.decryptUrlSafe(s);
135: assertEquals(input, output);
136: }
137:
138: /**
139: * @throws IOException
140: */
141: public void test_UrlSafe_1() throws IOException {
142: for (int i = 0; i < 200; i++) {
143: byte bytes1[] = new byte[200];
144: new Random().nextBytes(bytes1);
145:
146: byte[] s = new Base64UrlSafe().encode(bytes1);
147:
148: byte[] bytes2 = new Base64UrlSafe().decode(s);
149: boolean isEqual = ByteBuffer.wrap(bytes1).equals(
150: ByteBuffer.wrap(bytes2));
151: assertEquals(true, isEqual);
152: }
153: }
154:
155: /**
156: * @throws IOException
157: */
158: public void test_UrlSafe_1a() throws IOException {
159: String input = "wicket:interface=:2:entityTree:node:node:0:node:nodeLink::IBehaviorListener";
160:
161: byte[] s = new Base64UrlSafe().encode(input.getBytes());
162:
163: byte[] bytes2 = new Base64UrlSafe().decode(s);
164: String output = new String(bytes2);
165: boolean isEqual = input.equals(output);
166: assertEquals(true, isEqual);
167: }
168:
169: /**
170: * @throws IOException
171: */
172: public void test_UrlSafe_1b() throws IOException {
173: String input = "wicket:interface=:2:entityTree:node:node:0:node:nodeLink::IBehaviorListenerA";
174:
175: byte[] s = new Base64UrlSafe().encode(input.getBytes());
176:
177: byte[] bytes2 = new Base64UrlSafe().decode(s);
178: String output = new String(bytes2);
179: boolean isEqual = input.equals(output);
180: assertEquals(true, isEqual);
181: }
182:
183: /**
184: * @throws IOException
185: */
186: public void test_UrlSafe_1c() throws IOException {
187: String input = "wicket:interface=:2:entityTree:node:node:0:node:nodeLink::IBehaviorListenerAB";
188:
189: byte[] s = new Base64UrlSafe().encode(input.getBytes());
190:
191: byte[] bytes2 = new Base64UrlSafe().decode(s);
192: String output = new String(bytes2);
193: boolean isEqual = input.equals(output);
194: assertEquals(true, isEqual);
195: }
196:
197: /**
198: * @throws IOException
199: */
200: public void test_UrlSafe_1d() throws IOException {
201: String input = "wicket:interface=:2:entityTree:node:node:0:node:nodeLink::IBehaviorListenerABC";
202:
203: byte[] s = new Base64UrlSafe().encode(input.getBytes());
204:
205: byte[] bytes2 = new Base64UrlSafe().decode(s);
206: String output = new String(bytes2);
207: boolean isEqual = input.equals(output);
208: assertEquals(true, isEqual);
209: }
210:
211: /**
212: * @throws IOException
213: */
214: public void test_UrlSafe_2() throws IOException {
215: String input = "This is a text which is longer than 76 character and which contains some none-ascii chars like these: �����?�`=";
216: String s = new NoCrypt().encryptUrlSafe(input);
217:
218: String output = new NoCrypt().decryptUrlSafe(s);
219: assertEquals(input, output);
220: }
221:
222: /**
223: * @throws IOException
224: */
225: public void test_UrlSafe_3() throws IOException {
226: String input = "wicket:interface=:2:entityTree:node:node:0:node:nodeLink::IBehaviorListener";
227: ICrypt crypt = new CachingSunJceCryptFactory(
228: ISecuritySettings.DEFAULT_ENCRYPTION_KEY).newCrypt();
229: String s = crypt.encryptUrlSafe(input);
230:
231: String output = crypt.decryptUrlSafe(s);
232: assertEquals(input, output);
233: }
234: }
|