File: 01basic.t

package info (click to toggle)
libconvert-ber-perl 1.3200-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 240 kB
  • sloc: perl: 2,237; makefile: 41
file content (156 lines) | stat: -rw-r--r-- 2,519 bytes parent folder | download | duplicates (9)
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
#!/usr/local/bin/perl

#
# Test that the primitive operators are working
#

use Convert::BER;

print "1..19\n";

$tcount = $test = 1;

sub test (&) {
    my $sub = shift;
    eval { $sub->() };

    print "not ok ",$test++,"\n"
        while($test < $tcount);

    warn "count mismatch test=$test tcount=$tcount"
	unless $test == $tcount;

    $tcount = $test;
}

##
## Test array tags
##

$tcount += 4;
test {
    my $ber = Convert::BER->new->encode( [ NULL, 0x35 ] => 0 ) or die;

	print "ok ",$test++,"\n";

    my $result = pack("C*", 0x35, 0x00);

    die unless $ber->buffer eq $result;

	print "ok ",$test++,"\n";

    my $null;

    $ber->decode( [ NULL, 0x35 ] => \$null) or die;

	print "ok ",$test++,"\n";

    die unless $null;

	print "ok ",$test++,"\n";
};

##
## Test array ref value
##

$tcount += 5;
test {

    my $ber = Convert::BER->new->encode( STRING => [qw(two strings)] );

    die unless $ber;

    	print "ok ",$test++,"\n";

    my $result = pack("C*", 0x04, 0x03, 0x74, 0x77, 0x6F, 0x04, 0x07, 0x73,
			    0x74, 0x72, 0x69, 0x6E, 0x67, 0x73);

    die unless $ber->buffer eq $result;

    	print "ok ",$test++,"\n";

    my @str = ();

    $ber->decode( STRING => \@str) or die;

    	print "ok ",$test++,"\n";

    die unless @str == 2;

    	print "ok ",$test++,"\n";

    die unless join("~",@str) eq "two~strings";

    	print "ok ",$test++,"\n";
};

##
## Test sub returning value
##

$tcount += 4;
test {

    my $ber = Convert::BER->new->encode( INTEGER => sub { 0xABCDEF } );

    die unless $ber;

    	print "ok ",$test++,"\n";

    my $result = pack("C*", 0x02, 0x04, 0x00, 0xAB, 0xCD, 0xEF);

    die unless $ber->buffer eq $result;

    	print "ok ",$test++,"\n";

    my $int;

    $ber->decode( INTEGER => \$int) or die;

    	print "ok ",$test++,"\n";

    die unless $int == 0xABCDEF;

    	print "ok ",$test++,"\n";

};

##
## Test sub returning array ref value
##

$tcount += 6;
test {

    my $ber = Convert::BER->new->encode( ENUM => sub { [ 0xFEDCBA, -96 ] } );

    die unless $ber;

    	print "ok ",$test++,"\n";

    my $result = pack("C*", 0x0A, 0x04, 0x00, 0xFE, 0xDC,
			    0xBA, 0x0A, 0x01, 0xA0);

    die unless $ber->buffer eq $result;

    	print "ok ",$test++,"\n";

    my @int = ();

    $ber->decode( ENUM => \@int) or die;

    	print "ok ",$test++,"\n";

    die unless @int == 2;

    	print "ok ",$test++,"\n";

    die unless $int[0] == 0xFEDCBA;

    	print "ok ",$test++,"\n";

    die unless $int[1] == -96;

    	print "ok ",$test++,"\n";
};