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

Add unit tests for handling invalid LiteDB files #2507

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
126 changes: 126 additions & 0 deletions LiteDB.Tests/Database/InvalidFile_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using FluentAssertions;
using Xunit;

namespace LiteDB.Tests.Database
{
public class InvalidFile_Tests
{
[Fact]
public void Test_AddDatabase_InvalidDatabase()
{
// Set the database name and file name
string dbName = "invalidDb";
string fileName = $"{dbName}.db";

// Create an invalid LiteDB database file for testing
File.WriteAllText(fileName, "Invalid content");

// Verify the file exists and content is correct
Assert.True(File.Exists(fileName));
Assert.Equal("Invalid content", File.ReadAllText(fileName));

// Act & Assert: Try to open the invalid database and expect an exception
Assert.Throws<LiteException>(() =>
{
using (var db = new LiteDatabase(fileName))
{
// Attempt to perform an operation to ensure the database file is read
var col = db.GetCollection("test");
col.Insert(new BsonDocument { ["name"] = "test" });
}
});

// Clean up: Remove the invalid database file after the test
if (File.Exists(fileName))
{
File.Delete(fileName);
}
}

[Fact]
public void Test_AddDatabase_InvalidDatabase_LargeFile()
{
// Set the database name and file name
string dbName = "largeInvalidDb";
string fileName = $"{dbName}.db";

// Create an invalid LiteDB database file with content larger than 16KB for testing
string invalidContent = new string('a', 16 * 1024 + 1);
File.WriteAllText(fileName, invalidContent);

// Verify the file exists and content is correct
Assert.True(File.Exists(fileName));
Assert.Equal(invalidContent, File.ReadAllText(fileName));

// Act & Assert: Try to open the invalid database and expect an exception
Assert.Throws<LiteException>(() =>
{
using (var db = new LiteDatabase(fileName))
{
// Attempt to perform an operation to ensure the database file is read
var col = db.GetCollection("test");
col.Insert(new BsonDocument { ["name"] = "test" });
}
});

// Clean up: Remove the invalid database file after the test
if (File.Exists(fileName))
{
File.Delete(fileName);
}
}

[Fact]
public void Test_AddDatabase_InvalidDatabase_MemoryStream()
{
// Create an invalid LiteDB database content
byte[] invalidContent = System.Text.Encoding.UTF8.GetBytes("Invalid content");

using (var stream = new MemoryStream(invalidContent))
{
// Act & Assert: Try to open the invalid database and expect an exception
Exception ex = Record.Exception(() =>
{
using (var db = new LiteDatabase(stream))
{
// Attempt to perform an operation to ensure the database file is read
var col = db.GetCollection("test");
col.Insert(new BsonDocument { ["name"] = "test" });
}
});

Assert.NotNull(ex);
Assert.IsType<LiteException>(ex);
}
}

[Fact]
public void Test_AddDatabase_InvalidDatabase_LargeFile_MemoryStream()
{
// Create an invalid LiteDB database content larger than 16KB
byte[] invalidContent = new byte[16 * 1024 + 1];
for (int i = 0; i < invalidContent.Length; i++) invalidContent[i] = (byte)'a';

using (var stream = new MemoryStream(invalidContent))
{
// Act & Assert: Try to open the invalid database and expect an exception
Exception ex = Record.Exception(() =>
{
using (var db = new LiteDatabase(stream))
{
// Attempt to perform an operation to ensure the database file is read
var col = db.GetCollection("test");
col.Insert(new BsonDocument { ["name"] = "test" });
}
});

Assert.NotNull(ex);
Assert.IsType<LiteException>(ex);
}
}
}
}