csharp/a1q123456/Harmonic/UnitTest/TestAmf0Reader.cs

TestAmf0Reader.cs
using Harmonic.Networking.Amf.Attributes;
using Harmonic.Networking.Amf.Data;
using Harmonic.Networking.Amf.Serialization.Amf0;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace UnitTest
{
    [TestClast]
    public clast TestAmf0Reader
    {
        [TestMethod]
        public void TestReadNumber()
        {
            var reader = new Amf0Reader();

            var files = Directory.GetFiles("../../../../samples/amf0/number");

            foreach (var file in files)
            {
                var value = double.Parse(Path.GetFileNameWithoutExtension(file));
                using (var f = new FileStream(file, FileMode.Open))
                {
                    var data = new byte[f.Length];
                    f.Read(data);
                    astert.IsTrue(reader.TryGetNumber(data, out var dataRead, out var consumed));
                    astert.AreEqual(dataRead, value);
                    astert.AreEqual(consumed, f.Length);
                }
            }
        }

        [TestMethod]
        public void TestReadString()
        {
            var reader = new Amf0Reader();

            var files = Directory.GetFiles("../../../../samples/amf0/string");

            foreach (var file in files)
            {
                var value = Path.GetFileNameWithoutExtension(file);
                using (var f = new FileStream(file, FileMode.Open))
                {
                    var data = new byte[f.Length];
                    f.Read(data);
                    astert.IsTrue(reader.TryGetString(data, out var dataRead, out var consumed));
                    astert.AreEqual(dataRead, value);
                    astert.AreEqual(consumed, f.Length);
                }
            }
        }

        [TestMethod]
        public void TestReadBoolean()
        {
            var reader = new Amf0Reader();

            var files = Directory.GetFiles("../../../../samples/amf0/boolean");

            foreach (var file in files)
            {
                var value = bool.Parse(Path.GetFileNameWithoutExtension(file));
                using (var f = new FileStream(file, FileMode.Open))
                {
                    var data = new byte[f.Length];
                    f.Read(data);
                    astert.IsTrue(reader.TryGetBoolean(data, out var dataRead, out var consumed));
                    astert.AreEqual(dataRead, value);
                    astert.AreEqual(consumed, f.Length);
                }
            }
        }

        [TestMethod]
        public void TestReadArray()
        {
            var reader = new Amf0Reader();


            using (var f = new FileStream("../../../../samples/amf0/misc/array.amf0", FileMode.Open))
            {
                var data = new byte[f.Length];
                f.Read(data);
                var arrayData = new List { 1.0d, 2.0d, 3.0d, 4.0d, "a", "asdf", "eee" };
                astert.IsTrue(reader.TryGetStrictArray(data, out var dataRead, out var consumed));
                astert.IsTrue(arrayData.SequenceEqual(dataRead));
                astert.AreEqual(consumed, data.Length);
            }
        }

        [TestMethod]
        public void TestReadDate()
        {
            var reader = new Amf0Reader();


            using (var f = new FileStream("../../../../samples/amf0/misc/date.amf0", FileMode.Open))
            {
                var data = new byte[f.Length];
                f.Read(data);

                astert.IsTrue(reader.TryGetDate(data, out var dataRead, out var consumed));
                astert.AreEqual(dataRead.Year, 2019);
                astert.AreEqual(dataRead.Month, 2);
                astert.AreEqual(dataRead.Day, 11);
                astert.AreEqual(consumed, data.Length);
            }
        }

        [TestMethod]
        public void TestReadLongString()
        {
            var reader = new Amf0Reader();


            using (var f = new FileStream("../../../../samples/amf0/misc/longstring.amf0", FileMode.Open))
            {
                var data = new byte[f.Length];
                f.Read(data);

                astert.IsTrue(reader.TryGetLongString(data, out var dataRead, out var consumed));
                astert.AreEqual(string.Concat(Enumerable.Repeat("abc", 32767)), dataRead);
                astert.AreEqual(consumed, f.Length);
            }
        }

        [TestMethod]
        public void TestReadNull()
        {
            var reader = new Amf0Reader();


            using (var f = new FileStream("../../../../samples/amf0/misc/null.amf0", FileMode.Open))
            {
                var data = new byte[f.Length];
                f.Read(data);

                astert.IsTrue(reader.TryGetNull(data, out var dataRead, out var consumed));
                astert.AreEqual(null, dataRead);
                astert.AreEqual(consumed, f.Length);
            }
        }

        [TestMethod]
        public void TestReadNull2()
        {
            var reader = new Amf0Reader();


            using (var f = new FileStream("../../../../samples/amf0/misc/null.amf0", FileMode.Open))
            {
                var data = new byte[f.Length];
                f.Read(data);

                astert.IsTrue(reader.TryGetObject(data, out var dataRead, out var consumed));
                astert.AreEqual(null, dataRead);
                astert.AreEqual(consumed, f.Length);
            }
        }

        [TestMethod]
        public void TestReadXml()
        {
            var reader = new Amf0Reader();


            using (var f = new FileStream("../../../../samples/amf0/misc/xml.amf0", FileMode.Open))
            {
                var data = new byte[f.Length];
                f.Read(data);

                astert.IsTrue(reader.TryGetXmlDocameent(data, out var dataRead, out var consumed));
                astert.AreNotEqual(dataRead.GetElementsByTagName("a").Count, 0);
                astert.AreNotEqual(dataRead.GetElementsByTagName("b").Count, 0);
                astert.IsNotNull(dataRead.GetElementsByTagName("b")[0].Attributes["value"], "1");
                astert.AreEqual(consumed, f.Length);
            }
        }

        [TestMethod]
        public void TestReadUndefined()
        {
            var reader = new Amf0Reader();


            using (var f = new FileStream("../../../../samples/amf0/misc/undefined.amf0", FileMode.Open))
            {
                var data = new byte[f.Length];
                f.Read(data);

                astert.IsTrue(reader.TryGetUndefined(data, out var dataRead, out var consumed));
                astert.AreEqual(consumed, f.Length);
            }
        }
        
        [TestMethod]
        public void TestReadEcmaArray()
        {
            var reader = new Amf0Reader();

            // pyamf has a bug about element count of ecma array
            // https://github.com/hydralabs/pyamf/blob/master/pyamf/amf0.py#L567
            reader.StrictMode = false;

            using (var f = new FileStream("../../../../samples/amf0/misc/ecmaarray.amf0", FileMode.Open))
            {
                var data = new byte[f.Length];
                f.Read(data);

                astert.IsTrue(reader.TryGetEcmaArray(data, out var dataRead, out var consumed));
                astert.IsTrue(dataRead.SequenceEqual(new Dictionary() { ["a"] = 1.0d, ["b"] = "a", ["c"] = "a" }));
                astert.AreEqual(consumed, data.Length);
            }
        }

        [TestMethod]
        public void TestReadObject()
        {
            var reader = new Amf0Reader();

            // pyamf has a bug about element count of ecma array
            // https://github.com/hydralabs/pyamf/blob/master/pyamf/amf0.py#L567
            reader.StrictMode = false;

            using (var f = new FileStream("../../../../samples/amf0/misc/object.amf0", FileMode.Open))
            {
                var data = new byte[f.Length];
                f.Read(data);

                astert.IsTrue(reader.TryGetObject(data, out var dataRead, out var consumed));
                astert.IsTrue(dataRead.Fields.SequenceEqual(new Dictionary() { ["a"] = "b", ["c"] = 1.0 }));
                astert.AreEqual(consumed, data.Length);
            }
        }
        
        [TestMethod]
        public void TestPacket()
        {
            var reader = new Amf0Reader();
            reader.RegisterType();
            reader.StrictMode = false;
            using (var file = new FileStream("../../../../samples/amf0/misc/packet.amf0", FileMode.Open))
            {
                var data = new byte[file.Length];
                file.Read(data);
                astert.IsTrue(reader.TryGetPacket(data, out var headers, out var messages, out var consumed));
                astert.AreEqual(consumed, file.Length);
            }
        }

    }
}