001: // -*- Java -*-
002: //
003: // Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu>
004: // Copyright (c) 2005, University of Colorado at Boulder
005: // All rights reserved.
006: //
007: // Redistribution and use in source and binary forms, with or without
008: // modification, are permitted provided that the following conditions are
009: // met:
010: //
011: // * Redistributions of source code must retain the above copyright
012: // notice, this list of conditions and the following disclaimer.
013: //
014: // * Redistributions in binary form must reproduce the above copyright
015: // notice, this list of conditions and the following disclaimer in the
016: // documentation and/or other materials provided with the distribution.
017: //
018: // * Neither the name of the University of Colorado at Boulder nor the
019: // names of its contributors may be used to endorse or promote
020: // products derived from this software without specific prior written
021: // permission.
022: //
023: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
024: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
025: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
026: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
027: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
028: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
029: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
030: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
031: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
033: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034: //
035: package org.xbill.DNS.utils;
036:
037: import junit.framework.TestCase;
038:
039: public class base64Test extends TestCase {
040: public base64Test(String name) {
041: super (name);
042: }
043:
044: public void test_toString_empty() {
045: byte[] data = new byte[0];
046: String out = base64.toString(data);
047: assertEquals("", out);
048: }
049:
050: public void test_toString_basic1() {
051: byte[] data = { 0 };
052: String out = base64.toString(data);
053: assertEquals("AA==", out);
054: }
055:
056: public void test_toString_basic2() {
057: byte[] data = { 0, 0 };
058: String out = base64.toString(data);
059: assertEquals("AAA=", out);
060: }
061:
062: public void test_toString_basic3() {
063: byte[] data = { 0, 0, 1 };
064: String out = base64.toString(data);
065: assertEquals("AAAB", out);
066: }
067:
068: public void test_toString_basic4() {
069: byte[] data = { (byte) 0xFC, 0, 0 };
070: String out = base64.toString(data);
071: assertEquals("/AAA", out);
072: }
073:
074: public void test_toString_basic5() {
075: byte[] data = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
076: String out = base64.toString(data);
077: assertEquals("////", out);
078: }
079:
080: public void test_toString_basic6() {
081: byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
082: String out = base64.toString(data);
083: assertEquals("AQIDBAUGBwgJ", out);
084: }
085:
086: public void test_formatString_empty1() {
087: String out = base64.formatString(new byte[0], 5, "", false);
088: assertEquals("", out);
089: }
090:
091: public void test_formatString_shorter() {
092: byte[] in = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // "AQIDBAUGBwgJ" (12 chars)
093: String out = base64.formatString(in, 13, "", false);
094: assertEquals("AQIDBAUGBwgJ", out);
095: }
096:
097: public void test_formatString_sameLength() {
098: byte[] in = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // "AQIDBAUGBwgJ" (12 chars)
099: String out = base64.formatString(in, 12, "", false);
100: assertEquals("AQIDBAUGBwgJ", out);
101: }
102:
103: public void test_formatString_oneBreak() {
104: byte[] in = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // "AQIDBAUGBwgJ" (12 chars)
105: String out = base64.formatString(in, 10, "", false);
106: assertEquals("AQIDBAUGBw\ngJ", out);
107: }
108:
109: public void test_formatString_twoBreaks1() {
110: byte[] in = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // "AQIDBAUGBwgJ" (12 chars)
111: String out = base64.formatString(in, 5, "", false);
112: assertEquals("AQIDB\nAUGBw\ngJ", out);
113: }
114:
115: public void test_formatString_twoBreaks2() {
116: byte[] in = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // "AQIDBAUGBwgJ" (12 chars)
117: String out = base64.formatString(in, 4, "", false);
118: assertEquals("AQID\nBAUG\nBwgJ", out);
119: }
120:
121: public void test_formatString_shorterWithPrefix() {
122: byte[] in = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // "AQIDBAUGBwgJ" (12 chars)
123: String out = base64.formatString(in, 13, "!_", false);
124: assertEquals("!_AQIDBAUGBwgJ", out);
125: }
126:
127: public void test_formatString_sameLengthWithPrefix() {
128: byte[] in = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // "AQIDBAUGBwgJ" (12 chars)
129: String out = base64.formatString(in, 12, "!_", false);
130: assertEquals("!_AQIDBAUGBwgJ", out);
131: }
132:
133: public void test_formatString_oneBreakWithPrefix() {
134: byte[] in = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // "AQIDBAUGBwgJ" (12 chars)
135: String out = base64.formatString(in, 10, "!_", false);
136: assertEquals("!_AQIDBAUGBw\n!_gJ", out);
137: }
138:
139: public void test_formatString_twoBreaks1WithPrefix() {
140: byte[] in = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // "AQIDBAUGBwgJ" (12 chars)
141: String out = base64.formatString(in, 5, "!_", false);
142: assertEquals("!_AQIDB\n!_AUGBw\n!_gJ", out);
143: }
144:
145: public void test_formatString_twoBreaks2WithPrefix() {
146: byte[] in = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // "AQIDBAUGBwgJ" (12 chars)
147: String out = base64.formatString(in, 4, "!_", false);
148: assertEquals("!_AQID\n!_BAUG\n!_BwgJ", out);
149: }
150:
151: public void test_formatString_shorterWithPrefixAndClose() {
152: byte[] in = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // "AQIDBAUGBwgJ" (12 chars)
153: String out = base64.formatString(in, 13, "!_", true);
154: assertEquals("!_AQIDBAUGBwgJ )", out);
155: }
156:
157: public void test_formatString_sameLengthWithPrefixAndClose() {
158: byte[] in = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // "AQIDBAUGBwgJ" (12 chars)
159: String out = base64.formatString(in, 12, "!_", true);
160: assertEquals("!_AQIDBAUGBwgJ )", out);
161: }
162:
163: public void test_formatString_oneBreakWithPrefixAndClose() {
164: byte[] in = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // "AQIDBAUGBwgJ" (12 chars)
165: String out = base64.formatString(in, 10, "!_", true);
166: assertEquals("!_AQIDBAUGBw\n!_gJ )", out);
167: }
168:
169: public void test_formatString_twoBreaks1WithPrefixAndClose() {
170: byte[] in = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // "AQIDBAUGBwgJ" (12 chars)
171: String out = base64.formatString(in, 5, "!_", true);
172: assertEquals("!_AQIDB\n!_AUGBw\n!_gJ )", out);
173: }
174:
175: public void test_formatString_twoBreaks2WithPrefixAndClose() {
176: byte[] in = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // "AQIDBAUGBwgJ" (12 chars)
177: String out = base64.formatString(in, 4, "!_", true);
178: assertEquals("!_AQID\n!_BAUG\n!_BwgJ )", out);
179: }
180:
181: private void assertEquals(byte[] exp, byte[] act) {
182: assertEquals(exp.length, act.length);
183: for (int i = 0; i < exp.length; ++i) {
184: assertEquals(exp[i], act[i]);
185: }
186: }
187:
188: public void test_fromString_empty1() {
189: byte[] data = new byte[0];
190: byte[] out = base64.fromString("");
191: assertEquals(new byte[0], out);
192: }
193:
194: public void test_fromString_basic1() {
195: byte[] exp = { 0 };
196: byte[] out = base64.fromString("AA==");
197: assertEquals(exp, out);
198: }
199:
200: public void test_fromString_basic2() {
201: byte[] exp = { 0, 0 };
202: byte[] out = base64.fromString("AAA=");
203: assertEquals(exp, out);
204: }
205:
206: public void test_fromString_basic3() {
207: byte[] exp = { 0, 0, 1 };
208: byte[] out = base64.fromString("AAAB");
209: assertEquals(exp, out);
210: }
211:
212: public void test_fromString_basic4() {
213: byte[] exp = { (byte) 0xFC, 0, 0 };
214: byte[] out = base64.fromString("/AAA");
215: assertEquals(exp, out);
216: }
217:
218: public void test_fromString_basic5() {
219: byte[] exp = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
220: byte[] out = base64.fromString("////");
221: assertEquals(exp, out);
222: }
223:
224: public void test_fromString_basic6() {
225: byte[] exp = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
226: byte[] out = base64.fromString("AQIDBAUGBwgJ");
227: assertEquals(exp, out);
228: }
229:
230: public void test_fromString_invalid1() {
231: byte[] out = base64.fromString("AAA");
232: assertNull(out);
233: }
234:
235: public void test_fromString_invalid2() {
236: byte[] out = base64.fromString("AA");
237: assertNull(out);
238: }
239:
240: public void test_fromString_invalid3() {
241: byte[] out = base64.fromString("A");
242: assertNull(out);
243: }
244:
245: public void test_fromString_invalid4() {
246: byte[] out = base64.fromString("BB==");
247: assertNull(out);
248: }
249:
250: public void test_fromString_invalid5() {
251: byte[] out = base64.fromString("BBB=");
252: assertNull(out);
253: }
254:
255: }
|