Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust encryption tests to check sqlite3mc #567

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/common/sqlcipher-2.0-beta-testkey.db
Binary file not shown.
Binary file added src/common/sqlcipher-4.0-testkey.db
Binary file not shown.
72 changes: 68 additions & 4 deletions src/common/tests_xunit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3339,12 +3339,24 @@ private static bool is_sqlcipher()
}
}

private static bool is_sqlite3mc()
{
using (sqlite3 db = ugly.open(":memory:"))
{
var s = db.query_scalar<string>("PRAGMA cipher");
return !string.IsNullOrEmpty(s);
}
}

[Fact]
public void test_encrypted_file_with_pragma()
{
if (is_sqlcipher())
if (is_sqlcipher() || is_sqlite3mc())
{
Assert.Contains("sqlcipher", raw.GetNativeLibraryName());
if (is_sqlcipher())
Assert.Contains("sqlcipher", raw.GetNativeLibraryName());
else
Assert.Contains("sqlite3mc", raw.GetNativeLibraryName());
string name;
using (sqlite3 db = ugly.open(":memory:"))
{
Expand Down Expand Up @@ -3393,15 +3405,19 @@ bool check_sum(string pass)
else
{
Assert.DoesNotContain("sqlcipher", raw.GetNativeLibraryName());
Assert.DoesNotContain("sqlite3mc", raw.GetNativeLibraryName());
}
}

[Fact]
public void test_encrypted_file_with_key()
{
if (is_sqlcipher())
if (is_sqlcipher() || is_sqlite3mc())
{
Assert.Contains("sqlcipher", raw.GetNativeLibraryName());
if (is_sqlcipher())
Assert.Contains("sqlcipher", raw.GetNativeLibraryName());
else
Assert.Contains("sqlite3mc", raw.GetNativeLibraryName());
string name;
using (sqlite3 db = ugly.open(":memory:"))
{
Expand Down Expand Up @@ -3460,6 +3476,54 @@ bool check_sum(byte[] k)
else
{
Assert.DoesNotContain("sqlcipher", raw.GetNativeLibraryName());
Assert.DoesNotContain("sqlite3mc", raw.GetNativeLibraryName());
}
}

[Fact]
public void test_database_is_SQLCipher4()
{
if (is_sqlite3mc())
{
Assert.Contains("sqlite3mc", raw.GetNativeLibraryName());
// Test to access database encrypted with SQLCipher version 4
// Result: 78536 1 1 one one 1 2 one two
using (sqlite3 db = ugly.open("file:sqlcipher-4.0-testkey.db?cipher=sqlcipher&legacy=4"))
{
const string pswd = "testkey";
var key = u.to_utf8(pswd);
db.key(key);
var value = db.query_scalar<int>("select count(*) from t1");
Assert.Equal(78536, value);
}
}
else
{
Assert.DoesNotContain("sqlite3mc", raw.GetNativeLibraryName());
}
}

[Fact]
public void test_database_is_custom_SQLCipher2()
{
if (is_sqlite3mc())
{
Assert.Contains("sqlite3mc", raw.GetNativeLibraryName());
// Test to access database encrypted with SQLCipher version 2
// using 4000 iterations for the HMAC key derivation and a HMAC salt mask of zero
// Result: 38768 test-0-0 test-0-1 test-1-0 test-1-1
using (sqlite3 db = ugly.open("file:sqlcipher-2.0-beta-testkey.db?cipher=sqlcipher&legacy=2&fast_kdf_iter=4000&hmac_salt_mask=0"))
{
const string pswd = "testkey";
var key = u.to_utf8(pswd);
db.key(key);
var value = db.query_scalar<int>("select count(*) from t1");
Assert.Equal(38768, value);
}
}
else
{
Assert.DoesNotContain("sqlite3mc", raw.GetNativeLibraryName());
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/t2/tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@
<Compile Include="Program.cs" />
</ItemGroup>

<ItemGroup>
<None Update="..\common\sqlcipher-2.0-beta-testkey.db" CopyToOutputDirectory="PreserveNewest" />
<None Update="..\common\sqlcipher-4.0-testkey.db" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions src/tests/tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@
<Compile Include="my_batteries_v2.cs" />
</ItemGroup>

<ItemGroup>
<None Update="..\common\sqlcipher-2.0-beta-testkey.db" CopyToOutputDirectory="PreserveNewest" />
<None Update="..\common\sqlcipher-4.0-testkey.db" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
Loading