From abb1d5ccd981e5d18364636c27905929a02b691b Mon Sep 17 00:00:00 2001 From: Wuming Pan Date: Wed, 12 Jun 2024 15:00:23 +0800 Subject: [PATCH 1/2] InvalidFile_Tests --- LiteDB.Tests/Database/InvalidFile_Tests.cs | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 LiteDB.Tests/Database/InvalidFile_Tests.cs diff --git a/LiteDB.Tests/Database/InvalidFile_Tests.cs b/LiteDB.Tests/Database/InvalidFile_Tests.cs new file mode 100644 index 000000000..b7054385b --- /dev/null +++ b/LiteDB.Tests/Database/InvalidFile_Tests.cs @@ -0,0 +1,77 @@ +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(() => + { + 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(() => + { + 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); + } + } + } +} \ No newline at end of file From b1724014dc79a3569c3eaa8abb631710fa113b9c Mon Sep 17 00:00:00 2001 From: Wuming Pan Date: Wed, 12 Jun 2024 16:21:54 +0800 Subject: [PATCH 2/2] Test_InvalidDatabase_MemoryStream --- LiteDB.Tests/Database/InvalidFile_Tests.cs | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/LiteDB.Tests/Database/InvalidFile_Tests.cs b/LiteDB.Tests/Database/InvalidFile_Tests.cs index b7054385b..3ec3623f2 100644 --- a/LiteDB.Tests/Database/InvalidFile_Tests.cs +++ b/LiteDB.Tests/Database/InvalidFile_Tests.cs @@ -73,5 +73,54 @@ public void Test_AddDatabase_InvalidDatabase_LargeFile() 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(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(ex); + } + } } } \ No newline at end of file