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