File: serializer-testlib

package info (click to toggle)
libdata-serializer-perl 0.60-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 400 kB
  • ctags: 125
  • sloc: perl: 978; makefile: 5
file content (472 lines) | stat: -rw-r--r-- 26,145 bytes parent folder | download | duplicates (3)
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package My::TestObj;
#use utf8;

#Need to play nice on different Filesystems!

use File::Spec;
my $file_path = File::Spec->catfile('t', 'datastore.txt');

sub new {
  my $class = (shift);
  my $self = {};
  $self->{monkey} = [qw(a b c d e f g h)];
  bless $self, $class;
  return $self;
}

sub data_test {
  my $self = (shift);
  return $self->{monkey}->[4];
}

sub package_test {
  return ref( (shift) );
}

package main;

use IO::File;

$object_package = 'My::TestObj';

%counts = (
                simpleobject  => 2,
                simplescalar  => 1,
                trickyscalar  => 1,
                simplescalarref  => 1,
                simplearray   => 1,
                complexarray  => 2,
                selfrefarray   => 1,
                simplehash    => 1,
                utf8hash    => 2,
                complexhash   => 3,
                selfrefhash   => 1,
           );


%testrefs = (

   simpleobject    => My::TestObj->new(),
   trickyscalar    => '{foo=>"bar"}',
   simplescalar    => 'pointless',
   simplescalarref    => \'pointless',
   simplehash    => { alpha => 1,
	   	     beta  => 2 },
   simplearray   => [qw ( one two three ) ],


   complexarray  => ['one', {alpha => 1, beta => 2},['bob','dole']],

   selfrefarray  => ['one', {alpha => 1, beta => 2},['bob','dole']],

   complexhash   => {
			apple  => [qw ( one two three ) ],
			orange => { alpha => 1, beta  => 2 },
			pear   => { 
				chain => {
					hippie => 'smelly', 
					donkey => 'worship',
					 }, 
				monkey => 3,
			 	  },
		 },
   selfrefhash   => {
			apple  => [qw ( one two three ) ],
		 },
	);

{
	$testrefs{utf8hash} = { alpha => q|“water” and “road”|,
				beta => q|mañana| };
}


$testrefs{selfrefhash}->{pear}   = $testrefs{selfrefhash}->{apple};

$testrefs{selfrefarray}->[3]   = $testrefs{selfrefarray}->[1];

%serializers = (
                        'Data::Dumper'  => {
                                                simpleobject  => $counts{simpleobject},
                                                simplescalar  => $counts{simplescalar},
                                                simplescalarref  => $counts{simplescalarref},
                                                trickyscalar  => $counts{trickyscalar},
                                                simplearray  => $counts{simplearray},
                                                complexarray => $counts{complexarray},
                                                selfrefarray  => $counts{selfrefarray},
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => $counts{utf8hash},
                                                complexhash  => $counts{complexhash},
                                                selfrefhash  => $counts{selfrefhash},
                                           },
                        'Data::Taxi'  => {
                                                simpleobject  => $counts{simpleobject},
                                              #  simplescalar  => $counts{simplescalar},		#doesn't support this
                                                simplearray  => $counts{simplearray},
                                                complexarray => $counts{complexarray},
                                                selfrefarray  => $counts{selfrefarray},
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => $counts{utf8hash},
                                                complexhash  => $counts{complexhash},
                                                selfrefhash  => $counts{selfrefhash},
                                           },
                        'Data::Denter'  => {
                                                simpleobject  => $counts{simpleobject},
                                                simplescalar  => $counts{simplescalar},
                                                simplescalarref  => $counts{simplescalarref},
                                                trickyscalar  => $counts{trickyscalar},
                                                simplearray  => $counts{simplearray},
                                                complexarray => $counts{complexarray},
                                                selfrefarray  => $counts{selfrefarray},
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => $counts{utf8hash},
                                                complexhash  => $counts{complexhash},
                                                selfrefhash  => $counts{selfrefhash},
                                           },
                        'Storable'      => {
                                                simpleobject  => $counts{simpleobject},
                                                simplescalar  => 0,	#Storable cannot serialize scalars
                                                trickyscalar  => 0,	#Storable cannot serialize scalars
                                                simplescalarref  => $counts{simplescalarref},
                                                simplearray  => $counts{simplearray},
                                                complexarray => $counts{complexarray},
                                                selfrefarray  => $counts{selfrefarray},
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => $counts{utf8hash},
                                                complexhash  => $counts{complexhash},
                                                selfrefhash  => $counts{selfrefhash},
                                           },
                        'FreezeThaw'    => {
                                                simpleobject  => $counts{simpleobject},
                                                simplescalar  => $counts{simplescalar},
                                                simplescalarref  => $counts{simplescalarref},
                                                trickyscalar  => $counts{trickyscalar},
                                                simplearray  => $counts{simplearray},
                                                complexarray => $counts{complexarray},
                                                selfrefarray  => $counts{selfrefarray},
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => $counts{utf8hash},
                                                complexhash  => $counts{complexhash},
                                                selfrefhash  => $counts{selfrefhash},
                                           },
                        'Config::General' => {
                                                simpleobject  => 0,
                                                simplescalar  => 0,
                                                simplearray  => 0,
                                                complexarray => 0,
                                                selfrefarray  => 0,
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => $counts{utf8hash},
                                                complexhash  => $counts{complexhash},
                                                selfrefhash  => $counts{selfrefhash},
                                           },
                        'YAML'  => {
                                                simpleobject  => $counts{simpleobject},
                                                simplescalar  => $counts{simplescalar},
                                                simplescalarref  => $counts{simplescalarref},
                                                trickyscalar  => $counts{trickyscalar},
                                                simplearray  => $counts{simplearray},
                                                complexarray => $counts{complexarray},
                                                selfrefarray  => $counts{selfrefarray},
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => $counts{utf8hash},
                                                complexhash  => $counts{complexhash},
                                                selfrefhash  => $counts{selfrefhash},
                                           },
                        'YAML::Syck'  => {
                                                simpleobject  => $counts{simpleobject},
                                                simplescalar  => $counts{simplescalar},
                                                simplescalarref  => $counts{simplescalarref},
                                                trickyscalar  => $counts{trickyscalar},
                                                simplearray  => $counts{simplearray},
                                                complexarray => $counts{complexarray},
                                                selfrefarray  => $counts{selfrefarray},
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => $counts{utf8hash},
                                                complexhash  => $counts{complexhash},
                                                selfrefhash  => $counts{selfrefhash},
                                           },
                        'JSON'  => {
                                                simpleobject  => 0,	#JSON doesn't do objects
                                                simplescalar  => 0,	#JSON doesn't do scalars
                                                simplescalarref  => 0,	#JSON doesn't do scalars
                                                trickyscalar  => 0,	#JSON doesn't do scalars
                                                simplearray  => $counts{simplearray},
                                                complexarray => $counts{complexarray},
                                                selfrefarray  => $counts{selfrefarray},
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => $counts{utf8hash},
                                                complexhash  => $counts{complexhash},
                                                selfrefhash  => $counts{selfrefhash},
                                           },
                        'JSON::Syck'  => {
                                                simpleobject  => 0,	#JSON doesn't do objects
                                                simplescalar  => 0,	#JSON doesn't do scalars
                                                simplescalarref  => 0,	#JSON doesn't do scalars
                                                trickyscalar  => 0,	#JSON doesn't do scalars
                                                simplearray  => $counts{simplearray},
                                                complexarray => $counts{complexarray},
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => $counts{utf8hash},
                                                complexhash  => $counts{complexhash},
                                                #selfrefarray  => $counts{selfrefarray},
                                                #selfrefhash  => $counts{selfrefhash},
                                                selfrefarray  => 0,	#JSON no longer supports "circular references"
                                                selfrefhash  => 0,	#JSON no longer supports "circular references"
                                           },
                        'XML::Dumper'   => {
                                                simpleobject  => $counts{simpleobject},
                                                simplescalar  => $counts{simplescalar},
                                                simplescalarref  => $counts{simplescalarref},
                                                trickyscalar  => $counts{trickyscalar},
                                                simplearray  => $counts{simplearray},
                                                complexarray => $counts{complexarray},
                                                selfrefarray  => $counts{selfrefarray},
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => 0, #Chokes on UTF8
                                                complexhash  => $counts{complexhash},
                                                selfrefhash  => $counts{selfrefhash},
                                           },
                        'XML::Simple'   => {
                                                simpleobject  => 0,
                                                simplescalar  => 0,
                                                simplearray  => $counts{simplearray},
                                                complexarray => $counts{complexarray},
                                                selfrefarray  => $counts{selfrefarray},
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => 0, #Chokes on UTF8 sometimes
                                                complexhash  => $counts{complexhash},
                                                selfrefhash  => $counts{selfrefhash},
                                           },
                        'PHP::Serialization'    => {
                                                simpleobject  => 0,
                                                simplescalar  => 0,
                                                simplearray  => $counts{simplearray},
                                                complexarray => $counts{complexarray},
                                                selfrefarray  => $counts{selfrefarray},
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => $counts{utf8hash},
                                                complexhash  => $counts{complexhash},
                                                selfrefhash  => $counts{selfrefhash},
                                           },
                        'Bencode'  => {
                                                simpleobject  => 0,	#Bencode does not support serializing objects
                                                simplescalarref  => 0,	#Bencode does not support serializing saclar references
                                                simplescalar  => $counts{simplescalar},
                                                trickyscalar  => $counts{trickyscalar},
                                                simplearray  => $counts{simplearray},
                                                complexarray => $counts{complexarray},
                                                selfrefarray  => $counts{selfrefarray},
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => $counts{utf8hash},
                                                complexhash  => $counts{complexhash},
                                                selfrefhash  => $counts{selfrefhash},
                                           },
                        'Convert::Bencode'  => {
                                                simpleobject  => 0,	#Convert::Bencode does not support serializing objects
                                                simplescalarref  => 0,	#Convert::Bencode does not support serializing saclar references
                                                simplescalar  => $counts{simplescalar},
                                                trickyscalar  => $counts{trickyscalar},
                                                simplearray  => $counts{simplearray},
                                                complexarray => $counts{complexarray},
                                                selfrefarray  => $counts{selfrefarray},
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => $counts{utf8hash},
                                                complexhash  => $counts{complexhash},
                                                selfrefhash  => $counts{selfrefhash},
                                           },
                        'Convert::Bencode_XS'  => {
                                                simpleobject  => 0,	#Convert::Bencode_XS does not support serializing objects
                                                simplescalarref  => 0,	#Convert::Bencode_XS does not support serializing saclar references
                                                simplescalar  => $counts{simplescalar},
                                                trickyscalar  => $counts{trickyscalar},
                                                simplearray  => $counts{simplearray},
                                                complexarray => $counts{complexarray},
                                                selfrefarray  => $counts{selfrefarray},
                                                simplehash   => $counts{simplehash},
                                                utf8hash   => $counts{utf8hash},
                                                complexhash  => $counts{complexhash},
                                                selfrefhash  => $counts{selfrefhash},
                                           },
                  );

%features = (
                'objraw'     	 => [],
                'raw'     	 => [],
                'rawnew'     	 => [],
                'non-portable'   => [],
                'basic'     	 => [],
                'cgopt'     	 => [],
                'xmldumpopt'   	 => [],
                'encryption'     => [qw (Crypt::CBC Crypt::Blowfish)],
                'compresszlib'    => [qw (Compress::Zlib)],
                'compressppmd'    => [qw (Compress::PPMd)],
                'encoding'       => [qw (MIME::Base64)],
                'md5'       => [qw (Crypt::CBC Crypt::Blowfish Digest::MD5)],
                'sha1'     	 => [qw (Crypt::CBC Crypt::Blowfish Digest::SHA1)],
                'sha-256'     	 => [qw (Crypt::CBC Crypt::Blowfish Digest::SHA)],
                );


sub run_test {
        my ($T,$serializer,$test,$features) = @_;
        $T->msg("Test $serializer $test $features");  # message for the log
	my $obj;
        my @features = (split(" ", $features));
        if (grep {/^objraw$/} @features) {
        	$obj = Data::Serializer::Raw->new(serializer=>$serializer);
	} else {
        	$obj = Data::Serializer->new(serializer=>$serializer);
	}
        foreach my $feature (@features) {
                if ($feature eq 'basic') {
                        #do nothing special
                } elsif ($feature eq 'non-portable') {
                        #make sure we work without ascii armoring
                        $obj->portable(0);
                } elsif ($feature eq 'encryption') {
                        #setup a secret so we use encryption on this run
                        $obj->secret('test');
                } elsif ($feature eq 'md5') {
			#only relevant if we have a secret
                        $obj->secret('test');
                        $obj->digester('MD5');
                } elsif ($feature eq 'sha1') {
			#only relevant if we have a secret
                        $obj->secret('test');
                        $obj->digester('SHA-256');
                } elsif ($feature eq 'sha-256') {
			#only relevant if we have a secret
                        $obj->secret('test');
                        $obj->digester('SHA-256');
                } elsif ($feature eq 'compresszlib') {
                        # use compression 
                        $obj->compress(1);
                        $obj->compressor("Compress::Zlib");
                } elsif ($feature eq 'compressppmd') {
                        # use compression 
                        $obj->compress(1);
                        $obj->compressor("Compress::PPMd");
                } elsif ($feature eq 'encoding') {
                        # test alternate encoding
                        $obj->encoding('b64');
                } elsif ($feature eq 'cgopt') {
                        # test options for Config::General
                        $obj->options( {
                                         -LowerCaseNames       => 1,
                                         -UseApacheInclude     => 1,
                                         -MergeDuplicateBlocks => 1,
                                         -AutoTrue             => 1,
                                         -InterPolateVars      => 1
                                       });
                } elsif ($feature eq 'xmldumpopt') {
                        # test options for XML::Dumper
                        $obj->options( {
                                         dtd       => 1,
                                       });
                }
        }
        my ($frozen,$thawed);
        if (grep {/^objraw$/} @features) {
          $frozen = $obj->serialize($testrefs{$test});
          $thawed = $obj->deserialize($frozen);
        }elsif (grep {/^raw$/} @features) {
          $frozen = $obj->raw_serialize($testrefs{$test});
          $thawed = $obj->raw_deserialize($frozen);
        } elsif (grep {/^rawnew$/} @features) {
          my $newobj = Data::Serializer->new(
					serializer=>$serializer,
					raw => 1,
					#portable=>undef,
					#serializer_token=>undef,
						);
          $frozen = $obj->serialize($testrefs{$test});
          $thawed = $obj->deserialize($frozen);
	
	  
        } elsif (grep {/fh-storage/} @features) {
    	  my $fh = IO::File->new($file_path, O_CREAT|O_WRONLY, 0600);
          $obj->store($testrefs{$test},$fh);
          $fh->close(); 

    	  $fh = IO::File->new($file_path, O_RDONLY);
          $thawed = $obj->retrieve($fh);
          $fh->close(); 
          unlink($file_path);
        } elsif (grep {/storage/} @features) {
          $obj->store($testrefs{$test},$file_path);
          $thawed = $obj->retrieve($file_path);
          unlink($file_path);
        } elsif (grep {/rawstorage1/} @features) {
          my $newobj = Data::Serializer->new(
					serializer=>$serializer,
					raw => 1,
						);
          $newobj->store($testrefs{$test},$file_path);
          $thawed = $newobj->retrieve($file_path);
          unlink($file_path);
        } elsif (grep {/rawstorage2/} @features) {
          my $newobj = Data::Serializer::Raw->new(
					serializer=>$serializer,
						);
          $newobj->store($testrefs{$test},$file_path);
          $thawed = $newobj->retrieve($file_path);
          unlink($file_path);
        } else {
          $frozen = $obj->serialize($testrefs{$test});
          $thawed = $obj->deserialize($frozen);
        }
        if ($test eq 'simplescalar') {
                $T->ok_eq($testrefs{$test}, $thawed,'scalar');
        }elsif ($test eq 'trickyscalar') {
                $T->ok_eq($testrefs{$test}, $thawed,'tricky scalar');
        }elsif ($test eq 'simplescalarref') {
                $T->ok_eq(${$testrefs{$test}}, ${$thawed},'simple scalar ref');
        }elsif ($test eq 'simpleobject') {
                $T->ok_eq($testrefs{$test}->package_test(), $thawed->package_test,'object namespace');
                $T->ok_eq($testrefs{$test}->data_test(), $thawed->data_test,'object data');
        }elsif ($test eq 'simplearray') {
                $T->ok_eq($testrefs{$test}->[0], $thawed->[0],'array');
        } elsif ($test eq 'complexarray') {
                $T->ok_eq($testrefs{$test}->[2]->[0], $thawed->[2]->[0],'array of arrays');
                $T->ok_eqnum($testrefs{$test}->[1]->{alpha}, $thawed->[1]->{alpha},'array of hashes');
        } elsif ($test eq 'simplehash') {
                $T->ok_eqnum($testrefs{$test}->{alpha}, $thawed->{alpha},'hash');
        } elsif ($test eq 'utf8hash') {
                $T->ok_eq($testrefs{$test}->{alpha}, $thawed->{alpha},'utf8 hash');
                $T->ok_eq($testrefs{$test}->{beta}, $thawed->{beta},'utf8 hash 2');
        } elsif ($test eq 'complexhash') {
                $T->ok_eq($testrefs{$test}->{apple}->[1], $thawed->{apple}->[1],'hash of arrays');
                $T->ok_eqnum($testrefs{$test}->{orange}->{alpha}, $thawed->{orange}->{alpha},'hash of hashes');
                $T->ok_eq($testrefs{$test}->{pear}->{chain}->{hippie}, $thawed->{pear}->{chain}->{hippie},'hash of hash of hashes');
        } elsif ($test eq 'selfrefhash') {
                $T->ok_eq($testrefs{$test}->{apple}->[1], $thawed->{pear}->[1],'hash self ref to internal array');
        } elsif ($test eq 'selfrefarray') {
                $T->ok_eq($testrefs{$test}->[1]->{alpha}, $thawed->[3]->{alpha},'array self ref to internal array');
        }
}

sub test_feature {
	my ($feature) = @_;
	foreach my $module (@{$features{$feature}}) {
		return 0 unless (eval "require $module");
        }
        return 1;
}

sub test_serializer {
	my ($serializer) = @_;
        return 1 if (eval "require $serializer");
	return 0;
}


sub find_features {
	my $T = (shift);
	foreach my $type (@_) {
       		if (test_feature($type)) {
               		$found_type{$type} = 1;
               		$T->msg("Found feature $type");
        	}
       	}
}