Debian Codegraph @

tests/core/io/test_stream_peer_gzip.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_stream_peer_gzip.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 "core/io/stream_peer_gzip.h"
34 #include "tests/test_macros.h"
35
36 namespace TestStreamPeerGZIP {
37
38 const String hello = "Hello World!!!";
39
40 TEST_CASE("[StreamPeerGZIP] Initialization") {
41 Ref<StreamPeerGZIP> spgz;
42 spgz.instantiate();
43 CHECK_EQ(spgz->get_available_bytes(), 0);
44 }
45
46 TEST_CASE("[StreamPeerGZIP] Compress/Decompress") {
47 Ref<StreamPeerGZIP> spgz;
48 spgz.instantiate();
49
50 bool is_deflate = false;
51
52 SUBCASE("GZIP") {
53 is_deflate = false;
54 }
55
56 SUBCASE("DEFLATE") {
57 is_deflate = true;
58 }
59
60 CHECK_EQ(spgz->start_compression(is_deflate), Error::OK);
61 CHECK_EQ(spgz->put_data(hello.to_ascii_buffer().ptr(), hello.to_ascii_buffer().size()), Error::OK);
62 CHECK_EQ(spgz->finish(), Error::OK);
63
64 Vector<uint8_t> hello_compressed;
65 int hello_compressed_size = spgz->get_available_bytes();
66 hello_compressed.resize(hello_compressed_size);
67 CHECK_EQ(spgz->get_data(hello_compressed.ptrw(), hello_compressed_size), Error::OK);
68
69 spgz->clear();
70
71 CHECK_EQ(spgz->start_decompression(is_deflate), Error::OK);
72 CHECK_EQ(spgz->put_data(hello_compressed.ptr(), hello_compressed.size()), Error::OK);
73
74 Vector<uint8_t> hello_decompressed;
75 int hello_decompressed_size = spgz->get_available_bytes();
76 hello_decompressed.resize(hello_decompressed_size);
77 CHECK_EQ(spgz->get_data(hello_decompressed.ptrw(), hello_decompressed_size), Error::OK);
78 CHECK_EQ(hello_decompressed, hello.to_ascii_buffer());
79 }
80
81 TEST_CASE("[StreamPeerGZIP] Compress/Decompress big chunks of data") { // GH-97201
82 Ref<StreamPeerGZIP> spgz;
83 spgz.instantiate();
84 CHECK_EQ(spgz->start_compression(false), Error::OK);
85
86 Vector<uint8_t> big_data;
87 big_data.resize(2500);
88 // Filling it with random data because the issue is related to the size of the data when it's compress.
89 // Random data results in bigger compressed data size.
90 for (int i = 0; i < big_data.size(); i++) {
91 big_data.write[i] = Math::random(48, 122);
92 }
93 CHECK_EQ(spgz->put_data(big_data.ptr(), big_data.size()), Error::OK);
94 CHECK_EQ(spgz->finish(), Error::OK);
95
96 Vector<uint8_t> big_data_compressed;
97 int big_data_compressed_size = spgz->get_available_bytes();
98 big_data_compressed.resize(big_data_compressed_size);
99 CHECK_EQ(spgz->get_data(big_data_compressed.ptrw(), big_data_compressed_size), Error::OK);
100
101 spgz->clear();
102
103 CHECK_EQ(spgz->start_decompression(false), Error::OK);
104 CHECK_EQ(spgz->put_data(big_data_compressed.ptr(), big_data_compressed.size()), Error::OK);
105
106 Vector<uint8_t> big_data_decompressed;
107 int big_data_decompressed_size = spgz->get_available_bytes();
108 big_data_decompressed.resize(big_data_decompressed_size);
109 CHECK_EQ(spgz->get_data(big_data_decompressed.ptrw(), big_data_decompressed_size), Error::OK);
110 CHECK_EQ(big_data_decompressed, big_data);
111 }
112
113 TEST_CASE("[StreamPeerGZIP] Can't start twice") {
114 Ref<StreamPeerGZIP> spgz;
115 spgz.instantiate();
116 CHECK_EQ(spgz->start_compression(false), Error::OK);
117
118 ERR_PRINT_OFF;
119 CHECK_EQ(spgz->start_compression(false), Error::ERR_ALREADY_IN_USE);
120 CHECK_EQ(spgz->start_decompression(false), Error::ERR_ALREADY_IN_USE);
121 ERR_PRINT_ON;
122 }
123
124 TEST_CASE("[StreamPeerGZIP] Can't start with a buffer size equal or less than zero") {
125 Ref<StreamPeerGZIP> spgz;
126 spgz.instantiate();
127
128 ERR_PRINT_OFF;
129 CHECK_EQ(spgz->start_compression(false, 0), Error::ERR_INVALID_PARAMETER);
130 CHECK_EQ(spgz->start_compression(false, -1), Error::ERR_INVALID_PARAMETER);
131 CHECK_EQ(spgz->start_decompression(false, 0), Error::ERR_INVALID_PARAMETER);
132 CHECK_EQ(spgz->start_decompression(false, -1), Error::ERR_INVALID_PARAMETER);
133 ERR_PRINT_ON;
134 }
135
136 TEST_CASE("[StreamPeerGZIP] Can't put/get data with a buffer size less than zero") {
137 Ref<StreamPeerGZIP> spgz;
138 spgz.instantiate();
139 CHECK_EQ(spgz->start_compression(false), Error::OK);
140
141 ERR_PRINT_OFF;
142 CHECK_EQ(spgz->put_data(hello.to_ascii_buffer().ptr(), -1), Error::ERR_INVALID_PARAMETER);
143
144 Vector<uint8_t> hello_compressed;
145 hello_compressed.resize(5);
146 CHECK_EQ(spgz->get_data(hello_compressed.ptrw(), -1), Error::ERR_INVALID_PARAMETER);
147 ERR_PRINT_ON;
148 }
149
150 TEST_CASE("[StreamPeerGZIP] Needs to be started before use") {
151 Ref<StreamPeerGZIP> spgz;
152 spgz.instantiate();
153
154 ERR_PRINT_OFF;
155 CHECK_EQ(spgz->put_data(hello.to_ascii_buffer().ptr(), hello.to_ascii_buffer().size()), Error::ERR_UNCONFIGURED);
156 ERR_PRINT_ON;
157 }
158
159 TEST_CASE("[StreamPeerGZIP] Can't be finished after clear or if it's decompressing") {
160 Ref<StreamPeerGZIP> spgz;
161 spgz.instantiate();
162
163 CHECK_EQ(spgz->start_compression(false), Error::OK);
164 spgz->clear();
165 ERR_PRINT_OFF;
166 CHECK_EQ(spgz->finish(), Error::ERR_UNAVAILABLE);
167 ERR_PRINT_ON;
168
169 spgz->clear();
170 CHECK_EQ(spgz->start_decompression(false), Error::OK);
171 ERR_PRINT_OFF;
172 CHECK_EQ(spgz->finish(), Error::ERR_UNAVAILABLE);
173 ERR_PRINT_ON;
174 }
175
176 TEST_CASE("[StreamPeerGZIP] Fails to get if nothing was compress/decompress") {
177 Ref<StreamPeerGZIP> spgz;
178 spgz.instantiate();
179
180 SUBCASE("Compression") {
181 CHECK_EQ(spgz->start_compression(false), Error::OK);
182 }
183
184 SUBCASE("Decompression") {
185 CHECK_EQ(spgz->start_decompression(false), Error::OK);
186 }
187
188 ERR_PRINT_OFF;
189 Vector<uint8_t> hello_compressed;
190 hello_compressed.resize(5);
191 CHECK_EQ(spgz->get_data(hello_compressed.ptrw(), hello_compressed.size()), Error::ERR_UNAVAILABLE);
192 ERR_PRINT_ON;
193 }
194
195 } // namespace TestStreamPeerGZIP