/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package org.apache.commons.codec.binary; import java.util.Arrays; import junit.framework.TestCase; public class Base32Test extends TestCase { private static final String [][] BASE32_TEST_CASES = { // RFC 4648 {"" ,""}, {"f" ,"MY======"}, {"fo" ,"MZXQ===="}, {"foo" ,"MZXW6==="}, {"foob" ,"MZXW6YQ="}, {"fooba" ,"MZXW6YTB"}, {"foobar" ,"MZXW6YTBOI======"}, }; private static final String [][] BASE32HEX_TEST_CASES = { // RFC 4648 {"" ,""}, {"f" ,"CO======"}, {"fo" ,"CPNG===="}, {"foo" ,"CPNMU==="}, {"foob" ,"CPNMUOG="}, {"fooba" ,"CPNMUOJ1"}, {"foobar" ,"CPNMUOJ1E8======"}, }; private static final String [][] BASE32_TEST_CASES_CHUNKED = { //Chunked {"" ,""}, {"f" ,"MY======\r\n"}, {"fo" ,"MZXQ====\r\n"}, {"foo" ,"MZXW6===\r\n"}, {"foob" ,"MZXW6YQ=\r\n"}, {"fooba" ,"MZXW6YTB\r\n"}, {"foobar" ,"MZXW6YTBOI======\r\n"}, }; public void testBase32Samples() throws Exception { Base32 codec = new Base32(); for (int i = 0; i < BASE32_TEST_CASES.length; i++) { assertEquals(BASE32_TEST_CASES[i][1], codec.encodeAsString(BASE32_TEST_CASES[i][0].getBytes("UTF-8"))); } } public void testBase32HexSamples() throws Exception { Base32 codec = new Base32(true); for (int i = 0; i < BASE32HEX_TEST_CASES.length; i++) { assertEquals(BASE32HEX_TEST_CASES[i][1], codec.encodeAsString(BASE32HEX_TEST_CASES[i][0].getBytes("UTF-8"))); } } public void testBase32Chunked () throws Exception { Base32 codec = new Base32(20); for (int i = 0; i < BASE32_TEST_CASES_CHUNKED.length; i++) { assertEquals(BASE32_TEST_CASES_CHUNKED[i][1], codec.encodeAsString(BASE32_TEST_CASES_CHUNKED[i][0].getBytes("UTF-8"))); } } public void testSingleCharEncoding() { for (int i = 0; i < 20; i++) { Base32 codec = new Base32(); byte unencoded[] = new byte[i]; byte allInOne[] = codec.encode(unencoded); codec = new Base32(); for (int j=0; j< unencoded.length; j++) { codec.encode(unencoded, j, 1); } codec.encode(unencoded, 0, -1); byte singly[] = new byte[allInOne.length]; codec.readResults(singly, 0, 100); if (!Arrays.equals(allInOne, singly)){ fail(); } } } public void testRandomBytes() { for (int i = 0; i < 20; i++) { Base32 codec = new Base32(); byte[][] b = Base32TestData.randomData(codec, i); assertEquals(""+i+" "+codec.lineLength,b[1].length,codec.getEncodedLength(b[0])); //assertEquals(b[0],codec.decode(b[1])); } } public void testRandomBytesChunked() { for (int i = 0; i < 20; i++) { Base32 codec = new Base32(10); byte[][] b = Base32TestData.randomData(codec, i); assertEquals(""+i+" "+codec.lineLength,b[1].length,codec.getEncodedLength(b[0])); //assertEquals(b[0],codec.decode(b[1])); } } public void testRandomBytesHex() { for (int i = 0; i < 20; i++) { Base32 codec = new Base32(true); byte[][] b = Base32TestData.randomData(codec, i); assertEquals(""+i+" "+codec.lineLength,b[1].length,codec.getEncodedLength(b[0])); //assertEquals(b[0],codec.decode(b[1])); } } }