forked from litedb-org/LiteDB
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
SpaceCheetah
committed
Jun 4, 2024
1 parent
023c9ae
commit 0ded5de
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.IO; | ||
using Xunit; | ||
|
||
namespace LiteDB.Tests.Issues; | ||
|
||
public class Issue_2458_Tests | ||
{ | ||
[Fact] | ||
public void NegativeSeekFails() | ||
{ | ||
using var db = new LiteDatabase(":memory:"); | ||
var fs = db.FileStorage; | ||
AddTestFile("test", 1, fs); | ||
using Stream stream = fs.OpenRead("test"); | ||
Assert.Throws<ArgumentOutOfRangeException>(() => stream.Position = -1); | ||
} | ||
|
||
//https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.position?view=net-8.0 says seeking to a position | ||
//beyond the end of a stream is supported, so implementations should support it (error on read). | ||
[Fact] | ||
public void SeekPastFileSucceds() | ||
{ | ||
using var db = new LiteDatabase(":memory:"); | ||
var fs = db.FileStorage; | ||
AddTestFile("test", 1, fs); | ||
using Stream stream = fs.OpenRead("test"); | ||
stream.Position = Int32.MaxValue; | ||
} | ||
|
||
[Fact] | ||
public void SeekShortChunks() | ||
{ | ||
using var db = new LiteDatabase(":memory:"); | ||
var fs = db.FileStorage; | ||
using(Stream writeStream = fs.OpenWrite("test", "test")) | ||
{ | ||
writeStream.WriteByte(0); | ||
writeStream.Flush(); //Create single-byte chunk just containing a 0 | ||
writeStream.WriteByte(1); | ||
writeStream.Flush(); | ||
writeStream.WriteByte(2); | ||
} | ||
using Stream readStream = fs.OpenRead("test"); | ||
readStream.Position = 2; | ||
Assert.Equal(2, readStream.ReadByte()); | ||
} | ||
|
||
private void AddTestFile(string id, long length, ILiteStorage<string> fs) | ||
{ | ||
using Stream writeStream = fs.OpenWrite(id, id); | ||
writeStream.Write(new byte[length]); | ||
} | ||
} |