OLD | NEW |
1 // Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 import 'package:expect/expect.dart'; | 5 import 'package:expect/expect.dart'; |
6 import 'package:mdns/src/packet.dart'; | 6 import 'package:mdns/src/packet.dart'; |
7 | 7 |
8 main() { | 8 main() { |
9 testValidPackages(); | 9 testValidPackages(); |
10 testBadPackages(); | 10 testBadPackages(); |
11 testHexDumpList(); | 11 testHexDumpList(); |
12 } | 12 } |
13 | 13 |
14 testValidPackages() { | 14 testValidPackages() { |
15 var result; | 15 var result; |
16 result = decodeMDnsResponse(package1); | 16 result = decodeMDnsResponse(package1); |
17 Expect.equals(1, result.length); | 17 Expect.equals(1, result.length); |
18 Expect.equals('raspberrypi.local', result[0].name); | 18 Expect.equals('raspberrypi.local', result[0].name); |
19 Expect.equals('192.168.1.191', result[0].address.address); | 19 Expect.equals('192.168.1.191', result[0].address.address); |
20 | 20 |
21 result = decodeMDnsResponse(package2); | 21 result = decodeMDnsResponse(package2); |
22 Expect.equals(2, result.length); | 22 Expect.equals(2, result.length); |
23 Expect.equals('raspberrypi.local', result[0].name); | 23 Expect.equals('raspberrypi.local', result[0].name); |
24 Expect.equals('192.168.1.191', result[0].address.address); | 24 Expect.equals('192.168.1.191', result[0].address.address); |
25 Expect.equals('raspberrypi.local', result[1].name); | 25 Expect.equals('raspberrypi.local', result[1].name); |
26 Expect.equals('169.254.95.83', result[1].address.address); | 26 Expect.equals('169.254.95.83', result[1].address.address); |
27 | 27 |
28 // This packet has no IPv4 address. | |
29 result = decodeMDnsResponse(package3); | 28 result = decodeMDnsResponse(package3); |
30 Expect.equals(0, result.length); | 29 Expect.equals(8, result.length); |
| 30 Expect.equals(""" |
| 31 RR 16 [0] |
| 32 RR 12 raspberrypi._udisks-ssh._tcp.local |
| 33 RR 33 raspberrypi.local |
| 34 RR 16 [0] |
| 35 RR 12 _udisks-ssh._tcp.local |
| 36 RR 12 raspberrypi [b8:27:eb:03:92:4b]._workstation._tcp.local |
| 37 RR 33 raspberrypi.local |
| 38 RR 12 _workstation._tcp.local""", |
| 39 result.join('\n')); |
| 40 |
| 41 result = decodeMDnsResponse(packagePtrResponse); |
| 42 Expect.equals(6, result.length); |
| 43 Expect.equals(""" |
| 44 RR 12 fletch-agent on raspberrypi._fletch_agent._tcp.local |
| 45 RR 16 [0] |
| 46 RR 33 raspberrypi.local |
| 47 RR 28 [254, 128, 0, 0, 0, 0, 0, 0, 186, 39, 235, 255, 254, 105, 110, 58] |
| 48 RR 1 InternetAddress('192.168.1.1', IP_V4) |
| 49 RR 1 InternetAddress('169.254.167.172', IP_V4)""", |
| 50 result.join('\n')); |
31 } | 51 } |
32 | 52 |
33 testBadPackages() { | 53 testBadPackages() { |
34 for (var p in [package1, package2, package3]) { | 54 for (var p in [package1, package2, package3]) { |
35 for (int i = 0; i < p.length; i++) { | 55 for (int i = 0; i < p.length; i++) { |
36 decodeMDnsResponse(p.sublist(0, i)); | 56 decodeMDnsResponse(p.sublist(0, i)); |
37 } | 57 } |
38 } | 58 } |
39 } | 59 } |
40 | 60 |
41 testHexDumpList() { | 61 testHexDumpList() { |
42 // Call hexDumpList to get rid of hint. | 62 // Call hexDumpList to get rid of hint. |
| 63 formatHexStream(""); |
43 hexDumpList([]); | 64 hexDumpList([]); |
44 } | 65 } |
45 | 66 |
46 // One address. | 67 // One address. |
47 var package1 = [ | 68 var package1 = [ |
48 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x01, | 69 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x01, |
49 0x00, 0x00, 0x00, 0x00, 0x0b, 0x72, 0x61, 0x73, | 70 0x00, 0x00, 0x00, 0x00, 0x0b, 0x72, 0x61, 0x73, |
50 0x70, 0x62, 0x65, 0x72, 0x72, 0x79, 0x70, 0x69, | 71 0x70, 0x62, 0x65, 0x72, 0x72, 0x79, 0x70, 0x69, |
51 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, | 72 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, |
52 0x01, 0x80, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, | 73 0x01, 0x80, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, |
53 0x04, 0xc0, 0xa8, 0x01, 0xbf]; | 74 0x04, 0xc0, 0xa8, 0x01, 0xbf]; |
54 | 75 |
55 // Two addresses. | 76 // Two addresses. |
56 var package2 = [ | 77 var package2 = [ |
57 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x02, | 78 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x02, |
58 0x00, 0x00, 0x00, 0x00, 0x0b, 0x72, 0x61, 0x73, | 79 0x00, 0x00, 0x00, 0x00, 0x0b, 0x72, 0x61, 0x73, |
59 0x70, 0x62, 0x65, 0x72, 0x72, 0x79, 0x70, 0x69, | 80 0x70, 0x62, 0x65, 0x72, 0x72, 0x79, 0x70, 0x69, |
60 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, | 81 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, |
61 0x01, 0x80, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, | 82 0x01, 0x80, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, |
62 0x04, 0xc0, 0xa8, 0x01, 0xbf, 0xc0, 0x0c, 0x00, | 83 0x04, 0xc0, 0xa8, 0x01, 0xbf, 0xc0, 0x0c, 0x00, |
63 0x01, 0x80, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, | 84 0x01, 0x80, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, |
64 0x04, 0xa9, 0xfe, 0x5f, 0x53]; | 85 0x04, 0xa9, 0xfe, 0x5f, 0x53]; |
65 | 86 |
66 // Seven mixed answers. | 87 // Eight mixed answers. |
67 var package3 = [ | 88 var package3 = [ |
68 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x08, | 89 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x08, |
69 0x00, 0x00, 0x00, 0x00, 0x1f, 0x72, 0x61, 0x73, | 90 0x00, 0x00, 0x00, 0x00, 0x1f, 0x72, 0x61, 0x73, |
70 0x70, 0x62, 0x65, 0x72, 0x72, 0x79, 0x70, 0x69, | 91 0x70, 0x62, 0x65, 0x72, 0x72, 0x79, 0x70, 0x69, |
71 0x20, 0x5b, 0x62, 0x38, 0x3a, 0x32, 0x37, 0x3a, | 92 0x20, 0x5b, 0x62, 0x38, 0x3a, 0x32, 0x37, 0x3a, |
72 0x65, 0x62, 0x3a, 0x30, 0x33, 0x3a, 0x39, 0x32, | 93 0x65, 0x62, 0x3a, 0x30, 0x33, 0x3a, 0x39, 0x32, |
73 0x3a, 0x34, 0x62, 0x5d, 0x0c, 0x5f, 0x77, 0x6f, | 94 0x3a, 0x34, 0x62, 0x5d, 0x0c, 0x5f, 0x77, 0x6f, |
74 0x72, 0x6b, 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, | 95 0x72, 0x6b, 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, |
75 0x6e, 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, | 96 0x6e, 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, |
76 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x10, 0x80, | 97 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x10, 0x80, |
(...skipping 13 matching lines...) Expand all Loading... |
90 0x6e, 0x73, 0x2d, 0x73, 0x64, 0x04, 0x5f, 0x75, | 111 0x6e, 0x73, 0x2d, 0x73, 0x64, 0x04, 0x5f, 0x75, |
91 0x64, 0x70, 0xc0, 0x3e, 0x00, 0x0c, 0x00, 0x01, | 112 0x64, 0x70, 0xc0, 0x3e, 0x00, 0x0c, 0x00, 0x01, |
92 0x00, 0x00, 0x11, 0x94, 0x00, 0x02, 0xc0, 0x50, | 113 0x00, 0x00, 0x11, 0x94, 0x00, 0x02, 0xc0, 0x50, |
93 0xc0, 0x2c, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, | 114 0xc0, 0x2c, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, |
94 0x11, 0x94, 0x00, 0x02, 0xc0, 0x0c, 0xc0, 0x0c, | 115 0x11, 0x94, 0x00, 0x02, 0xc0, 0x0c, 0xc0, 0x0c, |
95 0x00, 0x21, 0x80, 0x01, 0x00, 0x00, 0x00, 0x78, | 116 0x00, 0x21, 0x80, 0x01, 0x00, 0x00, 0x00, 0x78, |
96 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, | 117 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, |
97 0xc0, 0x88, 0xc0, 0xa3, 0x00, 0x0c, 0x00, 0x01, | 118 0xc0, 0x88, 0xc0, 0xa3, 0x00, 0x0c, 0x00, 0x01, |
98 0x00, 0x00, 0x11, 0x94, 0x00, 0x02, 0xc0, 0x2c]; | 119 0x00, 0x00, 0x11, 0x94, 0x00, 0x02, 0xc0, 0x2c]; |
99 | 120 |
| 121 var packagePtrResponse = <int>[ |
| 122 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x06, |
| 123 0x00, 0x00, 0x00, 0x00, 0x0d, 0x5f, 0x66, 0x6c, |
| 124 0x65, 0x74, 0x63, 0x68, 0x5f, 0x61, 0x67, 0x65, |
| 125 0x6e, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, |
| 126 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, |
| 127 0x00, 0x01, 0x00, 0x00, 0x11, 0x94, 0x00, 0x1e, |
| 128 0x1b, 0x66, 0x6c, 0x65, 0x74, 0x63, 0x68, 0x2d, |
| 129 0x61, 0x67, 0x65, 0x6e, 0x74, 0x20, 0x6f, 0x6e, |
| 130 0x20, 0x72, 0x61, 0x73, 0x70, 0x62, 0x65, 0x72, |
| 131 0x72, 0x79, 0x70, 0x69, 0xc0, 0x0c, 0xc0, 0x30, |
| 132 0x00, 0x10, 0x80, 0x01, 0x00, 0x00, 0x11, 0x94, |
| 133 0x00, 0x01, 0x00, 0xc0, 0x30, 0x00, 0x21, 0x80, |
| 134 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, 0x14, 0x00, |
| 135 0x00, 0x00, 0x00, 0x2f, 0x59, 0x0b, 0x72, 0x61, |
| 136 0x73, 0x70, 0x62, 0x65, 0x72, 0x72, 0x79, 0x70, |
| 137 0x69, 0xc0, 0x1f, 0xc0, 0x6d, 0x00, 0x1c, 0x80, |
| 138 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, 0x10, 0xfe, |
| 139 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xba, |
| 140 0x27, 0xeb, 0xff, 0xfe, 0x69, 0x6e, 0x3a, 0xc0, |
| 141 0x6d, 0x00, 0x01, 0x80, 0x01, 0x00, 0x00, 0x00, |
| 142 0x78, 0x00, 0x04, 0xc0, 0xa8, 0x01, 0x01, 0xc0, |
| 143 0x6d, 0x00, 0x01, 0x80, 0x01, 0x00, 0x00, 0x00, |
| 144 0x78, 0x00, 0x04, 0xa9, 0xfe, 0xa7, 0xac |
| 145 ]; |
| 146 |
| 147 // Support code to generate the hex-lists above from |
| 148 // a hex-stream. |
| 149 void formatHexStream(String hexStream) { |
| 150 var s = ''; |
| 151 for (int i = 0; i < hexStream.length / 2; i++) { |
| 152 if (s.length != 0) s += ', '; |
| 153 s += '0x'; |
| 154 var x = hexStream.substring(i * 2, i * 2 + 2); |
| 155 s += x; |
| 156 if (((i + 1) % 8) == 0) { |
| 157 s += ','; |
| 158 print(s); |
| 159 s = ''; |
| 160 } |
| 161 } |
| 162 if (s.length != 0) print(s); |
| 163 } |
| 164 |
100 // Support code for generating the hex-lists above. | 165 // Support code for generating the hex-lists above. |
101 void hexDumpList(List package) { | 166 void hexDumpList(List package) { |
102 var s = ''; | 167 var s = ''; |
103 for (int i = 0; i < package.length; i++) { | 168 for (int i = 0; i < package.length; i++) { |
104 if (s.length != 0) s += ', '; | 169 if (s.length != 0) s += ', '; |
105 s += '0x'; | 170 s += '0x'; |
106 var x = package[i].toRadixString(16); | 171 var x = package[i].toRadixString(16); |
107 if (x.length == 1) s += '0'; | 172 if (x.length == 1) s += '0'; |
108 s += x; | 173 s += x; |
109 if (((i + 1) % 8) == 0) { | 174 if (((i + 1) % 8) == 0) { |
110 s += ','; | 175 s += ','; |
111 print(s); | 176 print(s); |
112 s = ''; | 177 s = ''; |
113 } | 178 } |
114 } | 179 } |
115 if (s.length != 0) print(s); | 180 if (s.length != 0) print(s); |
116 } | 181 } |
OLD | NEW |