2022-08-13 15:47:08 +02:00
|
|
|
// Copyright 2022, FOSS-VG Developers and Contributers
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied
|
|
|
|
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// version 3 along with this program.
|
|
|
|
// If not, see https://www.gnu.org/licenses/agpl-3.0.en.html
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#include "assert.hpp"
|
|
|
|
|
|
|
|
#include "../lib/nbt.hpp"
|
|
|
|
#include "../lib/error.hpp"
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
std::cout
|
|
|
|
<< "################################################################################\n"
|
|
|
|
<< "NBT size helper tests\n"
|
|
|
|
<< "################################################################################"
|
|
|
|
<< std::endl;
|
|
|
|
|
2022-08-13 17:23:35 +02:00
|
|
|
// a full copy of /resources/NBT_data/simple_nbt
|
|
|
|
//
|
|
|
|
// This contains all tag types. It is most likely not suitable for
|
|
|
|
// tests chasing edge cases as it is - as the name says - really
|
|
|
|
// basic NBT data.
|
|
|
|
uint8_t simpleNBTData[] = {
|
|
|
|
0x0a, 0x00, 0x00, 0x08, 0x00, 0x3e, 0x53, 0x70,
|
|
|
|
0x61, 0x63, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
|
|
|
|
0x20, 0x73, 0x70, 0x65, 0x63, 0x69, 0x61, 0x6c,
|
|
|
|
0x20, 0x63, 0x68, 0x61, 0x72, 0x61, 0x63, 0x74,
|
|
|
|
0x65, 0x72, 0x73, 0x20, 0x61, 0x72, 0x65, 0x20,
|
|
|
|
0x61, 0x6c, 0x6c, 0x6f, 0x77, 0x65, 0x64, 0x20,
|
|
|
|
0x69, 0x6e, 0x20, 0x74, 0x61, 0x67, 0x20, 0x6e,
|
|
|
|
0x61, 0x6d, 0x65, 0x73, 0x2c, 0x20, 0x72, 0x69,
|
|
|
|
0x67, 0x68, 0x74, 0x3f, 0x00, 0x16, 0x49, 0x64,
|
|
|
|
0x6b, 0x2e, 0x20, 0x4c, 0x65, 0x74, 0xe2, 0x80,
|
|
|
|
0x99, 0x73, 0x20, 0x66, 0x69, 0x6e, 0x64, 0x20,
|
|
|
|
0x6f, 0x75, 0x74, 0x2e, 0x0a, 0x00, 0x08, 0x63,
|
|
|
|
0x6f, 0x6d, 0x70, 0x6f, 0x75, 0x6e, 0x64, 0x03,
|
|
|
|
0x00, 0x0b, 0x73, 0x6f, 0x6d, 0x65, 0x5f, 0x6e,
|
|
|
|
0x75, 0x6d, 0x62, 0x65, 0x72, 0xd3, 0x07, 0x23,
|
|
|
|
0x41, 0x08, 0x00, 0x09, 0x73, 0x6f, 0x6d, 0x65,
|
|
|
|
0x5f, 0x74, 0x65, 0x78, 0x74, 0x00, 0x0c, 0x65,
|
|
|
|
0x61, 0x74, 0x20, 0x61, 0x20, 0x63, 0x6f, 0x6f,
|
|
|
|
0x6b, 0x69, 0x65, 0x00, 0x06, 0x00, 0x06, 0x64,
|
|
|
|
0x6f, 0x75, 0x62, 0x6c, 0x65, 0x41, 0x23, 0x07,
|
|
|
|
0xd3, 0x4e, 0xfd, 0x07, 0xf1, 0x05, 0x00, 0x05,
|
|
|
|
0x66, 0x6c, 0x6f, 0x61, 0x74, 0x42, 0x0d, 0x12,
|
|
|
|
0x43, 0x02, 0x00, 0x05, 0x69, 0x6e, 0x74, 0x31,
|
|
|
|
0x36, 0x07, 0xd0, 0x03, 0x00, 0x05, 0x69, 0x6e,
|
|
|
|
0x74, 0x33, 0x32, 0x00, 0x9a, 0x21, 0x12, 0x0b,
|
|
|
|
0x00, 0x0b, 0x69, 0x6e, 0x74, 0x33, 0x32, 0x5f,
|
|
|
|
0x61, 0x72, 0x72, 0x61, 0x79, 0x00, 0x00, 0x00,
|
|
|
|
0x04, 0x17, 0xbf, 0xe8, 0x3c, 0x00, 0x00, 0xa8,
|
|
|
|
0xfb, 0x7f, 0xff, 0xff, 0xff, 0x61, 0x72, 0x72,
|
|
|
|
0x61, 0x04, 0x00, 0x05, 0x69, 0x6e, 0x74, 0x36,
|
|
|
|
0x34, 0x00, 0x00, 0x00, 0xbc, 0x97, 0xde, 0x9e,
|
|
|
|
0x3e, 0x0c, 0x00, 0x0b, 0x69, 0x6e, 0x74, 0x36,
|
|
|
|
0x34, 0x5f, 0x61, 0x72, 0x72, 0x61, 0x79, 0x00,
|
|
|
|
0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x03, 0xa8, 0xf9, 0x00, 0x00, 0x00, 0x00, 0x01,
|
|
|
|
0x67, 0xe7, 0xbd, 0x7f, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0x00, 0x00, 0xab, 0xd8, 0x00,
|
|
|
|
0x00, 0xab, 0xd8, 0x00, 0x0b, 0x12, 0x21, 0x00,
|
|
|
|
0x79, 0x61, 0x72, 0x01, 0x00, 0x04, 0x69, 0x6e,
|
|
|
|
0x74, 0x38, 0x64, 0x07, 0x00, 0x0a, 0x69, 0x6e,
|
|
|
|
0x74, 0x38, 0x5f, 0x61, 0x72, 0x72, 0x61, 0x79,
|
|
|
|
0x00, 0x00, 0x00, 0x08, 0x71, 0x35, 0x77, 0x62,
|
|
|
|
0x54, 0x64, 0xf5, 0x32, 0x09, 0x00, 0x09, 0x6c,
|
|
|
|
0x69, 0x73, 0x74, 0x5f, 0x69, 0x6e, 0x74, 0x38,
|
|
|
|
0x01, 0x00, 0x00, 0x00, 0x05, 0x41, 0x60, 0x4e,
|
|
|
|
0x7f, 0xfa, 0x09, 0x00, 0x0c, 0x6c, 0x69, 0x73,
|
|
|
|
0x74, 0x5f, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
|
|
|
|
0x73, 0x08, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x06,
|
|
|
|
0x50, 0x61, 0x63, 0x6d, 0x61, 0x6e, 0x00, 0x03,
|
|
|
|
0x61, 0x74, 0x65, 0x00, 0x03, 0x61, 0x6c, 0x6c,
|
|
|
|
0x00, 0x03, 0x74, 0x68, 0x65, 0x00, 0x04, 0x64,
|
|
|
|
0x6f, 0x74, 0x73, 0x00, 0x02, 0x73, 0x6f, 0x00,
|
|
|
|
0x03, 0x6e, 0x6f, 0x77, 0x00, 0x06, 0x68, 0x65,
|
|
|
|
0xe2, 0x80, 0x99, 0x73, 0x00, 0x06, 0x63, 0x6f,
|
|
|
|
0x6d, 0x69, 0x6e, 0x67, 0x00, 0x03, 0x66, 0x6f,
|
|
|
|
0x72, 0x00, 0x03, 0x74, 0x68, 0x65, 0x00, 0x06,
|
|
|
|
0x77, 0x6f, 0x72, 0x64, 0x73, 0x2e, 0x08, 0x00,
|
|
|
|
0x06, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x00,
|
|
|
|
0x0c, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57,
|
|
|
|
0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00
|
2022-08-13 15:47:08 +02:00
|
|
|
};
|
2022-08-13 17:23:35 +02:00
|
|
|
uint64_t simpleNBTDataSize = 478;
|
|
|
|
|
|
|
|
//##################################################################
|
|
|
|
// Basic valid data tests
|
|
|
|
//##################################################################
|
|
|
|
// const uint8_t END = 0;
|
2022-09-19 11:20:51 +02:00
|
|
|
ErrorOr<uint64_t> totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0x93);
|
|
|
|
ErrorOr<int32_t> dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0x93);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(!totalSize.isError);
|
|
|
|
ASSERT(totalSize.value == 1);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 0);
|
|
|
|
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 477);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 477);
|
2022-08-13 15:47:08 +02:00
|
|
|
|
|
|
|
ASSERT(!totalSize.isError);
|
|
|
|
ASSERT(totalSize.value == 1);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 0);
|
|
|
|
|
|
|
|
//const uint8_t INT8 = 1;
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0x133);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0x133);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(!totalSize.isError);
|
|
|
|
ASSERT(totalSize.value == 8);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 1);
|
|
|
|
|
2022-08-13 15:47:08 +02:00
|
|
|
//const uint8_t INT16 = 2;
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0xb1);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0xb1);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(!totalSize.isError);
|
|
|
|
ASSERT(totalSize.value == 10);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 1);
|
|
|
|
|
2022-08-13 15:47:08 +02:00
|
|
|
//const uint8_t INT32 = 3;
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0xbb);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0xbb);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(!totalSize.isError);
|
|
|
|
ASSERT(totalSize.value == 12);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 1);
|
|
|
|
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0x67);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0x67);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(!totalSize.isError);
|
|
|
|
ASSERT(totalSize.value == 18);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 1);
|
|
|
|
|
2022-08-13 15:47:08 +02:00
|
|
|
//const uint8_t INT64 = 4;
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0xe9);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0xe9);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(!totalSize.isError);
|
|
|
|
ASSERT(totalSize.value == 16);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 1);
|
|
|
|
|
2022-08-13 15:47:08 +02:00
|
|
|
//const uint8_t FLOAT = 5;
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0xa5);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0xa5);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(!totalSize.isError);
|
|
|
|
ASSERT(totalSize.value == 12);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 1);
|
|
|
|
|
2022-08-13 15:47:08 +02:00
|
|
|
//const uint8_t DOUBLE = 6;
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0x94);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0x94);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(!totalSize.isError);
|
|
|
|
ASSERT(totalSize.value == 17);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 1);
|
|
|
|
|
2022-08-13 15:47:08 +02:00
|
|
|
//const uint8_t INT8_ARRAY = 7;
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0x13b);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0x13b);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(!totalSize.isError);
|
|
|
|
ASSERT(totalSize.value == 25);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 8);
|
|
|
|
|
2022-08-13 15:47:08 +02:00
|
|
|
//const uint8_t STRING = 8;
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0x1c6);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0x1c6);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(!totalSize.isError);
|
|
|
|
ASSERT(totalSize.value == 23);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 12);
|
|
|
|
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0x79);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0x79);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(!totalSize.isError);
|
|
|
|
ASSERT(totalSize.value == 26);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 12);
|
|
|
|
|
2022-08-13 15:47:08 +02:00
|
|
|
//const uint8_t LIST = 9;
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0x154);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0x154);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(totalSize.isError);
|
|
|
|
ASSERT(totalSize.errorCode == ErrorCodes::NOT_YET_KNOWN);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 5);
|
|
|
|
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0x16a);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0x16a);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(totalSize.isError);
|
|
|
|
ASSERT(totalSize.errorCode == ErrorCodes::NOT_YET_KNOWN);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 12);
|
|
|
|
|
2022-08-13 15:47:08 +02:00
|
|
|
//const uint8_t COMPOUND = 10;
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0x5c);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0x5c);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(totalSize.isError);
|
|
|
|
ASSERT(totalSize.errorCode == ErrorCodes::NOT_YET_KNOWN);
|
|
|
|
ASSERT(dataLength.isError);
|
|
|
|
ASSERT(dataLength.errorCode == ErrorCodes::NOT_YET_KNOWN);
|
|
|
|
|
2022-08-13 15:47:08 +02:00
|
|
|
//const uint8_t INT32_ARRAY= 11;
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0xc7);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0xc7);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(!totalSize.isError);
|
|
|
|
ASSERT(totalSize.value == 34);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 4);
|
|
|
|
|
2022-08-13 15:47:08 +02:00
|
|
|
//const uint8_t INT64_ARRAY= 12;
|
2022-09-19 11:20:51 +02:00
|
|
|
totalSize = NBT::Helper::totalTagSize(simpleNBTData, simpleNBTDataSize, 0xf9);
|
|
|
|
dataLength = NBT::Helper::containedDataLength(simpleNBTData, simpleNBTDataSize, 0xf9);
|
2022-08-13 17:23:35 +02:00
|
|
|
|
|
|
|
ASSERT(!totalSize.isError);
|
|
|
|
ASSERT(totalSize.value == 58);
|
|
|
|
ASSERT(!dataLength.isError);
|
|
|
|
ASSERT(dataLength.value == 5);
|
|
|
|
|
|
|
|
std::cout << "Passed simple valid data test." << std::endl;
|
|
|
|
|
|
|
|
//TODO: add tests for errors and edge cases
|
2022-08-13 15:47:08 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|