001: /*
002: * Copyright (C) 2004-2007 Stephen Ostermiller
003: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * See COPYING.TXT for details.
016: */
017: package com.Ostermiller.util;
018:
019: import java.util.*;
020:
021: /**
022: * Test cases for Base64
023: */
024: class Base64Tests {
025:
026: private static class TestCase {
027: private String encoded;
028: private byte[] decoded;
029:
030: /**
031: * @param encoded
032: * @param decoded
033: */
034: public TestCase(String encoded, byte[] decoded) {
035: this .encoded = encoded;
036: this .decoded = decoded;
037: }
038:
039: private void test() throws Exception {
040: String enc = Base64.encodeToString(decoded);
041: if (!encoded.equals(enc)) {
042: throw new Exception("Decoding problem, expected '"
043: + encoded + "' got '" + enc + "'.");
044: }
045: byte[] b = Base64.decodeToBytes(encoded);
046: if (!byteArraysEqual(b, decoded)) {
047: throw new Exception("Encoding problem, started with '"
048: + encoded + "'.");
049: }
050: }
051: }
052:
053: private static boolean byteArraysEqual(byte[] b1, byte[] b2) {
054: if (b1.length != b2.length)
055: return false;
056: for (int i = 0; i < b1.length; i++) {
057: if (b1[i] != b2[i])
058: return false;
059: }
060: return true;
061: }
062:
063: private static final TestCase[] testCases = new TestCase[] {
064: new TestCase("", new byte[] {}),
065: new TestCase("aA==", new byte[] { 'h' }),
066: new TestCase("dGU=", new byte[] { 't', 'e' }),
067: new TestCase("Y29i", new byte[] { 'c', 'o', 'b' }), };
068:
069: /**
070: * Main method for tests
071: * @param args command line arguments (ignored)
072: */
073: public static void main(String[] args) {
074: try {
075: for (int i = 0; i < testCases.length; i++) {
076: testCases[i].test();
077: }
078: for (int i = 0; i < 1024; i++) {
079: byte[] before = randBytes();
080: byte[] after = Base64.decodeToBytes(Base64
081: .encodeToString(before));
082: if (!byteArraysEqual(before, after)) {
083: throw new Exception(
084: "Could not decode and then re-encode:\n before: "
085: + bytesToString(before)
086: + "\n after: "
087: + bytesToString(after));
088: }
089: }
090:
091: } catch (Exception x) {
092: x.printStackTrace(System.err);
093: System.exit(1);
094: }
095: System.exit(0);
096: }
097:
098: private static String bytesToString(byte[] b) {
099: StringBuffer sb = new StringBuffer();
100: for (int i = 0; i < b.length; i++) {
101: sb.append("'").append(b[i]).append("',");
102: }
103: return sb.toString();
104: }
105:
106: private static byte[] randBytes() {
107: Random rand = new Random();
108: byte[] bytes = new byte[rand.nextInt(128) * 3];
109: for (int i = 0; i < bytes.length; i++) {
110: bytes[i] = (byte) (rand.nextInt() & 0xff);
111: }
112: return bytes;
113: }
114: }
|