Пример #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));
            }
        }
        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);
        }
        public void Test2()
        {
            PrivateKey key = PrivateKey.CreateKey();

            ElasticLayout layout = ElasticLayout.DefineLayout(1024);
            Elastic3D     to     = new Elastic3D((byte)(1 + RndProvider.Next(layout.X)), (byte)(1 + RndProvider.Next(layout.Y)), (byte)(1 + RndProvider.Next(layout.Z)));

            Message message = new Message.Builder()
                              .Body("rndBytes", RndProvider.GetNonZeroBytes(128))
                              .Build(key);

            PrivateKey routerKey = PrivateKey.CreateKey();

            message.RouteTo(to.TimeToLive(), to, layout, routerKey);

            Payload routes = message.Routes();

            Assert.Equal(to.TimeToLive(), routes.Value <byte>("ttl"));
            Assert.Equal(to, routes.Value <Elastic3D>("to"));
            Assert.Equal(layout, routes.Value <ElasticLayout>("layout"));

            Address router = message.Router();

            Assert.Equal(routerKey.Address, router);
            Assert.True(message.VerifyRouter());

            message.TimeToLive(0);

            Assert.Equal(0, message.TimeToLive());

            Assert.Equal(0, new Elastic3D(0, 0, 0).TimeToLive());
            Assert.Equal(1, new Elastic3D(16, 0, 0).TimeToLive());
            Assert.Equal(2, new Elastic3D(13, 15, 0).TimeToLive());
            Assert.Equal(3, new Elastic3D(1, 2, 3).TimeToLive());
        }
        public static ElasticAddress Pick(this ElasticAddress[] peers, ElasticAddress me)
        {
            if (peers.Length == 0)
            {
                return(default(ElasticAddress));
            }

            if (peers.Contains(me))
            {
                return(me);
            }

            return(peers[RndProvider.Next(peers.Length)]);
        }