public void TestCreateBlobs()
        {
            RunTestVariants(() => {
                var blobToStore = Encoding.UTF8.GetBytes("This is a blob to store in the store!");

                // Add the blob to the store:
                var key      = new C4BlobKey();
                var localKey = &key;
                LiteCoreBridge.Check(err => {
                    return(Native.c4blob_create(_store, blobToStore, null, localKey, err));
                });

                var str = Native.c4blob_keyToString(key);
                str.Should().Be("sha1-QneWo5IYIQ0ZrbCG0hXPGC6jy7E=", "because the blob key should hash correctly");

                // Read it back and compare
                var blobSize = Native.c4blob_getSize(_store, key);
                blobSize.Should().BeGreaterOrEqualTo(blobToStore.Length, "because the size should be a conservative estimate, never lower");
                if (_encrypted)
                {
                    blobSize.Should().BeLessOrEqualTo(blobToStore.Length + 16, "because the estimate should never be more than 16 bytes off");
                }
                else
                {
                    blobSize.Should().Be(blobToStore.Length, "because unecrypted blobs should have the exact size");
                }

                C4Error error;
                var gotBlob = Native.c4blob_getContents(_store, key, &error);
                gotBlob.Should().NotBeNull("because the attachment should be readable");
                blobToStore.Should().Equal(gotBlob, "because the attachment shouldn't change");

                var p = Native.c4blob_getFilePath(_store, key, &error);
                if (_encrypted)
                {
                    p.Should().BeNull("because an encrypted store will not return a file path");
                    error.code.Should().Be((int)C4ErrorCode.WrongFormat, "because otherwise an unexpected error occurred");
                }
                else
                {
                    p.Should().NotBeNull("because otherwise the DB failed to return its blob store");
                    var filename = "QneWo5IYIQ0ZrbCG0hXPGC6jy7E=.blob";
                    Path.GetFileName(p).Should().Be(filename, "because otherwise the store returned an invalid filename");
                }

                // Try storing it again
                var key2 = new C4BlobKey();
                localKey = &key2;
                LiteCoreBridge.Check(err => {
                    return(Native.c4blob_create(_store, blobToStore, null, localKey, err));
                });
                key.Equals(key2).Should().BeTrue("because the two keys are for the same attachment");
            });
        }
Exemple #2
0
        public void TestCreateBlobs()
        {
            RunTestVariants(() => {
                var blobToStore = Encoding.UTF8.GetBytes("This is a blob to store in the store!");

                // Add the blob to the store:
                var key      = new C4BlobKey();
                var localKey = &key;
                LiteCoreBridge.Check(err => {
                    return(Native.c4blob_create(_store, blobToStore, localKey, err));
                });

                var str = Native.c4blob_keyToString(key);
                str.Should().Be("sha1-QneWo5IYIQ0ZrbCG0hXPGC6jy7E=", "because the blob key should hash correctly");

                // Read it back and compare
                var blobSize = Native.c4blob_getSize(_store, key);
                blobSize.Should().BeGreaterOrEqualTo(blobToStore.Length, "because the size should be a conservative estimate, never lower");
                if (_encrypted)
                {
                    blobSize.Should().BeLessOrEqualTo(blobToStore.Length + 16, "because the estimate should never be more than 16 bytes off");
                }
                else
                {
                    blobSize.Should().Be(blobToStore.Length, "because unecrypted blobs should have the exact size");
                }

                C4Error error;
                var gotBlob = Native.c4blob_getContents(_store, key, &error);
                gotBlob.Should().NotBeNull("because the attachment should be readable");
                blobToStore.Should().Equal(gotBlob, "because the attachment shouldn't change");

                // Try storing it again
                var key2 = new C4BlobKey();
                localKey = &key2;
                LiteCoreBridge.Check(err => {
                    return(Native.c4blob_create(_store, blobToStore, localKey, err));
                });
                key.Equals(key2).Should().BeTrue("because the two keys are for the same attachment");
            });
        }