package Base64Tests import Base64 import Bitwise import ChunkedString @Test function base64EncoderBigChunksTest() let encoder = new Base64Encoder(128) // Hell encoder..writeByte(0x48) ..writeByte(0x65) ..writeByte(0x6c) ..writeByte(0x6c) // o, w encoder..writeByte(0x6f) ..writeByte(0x2c) ..writeByte(0x20) ..writeByte(0x77) // orld encoder..writeByte(0x6f) ..writeByte(0x72) ..writeByte(0x6c) ..writeByte(0x64) // ! encoder.writeByte(0x21) let data = encoder.intoData() data.getUnsafeString().assertEquals("SGVsbG8sIHdvcmxkIQ==") destroy data @Test function base64EncoderSmallChunksTest() let encoder = new Base64Encoder(16) ..writeInt(byte4(0x48, 0x65, 0x6c, 0x6c).toInt()) ..writeInt(byte4(0x6f, 0x2c, 0x20, 0x77).toInt()) ..writeInt(byte4(0x6f, 0x72, 0x6c, 0x64).toInt()) ..writeInt(byte4(0x21, 0x20, 0x20, 0x20).toInt()) let data = encoder.intoData() var str = "" while data.hasChunk() str += data.readChunk() destroy data str.assertEquals("SGVsbG8sIHdvcmxkISAgIA==") @Test function base64DecoderTest() let decoder = new Base64Decoder()..append("SGVsbG8sIHdvcmxkIQ==") let data = decoder.intoData() data.readByte().assertEquals(0x48) data.readByte().assertEquals(0x65) data.readByte().assertEquals(0x6c) data.readByte().assertEquals(0x6c) data.readByte().assertEquals(0x6f) data.readByte().assertEquals(0x2c) data.readByte().assertEquals(0x20) data.readByte().assertEquals(0x77) data.readByte().assertEquals(0x6f) data.readByte().assertEquals(0x72) data.readByte().assertEquals(0x6c) data.readByte().assertEquals(0x64) data.readByte().assertEquals(0x21) destroy data @Test function base64NegativeIntegersTest() let encoder = new Base64Encoder(16) ..writeInt(-1000) ..writeInt(1000) ..writeShort(1000) let data = new Base64Decoder()..consume(encoder.intoData()).intoData() data.readInt().assertEquals(-1000) data.readInt().assertEquals(1000) data.readShort().assertEquals(1000) destroy data constant bytes = [0xb3, 0xfb, 0xc6, 0xec, 0x89, 0xf9, 0x25, 0x22, 0x4a, 0xe0, 0x99, 0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JV+iOr/ulicukbFQfc74hI4PgjvjM5+0QW6UX6187vL0zsIZF/qeLdctHeVlrQngB1d", "G3UqtI/CjGSD3qYVRT7/4kVoWn+FOzCQvOWItXbU4n35fv7RXaBpWzkyAJKOhDAeJ5+KbbhnBycsZuwm4DHnQ+brJd3SEYEm69KE", "1bWbThBpYxlbD8Lt+Uyzo7YE3876VdXTDd46hWzgoytBrHx9+XGqVGMqEkREbUm7usNH76e2Cm1PcDNz6AXNBL9uhXIJCpTLcBB6", "C1A41wRZhhfTC0VHzZ9eXkMEEm/smDzlpt/x/4R5syb31HqGiwMj+YwkTMIVCW+EvCDBTGAKMBIOdAPkfvAu49B762jERP9e1/v1", "8/R/L8YcMOBeSCCQRYRXGLNSLRqbQdxiYGq1hZXDiehdESn9YCL2NFLcbuFwyJIGpDEe/SRCXy5g05VtPDZbbbpUM7rkK2ydI2b+", "AfJk67jP+xnGiUNCGZ8T65qkzcS7pp1OCOVHBl7Vyq5nqLMo1mtjiheUOI6KDFtR6C1Jcf3Pvk8/dq5f3SEvB3nUwxkCO+bk1T1M", "sfVqD3G6hleE6G69vZGqrwnbeS0JLaOy510Wv5uMeN7fN3NQ3Hlc18OUGEOIUezi/NfJ6cNWsx+1cyBVWq3yx1xEQlEoz4xjB9/Z", "iQmkvGIIfy0q+7YhEiCaYPOQekGZSPIqCIcw7+oH2Kuwu9kFH/+2a/4UNrJYkuZPP6Gpew5sYuXy8ykPdQeWohGWyrhKaVEAHEuc", "UypF7pAoGg8K/MX6OPe5o9Ue91F7T+iShY8RplNX9FzlwqecVlv2TpeXNgvVvollBQ=="] @Test function base64EncoderStressTest() let encoder = new Base64Encoder() for i = 0 to bytes.length - 1 encoder.writeByte(bytes[i]) let data = encoder.intoData() var actual = "" // the test string is less than the limit while data.hasChunk() actual += data.readChunk() destroy data var expected = "" for i = 0 to chunks.length - 1 expected += chunks[i] actual.assertEquals(expected) @Test function base64DecoderStressTest() let decoder = new Base64Decoder() for i = 0 to chunks.length - 1 decoder.append(chunks[i]) let data = decoder.intoData() data.size().assertEquals(bytes.length) for i = 0 to data.size() - 1 data.getByte(i).assertEquals(bytes[i]) destroy data