zlibstream_test.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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/izlibstream.h"
  22. #include "io/ozlibstream.h"
  23. #include <fstream>
  24. #include <sstream>
  25. #include "data.h"
  26. using namespace zlib;
  27. class zlibstream_test : public CxxTest::TestSuite
  28. {
  29. private:
  30. std::string bigtest;
  31. public:
  32. zlibstream_test()
  33. {
  34. std::string input(__binary_bigtest_uncompr_start, __binary_bigtest_uncompr_end);
  35. std::istringstream bigtest_f(input, std::ios::binary);
  36. std::stringbuf bigtest_b;
  37. bigtest_f >> &bigtest_b;
  38. bigtest = bigtest_b.str();
  39. if(!bigtest_f || bigtest.size() == 0)
  40. throw std::runtime_error("Could not read bigtest_uncompr file");
  41. }
  42. void test_inflate_gzip()
  43. {
  44. std::string input(__binary_bigtest_nbt_start, __binary_bigtest_nbt_end);
  45. std::istringstream gzip_in(input, std::ios::binary);
  46. TS_ASSERT(gzip_in);
  47. std::stringbuf data;
  48. //Small buffer so not all fits at once (the compressed file is 561 bytes)
  49. {
  50. izlibstream igzs(gzip_in, 256);
  51. igzs.exceptions(std::ios::failbit | std::ios::badbit);
  52. TS_ASSERT(igzs.good());
  53. TS_ASSERT_THROWS_NOTHING(igzs >> &data);
  54. TS_ASSERT(igzs);
  55. TS_ASSERT(igzs.eof());
  56. TS_ASSERT_EQUALS(data.str(), bigtest);
  57. }
  58. //Clear and reuse buffers
  59. data.str("");
  60. gzip_in.clear();
  61. gzip_in.seekg(0);
  62. //Now try the same with larger buffer (but not large enough for all output, uncompressed size 1561 bytes)
  63. {
  64. izlibstream igzs(gzip_in, 1000);
  65. igzs.exceptions(std::ios::failbit | std::ios::badbit);
  66. TS_ASSERT(igzs.good());
  67. TS_ASSERT_THROWS_NOTHING(igzs >> &data);
  68. TS_ASSERT(igzs);
  69. TS_ASSERT(igzs.eof());
  70. TS_ASSERT_EQUALS(data.str(), bigtest);
  71. }
  72. data.str("");
  73. gzip_in.clear();
  74. gzip_in.seekg(0);
  75. //Now with large buffer
  76. {
  77. izlibstream igzs(gzip_in, 4000);
  78. igzs.exceptions(std::ios::failbit | std::ios::badbit);
  79. TS_ASSERT(igzs.good());
  80. TS_ASSERT_THROWS_NOTHING(igzs >> &data);
  81. TS_ASSERT(igzs);
  82. TS_ASSERT(igzs.eof());
  83. TS_ASSERT_EQUALS(data.str(), bigtest);
  84. }
  85. }
  86. void test_inflate_zlib()
  87. {
  88. std::string input(__binary_bigtest_zlib_start, __binary_bigtest_zlib_end);
  89. std::istringstream zlib_in(input, std::ios::binary);
  90. TS_ASSERT(zlib_in);
  91. std::stringbuf data;
  92. izlibstream izls(zlib_in, 256);
  93. izls.exceptions(std::ios::failbit | std::ios::badbit);
  94. TS_ASSERT(izls.good());
  95. TS_ASSERT_THROWS_NOTHING(izls >> &data);
  96. TS_ASSERT(izls);
  97. TS_ASSERT(izls.eof());
  98. TS_ASSERT_EQUALS(data.str(), bigtest);
  99. }
  100. void test_inflate_corrupt()
  101. {
  102. std::string input(__binary_bigtest_corrupt_nbt_start, __binary_bigtest_corrupt_nbt_end);
  103. std::istringstream gzip_in(input, std::ios::binary);
  104. TS_ASSERT(gzip_in);
  105. std::vector<char> buf(bigtest.size());
  106. izlibstream igzs(gzip_in);
  107. igzs.exceptions(std::ios::failbit | std::ios::badbit);
  108. TS_ASSERT_THROWS(igzs.read(buf.data(), buf.size()), zlib_error);
  109. TS_ASSERT(igzs.bad());
  110. }
  111. void test_inflate_eof()
  112. {
  113. std::string input(__binary_bigtest_eof_nbt_start, __binary_bigtest_eof_nbt_end);
  114. std::istringstream gzip_in(input, std::ios::binary);
  115. TS_ASSERT(gzip_in);
  116. std::vector<char> buf(bigtest.size());
  117. izlibstream igzs(gzip_in);
  118. igzs.exceptions(std::ios::failbit | std::ios::badbit);
  119. TS_ASSERT_THROWS(igzs.read(buf.data(), buf.size()), zlib_error);
  120. TS_ASSERT(igzs.bad());
  121. }
  122. void test_inflate_trailing()
  123. {
  124. //This file contains additional uncompressed data after the zlib-compressed data
  125. std::string input(__binary_trailing_data_zlib_start, __binary_trailing_data_zlib_end);
  126. std::istringstream file(input, std::ios::binary);
  127. izlibstream izls(file, 32);
  128. TS_ASSERT(file && izls);
  129. std::string str;
  130. izls >> str;
  131. TS_ASSERT(izls);
  132. TS_ASSERT(izls.eof());
  133. TS_ASSERT_EQUALS(str, "foobar");
  134. //Now read the uncompressed data
  135. TS_ASSERT(file);
  136. TS_ASSERT(!file.eof());
  137. file >> str;
  138. TS_ASSERT(!file.bad());
  139. TS_ASSERT_EQUALS(str, "barbaz");
  140. }
  141. void test_deflate_zlib()
  142. {
  143. //Here we assume that inflating works and has already been tested
  144. std::stringstream str;
  145. std::stringbuf output;
  146. //Small buffer
  147. {
  148. ozlibstream ozls(str, -1, false, 256);
  149. ozls.exceptions(std::ios::failbit | std::ios::badbit);
  150. TS_ASSERT_THROWS_NOTHING(ozls << bigtest);
  151. TS_ASSERT(ozls.good());
  152. TS_ASSERT_THROWS_NOTHING(ozls.close());
  153. TS_ASSERT(ozls.good());
  154. }
  155. TS_ASSERT(str.good());
  156. {
  157. izlibstream izls(str);
  158. TS_ASSERT_THROWS_NOTHING(izls >> &output);
  159. TS_ASSERT(izls);
  160. }
  161. TS_ASSERT_EQUALS(output.str(), bigtest);
  162. str.clear(); str.str("");
  163. output.str("");
  164. //Medium sized buffer
  165. //Write first half, then flush and write second half
  166. {
  167. ozlibstream ozls(str, 9, false, 512);
  168. ozls.exceptions(std::ios::failbit | std::ios::badbit);
  169. std::string half1 = bigtest.substr(0, bigtest.size()/2);
  170. std::string half2 = bigtest.substr(bigtest.size()/2);
  171. TS_ASSERT_THROWS_NOTHING(ozls << half1 << std::flush << half2);
  172. TS_ASSERT(ozls.good());
  173. TS_ASSERT_THROWS_NOTHING(ozls.close());
  174. TS_ASSERT(ozls.good());
  175. }
  176. TS_ASSERT(str.good());
  177. {
  178. izlibstream izls(str);
  179. izls >> &output;
  180. TS_ASSERT(izls);
  181. }
  182. TS_ASSERT_EQUALS(output.str(), bigtest);
  183. str.clear(); str.str("");
  184. output.str("");
  185. //Large buffer
  186. {
  187. ozlibstream ozls(str, 1, false, 4000);
  188. ozls.exceptions(std::ios::failbit | std::ios::badbit);
  189. TS_ASSERT_THROWS_NOTHING(ozls << bigtest);
  190. TS_ASSERT(ozls.good());
  191. TS_ASSERT_THROWS_NOTHING(ozls.close());
  192. TS_ASSERT_THROWS_NOTHING(ozls.close()); //closing twice shouldn't be a problem
  193. TS_ASSERT(ozls.good());
  194. }
  195. TS_ASSERT(str.good());
  196. {
  197. izlibstream izls(str);
  198. izls >> &output;
  199. TS_ASSERT(izls);
  200. }
  201. TS_ASSERT_EQUALS(output.str(), bigtest);
  202. }
  203. void test_deflate_gzip()
  204. {
  205. std::stringstream str;
  206. std::stringbuf output;
  207. {
  208. ozlibstream ozls(str, -1, true);
  209. ozls.exceptions(std::ios::failbit | std::ios::badbit);
  210. TS_ASSERT_THROWS_NOTHING(ozls << bigtest);
  211. TS_ASSERT(ozls.good());
  212. TS_ASSERT_THROWS_NOTHING(ozls.close());
  213. TS_ASSERT(ozls.good());
  214. }
  215. TS_ASSERT(str.good());
  216. {
  217. izlibstream izls(str);
  218. izls >> &output;
  219. TS_ASSERT(izls);
  220. }
  221. TS_ASSERT_EQUALS(output.str(), bigtest);
  222. }
  223. void test_deflate_closed()
  224. {
  225. std::stringstream str;
  226. {
  227. ozlibstream ozls(str);
  228. ozls.exceptions(std::ios::failbit | std::ios::badbit);
  229. TS_ASSERT_THROWS_NOTHING(ozls << bigtest);
  230. TS_ASSERT_THROWS_NOTHING(ozls.close());
  231. TS_ASSERT_THROWS_NOTHING(ozls << "foo");
  232. TS_ASSERT_THROWS_ANYTHING(ozls.close());
  233. TS_ASSERT(ozls.bad());
  234. TS_ASSERT(!str);
  235. }
  236. str.clear();
  237. str.seekp(0);
  238. {
  239. ozlibstream ozls(str);
  240. //this time without exceptions
  241. TS_ASSERT_THROWS_NOTHING(ozls << bigtest);
  242. TS_ASSERT_THROWS_NOTHING(ozls.close());
  243. TS_ASSERT_THROWS_NOTHING(ozls << "foo" << std::flush);
  244. TS_ASSERT(ozls.bad());
  245. TS_ASSERT_THROWS_NOTHING(ozls.close());
  246. TS_ASSERT(ozls.bad());
  247. TS_ASSERT(!str);
  248. }
  249. }
  250. };