1+ using System ;
2+ using System . IO ;
3+ using Xunit ;
4+
5+ namespace LiteDB . Tests . Issues ;
6+
7+ public class Issue_2458_Tests
8+ {
9+ [ Fact ]
10+ public void NegativeSeekFails ( )
11+ {
12+ using var db = new LiteDatabase ( ":memory:" ) ;
13+ var fs = db . FileStorage ;
14+ AddTestFile ( "test" , 1 , fs ) ;
15+ using Stream stream = fs . OpenRead ( "test" ) ;
16+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => stream . Position = - 1 ) ;
17+ }
18+
19+ //https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.position?view=net-8.0 says seeking to a position
20+ //beyond the end of a stream is supported, so implementations should support it (error on read).
21+ [ Fact ]
22+ public void SeekPastFileSucceds ( )
23+ {
24+ using var db = new LiteDatabase ( ":memory:" ) ;
25+ var fs = db . FileStorage ;
26+ AddTestFile ( "test" , 1 , fs ) ;
27+ using Stream stream = fs . OpenRead ( "test" ) ;
28+ stream . Position = Int32 . MaxValue ;
29+ }
30+
31+ [ Fact ]
32+ public void SeekShortChunks ( )
33+ {
34+ using var db = new LiteDatabase ( ":memory:" ) ;
35+ var fs = db . FileStorage ;
36+ using ( Stream writeStream = fs . OpenWrite ( "test" , "test" ) )
37+ {
38+ writeStream . WriteByte ( 0 ) ;
39+ writeStream . Flush ( ) ; //Create single-byte chunk just containing a 0
40+ writeStream . WriteByte ( 1 ) ;
41+ writeStream . Flush ( ) ;
42+ writeStream . WriteByte ( 2 ) ;
43+ }
44+ using Stream readStream = fs . OpenRead ( "test" ) ;
45+ readStream . Position = 2 ;
46+ Assert . Equal ( 2 , readStream . ReadByte ( ) ) ;
47+ }
48+
49+ private void AddTestFile ( string id , long length , ILiteStorage < string > fs )
50+ {
51+ using Stream writeStream = fs . OpenWrite ( id , id ) ;
52+ writeStream . Write ( new byte [ length ] ) ;
53+ }
54+ }
0 commit comments