test_pck_packer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**************************************************************************/
  2. /* test_pck_packer.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. #ifndef TEST_PCK_PACKER_H
  31. #define TEST_PCK_PACKER_H
  32. #include "core/io/file_access_pack.h"
  33. #include "core/io/pck_packer.h"
  34. #include "core/os/os.h"
  35. #include "tests/test_utils.h"
  36. #include "thirdparty/doctest/doctest.h"
  37. namespace TestPCKPacker {
  38. TEST_CASE("[PCKPacker] Pack an empty PCK file") {
  39. PCKPacker pck_packer;
  40. const String output_pck_path = TestUtils::get_temp_path("output_empty.pck");
  41. CHECK_MESSAGE(
  42. pck_packer.pck_start(output_pck_path) == OK,
  43. "Starting a PCK file should return an OK error code.");
  44. CHECK_MESSAGE(
  45. pck_packer.flush() == OK,
  46. "Flushing the PCK should return an OK error code.");
  47. Error err;
  48. Ref<FileAccess> f = FileAccess::open(output_pck_path, FileAccess::READ, &err);
  49. CHECK_MESSAGE(
  50. err == OK,
  51. "The generated empty PCK file should be opened successfully.");
  52. CHECK_MESSAGE(
  53. f->get_length() >= 100,
  54. "The generated empty PCK file shouldn't be too small (it should have the PCK header).");
  55. CHECK_MESSAGE(
  56. f->get_length() <= 500,
  57. "The generated empty PCK file shouldn't be too large.");
  58. }
  59. TEST_CASE("[PCKPacker] Pack empty with zero alignment invalid") {
  60. PCKPacker pck_packer;
  61. const String output_pck_path = TestUtils::get_temp_path("output_empty.pck");
  62. ERR_PRINT_OFF;
  63. CHECK_MESSAGE(pck_packer.pck_start(output_pck_path, 0) != OK, "PCK with zero alignment should fail.");
  64. ERR_PRINT_ON;
  65. }
  66. TEST_CASE("[PCKPacker] Pack empty with invalid key") {
  67. PCKPacker pck_packer;
  68. const String output_pck_path = TestUtils::get_temp_path("output_empty.pck");
  69. ERR_PRINT_OFF;
  70. CHECK_MESSAGE(pck_packer.pck_start(output_pck_path, 32, "") != OK, "PCK with invalid key should fail.");
  71. ERR_PRINT_ON;
  72. }
  73. TEST_CASE("[PCKPacker] Pack a PCK file with some files and directories") {
  74. PCKPacker pck_packer;
  75. const String output_pck_path = TestUtils::get_temp_path("output_with_files.pck");
  76. CHECK_MESSAGE(
  77. pck_packer.pck_start(output_pck_path) == OK,
  78. "Starting a PCK file should return an OK error code.");
  79. const String base_dir = OS::get_singleton()->get_executable_path().get_base_dir();
  80. CHECK_MESSAGE(
  81. pck_packer.add_file("version.py", base_dir.path_join("../version.py"), "version.py") == OK,
  82. "Adding a file to the PCK should return an OK error code.");
  83. CHECK_MESSAGE(
  84. pck_packer.add_file("some/directories with spaces/to/create/icon.png", base_dir.path_join("../icon.png")) == OK,
  85. "Adding a file to a new subdirectory in the PCK should return an OK error code.");
  86. CHECK_MESSAGE(
  87. pck_packer.add_file("some/directories with spaces/to/create/icon.svg", base_dir.path_join("../icon.svg")) == OK,
  88. "Adding a file to an existing subdirectory in the PCK should return an OK error code.");
  89. CHECK_MESSAGE(
  90. pck_packer.add_file("some/directories with spaces/to/create/icon.png", base_dir.path_join("../logo.png")) == OK,
  91. "Overriding a non-flushed file to an existing subdirectory in the PCK should return an OK error code.");
  92. CHECK_MESSAGE(
  93. pck_packer.flush() == OK,
  94. "Flushing the PCK should return an OK error code.");
  95. Error err;
  96. Ref<FileAccess> f = FileAccess::open(output_pck_path, FileAccess::READ, &err);
  97. CHECK_MESSAGE(
  98. err == OK,
  99. "The generated non-empty PCK file should be opened successfully.");
  100. CHECK_MESSAGE(
  101. f->get_length() >= 18000,
  102. "The generated non-empty PCK file should be large enough to actually hold the contents specified above.");
  103. CHECK_MESSAGE(
  104. f->get_length() <= 27000,
  105. "The generated non-empty PCK file shouldn't be too large.");
  106. }
  107. } // namespace TestPCKPacker
  108. #endif // TEST_PCK_PACKER_H