Example #1
0
        public void ReadRootTag()
        {
            Assert.Throws <FileNotFoundException>(() => NbtFile.ReadRootTagName("NonExistentFile"));

            ReadRootTagInternal(TestFiles.Big, NbtCompression.None);
            ReadRootTagInternal(TestFiles.BigGZip, NbtCompression.GZip);
            ReadRootTagInternal(TestFiles.BigZLib, NbtCompression.ZLib);
        }
Example #2
0
 public bool Claims(string fileName)
 {
     if (fileName == null)
     {
         throw new ArgumentNullException("fileName");
     }
     return(NbtFile.ReadRootTagName(fileName) == RootTagName);
 }
Example #3
0
        void ReadRootTagInternal(String fileName, NbtCompression compression)
        {
            Assert.Throws <ArgumentOutOfRangeException>(() => NbtFile.ReadRootTagName(fileName, compression, true, -1));

            Assert.AreEqual("Level", NbtFile.ReadRootTagName(fileName));
            Assert.AreEqual("Level", NbtFile.ReadRootTagName(fileName, compression, true, 0));

            byte[] fileBytes = File.ReadAllBytes(fileName);
            using (var ms = new MemoryStream(fileBytes)) {
                using (var nss = new NonSeekableStream(ms)) {
                    Assert.Throws <ArgumentOutOfRangeException>(
                        () => NbtFile.ReadRootTagName(nss, compression, true, -1));
                    NbtFile.ReadRootTagName(nss, compression, true, 0);
                }
            }
        }
Example #4
0
        public void NullParameterTest()
        {
            Assert.Throws <ArgumentNullException>(() => new NbtFile((NbtCompound)null));
            Assert.Throws <ArgumentNullException>(() => new NbtFile((string)null));

            NbtFile file = new NbtFile();

            Assert.Throws <ArgumentNullException>(() => file.LoadFromBuffer(null, 0, 1, NbtCompression.None));
            Assert.Throws <ArgumentNullException>(() => file.LoadFromBuffer(null, 0, 1, NbtCompression.None, tag => true));
            Assert.Throws <ArgumentNullException>(() => file.LoadFromFile(null));
            Assert.Throws <ArgumentNullException>(() => file.LoadFromFile(null, NbtCompression.None, tag => true));
            Assert.Throws <ArgumentNullException>(() => file.LoadFromStream(null, NbtCompression.AutoDetect));
            Assert.Throws <ArgumentNullException>(() => file.LoadFromStream(null, NbtCompression.AutoDetect, tag => true));

            Assert.Throws <ArgumentNullException>(() => file.SaveToBuffer(null, 0, NbtCompression.None));
            Assert.Throws <ArgumentNullException>(() => file.SaveToFile(null, NbtCompression.None));
            Assert.Throws <ArgumentNullException>(() => file.SaveToStream(null, NbtCompression.None));

            Assert.Throws <ArgumentNullException>(() => NbtFile.ReadRootTagName(null));
            Assert.Throws <ArgumentNullException>(
                () => NbtFile.ReadRootTagName((Stream)null, NbtCompression.None, true, 0));
        }
Example #5
0
 public void ReadRootTag()
 {
     Assert.AreEqual(NbtFile.ReadRootTagName("TestFiles/test.nbt"), "hello world");
 }
Example #6
0
        public void CorruptFileRead()
        {
            byte[] emptyFile = new byte[0];
            Assert.Throws <EndOfStreamException>(() => TryReadBadFile(emptyFile));
            Assert.Throws <EndOfStreamException>(
                () => new NbtFile().LoadFromBuffer(emptyFile, 0, emptyFile.Length, NbtCompression.None));
            Assert.Throws <EndOfStreamException>(
                () => NbtFile.ReadRootTagName(new MemoryStream(emptyFile), NbtCompression.AutoDetect, true, 0));
            Assert.Throws <EndOfStreamException>(
                () => NbtFile.ReadRootTagName(new MemoryStream(emptyFile), NbtCompression.None, true, 0));

            byte[] badHeader =
            {
                0x02,             // TAG_Short ID (instead of TAG_Compound ID)
                0x00, 0x01, 0x66, // Root name: 'f'
                0x00              // end tag
            };
            Assert.Throws <NbtFormatException>(() => TryReadBadFile(badHeader));
            Assert.Throws <NbtFormatException>(
                () => new NbtFile().LoadFromBuffer(badHeader, 0, badHeader.Length, NbtCompression.None));
            Assert.Throws <NbtFormatException>(
                () => NbtFile.ReadRootTagName(new MemoryStream(badHeader), NbtCompression.None, true, 0));

            byte[] badStringLength =
            {
                0x0A,             // Compound tag
                0xFF, 0xFF, 0x66, // Root name 'f' (with string length given as "-1")
                0x00              // end tag
            };
            Assert.Throws <NbtFormatException>(() => TryReadBadFile(badStringLength));
            Assert.Throws <NbtFormatException>(
                () => new NbtFile().LoadFromBuffer(badStringLength, 0, badStringLength.Length, NbtCompression.None));
            Assert.Throws <NbtFormatException>(
                () => NbtFile.ReadRootTagName(new MemoryStream(badStringLength), NbtCompression.None, true, 0));

            byte[] abruptStringEnd =
            {
                0x0A,             // Compound tag
                0x00, 0xFF, 0x66, // Root name 'f' (string length given as 5)
                0x00              // premature end tag
            };
            Assert.Throws <EndOfStreamException>(() => TryReadBadFile(abruptStringEnd));
            Assert.Throws <EndOfStreamException>(
                () => new NbtFile().LoadFromBuffer(abruptStringEnd, 0, abruptStringEnd.Length, NbtCompression.None));
            Assert.Throws <EndOfStreamException>(
                () => NbtFile.ReadRootTagName(new MemoryStream(abruptStringEnd), NbtCompression.None, true, 0));

            byte[] badSecondTag =
            {
                0x0A,                         // Compound tag
                0x00, 0x01, 0x66,             // Root name: 'f'
                0xFF, 0x01, 0x4E, 0x7F, 0xFF, // Short tag named 'N' with invalid tag ID (0xFF instead of 0x02)
                0x00                          // end tag
            };
            Assert.Throws <NbtFormatException>(() => TryReadBadFile(badSecondTag));
            Assert.Throws <NbtFormatException>(
                () => new NbtFile().LoadFromBuffer(badSecondTag, 0, badSecondTag.Length, NbtCompression.None));

            byte[] badListType =
            {
                0x0A,             // Compound tag
                0x00, 0x01, 0x66, // Root name: 'f'
                0x09,             // List tag
                0x00, 0x01, 0x66, // List tag name: 'g'
                0xFF              // invalid list tag type (-1)
            };
            Assert.Throws <NbtFormatException>(() => TryReadBadFile(badListType));
            Assert.Throws <NbtFormatException>(
                () => new NbtFile().LoadFromBuffer(badListType, 0, badListType.Length, NbtCompression.None));

            byte[] badListSize =
            {
                0x0A,                   // Compound tag
                0x00, 0x01, 0x66,       // Root name: 'f'
                0x09,                   // List tag
                0x00, 0x01, 0x66,       // List tag name: 'g'
                0x01,                   // List type: Byte
                0xFF, 0xFF, 0xFF, 0xFF, // List size: -1
            };
            Assert.Throws <NbtFormatException>(() => TryReadBadFile(badListSize));
            Assert.Throws <NbtFormatException>(
                () => new NbtFile().LoadFromBuffer(badListSize, 0, badListSize.Length, NbtCompression.None));
        }
Example #7
0
 public bool Claims(string path)
 {
     return(NbtFile.ReadRootTagName(path) == RootTagName);
 }