|
| 1 | +package com.github.scribejava.core.base64; |
| 2 | + |
| 3 | +import java.io.UnsupportedEncodingException; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | +import static org.junit.Assert.assertEquals; |
| 8 | +import static org.junit.Assert.assertArrayEquals; |
| 9 | + |
| 10 | +public class Base64Test { |
| 11 | + |
| 12 | + private Base64 java8Base64; |
| 13 | + private byte[] helloWorldBytes; |
| 14 | + private byte[] helloWorldTwoLinesBytes; |
| 15 | + private byte[] helloWorldTwoLinesAndNewLineBytes; |
| 16 | + private byte[] helloWorldDifferentCharsBytes; |
| 17 | + |
| 18 | + @Before |
| 19 | + public void setUp() throws UnsupportedEncodingException { |
| 20 | + helloWorldBytes = "Hello World".getBytes("UTF-8"); |
| 21 | + helloWorldTwoLinesBytes = "Hello World\r\nNew Line2".getBytes("UTF-8"); |
| 22 | + helloWorldTwoLinesAndNewLineBytes = "Hello World\r\nSecond Line\r\n".getBytes("UTF-8"); |
| 23 | + helloWorldDifferentCharsBytes = ("`1234567890-=~!@#$%^&*()_+ёЁ\"№;:?qwertyuiop[]asdfghjkl;'zxcvbnm,./QWERTYUIOP" |
| 24 | + + "{}|ASDFGHJKL:ZXCVBNM<>?йфяцычувскамепинртгоьшлбщдюзж.хэъ\\ЙФЯЦЫЧУВСКАМЕПИНРТГОЬШЛБЩДЮЗЖ,ХЭЪ/\r\t\f\'" |
| 25 | + + "\b\n").getBytes("UTF-8"); |
| 26 | + java8Base64 = new Java8Base64(); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void allImplementationsAreAvailable() { |
| 31 | + assertTrue(Java8Base64.isAvailable()); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testEncode() { |
| 36 | + final String helloWorldEncoded = "SGVsbG8gV29ybGQ="; |
| 37 | + final String helloWorldTwoLinesEncoded = "SGVsbG8gV29ybGQNCk5ldyBMaW5lMg=="; |
| 38 | + final String helloWorldTwoLinesAndNewLineEncoded = "SGVsbG8gV29ybGQNClNlY29uZCBMaW5lDQo="; |
| 39 | + final String helloWorldDifferentCharsEncoded = "YDEyMzQ1Njc4OTAtPX4hQCMkJV4mKigpXyvRkdCBIuKEljs6P3F3ZXJ0eXVpb3B" |
| 40 | + + "bXWFzZGZnaGprbDsnenhjdmJubSwuL1FXRVJUWVVJT1B7fXxBU0RGR0hKS0w6WlhDVkJOTTw+P9C50YTRj9GG0YvRh9GD0LLRgdC" |
| 41 | + + "60LDQvNC10L/QuNC90YDRgtCz0L7RjNGI0LvQsdGJ0LTRjtC30LYu0YXRjdGKXNCZ0KTQr9Cm0KvQp9Cj0JLQodCa0JDQnNCV0J/" |
| 42 | + + "QmNCd0KDQotCT0J7QrNCo0JvQkdCp0JTQrtCX0JYs0KXQrdCqLw0JDCcICg=="; |
| 43 | + |
| 44 | + assertEquals(helloWorldEncoded, java8Base64.internalEncode(helloWorldBytes)); |
| 45 | + assertEquals(helloWorldTwoLinesEncoded, java8Base64.internalEncode(helloWorldTwoLinesBytes)); |
| 46 | + assertEquals(helloWorldTwoLinesAndNewLineEncoded, |
| 47 | + java8Base64.internalEncode(helloWorldTwoLinesAndNewLineBytes)); |
| 48 | + assertEquals(helloWorldDifferentCharsEncoded, java8Base64.internalEncode(helloWorldDifferentCharsBytes)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testEncodeUrlWithoutPadding() { |
| 53 | + final String helloWorldEncoded = "SGVsbG8gV29ybGQ"; |
| 54 | + final String helloWorldTwoLinesEncoded = "SGVsbG8gV29ybGQNCk5ldyBMaW5lMg"; |
| 55 | + final String helloWorldTwoLinesAndNewLineEncoded = "SGVsbG8gV29ybGQNClNlY29uZCBMaW5lDQo"; |
| 56 | + final String helloWorldDifferentCharsEncoded = "YDEyMzQ1Njc4OTAtPX4hQCMkJV4mKigpXyvRkdCBIuKEljs6P3F3ZXJ0eXVpb3B" |
| 57 | + + "bXWFzZGZnaGprbDsnenhjdmJubSwuL1FXRVJUWVVJT1B7fXxBU0RGR0hKS0w6WlhDVkJOTTw-P9C50YTRj9GG0YvRh9GD0LLRgdC" |
| 58 | + + "60LDQvNC10L_QuNC90YDRgtCz0L7RjNGI0LvQsdGJ0LTRjtC30LYu0YXRjdGKXNCZ0KTQr9Cm0KvQp9Cj0JLQodCa0JDQnNCV0J_" |
| 59 | + + "QmNCd0KDQotCT0J7QrNCo0JvQkdCp0JTQrtCX0JYs0KXQrdCqLw0JDCcICg"; |
| 60 | + |
| 61 | + assertEquals(helloWorldEncoded, java8Base64.internalEncodeUrlWithoutPadding(helloWorldBytes)); |
| 62 | + assertEquals(helloWorldTwoLinesEncoded, java8Base64.internalEncodeUrlWithoutPadding(helloWorldTwoLinesBytes)); |
| 63 | + assertEquals(helloWorldTwoLinesAndNewLineEncoded, |
| 64 | + java8Base64.internalEncodeUrlWithoutPadding(helloWorldTwoLinesAndNewLineBytes)); |
| 65 | + assertEquals(helloWorldDifferentCharsEncoded, |
| 66 | + java8Base64.internalEncodeUrlWithoutPadding(helloWorldDifferentCharsBytes)); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testDecodeMime() { |
| 71 | + final String helloWorldEncoded = "SGVsbG8gV29ybGQ="; |
| 72 | + final String helloWorldTwoLinesEncoded = "SGVsbG8gV29ybGQNCk5ldyBMaW5lMg=="; |
| 73 | + final String helloWorldTwoLinesAndNewLineEncoded = "SGVsbG8gV29ybGQNClNlY29uZCBMaW5lDQo="; |
| 74 | + final String helloWorldDifferentCharsEncoded = "YDEyMzQ1Njc4OTAtPX4hQCMkJV4mKigpXyvRkdCBIuKEljs6P3F3ZXJ0eXVpb3B" |
| 75 | + + "bXWFzZGZnaGprbDsnenhjdmJubSwuL1FXRVJUWVVJT1B7fXxBU0RGR0hKS0w6WlhDVkJOTTw+P9C50YTRj9GG0YvRh9GD0LLRgdC" |
| 76 | + + "60LDQvNC10L/QuNC90YDRgtCz0L7RjNGI0LvQsdGJ0LTRjtC30LYu0YXRjdGKXNCZ0KTQr9Cm0KvQp9Cj0JLQodCa0JDQnNCV0J/" |
| 77 | + + "QmNCd0KDQotCT0J7QrNCo0JvQkdCp0JTQrtCX0JYs0KXQrdCqLw0JDCcICg=="; |
| 78 | + |
| 79 | + assertArrayEquals(helloWorldBytes, java8Base64.internalDecodeMime(helloWorldEncoded)); |
| 80 | + assertArrayEquals(helloWorldTwoLinesBytes, java8Base64.internalDecodeMime(helloWorldTwoLinesEncoded)); |
| 81 | + assertArrayEquals(helloWorldTwoLinesAndNewLineBytes, |
| 82 | + java8Base64.internalDecodeMime(helloWorldTwoLinesAndNewLineEncoded)); |
| 83 | + assertArrayEquals(helloWorldDifferentCharsBytes, |
| 84 | + java8Base64.internalDecodeMime(helloWorldDifferentCharsEncoded)); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments