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