File: Amanda_IPC_Binary.pl

package info (click to toggle)
amanda 1%3A3.3.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 22,360 kB
  • sloc: ansic: 214,899; perl: 58,075; sh: 16,954; xml: 13,853; makefile: 2,228; awk: 431; lex: 405; yacc: 343; tcl: 118; sql: 19; sed: 16; php: 2
file content (242 lines) | stat: -rw-r--r-- 6,138 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
# Copyright (c) 2009 Zmanda, Inc.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
# Sunnyvale, CA 94086, USA, or: http://www.zmanda.com

use Test::More tests => 15;
use strict;
use warnings;

use lib "@amperldir@";
use Installcheck;
use Amanda::IPC::Binary;
use IO::Handle;
use Amanda::Debug;
use Data::Dumper;
use Carp;
use POSIX;

##
# Define a test protocol

package TestProtocol;
use base "Amanda::IPC::Binary";
use Amanda::IPC::Binary;

# cmd_id's
use constant SIMPLE => 1;
use constant FOO => 2;
use constant BAR => 3;

# arg_id's
use constant NAME => 1;
use constant NICKNAME => 2;
use constant MANDATORY => 3;
use constant OPTIONAL => 4;

magic(0x1234);

command(SIMPLE);

command(FOO,
    NAME, $IPC_BINARY_STRING,
    NICKNAME, $IPC_BINARY_STRING);

command(BAR,
    MANDATORY, 0,
    OPTIONAL, $IPC_BINARY_OPTIONAL);

package main;

# set up debugging so debug output doesn't interfere with test results
Amanda::Debug::dbopen("installcheck");
Installcheck::log_test_output();

# and disable Debug's die() and warn() overrides
Amanda::Debug::disable_die_override();

sub to_bytes {
    my @result;
    for my $byte (@_) {
	if (length($byte) == 1) {
	    push @result, $byte;
	} else {
	    push @result, chr(hex($byte));
	}
    }
    return join('', @result);
}

sub to_hex {
    my ($bytes) = @_;
    my @result;
    for my $byte (split //, $bytes) {
	if ((ord($byte) >= ord('a') and ord($byte) <= ord('z')) || $byte eq '-') {
	    push @result, $byte;
	} else {
	    push @result, sprintf("%02x", ord($byte));
	}
    }

    return join(" ", @result);
}

# first, try reading a pre-defined sequence of bytes; this lets us make sure
# byte-ordering is right on all platforms.
my $fh;
my $tmpfile = "$Installcheck::TMP/ipc-binary-test";
my ($chan, $msg);

open($fh, ">", $tmpfile);
print $fh to_bytes(
    qw(12 34), # magic
    qw(00 01), # cmd_id = SIMPLE
    qw(00 00 00 0A), # length
    qw(00 00), # count

    qw(12 34), # magic
    qw(00 02), # cmd_id = FOO
    qw(00 00 00 22), # length
    qw(00 02), # n_args
    qw(00 00 00 07), # length
    qw(00 01), # arg_id = NAME
    qw(n i k o l a s), # data
    qw(00 00 00 05), # length
    qw(00 02), # arg_id = NICKNAME
    qw(a t r u s), # data

    qw(12 34), # magic
    qw(00 03), # cmd_id = BAR
    qw(00 00 00 1f), # length
    qw(00 01), # n_args
    qw(00 00 00 0f), # length
    qw(00 03), # arg_id = MANDATORY
    qw(v e r b o d e n - v r u c h t), # data

    qw(12 34), # magic
    qw(00 03), # cmd_id = BAR
    qw(00 00 00 29), # length
    qw(00 02), # n_args
    qw(00 00 00 0a), # length
    qw(00 03), # arg_id = MANDATORY
    qw(o u d e - g e u z e), # data
    qw(00 00 00 09), # length
    qw(00 04), # arg_id = OPTIONAL
    qw(r o d e n b a c h), # data
);
close($fh);

open($fh, "<", $tmpfile);

$chan = TestProtocol->new();

$msg = $chan->read_message($fh);
is($msg->{'cmd_id'}, TestProtocol::SIMPLE,
    "got SIMPLE");

$msg = $chan->read_message($fh);
is($msg->{'cmd_id'}, TestProtocol::FOO,
    "got FOO");
is($msg->{'args'}[TestProtocol::NAME], "nikolas",
    "got NAME arg");
is($msg->{'args'}[TestProtocol::NICKNAME], "atrus",
    "got NICKNAME arg");

$msg = $chan->read_message($fh);
is($msg->{'cmd_id'}, TestProtocol::BAR,
    "got BAR");
is($msg->{'args'}[TestProtocol::MANDATORY], "verboden-vrucht",
    "got MANDATORY arg");
is($msg->{'args'}[TestProtocol::OPTIONAL], undef,
    "got no OPTIONAL arg");

$msg = $chan->read_message($fh);
is($msg->{'cmd_id'}, TestProtocol::BAR,
    "got BAR");
is($msg->{'args'}[TestProtocol::MANDATORY], "oude-geuze",
    "got MANDATORY arg");
is($msg->{'args'}[TestProtocol::OPTIONAL], "rodenbach",
    "got OPTIONAL arg");

$msg = $chan->read_message($fh);
is($msg, undef, "no more messages");

close($fh);

# now try writing a set of messages, and check that the result is what it should be
open($fh, ">", $tmpfile);
$chan = TestProtocol->new();

ok($chan->write_message($fh, $chan->message(
	TestProtocol::FOO,
	TestProtocol::NAME, "james",
	TestProtocol::NICKNAME, "jimmy")),
    "wrote FOO message");

ok($chan->write_message($fh, $chan->message(
	TestProtocol::BAR,
	TestProtocol::MANDATORY, "absolutely",
	TestProtocol::OPTIONAL, "maybe")),
    "wrote BAR message with optional arg");

ok($chan->write_message($fh, $chan->message(
	TestProtocol::BAR,
	TestProtocol::MANDATORY, "yessir")),
    "wrote BAR message without optional arg");

$chan->close();
close($fh);

my $bytes_expected = to_bytes(
    qw(12 34), # magic
    qw(00 02), # cmd_id = FOO
    qw(00 00 00 20), # length
    qw(00 02), # n_args
    qw(00 00 00 05), # length
    qw(00 01), # arg_id = NAME
    qw(j a m e s), # data
    qw(00 00 00 05), # length
    qw(00 02), # arg_id = NICKNAME
    qw(j i m m y), # data

    qw(12 34), # magic
    qw(00 03), # cmd_id = BAR
    qw(00 00 00 25), # length
    qw(00 02), # n_args
    qw(00 00 00 0a), # length
    qw(00 03), # arg_id = MANDATORY
    qw(a b s o l u t e l y), # data
    qw(00 00 00 05), # length
    qw(00 04), # arg_id = OPTIONAL
    qw(m a y b e), # data

    qw(12 34), # magic
    qw(00 03), # cmd_id = BAR
    qw(00 00 00 16), # length
    qw(00 01), # n_args
    qw(00 00 00 06), # length
    qw(00 03), # arg_id = MANDATORY
    qw(y e s s i r), # data
);

# slurp the contents of the temp file and see if it matches
open($fh, "<", $tmpfile);
my $bytes_written = do { local $/; <$fh> };
close($fh);

is(to_hex($bytes_written),
   to_hex($bytes_expected),
    "got the expected bytes");