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 */
018
019package org.apache.commons.codec.binary;
020
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.fail;
023
024import java.util.Arrays;
025
026import org.apache.commons.codec.Charsets;
027import org.junit.Test;
028
029public class Base32Test {
030
031    private static final String [][] BASE32_TEST_CASES = { // RFC 4648
032        {""       ,""},
033        {"f"      ,"MY======"},
034        {"fo"     ,"MZXQ===="},
035        {"foo"    ,"MZXW6==="},
036        {"foob"   ,"MZXW6YQ="},
037        {"fooba"  ,"MZXW6YTB"},
038        {"foobar" ,"MZXW6YTBOI======"},
039    };
040
041    private static final String [][] BASE32HEX_TEST_CASES = { // RFC 4648
042        {""       ,""},
043        {"f"      ,"CO======"},
044        {"fo"     ,"CPNG===="},
045        {"foo"    ,"CPNMU==="},
046        {"foob"   ,"CPNMUOG="},
047        {"fooba"  ,"CPNMUOJ1"},
048        {"foobar" ,"CPNMUOJ1E8======"},
049    };
050
051
052    private static final String [][] BASE32_TEST_CASES_CHUNKED = { //Chunked
053        {""       ,""},
054        {"f"      ,"MY======\r\n"},
055        {"fo"     ,"MZXQ====\r\n"},
056        {"foo"    ,"MZXW6===\r\n"},
057        {"foob"   ,"MZXW6YQ=\r\n"},
058        {"fooba"  ,"MZXW6YTB\r\n"},
059        {"foobar" ,"MZXW6YTBOI======\r\n"},
060    };
061
062    @Test
063    public void testBase32Samples() throws Exception {
064        final Base32 codec = new Base32();
065        for (final String[] element : BASE32_TEST_CASES) {
066                assertEquals(element[1], codec.encodeAsString(element[0].getBytes(Charsets.UTF_8)));
067        }
068    }
069
070    @Test
071    public void testBase32HexSamples() throws Exception {
072        final Base32 codec = new Base32(true);
073        for (final String[] element : BASE32HEX_TEST_CASES) {
074                assertEquals(element[1], codec.encodeAsString(element[0].getBytes(Charsets.UTF_8)));
075        }
076    }
077
078    @Test
079    public void testBase32Chunked () throws Exception {
080        final Base32 codec = new Base32(20);
081        for (final String[] element : BASE32_TEST_CASES_CHUNKED) {
082                assertEquals(element[1], codec.encodeAsString(element[0].getBytes(Charsets.UTF_8)));
083        }
084    }
085
086    @Test
087    public void testSingleCharEncoding() {
088        for (int i = 0; i < 20; i++) {
089            Base32 codec = new Base32();
090            final BaseNCodec.Context context = new BaseNCodec.Context();
091            final byte unencoded[] = new byte[i];
092            final byte allInOne[] = codec.encode(unencoded);
093            codec = new Base32();
094            for (int j=0; j< unencoded.length; j++) {
095                codec.encode(unencoded, j, 1, context);
096            }
097            codec.encode(unencoded, 0, -1, context);
098            final byte singly[] = new byte[allInOne.length];
099            codec.readResults(singly, 0, 100, context);
100            if (!Arrays.equals(allInOne, singly)){
101                fail();
102            }
103        }
104    }
105
106    @Test
107    public void testRandomBytes() {
108        for (int i = 0; i < 20; i++) {
109            final Base32 codec = new Base32();
110            final byte[][] b = Base32TestData.randomData(codec, i);
111            assertEquals(""+i+" "+codec.lineLength,b[1].length,codec.getEncodedLength(b[0]));
112            //assertEquals(b[0],codec.decode(b[1]));
113        }
114    }
115
116    @Test
117    public void testRandomBytesChunked() {
118        for (int i = 0; i < 20; i++) {
119            final Base32 codec = new Base32(10);
120            final byte[][] b = Base32TestData.randomData(codec, i);
121            assertEquals(""+i+" "+codec.lineLength,b[1].length,codec.getEncodedLength(b[0]));
122            //assertEquals(b[0],codec.decode(b[1]));
123        }
124    }
125
126    @Test
127    public void testRandomBytesHex() {
128        for (int i = 0; i < 20; i++) {
129            final Base32 codec = new Base32(true);
130            final byte[][] b = Base32TestData.randomData(codec, i);
131            assertEquals(""+i+" "+codec.lineLength,b[1].length,codec.getEncodedLength(b[0]));
132            //assertEquals(b[0],codec.decode(b[1]));
133        }
134    }
135}