1+ using System ;
2+ using System . IO ;
3+ using System . Linq ;
4+ using System . Collections . Generic ;
5+ using FluentAssertions ;
6+ using Xunit ;
7+
8+ namespace LiteDB . Tests . Database
9+ {
10+ public class InvalidFile_Tests
11+ {
12+ [ Fact ]
13+ public void Test_AddDatabase_InvalidDatabase ( )
14+ {
15+ // Set the database name and file name
16+ string dbName = "invalidDb" ;
17+ string fileName = $ "{ dbName } .db";
18+
19+ // Create an invalid LiteDB database file for testing
20+ File . WriteAllText ( fileName , "Invalid content" ) ;
21+
22+ // Verify the file exists and content is correct
23+ Assert . True ( File . Exists ( fileName ) ) ;
24+ Assert . Equal ( "Invalid content" , File . ReadAllText ( fileName ) ) ;
25+
26+ // Act & Assert: Try to open the invalid database and expect an exception
27+ Assert . Throws < LiteException > ( ( ) =>
28+ {
29+ using ( var db = new LiteDatabase ( fileName ) )
30+ {
31+ // Attempt to perform an operation to ensure the database file is read
32+ var col = db . GetCollection ( "test" ) ;
33+ col . Insert ( new BsonDocument { [ "name" ] = "test" } ) ;
34+ }
35+ } ) ;
36+
37+ // Clean up: Remove the invalid database file after the test
38+ if ( File . Exists ( fileName ) )
39+ {
40+ File . Delete ( fileName ) ;
41+ }
42+ }
43+
44+ [ Fact ]
45+ public void Test_AddDatabase_InvalidDatabase_LargeFile ( )
46+ {
47+ // Set the database name and file name
48+ string dbName = "largeInvalidDb" ;
49+ string fileName = $ "{ dbName } .db";
50+
51+ // Create an invalid LiteDB database file with content larger than 16KB for testing
52+ string invalidContent = new string ( 'a' , 16 * 1024 + 1 ) ;
53+ File . WriteAllText ( fileName , invalidContent ) ;
54+
55+ // Verify the file exists and content is correct
56+ Assert . True ( File . Exists ( fileName ) ) ;
57+ Assert . Equal ( invalidContent , File . ReadAllText ( fileName ) ) ;
58+
59+ // Act & Assert: Try to open the invalid database and expect an exception
60+ Assert . Throws < LiteException > ( ( ) =>
61+ {
62+ using ( var db = new LiteDatabase ( fileName ) )
63+ {
64+ // Attempt to perform an operation to ensure the database file is read
65+ var col = db . GetCollection ( "test" ) ;
66+ col . Insert ( new BsonDocument { [ "name" ] = "test" } ) ;
67+ }
68+ } ) ;
69+
70+ // Clean up: Remove the invalid database file after the test
71+ if ( File . Exists ( fileName ) )
72+ {
73+ File . Delete ( fileName ) ;
74+ }
75+ }
76+
77+ [ Fact ]
78+ public void Test_AddDatabase_InvalidDatabase_MemoryStream ( )
79+ {
80+ // Create an invalid LiteDB database content
81+ byte [ ] invalidContent = System . Text . Encoding . UTF8 . GetBytes ( "Invalid content" ) ;
82+
83+ using ( var stream = new MemoryStream ( invalidContent ) )
84+ {
85+ // Act & Assert: Try to open the invalid database and expect an exception
86+ Exception ex = Record . Exception ( ( ) =>
87+ {
88+ using ( var db = new LiteDatabase ( stream ) )
89+ {
90+ // Attempt to perform an operation to ensure the database file is read
91+ var col = db . GetCollection ( "test" ) ;
92+ col . Insert ( new BsonDocument { [ "name" ] = "test" } ) ;
93+ }
94+ } ) ;
95+
96+ Assert . NotNull ( ex ) ;
97+ Assert . IsType < LiteException > ( ex ) ;
98+ }
99+ }
100+
101+ [ Fact ]
102+ public void Test_AddDatabase_InvalidDatabase_LargeFile_MemoryStream ( )
103+ {
104+ // Create an invalid LiteDB database content larger than 16KB
105+ byte [ ] invalidContent = new byte [ 16 * 1024 + 1 ] ;
106+ for ( int i = 0 ; i < invalidContent . Length ; i ++ ) invalidContent [ i ] = ( byte ) 'a' ;
107+
108+ using ( var stream = new MemoryStream ( invalidContent ) )
109+ {
110+ // Act & Assert: Try to open the invalid database and expect an exception
111+ Exception ex = Record . Exception ( ( ) =>
112+ {
113+ using ( var db = new LiteDatabase ( stream ) )
114+ {
115+ // Attempt to perform an operation to ensure the database file is read
116+ var col = db . GetCollection ( "test" ) ;
117+ col . Insert ( new BsonDocument { [ "name" ] = "test" } ) ;
118+ }
119+ } ) ;
120+
121+ Assert . NotNull ( ex ) ;
122+ Assert . IsType < LiteException > ( ex ) ;
123+ }
124+ }
125+ }
126+ }
0 commit comments