@@ -73,5 +73,54 @@ public void Test_AddDatabase_InvalidDatabase_LargeFile()
7373 File . Delete ( fileName ) ;
7474 }
7575 }
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+ }
76125 }
77126}
0 commit comments