| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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") { |
| 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 |
|
| 89 |
|
| 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 |
} |