OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 |
| 5 // Test that the resource record cache works correctly. In particular, make |
| 6 // sure that it removes all entries for a name before inserting new records |
| 7 // of that name. |
| 8 |
| 9 import 'dart:io'; |
| 10 |
| 11 import 'package:expect/expect.dart'; |
| 12 import 'package:mdns/src/constants.dart' show RRType; |
| 13 import 'package:mdns/src/native_protocol_client.dart' |
| 14 show ResourceRecordCache; |
| 15 import 'package:mdns/src/packet.dart'; |
| 16 |
| 17 int entries(ResourceRecordCache cache) { |
| 18 int c = 0; |
| 19 for (int i = 0; i < cache.size; i++) { |
| 20 if (cache.buffer[i] != null) { |
| 21 ++c; |
| 22 } |
| 23 } |
| 24 return c; |
| 25 } |
| 26 |
| 27 main() { |
| 28 testOverwrite(); |
| 29 testTimeout(); |
| 30 } |
| 31 |
| 32 testOverwrite() { |
| 33 InternetAddress ip1 = new InternetAddress("192.168.1.1"); |
| 34 InternetAddress ip2 = new InternetAddress("192.168.1.2"); |
| 35 int valid = new DateTime.now().millisecondsSinceEpoch + 86400 * 1000; |
| 36 |
| 37 ResourceRecordCache cache = new ResourceRecordCache(); |
| 38 |
| 39 // Add two different records. |
| 40 cache.updateRecords([ |
| 41 new ResourceRecord(RRType.A, "hest", ip1, valid), |
| 42 new ResourceRecord(RRType.A, "fisk", ip2, valid)]); |
| 43 Expect.equals(2, entries(cache)); |
| 44 |
| 45 // Update these records. |
| 46 cache.updateRecords([ |
| 47 new ResourceRecord(RRType.A, "hest", ip1, valid), |
| 48 new ResourceRecord(RRType.A, "fisk", ip2, valid)]); |
| 49 Expect.equals(2, entries(cache)); |
| 50 |
| 51 // Add two records with the same name (should remove the old one |
| 52 // with that name only.) |
| 53 cache.updateRecords([ |
| 54 new ResourceRecord(RRType.A, "hest", ip1, valid), |
| 55 new ResourceRecord(RRType.A, "hest", ip2, valid)]); |
| 56 Expect.equals(3, entries(cache)); |
| 57 |
| 58 // Overwrite the two cached entries with one with the same name. |
| 59 cache.updateRecords([ |
| 60 new ResourceRecord(RRType.A, "hest", ip1, valid), |
| 61 ]); |
| 62 Expect.equals(2, entries(cache)); |
| 63 } |
| 64 |
| 65 testTimeout() { |
| 66 InternetAddress ip1 = new InternetAddress("192.168.1.1"); |
| 67 InternetAddress ip2 = new InternetAddress("192.168.1.2"); |
| 68 int valid = new DateTime.now().millisecondsSinceEpoch + 86400 * 1000; |
| 69 int notValid = new DateTime.now().millisecondsSinceEpoch - 1; |
| 70 |
| 71 ResourceRecordCache cache = new ResourceRecordCache(); |
| 72 |
| 73 cache.updateRecords([ |
| 74 new ResourceRecord(RRType.A, "hest", ip1, valid) |
| 75 ]); |
| 76 Expect.equals(1, entries(cache)); |
| 77 |
| 78 cache.updateRecords([ |
| 79 new ResourceRecord(RRType.A, "fisk", ip1, notValid) |
| 80 ]); |
| 81 |
| 82 var results; |
| 83 results = []; |
| 84 cache.lookup("hest", RRType.A, results); |
| 85 Expect.isFalse(results.isEmpty); |
| 86 |
| 87 results = []; |
| 88 cache.lookup("fisk", RRType.A, results); |
| 89 Expect.isTrue(results.isEmpty); |
| 90 Expect.equals(1, entries(cache)); |
| 91 } |
OLD | NEW |