write_test.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * libnbt++ - A library for the Minecraft Named Binary Tag format.
  3. * Copyright (C) 2013, 2015 ljfa-ag
  4. *
  5. * This file is part of libnbt++.
  6. *
  7. * libnbt++ is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * libnbt++ is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with libnbt++. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <cxxtest/TestSuite.h>
  21. #include "io/stream_writer.h"
  22. #include "io/stream_reader.h"
  23. #ifdef NBT_HAVE_ZLIB
  24. #include "io/ozlibstream.h"
  25. #include "io/izlibstream.h"
  26. #endif
  27. #include "nbt_tags.h"
  28. #include <iostream>
  29. #include <fstream>
  30. #include <sstream>
  31. #include "data.h"
  32. using namespace nbt;
  33. class read_test : public CxxTest::TestSuite
  34. {
  35. public:
  36. void test_stream_writer_big()
  37. {
  38. std::ostringstream os;
  39. nbt::io::stream_writer writer(os);
  40. TS_ASSERT_EQUALS(&writer.get_ostr(), &os);
  41. TS_ASSERT_EQUALS(writer.get_endian(), endian::big);
  42. writer.write_type(tag_type::End);
  43. writer.write_type(tag_type::Long);
  44. writer.write_type(tag_type::Int_Array);
  45. writer.write_num(int64_t(0x0102030405060708));
  46. writer.write_string("foobar");
  47. TS_ASSERT(os);
  48. std::string expected{
  49. 0, //tag_type::End
  50. 4, //tag_type::Long
  51. 11, //tag_type::Int_Array
  52. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, //0x0102030405060708 in Big Endian
  53. 0x00, 0x06, //string length in Big Endian
  54. 'f', 'o', 'o', 'b', 'a', 'r'
  55. };
  56. TS_ASSERT_EQUALS(os.str(), expected);
  57. //too long for NBT
  58. TS_ASSERT_THROWS(writer.write_string(std::string(65536, '.')), std::length_error);
  59. TS_ASSERT(!os);
  60. }
  61. void test_stream_writer_little()
  62. {
  63. std::ostringstream os;
  64. nbt::io::stream_writer writer(os, endian::little);
  65. TS_ASSERT_EQUALS(writer.get_endian(), endian::little);
  66. writer.write_num(int32_t(0x0a0b0c0d));
  67. writer.write_string("foobar");
  68. TS_ASSERT(os);
  69. std::string expected{
  70. 0x0d, 0x0c, 0x0b, 0x0a, //0x0a0b0c0d in Little Endian
  71. 0x06, 0x00, //string length in Little Endian
  72. 'f', 'o', 'o', 'b', 'a', 'r'
  73. };
  74. TS_ASSERT_EQUALS(os.str(), expected);
  75. TS_ASSERT_THROWS(writer.write_string(std::string(65536, '.')), std::length_error);
  76. TS_ASSERT(!os);
  77. }
  78. void test_write_payload_big()
  79. {
  80. std::ostringstream os;
  81. nbt::io::stream_writer writer(os);
  82. //tag_primitive
  83. writer.write_payload(tag_byte(127));
  84. writer.write_payload(tag_short(32767));
  85. writer.write_payload(tag_int(2147483647));
  86. writer.write_payload(tag_long(9223372036854775807));
  87. //Same values as in endian_str_test
  88. writer.write_payload(tag_float(std::stof("-0xCDEF01p-63")));
  89. writer.write_payload(tag_double(std::stod("-0x1DEF0102030405p-375")));
  90. TS_ASSERT_EQUALS(os.str(), (std::string{
  91. '\x7F',
  92. '\x7F', '\xFF',
  93. '\x7F', '\xFF', '\xFF', '\xFF',
  94. '\x7F', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF',
  95. '\xAB', '\xCD', '\xEF', '\x01',
  96. '\xAB', '\xCD', '\xEF', '\x01', '\x02', '\x03', '\x04', '\x05'
  97. }));
  98. os.str(""); //clear and reuse the stream
  99. //tag_string
  100. writer.write_payload(tag_string("barbaz"));
  101. TS_ASSERT_EQUALS(os.str(), (std::string{
  102. 0x00, 0x06, //string length in Big Endian
  103. 'b', 'a', 'r', 'b', 'a', 'z'
  104. }));
  105. TS_ASSERT_THROWS(writer.write_payload(tag_string(std::string(65536, '.'))), std::length_error);
  106. TS_ASSERT(!os);
  107. os.clear();
  108. //tag_byte_array
  109. os.str("");
  110. writer.write_payload(tag_byte_array{0, 1, 127, -128, -127});
  111. TS_ASSERT_EQUALS(os.str(), (std::string{
  112. 0x00, 0x00, 0x00, 0x05, //length in Big Endian
  113. 0, 1, 127, -128, -127
  114. }));
  115. os.str("");
  116. //tag_int_array
  117. writer.write_payload(tag_int_array{0x01020304, 0x05060708, 0x090a0b0c});
  118. TS_ASSERT_EQUALS(os.str(), (std::string{
  119. 0x00, 0x00, 0x00, 0x03, //length in Big Endian
  120. 0x01, 0x02, 0x03, 0x04,
  121. 0x05, 0x06, 0x07, 0x08,
  122. 0x09, 0x0a, 0x0b, 0x0c
  123. }));
  124. os.str("");
  125. //tag_list
  126. writer.write_payload(tag_list()); //empty list with undetermined type, should be written as list of tag_end
  127. writer.write_payload(tag_list(tag_type::Int)); //empty list of tag_int
  128. writer.write_payload(tag_list{ //nested list
  129. tag_list::of<tag_short>({0x3456, 0x789a}),
  130. tag_list::of<tag_byte>({0x0a, 0x0b, 0x0c, 0x0d})
  131. });
  132. TS_ASSERT_EQUALS(os.str(), (std::string{
  133. 0, //tag_type::End
  134. 0x00, 0x00, 0x00, 0x00, //length
  135. 3, //tag_type::Int
  136. 0x00, 0x00, 0x00, 0x00, //length
  137. 9, //tag_type::List
  138. 0x00, 0x00, 0x00, 0x02, //length
  139. //list 0
  140. 2, //tag_type::Short
  141. 0x00, 0x00, 0x00, 0x02, //length
  142. '\x34', '\x56',
  143. '\x78', '\x9a',
  144. //list 1
  145. 1, //tag_type::Byte
  146. 0x00, 0x00, 0x00, 0x04, //length
  147. 0x0a,
  148. 0x0b,
  149. 0x0c,
  150. 0x0d
  151. }));
  152. os.str("");
  153. //tag_compound
  154. /* Testing if writing compounds works properly is problematic because the
  155. order of the tags is not guaranteed. However with only two tags in a
  156. compound we only have two possible orderings.
  157. See below for a more thorough test that uses writing and re-reading. */
  158. writer.write_payload(tag_compound{});
  159. writer.write_payload(tag_compound{
  160. {"foo", "quux"},
  161. {"bar", tag_int(0x789abcde)}
  162. });
  163. std::string endtag{0x00};
  164. std::string subtag1{
  165. 8, //tag_type::String
  166. 0x00, 0x03, //key length
  167. 'f', 'o', 'o',
  168. 0x00, 0x04, //string length
  169. 'q', 'u', 'u', 'x'
  170. };
  171. std::string subtag2{
  172. 3, //tag_type::Int
  173. 0x00, 0x03, //key length
  174. 'b', 'a', 'r',
  175. '\x78', '\x9A', '\xBC', '\xDE'
  176. };
  177. TS_ASSERT(os.str() == endtag + subtag1 + subtag2 + endtag
  178. || os.str() == endtag + subtag2 + subtag1 + endtag);
  179. //Now for write_tag:
  180. os.str("");
  181. writer.write_tag("foo", tag_string("quux"));
  182. TS_ASSERT_EQUALS(os.str(), subtag1);
  183. TS_ASSERT(os);
  184. //too long key for NBT
  185. TS_ASSERT_THROWS(writer.write_tag(std::string(65536, '.'), tag_long(-1)), std::length_error);
  186. TS_ASSERT(!os);
  187. }
  188. void test_write_bigtest()
  189. {
  190. /* Like already stated above, because no order is guaranteed for
  191. tag_compound, we cannot simply test it by writing into a stream and directly
  192. comparing the output to a reference value.
  193. Instead, we assume that reading already works correctly and re-read the
  194. written tag.
  195. Smaller-grained tests are already done above. */
  196. std::string input(__binary_bigtest_uncompr_start, __binary_bigtest_uncompr_end);
  197. std::istringstream file(input, std::ios::binary);
  198. const auto orig_pair = io::read_compound(file);
  199. std::stringstream sstr;
  200. //Write into stream in Big Endian
  201. io::write_tag(orig_pair.first, *orig_pair.second, sstr);
  202. TS_ASSERT(sstr);
  203. //Read from stream in Big Endian and compare
  204. auto written_pair = io::read_compound(sstr);
  205. TS_ASSERT_EQUALS(orig_pair.first, written_pair.first);
  206. TS_ASSERT(*orig_pair.second == *written_pair.second);
  207. sstr.str(""); //Reset and reuse stream
  208. //Write into stream in Little Endian
  209. io::write_tag(orig_pair.first, *orig_pair.second, sstr, endian::little);
  210. TS_ASSERT(sstr);
  211. //Read from stream in Little Endian and compare
  212. written_pair = io::read_compound(sstr, endian::little);
  213. TS_ASSERT_EQUALS(orig_pair.first, written_pair.first);
  214. TS_ASSERT(*orig_pair.second == *written_pair.second);
  215. #ifdef NBT_HAVE_ZLIB
  216. //Now with gzip compression
  217. sstr.str("");
  218. zlib::ozlibstream ogzs(sstr, -1, true);
  219. io::write_tag(orig_pair.first, *orig_pair.second, ogzs);
  220. ogzs.close();
  221. TS_ASSERT(ogzs);
  222. TS_ASSERT(sstr);
  223. //Read and compare
  224. zlib::izlibstream igzs(sstr);
  225. written_pair = io::read_compound(igzs);
  226. TS_ASSERT(igzs);
  227. TS_ASSERT_EQUALS(orig_pair.first, written_pair.first);
  228. TS_ASSERT(*orig_pair.second == *written_pair.second);
  229. #endif
  230. }
  231. };