2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "gnashconfig.h"
25 #include <netinet/in.h>
27 #include <sys/types.h>
28 #include "GnashSystemIOHeaders.h"
29 #include <sys/types.h>
39 bool gofast
= false; // FIXME: this flag gets set based on
40 // an XML message written using
41 // SendCommand(""). This way a movie
42 // can optimize it's own performance
44 bool nodelay
= false; // FIXME: this flag gets set based on
45 // an XML message written using
46 // SendCommand(""). This way a movie
47 // can optimize it's own performance
52 using namespace cygnal
;
53 using namespace gnash
;
56 static void usage (void);
62 void test_Number(void);
63 void test_Boolean(void);
64 void test_String(void);
66 void test_Header(void);
68 void test_Packet(void);
71 main(int argc
, char *argv
[])
77 memset(buffer
, 0, 300);
79 while ((c
= getopt (argc
, argv
, "hdvsm:")) != -1) {
95 // get the file name from the command line
97 string filespec
= argv
[optind
];
98 cout
<< "Will use \"" << filespec
<< "\" for test " << endl
;
115 amfnum_t num
= 123456789;
117 // Write a number element
118 note("Test a Number element");
119 void *out
= amf_obj
.encodeElement(AMF::NUMBER
, &num
, 0);
120 if (amf_obj
.extractElementHeader(out
) == AMF::NUMBER
) {
121 runtest
.pass("Number header correct");
123 runtest
.fail("Number header not correct");
125 if (amf_obj
.extractElementLength(out
) == 8) {
126 runtest
.pass("Number length returned correct");
128 runtest
.fail("Number length returned not correct");
131 char *numptr
= (char *)&num
;
132 char *outptr
= (char *)out
+1;
133 if ((numptr
[0] == outptr
[7]) && (numptr
[1] == outptr
[6])
134 && (numptr
[2] == outptr
[5]) && (numptr
[3] == outptr
[4])
135 && (numptr
[4] == outptr
[3]) && (numptr
[5] == outptr
[2])
136 && (numptr
[6] == outptr
[1]) && (numptr
[7] == outptr
[0])
138 pass("Number swapped correct");
140 fail("Number swapped not correct");
144 char buf
[AMF_NUMBER_SIZE
+1];
145 amfnum_t value
= 0xf03fL
;
148 memset(buf
, 0, AMF_NUMBER_SIZE
+1);
149 fd
= open("number.amf", O_RDONLY
);
150 ret
= read(fd
, buf
, 12);
153 num
= amf_obj
.extractNumber(buf
);
155 // unsigned char hexint[32];
156 // hexify((unsigned char *)hexint, (unsigned char *)num, 8, false);
157 // cerr << "AMF number is: 0x" << hexint << endl;
158 // hexify((unsigned char *)hexint, (unsigned char *)&value, 8, false);
159 // cerr << "AMF value is: 0x" << hexint << endl;
161 if (((char *)num
)[7] == 0x3f) {
162 // if (memcmp(num, &value, AMF_NUMBER_SIZE) == 0) {
163 runtest
.pass("Extracted Number AMF object");
165 runtest
.fail("Extracted Number AMF object");
168 void *out
= amf_obj
.encodeNumber(*num
);
169 // hexify((unsigned char *)hexint, (unsigned char *)out, 9, false);
170 // cerr << "AMF encoded number is: 0x" << hexint << endl;
172 // hexify((unsigned char *)hexint, (unsigned char *)buf, 9, false);
173 // cerr << "AMF buff number is: 0x" << hexint << endl;
175 if (memcmp(out
, buf
, 9) == 0) {
176 runtest
.pass("Encoded AMF Number");
178 runtest
.fail("Encoded AMF Number");
190 // Write a number element
191 void *out
= amf_obj
.encodeElement(AMF::BOOLEAN
, &bo
, 0);
192 if (amf_obj
.extractElementHeader(out
) == AMF::BOOLEAN
) {
193 runtest
.pass("Boolean header correct");
195 runtest
.fail("Boolean header not correct");
197 if (amf_obj
.extractElementLength(out
) == 1) {
198 runtest
.pass("Boolean length returned correct");
200 runtest
.fail("Boolean length returned not correct");
202 if (*((char *)out
+ 1) == 0) {
203 pass("Boolean false returned correct");
205 runtest
.fail("Boolean false returned not correct");
208 out
= amf_obj
.encodeElement(AMF::BOOLEAN
, &bo
, 0);
209 if (*((char *)out
+ 1) == 1) {
210 runtest
.pass("Boolean true returned correct");
212 runtest
.fail("Boolean true returned not correct");
216 // Make sure we can read and write binary AMF strings
222 char buf
[AMF_VIDEO_PACKET_SIZE
+1];
224 // First see if we can read strings. This file is produced by
225 // using a network packet sniffer, and should be binary correct.
226 memset(buf
, 0, AMF_VIDEO_PACKET_SIZE
+1);
227 fd
= open("string1.amf", O_RDONLY
);
228 ret
= read(fd
, buf
, AMF_VIDEO_PACKET_SIZE
);
231 char *str
= amf_obj
.extractString(buf
);
232 if (strcmp(str
, "connect") == 0) {
233 runtest
.pass("Extracted \"connect\" string");
235 runtest
.fail("Extracted \"connect\" string");
238 // Now make sure we can also create strings. We'll create the same
239 // string we just read, and make sure they match.
240 char *connect
= "connect";
241 void *out
= amf_obj
.encodeElement(AMF::STRING
, connect
, strlen(connect
));
242 if (memcmp(out
, buf
, 10) == 0) {
243 runtest
.pass("Encoded \"connect\" string");
245 runtest
.fail("Encoded \"connect\" string");
252 // Each header consists of the following:
254 // * UTF string (including length bytes) - name
255 // * Boolean - specifies if understanding the header is `required'
256 // * Long - Length in bytes of header
257 // * Variable - Actual data (including a type code)
261 amfutf8_t name
, headname
;
266 note("Test the Header");
270 char *test
= "NumberTest";
271 name
.length
= strlen(test
);
274 element
= amf_obj
.encodeElement(AMF::NUMBER
, &num
, 0);
275 head
= amf_obj
.encodeHeader(&name
, true, sizeof(amfnum_t
), &num
);
277 char *ptr
= ((char *)head
) + 2;
278 if ((strncmp(ptr
, test
, name
.length
) == 0) && (ntohs(*(short *)head
) == name
.length
)) {
279 runtest
.pass("Header name correct");
281 runtest
.fail("Header name not correct");
284 ptr
= ((char *)head
) + 2 + name
.length
+ 1;
285 if (*ptr
== AMF::NUMBER
) {
286 runtest
.pass("Header Object type correct");
288 runtest
.fail("Header Object type not correct");
292 if (*ptr
== htonl(num
)) {
293 runtest
.pass("Header Object data correct");
295 runtest
.fail("Header Object data not correct");
305 // Write a number element
306 note("Test the Body");
315 // Write a number element
316 note("Test the Packet");
323 cerr
<< "This program tests the AMF library." << endl
;
324 cerr
<< "Usage: amftest [hv]" << endl
;
325 cerr
<< "-h\tHelp" << endl
;
326 cerr
<< "-v\tVerbose" << endl
;