Example #1
0
        public void TestInvalidDocID()
        {
            RunTestVariants(() =>
            {
                NativePrivate.c4log_warnOnErrors(false);
                LiteCoreBridge.Check(err => Native.c4db_beginTransaction(Db, err));
                try {
                    Action <C4Slice> checkPutBadDocID = (C4Slice docID) =>
                    {
                        C4Error e;
                        var rq   = new C4DocPutRequest();
                        rq.body  = Body;
                        rq.save  = true;
                        rq.docID = docID;
                        ((long)Native.c4doc_put(Db, &rq, null, &e)).Should()
                        .Be(0, "because the invalid doc ID should cause the put to fail");
                        e.domain.Should().Be(C4ErrorDomain.LiteCoreDomain);
                        e.code.Should().Be((int)C4ErrorCode.BadDocID);
                    };

                    checkPutBadDocID(C4Slice.Constant(""));
                    string tooLong = new string(Enumerable.Repeat('x', 241).ToArray());
                    using (var tooLong_ = new C4String(tooLong)) {
                        checkPutBadDocID(tooLong_.AsC4Slice());
                    }

                    checkPutBadDocID(C4Slice.Constant("oops\x00oops")); // Bad UTF-8
                    checkPutBadDocID(C4Slice.Constant("oops\noops"));   // Control characters
                } finally {
                    NativePrivate.c4log_warnOnErrors(true);
                    LiteCoreBridge.Check(err => Native.c4db_endTransaction(Db, true, err));
                }
            });
        }
        public void TestCreateBlobKeyMismatch()
        {
            RunTestVariants(() =>
            {
                var blobToStore            = C4Slice.Constant("This is a blob to store in the store!");
                C4BlobKey key, expectedKey = new C4BlobKey();
                var i = 0;
                foreach (var b in Enumerable.Repeat <byte>(0x55, sizeof(C4BlobKey)))
                {
                    expectedKey.bytes[i++] = b;
                }

                C4Error error;
                NativePrivate.c4log_warnOnErrors(false);
                try {
                    NativeRaw.c4blob_create(_store, blobToStore, &expectedKey, &key, &error).Should().BeFalse();
                    error.domain.Should().Be(C4ErrorDomain.LiteCoreDomain);
                    error.code.Should().Be((int)C4ErrorCode.CorruptData);
                } finally {
                    NativePrivate.c4log_warnOnErrors(true);
                }

                Native.c4blob_keyFromString("sha1-QneWo5IYIQ0ZrbCG0hXPGC6jy7E=", &expectedKey);
                NativeRaw.c4blob_create(_store, blobToStore, &expectedKey, &key, &error).Should().BeTrue();
            });
        }
Example #3
0
        public void TestOpenBundle()
        {
            RunTestVariants(() => {
                string bundleDBName = "cbl_core_test_bundle";
                var config2         = C4DatabaseConfig2.Clone(Native.c4db_getConfig2(Db));
                var tmp             = config2;

                var bundlePath = Path.Combine(TestDir, $"{bundleDBName}.cblite2{Path.DirectorySeparatorChar}");
                Native.c4db_deleteNamed(bundleDBName, TestDir, null);
                var bundle = (C4Database *)LiteCoreBridge.Check(err => {
                    var localConfig = tmp;
                    return(Native.c4db_openNamed(bundleDBName, &localConfig, err));
                });

                var path = Native.c4db_getPath(bundle);
                path.Should().Be(bundlePath, "because the database should store the correct path");
                LiteCoreBridge.Check(err => Native.c4db_close(bundle, err));
                Native.c4db_release(bundle);

                // Reopen without the 'create' flag:
                config2.flags &= ~C4DatabaseFlags.Create;
                tmp            = config2;
                bundle         = (C4Database *)LiteCoreBridge.Check(err => {
                    var localConfig = tmp;
                    return(Native.c4db_openNamed(bundleDBName, &localConfig, err));
                });
                LiteCoreBridge.Check(err => Native.c4db_close(bundle, err));
                Native.c4db_release(bundle);

                // Open nonexistent bundle
                ((long)Native.c4db_openNamed($"no_such_bundle{Path.DirectorySeparatorChar}", &config2, null)).Should().Be(0, "because the bundle does not exist");
                NativePrivate.c4log_warnOnErrors(true);
            });
        }
        public void TestParseInvalidBlobKeys()
        {
            NativePrivate.c4log_warnOnErrors(false);
            C4BlobKey key2;

            foreach (var invalid in new[] { "", "rot13-xxxx", "sha1-", "sha1-VVVVVVVVVVVVVVVVVVVVVV", "sha1-VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVU" })
            {
                Native.c4blob_keyFromString(invalid, &key2).Should().BeFalse($"because '{invalid}' is an invalid string");
            }

            NativePrivate.c4log_warnOnErrors(true);
        }
Example #5
0
        public void TestOpenBundle()
        {
            RunTestVariants(() => {
                var config    = C4DatabaseConfig.Clone(Native.c4db_getConfig(Db));
                config.flags |= C4DatabaseFlags.Bundled;
                var tmp       = config;

                var bundlePath = Path.Combine(TestDir, $"cbl_core_test_bundle{Path.DirectorySeparatorChar}");
                Native.c4db_deleteAtPath(bundlePath, &config, null);
                var bundle = (C4Database *)LiteCoreBridge.Check(err => {
                    var localConfig = tmp;
                    return(Native.c4db_open(bundlePath, &localConfig, err));
                });

                var path = Native.c4db_getPath(bundle);
                path.Should().Be(bundlePath, "because the database should store the correct path");
                LiteCoreBridge.Check(err => Native.c4db_close(bundle, err));
                Native.c4db_free(bundle);

                // Reopen without the 'create' flag:
                config.flags &= ~C4DatabaseFlags.Create;
                tmp           = config;
                bundle        = (C4Database *)LiteCoreBridge.Check(err => {
                    var localConfig = tmp;
                    return(Native.c4db_open(bundlePath, &localConfig, err));
                });
                LiteCoreBridge.Check(err => Native.c4db_close(bundle, err));
                Native.c4db_free(bundle);

                // Reopen with wrong storage type:
                NativePrivate.c4log_warnOnErrors(false);
                var engine           = config.storageEngine;
                config.storageEngine = "b0gus";
                ((long)Native.c4db_open(bundlePath, &config, null)).Should().Be(0, "because the storage engine is nonsense");
                config.storageEngine = engine;

                // Open nonexistent bundle
                ((long)Native.c4db_open($"no_such_bundle{Path.DirectorySeparatorChar}", &config, null)).Should().Be(0, "because the bundle does not exist");
                NativePrivate.c4log_warnOnErrors(true);

                config.Dispose();
            });
        }