Ejemplo n.º 1
0
        public void ShouldBeModifiable(int repeats)
        {
            for (int i = 0; i < repeats; i++)
            {
                PrivateKey key      = PrivateKey.CreateKey();
                byte[]     rndBytes = RndProvider.GetNonZeroBytes(1024);

                Payload body = new Payload.Builder()
                               .Value("rndBytes", rndBytes)
                               .Build();

                Message message = new Message.Builder()
                                  .WithBody(body)
                                  .Build(key);

                // serialize & deserialize
                Message received = Message.Parse(message.ToBytes());
                Assert.True(received.Verify(key.Address));

                // verify ok if header changed
                received.Headers.Set("timestamp", DateTime.Now);
                Assert.True(received.Verify(key.Address));

                // verify failed if body changed
                received.Body.Set("id", RndProvider.Next(100));
                Assert.False(received.Verify(key.Address));
            }
        }
Ejemplo n.º 2
0
        public void ShouldBeVerifiable(int repeats)
        {
            for (int i = 0; i < repeats; i++)
            {
                PrivateKey key      = PrivateKey.CreateKey();
                PrivateKey wrongKey = key.CKD(key.Bytes);
                byte[]     rndBytes = RndProvider.GetNonZeroBytes(1024);

                Payload body = new Payload.Builder()
                               .Value("rndBytes", rndBytes)
                               .Build();

                Message message = new Message.Builder()
                                  .WithBody(body)
                                  .Build(key);

                // serialize & deserialize
                Message received = Message.Parse(message.ToBytes());

                Assert.True(received.Verify(key.Address));
                Assert.False(received.Verify(wrongKey.Address));

                // verify failed if body has changed
                received.Body.Set("rndBytes", RndProvider.GetNonZeroBytes(128));
                Assert.False(received.Verify(key.Address));
            }
        }
        public void ShouldBeSerializable()
        {
            string name = RndProvider.GetString(16);
            int    age  = RndProvider.Next(1, 100);

            byte[] rndBytes = RndProvider.GetNonZeroBytes(128);

            // build
            Payload payload = new Payload.Builder()
                              .Value("name", name)
                              .Value("age", age)
                              .Value("rndBytes", rndBytes)
                              .Build();

            // serialize & deserialize
            Payload received = Payload.Parse(payload.ToBytes());

            // assert
            Assert.Equal(name, received.Value <string>("name"));
            Assert.Equal(age, received.Value <int>("age"));
            Assert.True(rndBytes.SequenceEqual(received.Value <byte[]>("rndBytes")));
            Assert.Equal(payload.Hash.ToHex(), received.Hash.ToHex());

            Assert.Equal(payload.Hash, received.Hash);
        }
Ejemplo n.º 4
0
        public void ShouldBeSerializable(int repeats)
        {
            for (int i = 0; i < repeats; i++)
            {
                PrivateKey key      = PrivateKey.CreateKey();
                byte[]     rndBytes = RndProvider.GetNonZeroBytes(1024);

                Payload body = new Payload.Builder()
                               .Value("rndBytes", rndBytes)
                               .Build();

                Message message = new Message.Builder()
                                  .WithBody(body)
                                  .Build(key);

                // serialize & deserialize
                Message received = Message.Parse(message.ToBytes());

                Assert.Equal(message.Hash, received.Hash);
                Assert.Equal(rndBytes, message.Value <byte[]>("rndBytes"));
                Assert.Equal(rndBytes, received.Value <byte[]>("rndBytes"));
                Assert.Equal(message.Value <byte[]>("rndBytes"), received.Value <byte[]>("rndBytes"));
                Assert.Equal(message.Headers.Hash, received.Headers.Hash);
                Assert.Equal(message.Body.Hash, received.Body.Hash);
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            ILoggable logger = new BLog.Builder()
                               .WithConsole(true)
                               .WithFilter(LogFilter.All)
                               .Build();

            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                Console.CancelKeyPress += (sender, e) =>
                {
                    e.Cancel = true;
                    cts.Cancel();
                };

                while (!cts.IsCancellationRequested)
                {
//                    Thread.Sleep(100);

                    Payload payload = new Payload.Builder()
                                      .Value("rndBytes", RndProvider.GetNonZeroBytes(MessageLength))
                                      .Build();

                    TcpClient.SendTo("127.0.0.1", PORT, payload.ToBytes());
                }
            }
        }
        public void ShouldBeAbleToOverlap()
        {
            Payload header = new Payload.Builder()
                             .Value("ver", 0x01)
                             .Build();

            Payload body = new Payload.Builder()
                           .Value("message", "Hello, Bryllite!")
                           .Value("rndBytes", RndProvider.GetNonZeroBytes(128))
                           .Build();

            Payload message = new Payload.Builder()
                              .Value("header", header)
                              .Value("body", body)
                              .Build();

            Payload received = Payload.Parse(message.ToBytes());

            Payload receivedHeader = received.Value <Payload>("header");
            Payload receivedBody   = received.Value <Payload>("body");

            Assert.Equal(message.Hash, received.Hash);
            Assert.Equal(header.Hash, receivedHeader.Hash);
            Assert.Equal(body.Hash, receivedBody.Hash);

            Assert.Equal(header.Value <int>("ver"), receivedHeader.Value <int>("ver"));
            Assert.Equal(body.Value <string>("message"), receivedBody.Value <string>("message"));
            Assert.Equal(body.Value <byte[]>("rndBytes"), receivedBody.Value <byte[]>("rndBytes"));
        }
        public void Test1()
        {
            Payload payload = new Payload.Builder()
                              .Value("exists", true)
                              .Build();

            Assert.True(payload.Value <bool>("exists"));
            Assert.False(payload.Value <bool>("not exists"));
        }
        public static void RouteTo(this Message message, byte ttl, Elastic3D to, ElasticLayout layout, PrivateKey routerKey)
        {
            Payload routes = new Payload.Builder()
                             .Value(TTL, ttl)
                             .Value(TO, to)
                             .Value(LAYOUT, layout)
                             .Value(ROUTER, routerKey.Address.HexAddress)
                             .Value(ROUTERSIG, routerKey.Sign(message.Body.Hash).ToBytes())
                             .Build();

            message.Headers.Set(ROUTES, routes);
        }
Ejemplo n.º 9
0
        private static byte[] ConvertToByteArray(SessionCollection sessionCollection)
        {
            var c = sessionCollection.Collection;

            var cbuilder = new Collection.Builder();

            cbuilder.DeviceId = c.DeviceId;

            cbuilder.ClientId = c.ClientId;

            cbuilder.Session = c.Session;

            cbuilder.SystemVersion = c.SystemVersion;

            cbuilder.ProductVersion = c.ProductVersion;

            cbuilder.System = c.System;

            cbuilder.ProductGitHash = c.ProductGitHash;

            foreach (var p in c.Payloads)
            {
                var pBuilder = new Payload.Builder();

                pBuilder.At = p.At;

                pBuilder.Event = p.Event;

                pBuilder.Nr = p.Nr;

                pBuilder.UserId = p.UserId;

                pBuilder.Ip = p.Ip;

                pBuilder.Parameters = p.Parameters;

                pBuilder.IsTesting = p.IsTesting;

                var payload = pBuilder.Build();

                cbuilder.AddPayloads(payload);
            }

            return cbuilder.Build().ToByteArray();
        }
Ejemplo n.º 10
0
        private static byte[] ConvertToByteArray(SessionCollection sessionCollection)
        {
            var c = sessionCollection.Collection;

            var cbuilder = new Collection.Builder();

            cbuilder.DeviceId = c.DeviceId;

            cbuilder.ClientId = c.ClientId;

            cbuilder.Session = c.Session;

            cbuilder.SystemVersion = c.SystemVersion;

            cbuilder.ProductVersion = c.ProductVersion;

            cbuilder.System = c.System;

            cbuilder.ProductGitHash = c.ProductGitHash;

            foreach (var p in c.Payloads)
            {
                var pBuilder = new Payload.Builder();

                pBuilder.At = p.At;

                pBuilder.Event = p.Event;

                pBuilder.Nr = p.Nr;

                pBuilder.UserId = p.UserId;

                pBuilder.Ip = p.Ip;

                pBuilder.Parameters = p.Parameters;

                pBuilder.IsTesting = p.IsTesting;

                var payload = pBuilder.Build();

                cbuilder.AddPayloads(payload);
            }

            return(cbuilder.Build().ToByteArray());
        }
        public void PayloadShouldBeParsable()
        {
            Elastic3D     to     = new Elastic3D(1, 2, 3);
            ElasticLayout layout = ElasticLayout.DefineLayout(1024);

            Payload payload = new Payload.Builder()
                              .Value("to", to)
                              .Value("layout", layout)
                              .Build();

            Payload received = Payload.Parse(payload.ToBytes());

            Elastic3D     receivedTo     = received.Value <Elastic3D>("to");
            ElasticLayout receivedLayout = received.Value <ElasticLayout>("layout");

            Assert.Equal(1, receivedTo.X);
            Assert.Equal(2, receivedTo.Y);
            Assert.Equal(3, receivedTo.Z);
            Assert.Equal(to, receivedTo);

            Assert.Equal(layout.X, receivedLayout.X);
            Assert.Equal(layout.Y, receivedLayout.Y);
            Assert.Equal(layout.Z, receivedLayout.Z);
        }