File: 202_misc.t

package info (click to toggle)
libconvert-binary-c-perl 0.74-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 9,100 kB
  • ctags: 21,416
  • sloc: ansic: 63,666; perl: 18,582; yacc: 2,143; makefile: 44
file content (431 lines) | stat: -rw-r--r-- 8,940 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
################################################################################
#
# $Project: /Convert-Binary-C $
# $Author: mhx $
# $Date: 2009/03/15 04:10:59 +0100 $
# $Revision: 32 $
# $Source: /tests/202_misc.t $
#
################################################################################
#
# Copyright (c) 2002-2009 Marcus Holland-Moritz. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
################################################################################

use Test;
use Convert::Binary::C @ARGV;

$^W = 1;

BEGIN { plan tests => 207 }

#===================================================================
# perform some average stuff
#===================================================================

eval {
  $p = new Convert::Binary::C PointerSize => 4,
                              EnumSize    => 4,
                              IntSize     => 4,
                              LongSize    => 4,
                              Alignment   => 2,
                              ByteOrder   => 'BigEndian',
                              EnumType    => 'String';
  $q = new Convert::Binary::C;
};
ok($@,'');

#-----------------------------------
# create some average ( ?? :-) code
#-----------------------------------

$code = <<'CCODE';
#define ONLY_ONE 1

typedef struct abc abc_type;

typedef struct never ever;

struct abc {
  abc_type *p1;
#if ONLY_ONE > 1
  abc_type *p2;
#endif
};

typedef unsigned long u32;

#define Day( which )  \
        which ## DAY

typedef enum {
  Day( MON ),
  Day( TUES ),
  Day( WEDNES ),
} day;

# \
  define  __SIX__ \
  ( sizeof( unsigned char * ) + sizeof( short ) )

   # define SIXTEEN \
     (sizeof "Hello\"\xfworld\069!")

typedef union zap {
  signed long  za[__SIX__];
  short        zb[SIXTEEN];
  char         zc[sizeof(struct never (*[2][3])[4])];
  ever        *zd[sizeof( abc_type )];
} ZAP;

CCODE

#-----------------------
# try to parse the code
#-----------------------

eval {
  $p->parse( $code );
  $q->parse( $code );
};
ok($@,'');

#------------------------
# reconfigure the parser
#------------------------

eval {
  $p->configure( Alignment => 8, EnumSize => 0 );
};
ok($@,'');

#--------------------------------
# and parse some additional code
#--------------------------------

$code = <<'CCODE';
typedef struct {
  abc_type xxx;
  u32 dusel, *fusel;
  int musel[((1<<1)+4)&0x00000002];
  union {
    char bytes[(12/2)%4][(0x10|010)>>3];
    day  today;
    long value;
  } test;
  struct ints fubar;
  union zap hello;
} husel;

#pragma pack( push, 1 )

struct packer {
  char  i;
  short am;
  char  really;
  long  packed;
};

#pragma pack( pop )

struct nopack {
  char  i;
  short am;
  char  not;
  long  packed;
};

CCODE

$c99_code = <<'CCODE' . $code;
#define \
MYINTS( ... \
) { int __VA_ARGS__; }

struct ints MYINTS( a, b, c );

CCODE

#-----------------------
# try to parse the code
#-----------------------

eval {
  $q->HasMacroVAARGS( 0 );
  $q->parse( $c99_code );
};
ok($@,qr/invalid macro argument/);

eval { $p->parse( $c99_code ) };
ok($@,'');

#------------------------
# reconfigure the parser
#------------------------

eval { $p->Alignment( 4 ) };
ok($@,'');

#-------------------
# test some offsets
#-------------------

ok($p->offsetof('packer', 'i'), 0);
ok($p->offsetof('packer', 'am'), 1);
ok($p->offsetof('packer', 'really'), 3);
ok($p->offsetof('packer', 'packed'), 4);

ok($p->offsetof('nopack', 'i'), 0);
ok($p->offsetof('nopack', 'am'), 2);
ok($p->offsetof('nopack', 'not'), 4);
ok($p->offsetof('nopack', 'packed'), 8);

#------------------------
# now try some unpacking
#------------------------

# on a pack()ed struct

$data = pack( 'cnCN', -47, 0x1234, 0x55, 2000000000 );

eval { $result = $p->unpack( 'packer', $data ) };
ok($@,'');

$refres = {
  i      => -47,
  am     => 0x1234,
  really => 0x55,
  packed => 2000000000,
};

reccmp( $refres, $result );

# on a 'normal' struct

$data = pack( 'cxnCx3N', -47, 0x1234, 0x55, 2000000000 );

eval { $result = $p->unpack( 'nopack', $data ) };
ok($@,'');

$refres = {
  i      => -47,
  am     => 0x1234,
  not    => 0x55,
  packed => 2000000000,
};

reccmp( $refres, $result );

#-----------------------
# test something bigger
#-----------------------

$data = pack( "N5c8N3C48", 123, 4711, 0xDEADBEEF,
              -42, 42, 1, 0, 0, 0, -2, 3, 0, 0,
              -10000, 5000, 8000, 1..48 );

eval { $result = $p->unpack( 'husel', $data ) };
ok($@,'');

eval { undef $p };
ok($@,'');


$refres = {
  xxx   => { p1 => 123 },
  dusel => 4711,
  fusel => 0xDEADBEEF,
  musel => [ -42, 42 ],
  test  => {
             bytes => [ [ 1, 0, 0 ], [ 0, -2, 3 ] ],
             today => 'TUESDAY',
             value => 16777216,
           },
  fubar => {
             a => -10000,
             b =>   5000,
             c =>   8000,
           },
  hello => {
             za => [16909060, 84281096, 151653132, 219025168, 286397204, 353769240],
             zb => [258, 772, 1286, 1800, 2314, 2828, 3342, 3856,
                    4370, 4884, 5398, 5912, 6426, 6940, 7454, 7968],
             zc => [1..24],
             zd => [16909060, 84281096, 151653132, 219025168],
           },
};

reccmp( $refres, $result );

#------------------------------------------------
# test pack/unpack/sizeof/typeof for basic types
#------------------------------------------------

$p = new Convert::Binary::C;

@tests = (
  ['char',        $p->CharSize      ],
  ['short',       $p->ShortSize     ],
  ['int',         $p->IntSize       ],
  ['long',        $p->LongSize      ],
  ['long long',   $p->LongLongSize  ],
  ['float',       $p->FloatSize     ],
  ['double',      $p->DoubleSize    ],
  ['long double', $p->LongDoubleSize],
);

for( @tests ) {
  my $size = eval { $p->sizeof( $_->[0] ) };
  ok( $@, '' );
  ok( $size, $_->[1] );
}

check_basic( $p );

# must work without parse data, too
$p->clean;
check_basic( $p );

#--------------------------------
# test offsetof in strange cases
#--------------------------------

eval {
  $p->configure( IntSize     => 4
               , LongSize    => 4
               , PointerSize => 4
               , EnumSize    => 4
               , Alignment   => 4
               )->parse(<<ENDC);
struct foo {
  int a;
  struct bar {
    int x, y;
  } ary[5];
  struct bar {
    int x, y;
  } aryary[5][5];
};
typedef int a[10];
typedef struct {
  char abc;
  long day;
  int *ptr;
} week;
struct test {
  week zap[8];
};
ENDC
};

@tests = (
  ['foo',           '.ary',           4],
  ['foo.ary[2]',    '.x',             0],
  ['foo.ary[2]',    '.y',             4],
  ['foo.ary[2]',    '',               0],
  ['foo.ary',       '[2].y',         20],
  ['foo.aryary[2]', '[2].y',         20],
  ['a',             '[9]',           36],
  ['test',          '.zap[5].day',   64],
  ['test.zap[2]',   '.day',           4],
  ['test',          '.zap[5].day+1', 65],
);

$SIG{__WARN__} = sub { push @warn, $_[0] };
ok( $@, '' );
for( @tests ) {
  my $off = eval { $p->offsetof( $_->[0], $_->[1] ) };
  ok( $@, '' );
  ok( $off, $_->[2] );
}
ok( scalar @warn, 1 );
ok( $warn[0], qr/^Empty string passed as member expression/ );

#------------------------------
# some simple tests for member
#------------------------------

@tests = (
  ['foo',           '.ary[0].x',      4],
  ['foo.ary[2]',    '.x',             0],
  ['foo.ary[2]',    '.y',             4],
  ['foo.ary',       '[2].y',         20],
  ['foo.aryary[2]', '[2].y',         20],
  ['a',             '[9]',           36],
  ['test',          '.zap[5].day',   64],
  ['test.zap[2]',   '.day',           4],
  ['test',          '.zap[5].day+1', 65],
);

@warn = ();
ok( $@, '' );
for( @tests ) {
  my @m = eval { $p->member( $_->[0], $_->[2] ) };
  ok( $@, '' );
  ok( scalar @m, 1 );
  ok( $m[0], $_->[1] );
}
ok( scalar @warn, 0 );

#------------------------------
# test 64-bit negative numbers
#------------------------------

$p->clean->parse(<<ENDC);

typedef signed long long i_64;

ENDC

$p->LongLongSize(8);

for my $bo (qw( BigEndian LittleEndian )) {
  $p->ByteOrder($bo);
  my $x = $p->pack('i_64', -1);
  ok($x, pack('C*', (255)x8));
}


sub check_basic
{
  my $c = shift;

  for my $t ( 'signed char'
            , 'unsigned short int'
            , 'long int'
            , 'signed int'
            , 'long long'
            )
  {
    ok( eval { $c->typeof( $t ) }, $t );
    ok( eval { $c->sizeof( $t ) } > 0 );
    ok( eval { $c->unpack( $t, $c->pack($t, 42) ) }, 42 );
  }
}

sub reccmp
{
  my($ref, $val) = @_;

  my $id = ref $ref;

  unless( $id ) {
    ok( $ref, $val );
    return;
  }

  if( $id eq 'ARRAY' ) {
    ok( @$ref == @$val );
    for( 0..$#$ref ) {
      reccmp( $ref->[$_], $val->[$_] );
    }
  }
  elsif( $id eq 'HASH' ) {
    ok( @{[keys %$ref]} == @{[keys %$val]} );
    for( keys %$ref ) {
      reccmp( $ref->{$_}, $val->{$_} );
    }
  }
}