Skip to content

Commit 9915d10

Browse files
committed
Improve test coverage
1 parent 344e7d9 commit 9915d10

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

fNbt.Test/MiscTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ public void NullValueTest() {
172172
}
173173

174174

175+
[Test]
176+
public void NbtTagNameTest() {
177+
Assert.AreEqual("TAG_End", NbtTag.GetCanonicalTagName(NbtTagType.End));
178+
Assert.IsNull(NbtTag.GetCanonicalTagName((NbtTagType)255));
179+
}
180+
181+
175182
[Test]
176183
public void PathTest() {
177184
// test NbtTag.Path property

fNbt.Test/NbtReaderTests.cs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -588,16 +588,44 @@ public void NonSeekableStreamSkip2() {
588588

589589

590590
[Test]
591-
public void CorruptFileRead() {
592-
byte[] emptyFile = new byte[0];
593-
Assert.Throws<EndOfStreamException>(() => TryReadBadFile(emptyFile));
591+
public void EndOfStreamFileRead() {
592+
byte[] data = {
593+
0x0A, // Compound tag
594+
0x00, 0x02, 0x66, 0x4E, // Root name 'fN'
595+
0x00 // end tag
596+
};
597+
598+
for (int i = 0; i < data.Length; i++) {
599+
var partialData = new byte[i];
600+
Array.Copy(data,partialData,i);
601+
TryReadIncompleteFile(partialData);
602+
if (i < 5)
603+
TryReadIncompleteRootTagName(partialData);
604+
}
605+
}
606+
607+
608+
static void TryReadIncompleteRootTagName(byte[] partialData) {
609+
610+
Assert.Throws<EndOfStreamException>(
611+
() => NbtFile.ReadRootTagName(new MemoryStream(partialData), NbtCompression.None, true, 0), "Length=" + partialData.Length);
594612
Assert.Throws<EndOfStreamException>(
595-
() => new NbtFile().LoadFromBuffer(emptyFile, 0, emptyFile.Length, NbtCompression.None));
613+
() => NbtFile.ReadRootTagName(new MemoryStream(partialData), NbtCompression.AutoDetect, true, 0), "Length=" + partialData.Length);
614+
}
615+
616+
617+
static void TryReadIncompleteFile(byte[] partialData) {
618+
619+
Assert.Throws<EndOfStreamException>(() => TryReadBadFile(partialData));
596620
Assert.Throws<EndOfStreamException>(
597-
() => NbtFile.ReadRootTagName(new MemoryStream(emptyFile), NbtCompression.AutoDetect, true, 0));
621+
() => new NbtFile().LoadFromBuffer(partialData, 0, partialData.Length, NbtCompression.None));
598622
Assert.Throws<EndOfStreamException>(
599-
() => NbtFile.ReadRootTagName(new MemoryStream(emptyFile), NbtCompression.None, true, 0));
623+
() => new NbtFile().LoadFromBuffer(partialData, 0, partialData.Length, NbtCompression.AutoDetect));
624+
}
625+
600626

627+
[Test]
628+
public void CorruptFileRead() {
601629
byte[] badHeader = {
602630
0x02, // TAG_Short ID (instead of TAG_Compound ID)
603631
0x00, 0x01, 0x66, // Root name: 'f'
@@ -620,17 +648,6 @@ public void CorruptFileRead() {
620648
Assert.Throws<NbtFormatException>(
621649
() => NbtFile.ReadRootTagName(new MemoryStream(badStringLength), NbtCompression.None, true, 0));
622650

623-
byte[] abruptStringEnd = {
624-
0x0A, // Compound tag
625-
0x00, 0xFF, 0x66, // Root name 'f' (string length given as 5)
626-
0x00 // premature end tag
627-
};
628-
Assert.Throws<EndOfStreamException>(() => TryReadBadFile(abruptStringEnd));
629-
Assert.Throws<EndOfStreamException>(
630-
() => new NbtFile().LoadFromBuffer(abruptStringEnd, 0, abruptStringEnd.Length, NbtCompression.None));
631-
Assert.Throws<EndOfStreamException>(
632-
() => NbtFile.ReadRootTagName(new MemoryStream(abruptStringEnd), NbtCompression.None, true, 0));
633-
634651
byte[] badSecondTag = {
635652
0x0A, // Compound tag
636653
0x00, 0x01, 0x66, // Root name: 'f'

0 commit comments

Comments
 (0)