Beispiel #1
0
        public void TestKeySize()
        {
            var exception = Assert.ThrowsException <ArgumentException>(() =>
            {
                SQLiteCryptoVFS.RegisterVFS(new byte[] { 10, 20, 30 });
            });

            Assert.AreEqual(exception.ParamName, "key");
            Assert.AreEqual(exception.Message, "Key must have 32 bytes\nParameter name: key");
        }
Beispiel #2
0
        public void TestEncryption()
        {
            CopyLibrary();

            var vfsName = SQLiteCryptoVFS.VFSName;

            var resultCode = SQLiteCryptoVFS.RegisterVFS(
                new byte[] {
                0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
                0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
            }
                );

            Assert.AreEqual(resultCode, raw.SQLITE_OK);

            const string filename = "test-encrypted.db";

            raw.SetProvider(new SQLite3Provider_sqlite3());
            Console.WriteLine($"{raw.sqlite3_libversion()}");
            Console.WriteLine($"{raw.sqlite3_libversion_number()}");

            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            resultCode = raw.sqlite3_open_v2(filename, out sqlite3 db, raw.SQLITE_OPEN_READWRITE | raw.SQLITE_OPEN_CREATE, vfsName);
            //resultCode = raw.sqlite3_open_v2(filename, out sqlite3 db, raw.SQLITE_OPEN_READWRITE | raw.SQLITE_OPEN_CREATE, null);
            Assert.AreEqual(resultCode, raw.SQLITE_OK);
            try
            {
                resultCode = raw.sqlite3_exec(db, "CREATE TABLE USER (_ID TEXT)");
                Assert.AreEqual(resultCode, raw.SQLITE_OK);

                const string plaintext = "Can you keep a secret?";

                {
                    resultCode = raw.sqlite3_prepare_v2(db, "INSERT INTO USER (_ID) VALUES (?)", out sqlite3_stmt stmt);
                    Assert.AreEqual(resultCode, raw.SQLITE_OK);
                    try
                    {
                        resultCode = raw.sqlite3_bind_text(stmt, 1, plaintext);
                        Assert.AreEqual(resultCode, raw.SQLITE_OK);

                        resultCode = raw.sqlite3_step(stmt);
                        Assert.AreEqual(resultCode, raw.SQLITE_DONE);
                    }
                    finally
                    {
                        resultCode = raw.sqlite3_finalize(stmt);
                        Assert.AreEqual(resultCode, raw.SQLITE_OK);
                    }
                }

                {
                    resultCode = raw.sqlite3_prepare_v2(db, "SELECT * FROM USER", out sqlite3_stmt stmt);
                    Assert.AreEqual(resultCode, raw.SQLITE_OK);
                    try
                    {
                        resultCode = raw.sqlite3_step(stmt);
                        Assert.AreEqual(resultCode, raw.SQLITE_ROW);

                        Assert.AreEqual(raw.sqlite3_column_text(stmt, 0), plaintext);
                    }
                    finally
                    {
                        resultCode = raw.sqlite3_finalize(stmt);
                        Assert.AreEqual(resultCode, raw.SQLITE_OK);
                    }
                }

                using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
                {
                    byte[] buffer = new byte[16];
                    fs.Read(buffer, 0, buffer.Length);
                    fs.Close();
                    var header = Encoding.ASCII.GetString(buffer);
                    Assert.AreNotEqual(header, "SQLite format 3\0");
                }
            }
            finally
            {
                resultCode = raw.sqlite3_close_v2(db);
                Assert.AreEqual(resultCode, raw.SQLITE_OK);
            }
        }