Skip to content
0.2.0-dev Download ZIP
105 lines ·4.7 KB Raw
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
31#pragma once
32
33#include "../zip_packer.h"
34#include "../zip_reader.h"
35
36#include "tests/test_macros.h"
37#include "tests/test_utils.h"
38
39namespace TestZip {
40
41void check_file_size(const String &p_path, int p_expected_size);
42
43TEST_CASE("[ZIPPacker] default compression") {
44 const String path = TestUtils::get_temp_path("compressed.zip");
45 Ref<ZIPPacker> packer;
46 packer.instantiate();
47 Error open_result = packer->open(path, ZIPPacker::APPEND_CREATE);
48 CHECK(open_result == OK);
49 Error start_file_result = packer->start_file("demo.txt");
50 CHECK(start_file_result == OK);
51 String text = "hello world!";
52 Error write_file_result = packer->write_file(text.to_utf8_buffer());
53 CHECK(write_file_result == OK);
54 Error close_file_result = packer->close_file();
55 CHECK(close_file_result == OK);
56 Error close_result = packer->close();
57 CHECK(close_result == OK);
58 check_file_size(path, 128);
59}
60
61TEST_CASE("[ZIPPacker] no compression") {
62 const String path = TestUtils::get_temp_path("uncompressed.zip");
63 Ref<ZIPPacker> packer;
64 packer.instantiate();
65 Error open_result = packer->open(path, ZIPPacker::APPEND_CREATE);
66 CHECK(open_result == OK);
67 packer->set_compression_level(ZIPPacker::COMPRESSION_NONE);
68 Error start_file_result = packer->start_file("demo.txt");
69 CHECK(start_file_result == OK);
70 String text = "hello world!";
71 Error write_file_result = packer->write_file(text.to_utf8_buffer());
72 CHECK(write_file_result == OK);
73 Error close_file_result = packer->close_file();
74 CHECK(close_file_result == OK);
75 Error close_result = packer->close();
76 CHECK(close_result == OK);
77 check_file_size(path, 131);
78}
79
80TEST_CASE("[ZIPReader] read files") {
81 String test_data = String("modules/zip/tests/data/").path_join("test.zip");
82 Ref<ZIPReader> reader;
83 reader.instantiate();
84 Error open_result = reader->open(test_data);
85 CHECK(open_result == OK);
86
87 const String hello_path = "hello.txt";
88 const String world_path = "world.txt";
89 PackedStringArray expected_files;
90 expected_files.push_back(hello_path);
91 expected_files.push_back(world_path);
92 CHECK(reader->get_files() == expected_files);
93
94 const String expected_hello_text = "hello world!";
95 const String expected_world_text = "game over!";
96 PackedByteArray hello_bytes = reader->read_file(hello_path, false);
97 PackedByteArray world_bytes = reader->read_file(world_path, true);
98 CHECK(hello_bytes == expected_hello_text.to_utf8_buffer());
99 CHECK(world_bytes == expected_world_text.to_utf8_buffer());
100
101 CHECK(reader->get_compression_level(hello_path, true) == 6);
102 CHECK(reader->get_compression_level(world_path, false) == 9);
103}
104
105} // namespace TestZip