test_zip.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**************************************************************************/
  2. /* test_zip.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "tests/test_macros.h"
  32. #include "tests/test_utils.h"
  33. #include "../zip_packer.h"
  34. #include "../zip_reader.h"
  35. namespace TestZip {
  36. void check_file_size(const String &p_path, int p_expected_size);
  37. TEST_CASE("[ZIPPacker] default compression") {
  38. const String path = TestUtils::get_temp_path("compressed.zip");
  39. Ref<ZIPPacker> packer;
  40. packer.instantiate();
  41. Error open_result = packer->open(path, ZIPPacker::APPEND_CREATE);
  42. CHECK(open_result == OK);
  43. Error start_file_result = packer->start_file("demo.txt");
  44. CHECK(start_file_result == OK);
  45. String text = "hello world!";
  46. Error write_file_result = packer->write_file(text.to_utf8_buffer());
  47. CHECK(write_file_result == OK);
  48. Error close_file_result = packer->close_file();
  49. CHECK(close_file_result == OK);
  50. Error close_result = packer->close();
  51. CHECK(close_result == OK);
  52. check_file_size(path, 128);
  53. }
  54. TEST_CASE("[ZIPPacker] no compression") {
  55. const String path = TestUtils::get_temp_path("uncompressed.zip");
  56. Ref<ZIPPacker> packer;
  57. packer.instantiate();
  58. Error open_result = packer->open(path, ZIPPacker::APPEND_CREATE);
  59. CHECK(open_result == OK);
  60. packer->set_compression_level(ZIPPacker::COMPRESSION_NONE);
  61. Error start_file_result = packer->start_file("demo.txt");
  62. CHECK(start_file_result == OK);
  63. String text = "hello world!";
  64. Error write_file_result = packer->write_file(text.to_utf8_buffer());
  65. CHECK(write_file_result == OK);
  66. Error close_file_result = packer->close_file();
  67. CHECK(close_file_result == OK);
  68. Error close_result = packer->close();
  69. CHECK(close_result == OK);
  70. check_file_size(path, 131);
  71. }
  72. TEST_CASE("[ZIPReader] read files") {
  73. String test_data = String("modules/zip/tests/data/").path_join("test.zip");
  74. Ref<ZIPReader> reader;
  75. reader.instantiate();
  76. Error open_result = reader->open(test_data);
  77. CHECK(open_result == OK);
  78. const String hello_path = "hello.txt";
  79. const String world_path = "world.txt";
  80. PackedStringArray expected_files;
  81. expected_files.push_back(hello_path);
  82. expected_files.push_back(world_path);
  83. CHECK(reader->get_files() == expected_files);
  84. const String expected_hello_text = "hello world!";
  85. const String expected_world_text = "game over!";
  86. PackedByteArray hello_bytes = reader->read_file(hello_path, false);
  87. PackedByteArray world_bytes = reader->read_file(world_path, true);
  88. CHECK(hello_bytes == expected_hello_text.to_utf8_buffer());
  89. CHECK(world_bytes == expected_world_text.to_utf8_buffer());
  90. CHECK(reader->get_compression_level(hello_path, true) == 6);
  91. CHECK(reader->get_compression_level(world_path, false) == 9);
  92. }
  93. } // namespace TestZip