Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/io/test_packet_peer.h
10278 views
1
/**************************************************************************/
2
/* test_packet_peer.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/packet_peer.h"
34
#include "tests/test_macros.h"
35
36
namespace TestPacketPeer {
37
38
TEST_CASE("[PacketPeer][PacketPeerStream] Encode buffer max size") {
39
Ref<PacketPeerStream> pps;
40
pps.instantiate();
41
42
SUBCASE("Default value") {
43
CHECK_EQ(pps->get_encode_buffer_max_size(), 8 * 1024 * 1024);
44
}
45
46
SUBCASE("Max encode buffer must be at least 1024 bytes") {
47
ERR_PRINT_OFF;
48
pps->set_encode_buffer_max_size(42);
49
ERR_PRINT_ON;
50
51
CHECK_EQ(pps->get_encode_buffer_max_size(), 8 * 1024 * 1024);
52
}
53
54
SUBCASE("Max encode buffer cannot exceed 256 MiB") {
55
ERR_PRINT_OFF;
56
pps->set_encode_buffer_max_size((256 * 1024 * 1024) + 42);
57
ERR_PRINT_ON;
58
59
CHECK_EQ(pps->get_encode_buffer_max_size(), 8 * 1024 * 1024);
60
}
61
62
SUBCASE("Should be next power of two") {
63
pps->set_encode_buffer_max_size(2000);
64
65
CHECK_EQ(pps->get_encode_buffer_max_size(), 2048);
66
}
67
}
68
69
TEST_CASE("[PacketPeer][PacketPeerStream] Read a variant from peer") {
70
String godot_rules = "Godot Rules!!!";
71
72
Ref<StreamPeerBuffer> spb;
73
spb.instantiate();
74
spb->put_var(godot_rules);
75
spb->seek(0);
76
77
Ref<PacketPeerStream> pps;
78
pps.instantiate();
79
pps->set_stream_peer(spb);
80
81
Variant value;
82
CHECK_EQ(pps->get_var(value), Error::OK);
83
CHECK_EQ(String(value), godot_rules);
84
}
85
86
TEST_CASE("[PacketPeer][PacketPeerStream] Read a variant from peer fails") {
87
Ref<PacketPeerStream> pps;
88
pps.instantiate();
89
90
Variant value;
91
ERR_PRINT_OFF;
92
CHECK_EQ(pps->get_var(value), Error::ERR_UNCONFIGURED);
93
ERR_PRINT_ON;
94
}
95
96
TEST_CASE("[PacketPeer][PacketPeerStream] Put a variant to peer") {
97
String godot_rules = "Godot Rules!!!";
98
99
Ref<StreamPeerBuffer> spb;
100
spb.instantiate();
101
102
Ref<PacketPeerStream> pps;
103
pps.instantiate();
104
pps->set_stream_peer(spb);
105
106
CHECK_EQ(pps->put_var(godot_rules), Error::OK);
107
108
spb->seek(0);
109
CHECK_EQ(String(spb->get_var()), godot_rules);
110
}
111
112
TEST_CASE("[PacketPeer][PacketPeerStream] Put a variant to peer out of memory failure") {
113
String more_than_1mb = String("*").repeat(1024 + 1);
114
115
Ref<StreamPeerBuffer> spb;
116
spb.instantiate();
117
118
Ref<PacketPeerStream> pps;
119
pps.instantiate();
120
pps->set_stream_peer(spb);
121
pps->set_encode_buffer_max_size(1024);
122
123
ERR_PRINT_OFF;
124
CHECK_EQ(pps->put_var(more_than_1mb), Error::ERR_OUT_OF_MEMORY);
125
ERR_PRINT_ON;
126
}
127
128
TEST_CASE("[PacketPeer][PacketPeerStream] Get packet buffer") {
129
String godot_rules = "Godot Rules!!!";
130
131
Ref<StreamPeerBuffer> spb;
132
spb.instantiate();
133
// First 4 bytes are the length of the string.
134
CharString cs = godot_rules.ascii();
135
Vector<uint8_t> buffer = { (uint8_t)(cs.length() + 1), 0, 0, 0 };
136
buffer.resize_initialized(4 + cs.length() + 1);
137
memcpy(buffer.ptrw() + 4, cs.get_data(), cs.length());
138
spb->set_data_array(buffer);
139
140
Ref<PacketPeerStream> pps;
141
pps.instantiate();
142
pps->set_stream_peer(spb);
143
144
buffer.clear();
145
CHECK_EQ(pps->get_packet_buffer(buffer), Error::OK);
146
147
CHECK_EQ(String(reinterpret_cast<const char *>(buffer.ptr())), godot_rules);
148
}
149
150
TEST_CASE("[PacketPeer][PacketPeerStream] Get packet buffer from an empty peer") {
151
Ref<StreamPeerBuffer> spb;
152
spb.instantiate();
153
154
Ref<PacketPeerStream> pps;
155
pps.instantiate();
156
pps->set_stream_peer(spb);
157
158
Vector<uint8_t> buffer;
159
ERR_PRINT_OFF;
160
CHECK_EQ(pps->get_packet_buffer(buffer), Error::ERR_UNAVAILABLE);
161
ERR_PRINT_ON;
162
CHECK_EQ(buffer.size(), 0);
163
}
164
165
TEST_CASE("[PacketPeer][PacketPeerStream] Put packet buffer") {
166
String godot_rules = "Godot Rules!!!";
167
168
Ref<StreamPeerBuffer> spb;
169
spb.instantiate();
170
171
Ref<PacketPeerStream> pps;
172
pps.instantiate();
173
pps->set_stream_peer(spb);
174
175
CHECK_EQ(pps->put_packet_buffer(godot_rules.to_ascii_buffer()), Error::OK);
176
177
spb->seek(0);
178
CHECK_EQ(spb->get_string(), godot_rules);
179
// First 4 bytes are the length of the string.
180
CharString cs = godot_rules.ascii();
181
Vector<uint8_t> buffer = { (uint8_t)cs.length(), 0, 0, 0 };
182
buffer.resize(4 + cs.length());
183
memcpy(buffer.ptrw() + 4, cs.get_data(), cs.length());
184
CHECK_EQ(spb->get_data_array(), buffer);
185
}
186
187
TEST_CASE("[PacketPeer][PacketPeerStream] Put packet buffer when is empty") {
188
Ref<StreamPeerBuffer> spb;
189
spb.instantiate();
190
191
Ref<PacketPeerStream> pps;
192
pps.instantiate();
193
pps->set_stream_peer(spb);
194
195
Vector<uint8_t> buffer;
196
CHECK_EQ(pps->put_packet_buffer(buffer), Error::OK);
197
198
CHECK_EQ(spb->get_size(), 0);
199
}
200
201
} // namespace TestPacketPeer
202
203