|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2019 Nathan Rajlich |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person |
| 5 | + * obtaining a copy of this software and associated documentation |
| 6 | + * files (the "Software"), to deal in the Software without |
| 7 | + * restriction, including without limitation the rights to use, |
| 8 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the |
| 10 | + * Software is furnished to do so, subject to the following |
| 11 | + * conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be |
| 14 | + * included in all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + */ |
| 25 | + |
| 26 | +package org.java_websocket.util; |
| 27 | + |
| 28 | +import org.junit.Assert; |
| 29 | +import org.junit.Rule; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.rules.ExpectedException; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | + |
| 35 | +public class Base64Test { |
| 36 | + |
| 37 | + @Rule public final ExpectedException thrown = ExpectedException.none(); |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testEncodeBytes() throws IOException { |
| 41 | + Assert.assertEquals("", Base64.encodeBytes(new byte[0])); |
| 42 | + Assert.assertEquals("QHE=", |
| 43 | + Base64.encodeBytes(new byte[] {49, 121, 64, 113, -63, 43, -24, 62, 4, 48}, 2, 2, 0)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testEncodeBytes2() throws IOException { |
| 48 | + thrown.expect(IllegalArgumentException.class); |
| 49 | + Base64.encodeBytes(new byte[0], -2, -2, -56); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testEncodeBytes3() throws IOException { |
| 54 | + thrown.expect(IllegalArgumentException.class); |
| 55 | + Base64.encodeBytes(new byte[] {64, -128, 32, 18, 16, 16, 0, 18, 16}, |
| 56 | + 2064072977, -2064007440, 10); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testEncodeBytes4() { |
| 61 | + thrown.expect(NullPointerException.class); |
| 62 | + Base64.encodeBytes(null); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testEncodeBytes5() throws IOException { |
| 67 | + thrown.expect(IllegalArgumentException.class); |
| 68 | + Base64.encodeBytes(null, 32766, 0, 8); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testEncodeBytesToBytes1() throws IOException { |
| 73 | + Assert.assertArrayEquals(new byte[] {95, 68, 111, 78, 55, 45, 61, 61}, |
| 74 | + Base64.encodeBytesToBytes(new byte[] {-108, -19, 24, 32}, 0, 4, 32)); |
| 75 | + Assert.assertArrayEquals(new byte[] {95, 68, 111, 78, 55, 67, 111, 61}, |
| 76 | + Base64.encodeBytesToBytes(new byte[] {-108, -19, 24, 32, -35}, 0, 5, 40)); |
| 77 | + Assert.assertArrayEquals(new byte[] {95, 68, 111, 78, 55, 67, 111, 61}, |
| 78 | + Base64.encodeBytesToBytes(new byte[] {-108, -19, 24, 32, -35}, 0, 5, 32)); |
| 79 | + Assert.assertArrayEquals(new byte[] {87, 50, 77, 61}, |
| 80 | + Base64.encodeBytesToBytes(new byte[] {115, 42, 123, 99, 10, -33, 75, 30, 91, 99}, 8, 2, 48)); |
| 81 | + Assert.assertArrayEquals(new byte[] {87, 50, 77, 61}, |
| 82 | + Base64.encodeBytesToBytes(new byte[] {115, 42, 123, 99, 10, -33, 75, 30, 91, 99}, 8, 2, 56)); |
| 83 | + Assert.assertArrayEquals(new byte[] {76, 53, 66, 61}, |
| 84 | + Base64.encodeBytesToBytes(new byte[] {113, 42, 123, 99, 10, -33, 75, 30, 88, 99}, 8, 2, 36));Assert.assertArrayEquals(new byte[] {87, 71, 77, 61}, |
| 85 | + Base64.encodeBytesToBytes(new byte[] {113, 42, 123, 99, 10, -33, 75, 30, 88, 99}, 8, 2, 4)); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testEncodeBytesToBytes2() throws IOException { |
| 90 | + thrown.expect(IllegalArgumentException.class); |
| 91 | + Base64.encodeBytesToBytes(new byte[] {83, 10, 91, 67, 42, -1, 107, 62, 91, 67}, 8, 6, 26); |
| 92 | + } |
| 93 | +} |
0 commit comments