summaryrefslogtreecommitdiff
path: root/tools/iap/device-ipod.t
diff options
context:
space:
mode:
Diffstat (limited to 'tools/iap/device-ipod.t')
-rw-r--r--tools/iap/device-ipod.t74
1 files changed, 74 insertions, 0 deletions
diff --git a/tools/iap/device-ipod.t b/tools/iap/device-ipod.t
new file mode 100644
index 0000000000..607184e331
--- /dev/null
+++ b/tools/iap/device-ipod.t
@@ -0,0 +1,74 @@
1use Test::More qw( no_plan );
2use strict;
3
4BEGIN { use_ok('Device::iPod'); }
5require_ok('Device::iPod');
6
7my $ipod = Device::iPod->new();
8my $m;
9my ($l, $c, $p);
10
11isa_ok($ipod, 'Device::iPod');
12
13# Frame a short command
14$m = $ipod->_frame_cmd("\x00\x02\x00\x06");
15ok(defined($m) && ($m eq "\xFF\x55\x04\x00\x02\x00\x06\xF4"), "Framed command valid");
16
17# Frame a long command
18$m = $ipod->_frame_cmd("\x00" x 1024);
19ok(defined($m) && ($m eq "\xFF\x55\x00\x04\x00" . ("\x00" x 1024) . "\xFC"), "Long framed command valid");
20
21# Frame an overly long command
22$m = $ipod->_frame_cmd("\x00" x 65537);
23ok(!defined($m) && ($ipod->error() =~ 'Command too long'), "Overly long command failed");
24
25# Unframe a short command
26($l, $c, $p) = $ipod->_unframe_cmd("\xFF\x55\x04\x00\x02\x00\x06\xF4");
27ok(defined($l) && ($l == 0x00) && ($c == 0x02) && ($p eq "\x00\x06"), "Unframed short command");
28
29# Unframe a long command
30($l, $c, $p) = $ipod->_unframe_cmd("\xFF\x55\x00\x04\x00" . ("\x00" x 1024) . "\xFC");
31ok(defined($l) && ($l == 0x00) && ($c == 0x00) && ($p eq "\x00" x 1022), "Unframed long command");
32
33# Frame without sync byte
34($l, $c, $p) = $ipod->_unframe_cmd("\x00");
35ok(!defined($l) && ($ipod->error() =~ /Could not unframe data/), "Frame without sync byte failed");
36
37# Frame without SOP byte
38($l, $c, $p) = $ipod->_unframe_cmd("\xFF");
39ok(!defined($l) && ($ipod->error() =~ /Could not unframe data/), "Frame without SOP byte failed");
40
41# Frame with length 0
42($l, $c, $p) = $ipod->_unframe_cmd("\xFF\x55\x00\x00\x00");
43ok(!defined($l) && ($ipod->error() =~ /Length is 0/), "Frame with length 0 failed");
44
45# Too short frame
46($l, $c, $p) = $ipod->_unframe_cmd("\xFF\x55\x03\x00\x00");
47ok(!defined($l) && ($ipod->error() =~ /Could not unframe data/), "Too short frame failed");
48
49# Invalid checksum
50($l, $c, $p) = $ipod->_unframe_cmd("\xFF\x55\x03\x00\x00\x00\x00");
51ok(!defined($l) && ($ipod->error() =~ /Invalid checksum/), "Invalid checksum failed");
52
53# Find a message in a string
54$ipod->{-inbuf} = "\x00\xFF\x55\x00\xFF\xFF\xFF\x55\x04\x00\x02\x00\x06\xF4";
55($c, $l) = $ipod->_message_in_buffer();
56ok(defined($l) && ($c == 6) && ($l == 8), "Found message in buffer");
57
58# Return message from a string
59$ipod->{-inbuf} = "\x00\xFF\x55\x00\xFF\xFF\xFF\x55\x04\x00\x02\x00\x06\xF4\x00";
60$m = $ipod->_message();
61ok(defined($m) && ($ipod->{-inbuf} eq "\x00"), "Retrieved message from buffer");
62
63# Return two messages from buffer
64$ipod->{-inbuf} = "\xffU\x04\x00\x02\x00\x13\xe7\xffU\x02\x00\x14\xea";
65$m = $ipod->_message();
66subtest "First message" => sub {
67 ok(defined($m), "Message received");
68 is($m, "\xffU\x04\x00\x02\x00\x13\xe7");
69};
70$m = $ipod->_message();
71subtest "Second message" => sub {
72 ok(defined($m), "Message received");
73 is($m, "\xffU\x02\x00\x14\xea");
74};