Beispiel #1
0
 public void ArrayEqualToItself()
 {
     byte[] b = { 0, 1, 2, 3, 4, 5, 6 };
     Assert.Equal(0, _comparer.Compare(b, b));
     Assert.True(ByteArrayComparer.ArraysEqual(b, b));
     Assert.True(_comparer.Equals(b, b));
 }
Beispiel #2
0
 public void UnequalLengthsNotEqual()
 {
     Assert.NotEqual(0, _comparer.Compare(new byte[] { 0 }, new byte[0]));
     Assert.NotEqual(0, _comparer.Compare(new byte[0], new byte[] { 0 }));
     Assert.False(ByteArrayComparer.ArraysEqual(new byte[0], new byte[] { 0 }));
     Assert.False(_comparer.Equals(new byte[0], new byte[] { 0 }));
     Assert.NotEqual(_comparer.GetHashCode(new byte[0]), _comparer.GetHashCode(new byte[] { 0 }));
 }
Beispiel #3
0
        public void TestContentHashListsNoPayloadOrderInvalidation()
        {
            var vsoContentHash1 = ContentHash.Random();
            var vsoContentHash2 = ContentHash.Random();

            var contentHashList = new ContentHashList(new[] { vsoContentHash1, vsoContentHash2 });

            byte[] hashOfContentHashes = contentHashList.GetHashOfHashes();

            var secondOrderContentHashList = new ContentHashList(new[] { vsoContentHash2, vsoContentHash1 });

            byte[] secondOrderHashOfContentHashes = secondOrderContentHashList.GetHashOfHashes();

            ByteArrayComparer.ArraysEqual(hashOfContentHashes, secondOrderHashOfContentHashes).Should().BeFalse();
        }
Beispiel #4
0
        public void TestContentHashListsEqualityCheckNoPayload()
        {
            var vsoContentHash1 = ContentHash.Random();
            var vsoContentHash2 = ContentHash.Random();

            var contentHashes   = new[] { vsoContentHash1, vsoContentHash2 };
            var contentHashList = new ContentHashList(contentHashes);

            byte[] hashOfContentHashes = contentHashList.GetHashOfHashes();

            var secondOrderContentHashList = new ContentHashList(contentHashes);

            byte[] secondOrderHashOfContentHashes = secondOrderContentHashList.GetHashOfHashes();

            ByteArrayComparer.ArraysEqual(hashOfContentHashes, secondOrderHashOfContentHashes).Should().BeTrue();
        }
Beispiel #5
0
        public void TestContentHashListsPayloadDoesNotInvalidate()
        {
            var vsoContentHash1 = ContentHash.Random();
            var vsoContentHash2 = ContentHash.Random();

            var contentHashes   = new[] { vsoContentHash1, vsoContentHash2 };
            var contentHashList = new ContentHashList(contentHashes);

            byte[] hashOfContentHashes = contentHashList.GetHashOfHashes();

            var secondOrderContentHashList = new ContentHashList(contentHashes, ThreadSafeRandom.GetBytes(RandomBytesSize));

            byte[] secondOrderHashOfContentHashes = secondOrderContentHashList.GetHashOfHashes();

            ByteArrayComparer.ArraysEqual(hashOfContentHashes, secondOrderHashOfContentHashes).Should().BeTrue();
        }
Beispiel #6
0
        public void TestContentHashListsEqualityCheckWithPayload()
        {
            var byteStream      = ThreadSafeRandom.GetBytes(RandomBytesSize);
            var vsoContentHash1 = ContentHash.Random();
            var vsoContentHash2 = ContentHash.Random();

            var contentHashes   = new[] { vsoContentHash1, vsoContentHash2 };
            var contentHashList = new ContentHashList(contentHashes, byteStream);

            byte[] hashOfContentHashes = contentHashList.GetHashOfHashes();

            var secondOrderContentHashList = new ContentHashList(contentHashes, byteStream);

            byte[] secondOrderHashOfContentHashes = secondOrderContentHashList.GetHashOfHashes();

            ByteArrayComparer.ArraysEqual(hashOfContentHashes, secondOrderHashOfContentHashes).Should().BeTrue();
        }
Beispiel #7
0
        /// <inheritdoc />
        public bool Equals(ContentHashList other)
        {
            if (other == null)
            {
                return(false);
            }

            if (!ReferenceEquals(_contentHashes, other._contentHashes))
            {
                if (_contentHashes.Length != other._contentHashes.Length)
                {
                    return(false);
                }

                if (_contentHashes.Where((t, i) => t != other._contentHashes[i]).Any())
                {
                    return(false);
                }
            }

            if (!ReferenceEquals(_payload, other._payload))
            {
                if (_payload == null || other._payload == null)
                {
                    return(false);
                }

                if (_payload.Length != other._payload.Length)
                {
                    return(false);
                }

                if (!ByteArrayComparer.ArraysEqual(_payload, other._payload))
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #8
0
 /// <inheritdoc/>
 public bool Equals(DedupNode other)
 {
     return(ByteArrayComparer.ArraysEqual(Hash, other.Hash));
 }
Beispiel #9
0
 /// <inheritdoc />
 public bool Equals(MachineLocation other) => ByteArrayComparer.ArraysEqual(Data, other.Data);
Beispiel #10
0
 /// <inheritdoc/>
 public bool Equals([AllowNull] ChunkInfo other)
 {
     return(Offset == other.Offset && Size == other.Size && ByteArrayComparer.ArraysEqual(Hash, other.Hash));
 }
Beispiel #11
0
 /// <inheritdoc />
 public bool Equals(Selector other)
 {
     return(ContentHash.Equals(other.ContentHash) && ByteArrayComparer.ArraysEqual(Output, other.Output));
 }
Beispiel #12
0
 public void NullsEqual()
 {
     Assert.Equal(0, _comparer.Compare(null, null));
     Assert.True(ByteArrayComparer.ArraysEqual(null, null));
     Assert.True(_comparer.Equals(null, null));
 }
        public async Task TestGarbageCollect()
        {
            var configuration = new RocksDbContentMetadataDatabaseConfiguration(_workingDirectory.Path)
            {
                CleanOnInitialize = false,
            };

            var context = new Context(Logger);
            var ctx     = new OperationContext(context);

            var keys = Enumerable.Range(0, 10).Select(i => (ShortHash)ContentHash.Random()).ToArray();

            void setBlob(RocksDbContentMetadataDatabase db, ShortHash key)
            {
                db.PutBlob(key, key.ToByteArray());
            }

            KeyCheckResult checkBlob(RocksDbContentMetadataDatabase db, ShortHash key)
            {
                if (db.TryGetBlob(key, out var blob))
                {
                    if (ByteArrayComparer.ArraysEqual(blob, key.ToByteArray()))
                    {
                        return(KeyCheckResult.Valid);
                    }
                    else
                    {
                        return(KeyCheckResult.Different);
                    }
                }
                else
                {
                    return(KeyCheckResult.Missing);
                }
            }

            {
                var db = new RocksDbContentMetadataDatabase(Clock, configuration);
                await db.StartupAsync(ctx).ShouldBeSuccess();

                db.SetGlobalEntry("test", "hello");
                setBlob(db, keys[0]);
                checkBlob(db, keys[0]).Should().Be(KeyCheckResult.Valid);

                await db.GarbageCollectAsync(ctx, force : true).ShouldBeSuccess();

                setBlob(db, keys[1]);
                checkBlob(db, keys[0]).Should().Be(KeyCheckResult.Valid);
                checkBlob(db, keys[1]).Should().Be(KeyCheckResult.Valid);

                await db.GarbageCollectAsync(ctx, force : true).ShouldBeSuccess();

                checkBlob(db, keys[0]).Should().Be(KeyCheckResult.Missing);
                checkBlob(db, keys[1]).Should().Be(KeyCheckResult.Valid);

                await db.ShutdownAsync(ctx).ShouldBeSuccess();
            }

            {
                var db = new RocksDbContentMetadataDatabase(Clock, configuration);
                await db.StartupAsync(ctx).ShouldBeSuccess();

                db.TryGetGlobalEntry("test", out var readValue);
                readValue.Should().Be("hello");

                setBlob(db, keys[2]);

                checkBlob(db, keys[0]).Should().Be(KeyCheckResult.Missing);
                checkBlob(db, keys[1]).Should().Be(KeyCheckResult.Valid);
                checkBlob(db, keys[2]).Should().Be(KeyCheckResult.Valid);
                await db.GarbageCollectAsync(ctx, force : true).ShouldBeSuccess();

                checkBlob(db, keys[0]).Should().Be(KeyCheckResult.Missing);
                checkBlob(db, keys[1]).Should().Be(KeyCheckResult.Missing);
                checkBlob(db, keys[2]).Should().Be(KeyCheckResult.Valid);

                await db.ShutdownAsync(ctx).ShouldBeSuccess();
            }
        }