Add missing newlines escape. Should fix build failures on lucid-linux-x86
[gnash.git] / cygnal / libamf / amftest.cpp
blobffe965c103ebd0837e315624beb6f81053065ece
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 // Foundation, Inc
4 //
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.
9 //
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.
14 //
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
19 #ifdef HAVE_CONFIG_H
20 #include "gnashconfig.h"
21 #endif
23 #ifdef HAVE_DEJAGNU_H
25 #include <netinet/in.h>
26 #include <string>
27 #include <sys/types.h>
28 #include "GnashSystemIOHeaders.h"
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <log.h>
33 #include <iostream>
34 #include <string>
36 #include "dejagnu.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
43 // when needed,
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
48 // when needed,
50 #include "amf.h"
52 using namespace cygnal;
53 using namespace gnash;
54 using namespace std;
56 static void usage (void);
58 static int verbosity;
60 TestState runtest;
62 void test_Number(void);
63 void test_Boolean(void);
64 void test_String(void);
66 void test_Header(void);
67 void test_Body(void);
68 void test_Packet(void);
70 int
71 main(int argc, char *argv[])
73 bool dump = false;
74 char buffer[300];
75 int c, retries = 3;
77 memset(buffer, 0, 300);
79 while ((c = getopt (argc, argv, "hdvsm:")) != -1) {
80 switch (c) {
81 case 'h':
82 usage ();
83 break;
85 case 'v':
86 verbosity++;
87 break;
89 default:
90 usage ();
91 break;
95 // get the file name from the command line
96 if (optind < argc) {
97 string filespec = argv[optind];
98 cout << "Will use \"" << filespec << "\" for test " << endl;
101 test_Number();
102 test_Boolean();
103 test_String();
105 // test_Header();
106 // test_Body();
107 // test_Packet();
110 void
111 test_Number(void)
113 AMF amf_obj;
114 #if 0
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");
122 } else {
123 runtest.fail("Number header not correct");
125 if (amf_obj.extractElementLength(out) == 8) {
126 runtest.pass("Number length returned correct");
127 } else {
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");
139 } else {
140 fail("Number swapped not correct");
142 #endif
143 int fd, ret;
144 char buf[AMF_NUMBER_SIZE+1];
145 amfnum_t value = 0xf03fL;
146 amfnum_t *num;
148 memset(buf, 0, AMF_NUMBER_SIZE+1);
149 fd = open("number.amf", O_RDONLY);
150 ret = read(fd, buf, 12);
151 close(fd);
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");
164 } else {
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");
177 } else {
178 runtest.fail("Encoded AMF Number");
181 delete num;
184 void
185 test_Boolean(void)
187 AMF amf_obj;
188 bool bo = false;
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");
194 } else {
195 runtest.fail("Boolean header not correct");
197 if (amf_obj.extractElementLength(out) == 1) {
198 runtest.pass("Boolean length returned correct");
199 } else {
200 runtest.fail("Boolean length returned not correct");
202 if (*((char *)out + 1) == 0) {
203 pass("Boolean false returned correct");
204 } else {
205 runtest.fail("Boolean false returned not correct");
207 bo = true;
208 out = amf_obj.encodeElement(AMF::BOOLEAN, &bo, 0);
209 if (*((char *)out + 1) == 1) {
210 runtest.pass("Boolean true returned correct");
211 } else {
212 runtest.fail("Boolean true returned not correct");
216 // Make sure we can read and write binary AMF strings
217 void
218 test_String(void)
220 AMF amf_obj;
221 int fd, ret;
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);
229 close(fd);
231 char *str = amf_obj.extractString(buf);
232 if (strcmp(str, "connect") == 0) {
233 runtest.pass("Extracted \"connect\" string");
234 } else {
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");
244 } else {
245 runtest.fail("Encoded \"connect\" string");
248 delete str;
251 #if 0
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)
258 void
259 test_Header(void){
260 AMF amf_obj;
261 amfutf8_t name, headname;
262 amfnum_t num;
263 void *element;
264 amfhead_t *head;
266 note("Test the Header");
268 num = 123456789;
270 char *test = "NumberTest";
271 name.length = strlen(test);
272 name.data = 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");
280 } else {
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");
287 } else {
288 runtest.fail("Header Object type not correct");
291 ptr++;
292 if (*ptr == htonl(num)) {
293 runtest.pass("Header Object data correct");
294 } else {
295 runtest.fail("Header Object data not correct");
299 void
300 test_Body(void)
302 AMF amf_obj;
303 void *out;
305 // Write a number element
306 note("Test the Body");
309 void
310 test_Packet(void)
312 AMF amf_obj;
313 void *out;
315 // Write a number element
316 note("Test the Packet");
318 #endif
320 static void
321 usage (void)
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;
327 exit (-1);
330 #endif