예제 #1
0
        [Test] public void Base64()
        {
            // All bytes from 0 to ff
            byte[] sbuf = new byte[256]; for (int i = 0; i != 256; ++i)
            {
                sbuf[i] = (byte)i;
            }
            byte[] dbuf = Encoding.ASCII.GetBytes("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIj" +
                                                  "JCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZH" +
                                                  "SElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWpr" +
                                                  "bG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P" +
                                                  "kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKz" +
                                                  "tLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX" +
                                                  "2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7" +
                                                  "/P3+/w==");

            int len = Base64Encoding.EncodeSize(sbuf.Length);

            Assert.Equal(dbuf.Length, len);

            int dst_length;

            byte[] dst = new byte[len];
            Base64Encoding.Encode(sbuf, dst, out dst_length);
            Assert.Equal(dbuf.Length, dst_length);
            for (int i = 0; i != dbuf.Length; ++i)
            {
                Assert.Equal(dbuf[i], dst[i]);
            }

            len = Base64Encoding.DecodeSize(dbuf.Length);
            Assert.True(len >= sbuf.Length);

            int src_length;

            byte[] src = new byte[len];
            Base64Encoding.Decode(dst, src, out src_length);
            Assert.Equal(sbuf.Length, src_length);
            for (int i = 0; i != sbuf.Length; ++i)
            {
                Assert.Equal(sbuf[i], src[i]);
            }

            // Random binary data
            Random r = new Random();

            for (int i = 0; i != sbuf.Length; ++i)
            {
                sbuf[i] = (byte)(r.Next(0xFF));
            }

            Base64Encoding.Encode(sbuf, dst, out dst_length);
            Base64Encoding.Decode(dst, src, out src_length);
            Assert.Equal(sbuf.Length, src_length);
            for (int i = 0; i != sbuf.Length; ++i)
            {
                Assert.Equal(sbuf[i], src[i]);
            }
        }
        public void Decode()
        {
            Base64Encoding e = new Base64Encoding();

            i = "dGVzdA==";
            o = e.DecodeAsString(i);
            Assert.Equal(o, "test");

            i = "dGVzdDA=";
            o = e.DecodeAsString(i);
            Assert.Equal(o, "test0");

            i = "dGVzdDAw";
            o = e.DecodeAsString(i);
            Assert.Equal(o, "test00");

            i = "dGVzdDAx";
            o = e.DecodeAsString(i);
            Assert.Equal(o, "test01");

            i = "dGVzdDAwMA==";
            o = e.DecodeAsString(i);
            Assert.Equal(o, "test000");

            byte[] bytes = null;

            bytes = e.Decode("AA==");
            Assert.Equal(bytes, new byte[] { 0, });

            bytes = e.Decode("AAA=");
            Assert.Equal(bytes, new byte[] { 0, 0, });

            bytes = e.Decode("AAAA");
            Assert.Equal(bytes, new byte[] { 0, 0, 0, });


            return;
        }