Beispiel #1
0
        public void Serialize()
        {
            var buffer = new byte[1024];
            var random = new Random();

            byte[] hash = random.NextBytes(20);
            var    f    = new Fingerprint(ValueKind.List, 123, hash);

            Assert.Equal(
                new Fingerprint(ValueKind.List, 123, hash.ToImmutableList()).Serialize(),
                f.Serialize()
                );
            Assert.Equal(f, Fingerprint.Deserialize(f.Serialize()));
            long estimatedSize = f.CountSerializationBytes();

            Assert.Equal(f.Serialize().LongLength, estimatedSize);
            long writtenSize = f.SerializeInto(buffer, 128L);

            Assert.Equal(estimatedSize, writtenSize);
            AssertEqual(f.Serialize(), buffer.Skip(128).Take((int)writtenSize).ToArray());
            Assert.Equal(f, Fingerprint.Deserialize(buffer, 128L, writtenSize));

            var s         = new MemoryStream();
            var formatter = new BinaryFormatter();

            formatter.Serialize(s, f);
            s.Seek(0, SeekOrigin.Begin);
            Assert.Equal(f, (Fingerprint)formatter.Deserialize(s));

            hash = random.NextBytes(20);
            f    = new Fingerprint(ValueKind.Dictionary, 456, hash);
            Assert.Equal(f, Fingerprint.Deserialize(f.Serialize()));
            estimatedSize = f.CountSerializationBytes();
            Assert.Equal(f.Serialize().LongLength, estimatedSize);
            writtenSize = f.SerializeInto(buffer, 512L);
            Assert.Equal(estimatedSize, writtenSize);
            AssertEqual(f.Serialize(), buffer.Skip(512).Take((int)writtenSize).ToArray());
            Assert.Equal(f, Fingerprint.Deserialize(buffer, 512L, writtenSize));

            byte[]          tooShort = f.Serialize().Take(8).ToArray();
            FormatException e        = Assert.Throws <FormatException>(
                () => Fingerprint.Deserialize(tooShort)
                );

            Assert.Contains("too short", e.Message, StringComparison.OrdinalIgnoreCase);

            byte[] invalidKind = f.Serialize();
            invalidKind[0] = byte.MaxValue;
            e = Assert.Throws <FormatException>(
                () => Fingerprint.Deserialize(invalidKind)
                );
            Assert.Contains("invalid value kind", e.Message, StringComparison.OrdinalIgnoreCase);

            s = new MemoryStream();
            formatter.Serialize(s, f);
            s.Seek(0, SeekOrigin.Begin);
            Assert.Equal(f, (Fingerprint)formatter.Deserialize(s));
        }
Beispiel #2
0
        public void Equality()
        {
            byte[] hashA = new Random().NextBytes(20);
            var    hashB = new byte[hashA.Length];

            hashA.CopyTo(hashB, 0);
            hashB[19] = (byte)(hashA[19] >= 0xff ? 0 : hashA[19] + 1);
            Assert.NotEqual(hashA, hashB);

            var l123A = new Fingerprint(ValueKind.List, 123, hashA);

            Assert.False(l123A.Equals(null));
            Assert.False(l123A.Equals("other"));

            var l123A_ = new Fingerprint(ValueKind.List, 123, hashA.ToImmutableList());

            Assert.Equal(l123A, l123A_);
            Assert.True(l123A.Equals((object)l123A_));
            Assert.Equal(l123A.GetHashCode(), l123A_.GetHashCode());
            Assert.Equal(l123A.Serialize(), l123A_.Serialize());
            Assert.True(l123A == l123A_);
            Assert.False(l123A != l123A_);

            var d123A = new Fingerprint(ValueKind.Dictionary, 123, hashA);

            Assert.NotEqual(l123A, d123A);
            Assert.False(l123A.Equals((object)d123A));
            Assert.NotEqual(l123A.GetHashCode(), d123A.GetHashCode());
            Assert.NotEqual(l123A.Serialize(), d123A.Serialize());
            Assert.False(l123A == d123A);
            Assert.True(l123A != d123A);

            var l122A = new Fingerprint(ValueKind.List, 122, hashA);

            Assert.NotEqual(l123A, l122A);
            Assert.False(l123A.Equals((object)l122A));
            Assert.NotEqual(l123A.GetHashCode(), l122A.GetHashCode());
            Assert.NotEqual(l123A.Serialize(), l122A.Serialize());
            Assert.False(l123A == l122A);
            Assert.True(l123A != l122A);

            var l123B = new Fingerprint(ValueKind.List, 123, hashB);

            Assert.NotEqual(l123A, l123B);
            Assert.False(l123A.Equals((object)l123B));
            Assert.NotEqual(l123A.GetHashCode(), l123B.GetHashCode());
            Assert.NotEqual(l123A.Serialize(), l123B.Serialize());
            Assert.False(l123A == l123B);
            Assert.True(l123A != l123B);
        }
Beispiel #3
0
        private static Fingerprint TwiddleHashByte(Fingerprint fingerprint, int index)
        {
            Contract.Requires(index >= 0 && index < FingerprintUtilities.FingerprintLength);

            var buffer = new byte[FingerprintUtilities.FingerprintLength];

            fingerprint.Serialize(buffer);
            buffer[index] = unchecked ((byte)~buffer[index]);

            return(FingerprintUtilities.CreateFrom(buffer));
        }
Beispiel #4
0
        public void TestSerialization()
        {
            Fingerprint fp1 = new Fingerprint(65);
            Fingerprint fp2 = new Fingerprint(65);

            fp1.Touch(0);
            fp1.Touch(2);
            fp1.Touch(31);
            fp1.Touch(32);

            Assert.False(fp2.Equals(fp1));

            var buffer = new x2net.Buffer();

            fp1.Serialize(new Serializer(buffer));
            buffer.Rewind();
            fp2.Deserialize(new Deserializer(buffer));

            Assert.True(fp2.Equals(fp1));
        }
 private string GetKey(Fingerprint weakFingerprint) => $"WF_{weakFingerprint.Serialize()}";
Beispiel #6
0
 /// <summary>
 /// Copies the content hash to a byte array starting at the given offset.
 /// </summary>
 /// <remarks>
 /// This requires that offset + Length be
 /// at most the length of the array.
 /// </remarks>
 public void CopyTo(byte[] buffer, int offset)
 {
     RawHash.Serialize(buffer, offset);
 }
Beispiel #7
0
        public void TestSerialization()
        {
            Fingerprint fp1 = new Fingerprint(65);
            Fingerprint fp2 = new Fingerprint(65);

            fp1.Touch(0);
            fp1.Touch(2);
            fp1.Touch(31);
            fp1.Touch(32);

            Assert.False(fp2.Equals(fp1));

            var buffer = new x2.Buffer();
            fp1.Serialize(new Serializer(buffer));
            buffer.Rewind();
            fp2.Deserialize(new Deserializer(buffer));

            Assert.True(fp2.Equals(fp1));
        }
 /// <inheritdoc />
 public RedisKey ToRedisKey(Fingerprint fingerprint)
 {
     return(fingerprint.Serialize());
 }