1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef UT_NET_MESSAGE
18 #define UT_NET_MESSAGE
20 class CUTNetMessage
: public Test::Suite
25 TEST_ADD(CUTNetMessage::messageSwap
);
26 TEST_ADD(CUTNetMessage::lockSubMEssage
);
27 TEST_ADD(CUTNetMessage::lockSubMEssageWithLongName
);
31 void lockSubMEssageWithLongName()
33 NLNET::CMessage
master("BIG");
36 for (uint8 i
=0; i
<10; ++i
)
43 // serial 4 sub messages
44 for (uint i
=0; i
<4; ++i
)
46 NLNET::CMessage
sub(NLMISC::toString("A_VERY_LONG_SUB_MESSAGE_NAME_%u", i
));
48 for (uint8 j
=0; j
<i
*4; ++j
)
53 string
s("A VERY LONG MESSAGE THAT COULD BE A PROBLEM TO HANDLE");
56 sizes
[i
] = sub
.length();
58 master
.serialMessage(sub
);
64 // now, unpack and check
66 // read the first master data
67 for (uint8 i
=0; i
<10; ++i
)
75 // unpack each sub message
76 for (uint i
=0; i
<4; ++i
)
79 master
.serial(subSize
);
81 master
.lockSubMessage(subSize
);
82 TEST_ASSERT(subSize
== sizes
[i
]);
84 TEST_ASSERT(master
.getName() == NLMISC::toString("A_VERY_LONG_SUB_MESSAGE_NAME_%u", i
));
85 TEST_ASSERT(master
.length() == sizes
[i
]);
87 for (uint8 j
=0; j
<i
*4; ++j
)
96 TEST_ASSERT(s
== "A VERY LONG MESSAGE THAT COULD BE A PROBLEM TO HANDLE");
98 TEST_ASSERT(master
.getPos() == master
.length());
100 master
.unlockSubMessage();
103 // rewind the message
104 master
.seek(master
.getHeaderSize(), NLMISC::IStream::begin
);
106 // read the first master data
107 for (uint8 i
=0; i
<10; ++i
)
115 // assign from each sub message
116 for (uint i
=0; i
<4; ++i
)
119 master
.serial(subSize
);
121 master
.lockSubMessage(subSize
);
123 TEST_ASSERT(subSize
== sizes
[i
]);
125 TEST_ASSERT(master
.getName() == NLMISC::toString("A_VERY_LONG_SUB_MESSAGE_NAME_%u", i
));
126 TEST_ASSERT(master
.length() == sizes
[i
]);
129 sub
.assignFromSubMessage(master
);
131 for (uint8 j
=0; j
<i
*4; ++j
)
140 TEST_ASSERT(s
== "A VERY LONG MESSAGE THAT COULD BE A PROBLEM TO HANDLE");
142 TEST_ASSERT(sub
.getPos() == sub
.length());
144 master
.unlockSubMessage();
149 void lockSubMEssage()
151 NLNET::CMessage
master("BIG");
154 for (uint8 i
=0; i
<10; ++i
)
161 // serial 4 sub messages
162 for (uint i
=0; i
<4; ++i
)
164 NLNET::CMessage
sub(NLMISC::toString("SUB_%u", i
));
166 for (uint8 j
=0; j
<i
*4; ++j
)
171 string
s("A MESSAGE");
174 sizes
[i
] = sub
.length();
176 master
.serialMessage(sub
);
179 // invert the message
182 // now, unpack and check
184 // read the first master data
185 for (uint8 i
=0; i
<10; ++i
)
193 // unpack each sub message
194 for (uint i
=0; i
<4; ++i
)
197 master
.serial(subSize
);
199 master
.lockSubMessage(subSize
);
200 TEST_ASSERT(subSize
== sizes
[i
]);
202 TEST_ASSERT(master
.getName() == NLMISC::toString("SUB_%u", i
));
203 TEST_ASSERT(master
.length() == sizes
[i
]);
205 for (uint8 j
=0; j
<i
*4; ++j
)
214 TEST_ASSERT(s
== "A MESSAGE");
216 TEST_ASSERT(master
.getPos() == master
.length());
218 master
.unlockSubMessage();
221 // rewind the message
222 master
.seek(master
.getHeaderSize(), NLMISC::IStream::begin
);
224 // read the first master data
225 for (uint8 i
=0; i
<10; ++i
)
233 // assign from each sub message
234 for (uint i
=0; i
<4; ++i
)
237 master
.serial(subSize
);
239 master
.lockSubMessage(subSize
);
241 TEST_ASSERT(subSize
== sizes
[i
]);
243 TEST_ASSERT(master
.getName() == NLMISC::toString("SUB_%u", i
));
244 TEST_ASSERT(master
.length() == sizes
[i
]);
247 sub
.assignFromSubMessage(master
);
249 for (uint8 j
=0; j
<i
*4; ++j
)
258 TEST_ASSERT(s
== "A MESSAGE");
260 TEST_ASSERT(sub
.getPos() == sub
.length());
262 master
.unlockSubMessage();
269 NLNET::CMessage msg2
;
273 NLNET::CMessage msg1
;
274 msg1
.setType("NAME", NLNET::CMessage::Request
);
284 // check that ms1 is empty now
285 TEST_ASSERT(msg1
.length() == 0);
286 TEST_ASSERT(!msg1
.typeIsSet());
289 TEST_ASSERT(!msg2
.isReading());
291 TEST_ASSERT(msg2
.typeIsSet());
292 TEST_ASSERT(msg2
.getName() == "NAME");
293 TEST_ASSERT(msg2
.getType() == NLNET::CMessage::Request
);
295 TEST_ASSERT(s
== "foo1");
297 TEST_ASSERT(s
== "foo2");