test_pck_packer.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*************************************************************************/
  2. /* test_pck_packer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "thirdparty/doctest/doctest.h"
  36. namespace TestPCKPacker {
  37. // Dummy 64-character encryption key (since it's required).
  38. constexpr const char *ENCRYPTION_KEY = "0000000000000000000000000000000000000000000000000000000000000000";
  39. TEST_CASE("[PCKPacker] Pack an empty PCK file") {
  40. PCKPacker pck_packer;
  41. const String output_pck_path = OS::get_singleton()->get_cache_path().plus_file("output_empty.pck");
  42. CHECK_MESSAGE(
  43. pck_packer.pck_start(
  44. output_pck_path,
  45. 32,
  46. ENCRYPTION_KEY) == OK,
  47. "Starting a PCK file should return an OK error code.");
  48. CHECK_MESSAGE(
  49. pck_packer.flush() == OK,
  50. "Flushing the PCK should return an OK error code.");
  51. Error err;
  52. FileAccessRef f = FileAccess::open(output_pck_path, FileAccess::READ, &err);
  53. CHECK_MESSAGE(
  54. err == OK,
  55. "The generated empty PCK file should be opened successfully.");
  56. CHECK_MESSAGE(
  57. f->get_len() >= 100,
  58. "The generated empty PCK file shouldn't be too small (it should have the PCK header).");
  59. CHECK_MESSAGE(
  60. f->get_len() <= 500,
  61. "The generated empty PCK file shouldn't be too large.");
  62. }
  63. TEST_CASE("[PCKPacker] Pack a PCK file with some files and directories") {
  64. PCKPacker pck_packer;
  65. const String output_pck_path = OS::get_singleton()->get_cache_path().plus_file("output_with_files.pck");
  66. CHECK_MESSAGE(
  67. pck_packer.pck_start(
  68. output_pck_path,
  69. 32,
  70. ENCRYPTION_KEY) == OK,
  71. "Starting a PCK file should return an OK error code.");
  72. const String base_dir = OS::get_singleton()->get_executable_path().get_base_dir();
  73. CHECK_MESSAGE(
  74. pck_packer.add_file("version.py", base_dir.plus_file("../version.py"), "version.py") == OK,
  75. "Adding a file to the PCK should return an OK error code.");
  76. CHECK_MESSAGE(
  77. pck_packer.add_file("some/directories with spaces/to/create/icon.png", base_dir.plus_file("../icon.png")) == OK,
  78. "Adding a file to a new subdirectory in the PCK should return an OK error code.");
  79. CHECK_MESSAGE(
  80. pck_packer.add_file("some/directories with spaces/to/create/icon.svg", base_dir.plus_file("../icon.svg")) == OK,
  81. "Adding a file to an existing subdirectory in the PCK should return an OK error code.");
  82. CHECK_MESSAGE(
  83. pck_packer.add_file("some/directories with spaces/to/create/icon.png", base_dir.plus_file("../logo.png")) == OK,
  84. "Overriding a non-flushed file to an existing subdirectory in the PCK should return an OK error code.");
  85. CHECK_MESSAGE(
  86. pck_packer.flush() == OK,
  87. "Flushing the PCK should return an OK error code.");
  88. Error err;
  89. FileAccessRef f = FileAccess::open(output_pck_path, FileAccess::READ, &err);
  90. CHECK_MESSAGE(
  91. err == OK,
  92. "The generated non-empty PCK file should be opened successfully.");
  93. CHECK_MESSAGE(
  94. f->get_len() >= 25000,
  95. "The generated non-empty PCK file should be large enough to actually hold the contents specified above.");
  96. CHECK_MESSAGE(
  97. f->get_len() <= 35000,
  98. "The generated non-empty PCK file shouldn't be too large.");
  99. }
  100. } // namespace TestPCKPacker
  101. #endif // TEST_PCK_PACKER_H