| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <queue> | 5 #include <queue> |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "net/base/rand_callback.h" | 9 #include "net/base/rand_callback.h" |
| 10 #include "net/base/test_completion_callback.h" | 10 #include "net/base/test_completion_callback.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 using ::testing::NiceMock; | 21 using ::testing::NiceMock; |
| 22 using ::testing::Exactly; | 22 using ::testing::Exactly; |
| 23 using ::testing::Return; | 23 using ::testing::Return; |
| 24 using ::testing::SaveArg; | 24 using ::testing::SaveArg; |
| 25 using ::testing::_; | 25 using ::testing::_; |
| 26 | 26 |
| 27 namespace net { | 27 namespace net { |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 const uint8 kSamplePacket1[] = { | 31 const uint8 |
| 32 // Header | 32 kSamplePacket1 |
| 33 0x00, 0x00, // ID is zeroed out | 33 [] = |
| 34 0x81, 0x80, // Standard query response, RA, no error | 34 { |
| 35 0x00, 0x00, // No questions (for simplicity) | 35 // Header |
| 36 0x00, 0x02, // 2 RRs (answers) | 36 0x00, 0x00, // ID is zeroed out |
| 37 0x00, 0x00, // 0 authority RRs | 37 0x81, 0x80, // Standard query response, RA, no error |
| 38 0x00, 0x00, // 0 additional RRs | 38 0x00, 0x00, // No questions (for simplicity) |
| 39 | 39 0x00, 0x02, // 2 RRs (answers) |
| 40 // Answer 1 | 40 0x00, 0x00, // 0 authority RRs |
| 41 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', | 41 0x00, 0x00, // 0 additional RRs |
| 42 0x04, '_', 't', 'c', 'p', | 42 |
| 43 0x05, 'l', 'o', 'c', 'a', 'l', | 43 // Answer 1 |
| 44 0x00, | 44 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
| 45 0x00, 0x0c, // TYPE is PTR. | 45 0x04, '_', 't', 'c', 'p', 0x05, 'l', 'o', |
| 46 0x00, 0x01, // CLASS is IN. | 46 'c', 'a', 'l', 0x00, 0x00, 0x0c, // TYPE is PTR. |
| 47 0x00, 0x00, // TTL (4 bytes) is 1 second; | 47 0x00, 0x01, // CLASS is IN. |
| 48 0x00, 0x01, | 48 0x00, 0x00, // TTL (4 bytes) is 1 second; |
| 49 0x00, 0x08, // RDLENGTH is 8 bytes. | 49 0x00, 0x01, 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 50 0x05, 'h', 'e', 'l', 'l', 'o', | 50 0x05, 'h', 'e', 'l', 'l', 'o', 0xc0, 0x0c, |
| 51 0xc0, 0x0c, | 51 |
| 52 | 52 // Answer 2 |
| 53 // Answer 2 | 53 0x08, '_', 'p', 'r', 'i', 'n', 't', 'e', |
| 54 0x08, '_', 'p', 'r', 'i', 'n', 't', 'e', 'r', | 54 'r', 0xc0, 0x14, // Pointer to "._tcp.local" |
| 55 0xc0, 0x14, // Pointer to "._tcp.local" | 55 0x00, 0x0c, // TYPE is PTR. |
| 56 0x00, 0x0c, // TYPE is PTR. | 56 0x00, 0x01, // CLASS is IN. |
| 57 0x00, 0x01, // CLASS is IN. | 57 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 49 seconds. |
| 58 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 49 seconds. | 58 0x24, 0x75, 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 59 0x24, 0x75, | 59 0x05, 'h', 'e', 'l', 'l', 'o', 0xc0, 0x32}; |
| 60 0x00, 0x08, // RDLENGTH is 8 bytes. | |
| 61 0x05, 'h', 'e', 'l', 'l', 'o', | |
| 62 0xc0, 0x32 | |
| 63 }; | |
| 64 | 60 |
| 65 const uint8 kCorruptedPacketBadQuestion[] = { | 61 const uint8 kCorruptedPacketBadQuestion[] = { |
| 66 // Header | 62 // Header |
| 67 0x00, 0x00, // ID is zeroed out | 63 0x00, 0x00, // ID is zeroed out |
| 68 0x81, 0x80, // Standard query response, RA, no error | 64 0x81, 0x80, // Standard query response, RA, no error |
| 69 0x00, 0x01, // One question | 65 0x00, 0x01, // One question |
| 70 0x00, 0x02, // 2 RRs (answers) | 66 0x00, 0x02, // 2 RRs (answers) |
| 71 0x00, 0x00, // 0 authority RRs | 67 0x00, 0x00, // 0 authority RRs |
| 72 0x00, 0x00, // 0 additional RRs | 68 0x00, 0x00, // 0 additional RRs |
| 73 | 69 |
| 74 // Question is corrupted and cannot be read. | 70 // Question is corrupted and cannot be read. |
| 75 0x99, 'h', 'e', 'l', 'l', 'o', | 71 0x99, 'h', 'e', 'l', 'l', 'o', 0x00, 0x00, 0x00, 0x00, 0x00, |
| 76 0x00, | 72 |
| 77 0x00, 0x00, | 73 // Answer 1 |
| 78 0x00, 0x00, | 74 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', 0x04, '_', 't', 'c', |
| 79 | 75 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0x00, 0x0c, // TYPE is PTR. |
| 80 // Answer 1 | 76 0x00, 0x01, // CLASS is IN. |
| 81 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', | 77 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 82 0x04, '_', 't', 'c', 'p', | 78 0x24, 0x74, 0x00, 0x99, // RDLENGTH is impossible |
| 83 0x05, 'l', 'o', 'c', 'a', 'l', | 79 0x05, 'h', 'e', 'l', 'l', 'o', 0xc0, 0x0c, |
| 84 0x00, | 80 |
| 85 0x00, 0x0c, // TYPE is PTR. | 81 // Answer 2 |
| 86 0x00, 0x01, // CLASS is IN. | 82 0x08, '_', 'p', 'r', // Useless trailing data. |
| 87 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | |
| 88 0x24, 0x74, | |
| 89 0x00, 0x99, // RDLENGTH is impossible | |
| 90 0x05, 'h', 'e', 'l', 'l', 'o', | |
| 91 0xc0, 0x0c, | |
| 92 | |
| 93 // Answer 2 | |
| 94 0x08, '_', 'p', 'r', // Useless trailing data. | |
| 95 }; | 83 }; |
| 96 | 84 |
| 97 const uint8 kCorruptedPacketUnsalvagable[] = { | 85 const uint8 kCorruptedPacketUnsalvagable[] = { |
| 98 // Header | 86 // Header |
| 99 0x00, 0x00, // ID is zeroed out | 87 0x00, 0x00, // ID is zeroed out |
| 100 0x81, 0x80, // Standard query response, RA, no error | 88 0x81, 0x80, // Standard query response, RA, no error |
| 101 0x00, 0x00, // No questions (for simplicity) | 89 0x00, 0x00, // No questions (for simplicity) |
| 102 0x00, 0x02, // 2 RRs (answers) | 90 0x00, 0x02, // 2 RRs (answers) |
| 103 0x00, 0x00, // 0 authority RRs | 91 0x00, 0x00, // 0 authority RRs |
| 104 0x00, 0x00, // 0 additional RRs | 92 0x00, 0x00, // 0 additional RRs |
| 105 | 93 |
| 106 // Answer 1 | 94 // Answer 1 |
| 107 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', | 95 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', 0x04, '_', 't', 'c', |
| 108 0x04, '_', 't', 'c', 'p', | 96 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0x00, 0x0c, // TYPE is PTR. |
| 109 0x05, 'l', 'o', 'c', 'a', 'l', | 97 0x00, 0x01, // CLASS is IN. |
| 110 0x00, | 98 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 111 0x00, 0x0c, // TYPE is PTR. | 99 0x24, 0x74, 0x00, 0x99, // RDLENGTH is impossible |
| 112 0x00, 0x01, // CLASS is IN. | 100 0x05, 'h', 'e', 'l', 'l', 'o', 0xc0, 0x0c, |
| 113 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 101 |
| 114 0x24, 0x74, | 102 // Answer 2 |
| 115 0x00, 0x99, // RDLENGTH is impossible | 103 0x08, '_', 'p', 'r', // Useless trailing data. |
| 116 0x05, 'h', 'e', 'l', 'l', 'o', | |
| 117 0xc0, 0x0c, | |
| 118 | |
| 119 // Answer 2 | |
| 120 0x08, '_', 'p', 'r', // Useless trailing data. | |
| 121 }; | 104 }; |
| 122 | 105 |
| 123 const uint8 kCorruptedPacketDoubleRecord[] = { | 106 const uint8 kCorruptedPacketDoubleRecord[] = { |
| 124 // Header | 107 // Header |
| 125 0x00, 0x00, // ID is zeroed out | 108 0x00, 0x00, // ID is zeroed out |
| 126 0x81, 0x80, // Standard query response, RA, no error | 109 0x81, 0x80, // Standard query response, RA, no error |
| 127 0x00, 0x00, // No questions (for simplicity) | 110 0x00, 0x00, // No questions (for simplicity) |
| 128 0x00, 0x02, // 2 RRs (answers) | 111 0x00, 0x02, // 2 RRs (answers) |
| 129 0x00, 0x00, // 0 authority RRs | 112 0x00, 0x00, // 0 authority RRs |
| 130 0x00, 0x00, // 0 additional RRs | 113 0x00, 0x00, // 0 additional RRs |
| 131 | 114 |
| 132 // Answer 1 | 115 // Answer 1 |
| 133 0x06, 'p', 'r', 'i', 'v', 'e', 't', | 116 0x06, 'p', 'r', 'i', 'v', 'e', 't', 0x05, |
| 134 0x05, 'l', 'o', 'c', 'a', 'l', | 117 'l', 'o', 'c', 'a', 'l', 0x00, 0x00, 0x01, // TYPE is A. |
| 135 0x00, | 118 0x00, 0x01, // CLASS is IN. |
| 136 0x00, 0x01, // TYPE is A. | 119 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 137 0x00, 0x01, // CLASS is IN. | 120 0x24, 0x74, 0x00, 0x04, // RDLENGTH is 4 |
| 138 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 121 0x05, 0x03, 0xc0, 0x0c, |
| 139 0x24, 0x74, | 122 |
| 140 0x00, 0x04, // RDLENGTH is 4 | 123 // Answer 2 -- Same key |
| 141 0x05, 0x03, | 124 0x06, 'p', 'r', 'i', 'v', 'e', 't', 0x05, |
| 142 0xc0, 0x0c, | 125 'l', 'o', 'c', 'a', 'l', 0x00, 0x00, 0x01, // TYPE is A. |
| 143 | 126 0x00, 0x01, // CLASS is IN. |
| 144 // Answer 2 -- Same key | 127 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 145 0x06, 'p', 'r', 'i', 'v', 'e', 't', | 128 0x24, 0x74, 0x00, 0x04, // RDLENGTH is 4 |
| 146 0x05, 'l', 'o', 'c', 'a', 'l', | 129 0x02, 0x03, 0x04, 0x05, |
| 147 0x00, | 130 }; |
| 148 0x00, 0x01, // TYPE is A. | 131 |
| 149 0x00, 0x01, // CLASS is IN. | 132 const uint8 |
| 150 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 133 kCorruptedPacketSalvagable |
| 151 0x24, 0x74, | 134 [] = |
| 152 0x00, 0x04, // RDLENGTH is 4 | 135 { |
| 153 0x02, 0x03, | 136 // Header |
| 154 0x04, 0x05, | 137 0x00, 0x00, // ID is zeroed out |
| 155 }; | 138 0x81, 0x80, // Standard query response, RA, no error |
| 156 | 139 0x00, 0x00, // No questions (for simplicity) |
| 157 const uint8 kCorruptedPacketSalvagable[] = { | 140 0x00, 0x02, // 2 RRs (answers) |
| 158 // Header | 141 0x00, 0x00, // 0 authority RRs |
| 159 0x00, 0x00, // ID is zeroed out | 142 0x00, 0x00, // 0 additional RRs |
| 160 0x81, 0x80, // Standard query response, RA, no error | 143 |
| 161 0x00, 0x00, // No questions (for simplicity) | 144 // Answer 1 |
| 162 0x00, 0x02, // 2 RRs (answers) | 145 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
| 163 0x00, 0x00, // 0 authority RRs | 146 0x04, '_', 't', 'c', 'p', 0x05, 'l', 'o', |
| 164 0x00, 0x00, // 0 additional RRs | 147 'c', 'a', 'l', 0x00, 0x00, 0x0c, // TYPE is PTR. |
| 165 | 148 0x00, 0x01, // CLASS is IN. |
| 166 // Answer 1 | 149 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 167 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', | 150 0x24, 0x74, 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 168 0x04, '_', 't', 'c', 'p', | 151 0x99, 'h', 'e', 'l', 'l', 'o', // Bad RDATA format. |
| 169 0x05, 'l', 'o', 'c', 'a', 'l', | 152 0xc0, 0x0c, |
| 170 0x00, | 153 |
| 171 0x00, 0x0c, // TYPE is PTR. | 154 // Answer 2 |
| 172 0x00, 0x01, // CLASS is IN. | 155 0x08, '_', 'p', 'r', 'i', 'n', 't', 'e', |
| 173 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 156 'r', 0xc0, 0x14, // Pointer to "._tcp.local" |
| 174 0x24, 0x74, | 157 0x00, 0x0c, // TYPE is PTR. |
| 175 0x00, 0x08, // RDLENGTH is 8 bytes. | 158 0x00, 0x01, // CLASS is IN. |
| 176 0x99, 'h', 'e', 'l', 'l', 'o', // Bad RDATA format. | 159 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 49 seconds. |
| 177 0xc0, 0x0c, | 160 0x24, 0x75, 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 178 | 161 0x05, 'h', 'e', 'l', 'l', 'o', 0xc0, 0x32}; |
| 179 // Answer 2 | 162 |
| 180 0x08, '_', 'p', 'r', 'i', 'n', 't', 'e', 'r', | 163 const uint8 |
| 181 0xc0, 0x14, // Pointer to "._tcp.local" | 164 kSamplePacket2 |
| 182 0x00, 0x0c, // TYPE is PTR. | 165 [] = |
| 183 0x00, 0x01, // CLASS is IN. | 166 { |
| 184 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 49 seconds. | 167 // Header |
| 185 0x24, 0x75, | 168 0x00, 0x00, // ID is zeroed out |
| 186 0x00, 0x08, // RDLENGTH is 8 bytes. | 169 0x81, 0x80, // Standard query response, RA, no error |
| 187 0x05, 'h', 'e', 'l', 'l', 'o', | 170 0x00, 0x00, // No questions (for simplicity) |
| 188 0xc0, 0x32 | 171 0x00, 0x02, // 2 RRs (answers) |
| 189 }; | 172 0x00, 0x00, // 0 authority RRs |
| 190 | 173 0x00, 0x00, // 0 additional RRs |
| 191 const uint8 kSamplePacket2[] = { | 174 |
| 192 // Header | 175 // Answer 1 |
| 193 0x00, 0x00, // ID is zeroed out | 176 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
| 194 0x81, 0x80, // Standard query response, RA, no error | 177 0x04, '_', 't', 'c', 'p', 0x05, 'l', 'o', |
| 195 0x00, 0x00, // No questions (for simplicity) | 178 'c', 'a', 'l', 0x00, 0x00, 0x0c, // TYPE is PTR. |
| 196 0x00, 0x02, // 2 RRs (answers) | 179 0x00, 0x01, // CLASS is IN. |
| 197 0x00, 0x00, // 0 authority RRs | 180 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 198 0x00, 0x00, // 0 additional RRs | 181 0x24, 0x74, 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 199 | 182 0x05, 'z', 'z', 'z', 'z', 'z', 0xc0, 0x0c, |
| 200 // Answer 1 | 183 |
| 201 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', | 184 // Answer 2 |
| 202 0x04, '_', 't', 'c', 'p', | 185 0x08, '_', 'p', 'r', 'i', 'n', 't', 'e', |
| 203 0x05, 'l', 'o', 'c', 'a', 'l', | 186 'r', 0xc0, 0x14, // Pointer to "._tcp.local" |
| 204 0x00, | 187 0x00, 0x0c, // TYPE is PTR. |
| 205 0x00, 0x0c, // TYPE is PTR. | 188 0x00, 0x01, // CLASS is IN. |
| 206 0x00, 0x01, // CLASS is IN. | 189 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 207 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 190 0x24, 0x74, 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 208 0x24, 0x74, | 191 0x05, 'z', 'z', 'z', 'z', 'z', 0xc0, 0x32}; |
| 209 0x00, 0x08, // RDLENGTH is 8 bytes. | |
| 210 0x05, 'z', 'z', 'z', 'z', 'z', | |
| 211 0xc0, 0x0c, | |
| 212 | |
| 213 // Answer 2 | |
| 214 0x08, '_', 'p', 'r', 'i', 'n', 't', 'e', 'r', | |
| 215 0xc0, 0x14, // Pointer to "._tcp.local" | |
| 216 0x00, 0x0c, // TYPE is PTR. | |
| 217 0x00, 0x01, // CLASS is IN. | |
| 218 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | |
| 219 0x24, 0x74, | |
| 220 0x00, 0x08, // RDLENGTH is 8 bytes. | |
| 221 0x05, 'z', 'z', 'z', 'z', 'z', | |
| 222 0xc0, 0x32 | |
| 223 }; | |
| 224 | 192 |
| 225 const uint8 kQueryPacketPrivet[] = { | 193 const uint8 kQueryPacketPrivet[] = { |
| 226 // Header | 194 // Header |
| 227 0x00, 0x00, // ID is zeroed out | 195 0x00, 0x00, // ID is zeroed out |
| 228 0x00, 0x00, // No flags. | 196 0x00, 0x00, // No flags. |
| 229 0x00, 0x01, // One question. | 197 0x00, 0x01, // One question. |
| 230 0x00, 0x00, // 0 RRs (answers) | 198 0x00, 0x00, // 0 RRs (answers) |
| 231 0x00, 0x00, // 0 authority RRs | 199 0x00, 0x00, // 0 authority RRs |
| 232 0x00, 0x00, // 0 additional RRs | 200 0x00, 0x00, // 0 additional RRs |
| 233 | 201 |
| 234 // Question | 202 // Question |
| 235 // This part is echoed back from the respective query. | 203 // This part is echoed back from the respective query. |
| 236 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', | 204 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', 0x04, '_', 't', 'c', |
| 237 0x04, '_', 't', 'c', 'p', | 205 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0x00, 0x0c, // TYPE is PTR. |
| 238 0x05, 'l', 'o', 'c', 'a', 'l', | 206 0x00, 0x01, // CLASS is IN. |
| 239 0x00, | |
| 240 0x00, 0x0c, // TYPE is PTR. | |
| 241 0x00, 0x01, // CLASS is IN. | |
| 242 }; | 207 }; |
| 243 | 208 |
| 244 const uint8 kQueryPacketPrivetA[] = { | 209 const uint8 kQueryPacketPrivetA[] = { |
| 245 // Header | 210 // Header |
| 246 0x00, 0x00, // ID is zeroed out | 211 0x00, 0x00, // ID is zeroed out |
| 247 0x00, 0x00, // No flags. | 212 0x00, 0x00, // No flags. |
| 248 0x00, 0x01, // One question. | 213 0x00, 0x01, // One question. |
| 249 0x00, 0x00, // 0 RRs (answers) | 214 0x00, 0x00, // 0 RRs (answers) |
| 250 0x00, 0x00, // 0 authority RRs | 215 0x00, 0x00, // 0 authority RRs |
| 251 0x00, 0x00, // 0 additional RRs | 216 0x00, 0x00, // 0 additional RRs |
| 252 | 217 |
| 253 // Question | 218 // Question |
| 254 // This part is echoed back from the respective query. | 219 // This part is echoed back from the respective query. |
| 255 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', | 220 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', 0x04, '_', 't', |
| 256 0x04, '_', 't', 'c', 'p', | 221 'c', 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0x00, 0x01, // TYPE is A. |
| 257 0x05, 'l', 'o', 'c', 'a', 'l', | 222 0x00, 0x01, // CLASS is IN. |
| 258 0x00, | |
| 259 0x00, 0x01, // TYPE is A. | |
| 260 0x00, 0x01, // CLASS is IN. | |
| 261 }; | 223 }; |
| 262 | 224 |
| 263 const uint8 kSamplePacketAdditionalOnly[] = { | 225 const uint8 kSamplePacketAdditionalOnly[] = { |
| 264 // Header | 226 // Header |
| 265 0x00, 0x00, // ID is zeroed out | 227 0x00, 0x00, // ID is zeroed out |
| 266 0x81, 0x80, // Standard query response, RA, no error | 228 0x81, 0x80, // Standard query response, RA, no error |
| 267 0x00, 0x00, // No questions (for simplicity) | 229 0x00, 0x00, // No questions (for simplicity) |
| 268 0x00, 0x00, // 2 RRs (answers) | 230 0x00, 0x00, // 2 RRs (answers) |
| 269 0x00, 0x00, // 0 authority RRs | 231 0x00, 0x00, // 0 authority RRs |
| 270 0x00, 0x01, // 0 additional RRs | 232 0x00, 0x01, // 0 additional RRs |
| 271 | 233 |
| 272 // Answer 1 | 234 // Answer 1 |
| 273 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', | 235 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', 0x04, '_', 't', 'c', |
| 274 0x04, '_', 't', 'c', 'p', | 236 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0x00, 0x0c, // TYPE is PTR. |
| 275 0x05, 'l', 'o', 'c', 'a', 'l', | 237 0x00, 0x01, // CLASS is IN. |
| 276 0x00, | 238 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 277 0x00, 0x0c, // TYPE is PTR. | 239 0x24, 0x74, 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 278 0x00, 0x01, // CLASS is IN. | 240 0x05, 'h', 'e', 'l', 'l', 'o', 0xc0, 0x0c, |
| 279 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | |
| 280 0x24, 0x74, | |
| 281 0x00, 0x08, // RDLENGTH is 8 bytes. | |
| 282 0x05, 'h', 'e', 'l', 'l', 'o', | |
| 283 0xc0, 0x0c, | |
| 284 }; | 241 }; |
| 285 | 242 |
| 286 const uint8 kSamplePacketNsec[] = { | 243 const uint8 kSamplePacketNsec[] = { |
| 287 // Header | 244 // Header |
| 288 0x00, 0x00, // ID is zeroed out | 245 0x00, 0x00, // ID is zeroed out |
| 289 0x81, 0x80, // Standard query response, RA, no error | 246 0x81, 0x80, // Standard query response, RA, no error |
| 290 0x00, 0x00, // No questions (for simplicity) | 247 0x00, 0x00, // No questions (for simplicity) |
| 291 0x00, 0x01, // 1 RR (answers) | 248 0x00, 0x01, // 1 RR (answers) |
| 292 0x00, 0x00, // 0 authority RRs | 249 0x00, 0x00, // 0 authority RRs |
| 293 0x00, 0x00, // 0 additional RRs | 250 0x00, 0x00, // 0 additional RRs |
| 294 | 251 |
| 295 // Answer 1 | 252 // Answer 1 |
| 296 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', | 253 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', 0x04, '_', 't', 'c', |
| 297 0x04, '_', 't', 'c', 'p', | 254 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0x00, 0x2f, // TYPE is NSEC. |
| 298 0x05, 'l', 'o', 'c', 'a', 'l', | 255 0x00, 0x01, // CLASS is IN. |
| 299 0x00, | 256 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 300 0x00, 0x2f, // TYPE is NSEC. | 257 0x24, 0x74, 0x00, 0x06, // RDLENGTH is 6 bytes. |
| 301 0x00, 0x01, // CLASS is IN. | 258 0xc0, 0x0c, 0x00, 0x02, 0x00, 0x08 // Only A record present |
| 302 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | |
| 303 0x24, 0x74, | |
| 304 0x00, 0x06, // RDLENGTH is 6 bytes. | |
| 305 0xc0, 0x0c, | |
| 306 0x00, 0x02, 0x00, 0x08 // Only A record present | |
| 307 }; | 259 }; |
| 308 | 260 |
| 309 const uint8 kSamplePacketAPrivet[] = { | 261 const uint8 kSamplePacketAPrivet[] = { |
| 310 // Header | 262 // Header |
| 311 0x00, 0x00, // ID is zeroed out | 263 0x00, 0x00, // ID is zeroed out |
| 312 0x81, 0x80, // Standard query response, RA, no error | 264 0x81, 0x80, // Standard query response, RA, no error |
| 313 0x00, 0x00, // No questions (for simplicity) | 265 0x00, 0x00, // No questions (for simplicity) |
| 314 0x00, 0x01, // 1 RR (answers) | 266 0x00, 0x01, // 1 RR (answers) |
| 315 0x00, 0x00, // 0 authority RRs | 267 0x00, 0x00, // 0 authority RRs |
| 316 0x00, 0x00, // 0 additional RRs | 268 0x00, 0x00, // 0 additional RRs |
| 317 | 269 |
| 318 // Answer 1 | 270 // Answer 1 |
| 319 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', | 271 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', 0x04, '_', 't', |
| 320 0x04, '_', 't', 'c', 'p', | 272 'c', 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0x00, 0x01, // TYPE is A. |
| 321 0x05, 'l', 'o', 'c', 'a', 'l', | 273 0x00, 0x01, // CLASS is IN. |
| 322 0x00, | 274 0x00, 0x00, // TTL (4 bytes) is 5 seconds |
| 323 0x00, 0x01, // TYPE is A. | 275 0x00, 0x05, 0x00, 0x04, // RDLENGTH is 4 bytes. |
| 324 0x00, 0x01, // CLASS is IN. | 276 0xc0, 0x0c, 0x00, 0x02, |
| 325 0x00, 0x00, // TTL (4 bytes) is 5 seconds | |
| 326 0x00, 0x05, | |
| 327 0x00, 0x04, // RDLENGTH is 4 bytes. | |
| 328 0xc0, 0x0c, | |
| 329 0x00, 0x02, | |
| 330 }; | 277 }; |
| 331 | 278 |
| 332 const uint8 kSamplePacketGoodbye[] = { | 279 const uint8 kSamplePacketGoodbye[] = { |
| 333 // Header | 280 // Header |
| 334 0x00, 0x00, // ID is zeroed out | 281 0x00, 0x00, // ID is zeroed out |
| 335 0x81, 0x80, // Standard query response, RA, no error | 282 0x81, 0x80, // Standard query response, RA, no error |
| 336 0x00, 0x00, // No questions (for simplicity) | 283 0x00, 0x00, // No questions (for simplicity) |
| 337 0x00, 0x01, // 2 RRs (answers) | 284 0x00, 0x01, // 2 RRs (answers) |
| 338 0x00, 0x00, // 0 authority RRs | 285 0x00, 0x00, // 0 authority RRs |
| 339 0x00, 0x00, // 0 additional RRs | 286 0x00, 0x00, // 0 additional RRs |
| 340 | 287 |
| 341 // Answer 1 | 288 // Answer 1 |
| 342 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', | 289 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', 0x04, '_', 't', 'c', |
| 343 0x04, '_', 't', 'c', 'p', | 290 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00, 0x00, 0x0c, // TYPE is PTR. |
| 344 0x05, 'l', 'o', 'c', 'a', 'l', | 291 0x00, 0x01, // CLASS is IN. |
| 345 0x00, | 292 0x00, 0x00, // TTL (4 bytes) is zero; |
| 346 0x00, 0x0c, // TYPE is PTR. | 293 0x00, 0x00, 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 347 0x00, 0x01, // CLASS is IN. | 294 0x05, 'z', 'z', 'z', 'z', 'z', 0xc0, 0x0c, |
| 348 0x00, 0x00, // TTL (4 bytes) is zero; | |
| 349 0x00, 0x00, | |
| 350 0x00, 0x08, // RDLENGTH is 8 bytes. | |
| 351 0x05, 'z', 'z', 'z', 'z', 'z', | |
| 352 0xc0, 0x0c, | |
| 353 }; | 295 }; |
| 354 | 296 |
| 355 std::string MakeString(const uint8* data, unsigned size) { | 297 std::string MakeString(const uint8* data, unsigned size) { |
| 356 return std::string(reinterpret_cast<const char*>(data), size); | 298 return std::string(reinterpret_cast<const char*>(data), size); |
| 357 } | 299 } |
| 358 | 300 |
| 359 class PtrRecordCopyContainer { | 301 class PtrRecordCopyContainer { |
| 360 public: | 302 public: |
| 361 PtrRecordCopyContainer() {} | 303 PtrRecordCopyContainer() {} |
| 362 ~PtrRecordCopyContainer() {} | 304 ~PtrRecordCopyContainer() {} |
| 363 | 305 |
| 364 bool is_set() const { return set_; } | 306 bool is_set() const { return set_; } |
| 365 | 307 |
| 366 void SaveWithDummyArg(int unused, const RecordParsed* value) { | 308 void SaveWithDummyArg(int unused, const RecordParsed* value) { Save(value); } |
| 367 Save(value); | |
| 368 } | |
| 369 | 309 |
| 370 void Save(const RecordParsed* value) { | 310 void Save(const RecordParsed* value) { |
| 371 set_ = true; | 311 set_ = true; |
| 372 name_ = value->name(); | 312 name_ = value->name(); |
| 373 ptrdomain_ = value->rdata<PtrRecordRdata>()->ptrdomain(); | 313 ptrdomain_ = value->rdata<PtrRecordRdata>()->ptrdomain(); |
| 374 ttl_ = value->ttl(); | 314 ttl_ = value->ttl(); |
| 375 } | 315 } |
| 376 | 316 |
| 377 bool IsRecordWith(std::string name, std::string ptrdomain) { | 317 bool IsRecordWith(std::string name, std::string ptrdomain) { |
| 378 return set_ && name_ == name && ptrdomain_ == ptrdomain; | 318 return set_ && name_ == name && ptrdomain_ == ptrdomain; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 390 }; | 330 }; |
| 391 | 331 |
| 392 class MDnsTest : public ::testing::Test { | 332 class MDnsTest : public ::testing::Test { |
| 393 public: | 333 public: |
| 394 virtual void SetUp() OVERRIDE; | 334 virtual void SetUp() OVERRIDE; |
| 395 void DeleteTransaction(); | 335 void DeleteTransaction(); |
| 396 void DeleteBothListeners(); | 336 void DeleteBothListeners(); |
| 397 void RunFor(base::TimeDelta time_period); | 337 void RunFor(base::TimeDelta time_period); |
| 398 void Stop(); | 338 void Stop(); |
| 399 | 339 |
| 400 MOCK_METHOD2(MockableRecordCallback, void(MDnsTransaction::Result result, | 340 MOCK_METHOD2(MockableRecordCallback, |
| 401 const RecordParsed* record)); | 341 void(MDnsTransaction::Result result, |
| 342 const RecordParsed* record)); |
| 402 | 343 |
| 403 MOCK_METHOD2(MockableRecordCallback2, void(MDnsTransaction::Result result, | 344 MOCK_METHOD2(MockableRecordCallback2, |
| 404 const RecordParsed* record)); | 345 void(MDnsTransaction::Result result, |
| 405 | 346 const RecordParsed* record)); |
| 406 | 347 |
| 407 protected: | 348 protected: |
| 408 void ExpectPacket(const uint8* packet, unsigned size); | 349 void ExpectPacket(const uint8* packet, unsigned size); |
| 409 void SimulatePacketReceive(const uint8* packet, unsigned size); | 350 void SimulatePacketReceive(const uint8* packet, unsigned size); |
| 410 | 351 |
| 411 MDnsClientImpl test_client_; | 352 MDnsClientImpl test_client_; |
| 412 IPEndPoint mdns_ipv4_endpoint_; | 353 IPEndPoint mdns_ipv4_endpoint_; |
| 413 StrictMock<MockMDnsSocketFactory> socket_factory_; | 354 StrictMock<MockMDnsSocketFactory> socket_factory_; |
| 414 | 355 |
| 415 // Transactions and listeners that can be deleted by class methods for | 356 // Transactions and listeners that can be deleted by class methods for |
| (...skipping 14 matching lines...) Expand all Loading... |
| 430 | 371 |
| 431 void MDnsTest::SetUp() { | 372 void MDnsTest::SetUp() { |
| 432 test_client_.StartListening(&socket_factory_); | 373 test_client_.StartListening(&socket_factory_); |
| 433 } | 374 } |
| 434 | 375 |
| 435 void MDnsTest::SimulatePacketReceive(const uint8* packet, unsigned size) { | 376 void MDnsTest::SimulatePacketReceive(const uint8* packet, unsigned size) { |
| 436 socket_factory_.SimulateReceive(packet, size); | 377 socket_factory_.SimulateReceive(packet, size); |
| 437 } | 378 } |
| 438 | 379 |
| 439 void MDnsTest::ExpectPacket(const uint8* packet, unsigned size) { | 380 void MDnsTest::ExpectPacket(const uint8* packet, unsigned size) { |
| 440 EXPECT_CALL(socket_factory_, OnSendTo(MakeString(packet, size))) | 381 EXPECT_CALL(socket_factory_, OnSendTo(MakeString(packet, size))).Times(2); |
| 441 .Times(2); | |
| 442 } | 382 } |
| 443 | 383 |
| 444 void MDnsTest::DeleteTransaction() { | 384 void MDnsTest::DeleteTransaction() { |
| 445 transaction_.reset(); | 385 transaction_.reset(); |
| 446 } | 386 } |
| 447 | 387 |
| 448 void MDnsTest::DeleteBothListeners() { | 388 void MDnsTest::DeleteBothListeners() { |
| 449 listener1_.reset(); | 389 listener1_.reset(); |
| 450 listener2_.reset(); | 390 listener2_.reset(); |
| 451 } | 391 } |
| 452 | 392 |
| 453 void MDnsTest::RunFor(base::TimeDelta time_period) { | 393 void MDnsTest::RunFor(base::TimeDelta time_period) { |
| 454 base::CancelableCallback<void()> callback(base::Bind(&MDnsTest::Stop, | 394 base::CancelableCallback<void()> callback( |
| 455 base::Unretained(this))); | 395 base::Bind(&MDnsTest::Stop, base::Unretained(this))); |
| 456 base::MessageLoop::current()->PostDelayedTask( | 396 base::MessageLoop::current()->PostDelayedTask( |
| 457 FROM_HERE, callback.callback(), time_period); | 397 FROM_HERE, callback.callback(), time_period); |
| 458 | 398 |
| 459 base::MessageLoop::current()->Run(); | 399 base::MessageLoop::current()->Run(); |
| 460 callback.Cancel(); | 400 callback.Cancel(); |
| 461 } | 401 } |
| 462 | 402 |
| 463 void MDnsTest::Stop() { | 403 void MDnsTest::Stop() { |
| 464 base::MessageLoop::current()->Quit(); | 404 base::MessageLoop::current()->Quit(); |
| 465 } | 405 } |
| 466 | 406 |
| 467 TEST_F(MDnsTest, PassiveListeners) { | 407 TEST_F(MDnsTest, PassiveListeners) { |
| 468 StrictMock<MockListenerDelegate> delegate_privet; | 408 StrictMock<MockListenerDelegate> delegate_privet; |
| 469 StrictMock<MockListenerDelegate> delegate_printer; | 409 StrictMock<MockListenerDelegate> delegate_printer; |
| 470 | 410 |
| 471 PtrRecordCopyContainer record_privet; | 411 PtrRecordCopyContainer record_privet; |
| 472 PtrRecordCopyContainer record_printer; | 412 PtrRecordCopyContainer record_printer; |
| 473 | 413 |
| 474 scoped_ptr<MDnsListener> listener_privet = | 414 scoped_ptr<MDnsListener> listener_privet = test_client_.CreateListener( |
| 475 test_client_.CreateListener(dns_protocol::kTypePTR, "_privet._tcp.local", | 415 dns_protocol::kTypePTR, "_privet._tcp.local", &delegate_privet); |
| 476 &delegate_privet); | 416 scoped_ptr<MDnsListener> listener_printer = test_client_.CreateListener( |
| 477 scoped_ptr<MDnsListener> listener_printer = | 417 dns_protocol::kTypePTR, "_printer._tcp.local", &delegate_printer); |
| 478 test_client_.CreateListener(dns_protocol::kTypePTR, "_printer._tcp.local", | |
| 479 &delegate_printer); | |
| 480 | 418 |
| 481 ASSERT_TRUE(listener_privet->Start()); | 419 ASSERT_TRUE(listener_privet->Start()); |
| 482 ASSERT_TRUE(listener_printer->Start()); | 420 ASSERT_TRUE(listener_printer->Start()); |
| 483 | 421 |
| 484 // Send the same packet twice to ensure no records are double-counted. | 422 // Send the same packet twice to ensure no records are double-counted. |
| 485 | 423 |
| 486 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) | 424 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) |
| 487 .Times(Exactly(1)) | 425 .Times(Exactly(1)) |
| 488 .WillOnce(Invoke( | 426 .WillOnce( |
| 489 &record_privet, | 427 Invoke(&record_privet, &PtrRecordCopyContainer::SaveWithDummyArg)); |
| 490 &PtrRecordCopyContainer::SaveWithDummyArg)); | |
| 491 | 428 |
| 492 EXPECT_CALL(delegate_printer, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) | 429 EXPECT_CALL(delegate_printer, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) |
| 493 .Times(Exactly(1)) | 430 .Times(Exactly(1)) |
| 494 .WillOnce(Invoke( | 431 .WillOnce( |
| 495 &record_printer, | 432 Invoke(&record_printer, &PtrRecordCopyContainer::SaveWithDummyArg)); |
| 496 &PtrRecordCopyContainer::SaveWithDummyArg)); | |
| 497 | |
| 498 | 433 |
| 499 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); | 434 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); |
| 500 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); | 435 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); |
| 501 | 436 |
| 502 EXPECT_TRUE(record_privet.IsRecordWith("_privet._tcp.local", | 437 EXPECT_TRUE(record_privet.IsRecordWith("_privet._tcp.local", |
| 503 "hello._privet._tcp.local")); | 438 "hello._privet._tcp.local")); |
| 504 | 439 |
| 505 EXPECT_TRUE(record_printer.IsRecordWith("_printer._tcp.local", | 440 EXPECT_TRUE(record_printer.IsRecordWith("_printer._tcp.local", |
| 506 "hello._printer._tcp.local")); | 441 "hello._printer._tcp.local")); |
| 507 | 442 |
| 508 listener_privet.reset(); | 443 listener_privet.reset(); |
| 509 listener_printer.reset(); | 444 listener_printer.reset(); |
| 510 } | 445 } |
| 511 | 446 |
| 512 TEST_F(MDnsTest, PassiveListenersCacheCleanup) { | 447 TEST_F(MDnsTest, PassiveListenersCacheCleanup) { |
| 513 StrictMock<MockListenerDelegate> delegate_privet; | 448 StrictMock<MockListenerDelegate> delegate_privet; |
| 514 | 449 |
| 515 PtrRecordCopyContainer record_privet; | 450 PtrRecordCopyContainer record_privet; |
| 516 PtrRecordCopyContainer record_privet2; | 451 PtrRecordCopyContainer record_privet2; |
| 517 | 452 |
| 518 scoped_ptr<MDnsListener> listener_privet = | 453 scoped_ptr<MDnsListener> listener_privet = test_client_.CreateListener( |
| 519 test_client_.CreateListener(dns_protocol::kTypePTR, "_privet._tcp.local", | 454 dns_protocol::kTypePTR, "_privet._tcp.local", &delegate_privet); |
| 520 &delegate_privet); | |
| 521 | 455 |
| 522 ASSERT_TRUE(listener_privet->Start()); | 456 ASSERT_TRUE(listener_privet->Start()); |
| 523 | 457 |
| 524 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) | 458 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) |
| 525 .Times(Exactly(1)) | 459 .Times(Exactly(1)) |
| 526 .WillOnce(Invoke( | 460 .WillOnce( |
| 527 &record_privet, | 461 Invoke(&record_privet, &PtrRecordCopyContainer::SaveWithDummyArg)); |
| 528 &PtrRecordCopyContainer::SaveWithDummyArg)); | |
| 529 | 462 |
| 530 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); | 463 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); |
| 531 | 464 |
| 532 EXPECT_TRUE(record_privet.IsRecordWith("_privet._tcp.local", | 465 EXPECT_TRUE(record_privet.IsRecordWith("_privet._tcp.local", |
| 533 "hello._privet._tcp.local")); | 466 "hello._privet._tcp.local")); |
| 534 | 467 |
| 535 // Expect record is removed when its TTL expires. | 468 // Expect record is removed when its TTL expires. |
| 536 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_REMOVED, _)) | 469 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_REMOVED, _)) |
| 537 .Times(Exactly(1)) | 470 .Times(Exactly(1)) |
| 538 .WillOnce(DoAll(InvokeWithoutArgs(this, &MDnsTest::Stop), | 471 .WillOnce(DoAll( |
| 539 Invoke(&record_privet2, | 472 InvokeWithoutArgs(this, &MDnsTest::Stop), |
| 540 &PtrRecordCopyContainer::SaveWithDummyArg))); | 473 Invoke(&record_privet2, &PtrRecordCopyContainer::SaveWithDummyArg))); |
| 541 | 474 |
| 542 RunFor(base::TimeDelta::FromSeconds(record_privet.ttl() + 1)); | 475 RunFor(base::TimeDelta::FromSeconds(record_privet.ttl() + 1)); |
| 543 | 476 |
| 544 EXPECT_TRUE(record_privet2.IsRecordWith("_privet._tcp.local", | 477 EXPECT_TRUE(record_privet2.IsRecordWith("_privet._tcp.local", |
| 545 "hello._privet._tcp.local")); | 478 "hello._privet._tcp.local")); |
| 546 } | 479 } |
| 547 | 480 |
| 548 TEST_F(MDnsTest, MalformedPacket) { | 481 TEST_F(MDnsTest, MalformedPacket) { |
| 549 StrictMock<MockListenerDelegate> delegate_printer; | 482 StrictMock<MockListenerDelegate> delegate_printer; |
| 550 | 483 |
| 551 PtrRecordCopyContainer record_printer; | 484 PtrRecordCopyContainer record_printer; |
| 552 | 485 |
| 553 scoped_ptr<MDnsListener> listener_printer = | 486 scoped_ptr<MDnsListener> listener_printer = test_client_.CreateListener( |
| 554 test_client_.CreateListener(dns_protocol::kTypePTR, "_printer._tcp.local", | 487 dns_protocol::kTypePTR, "_printer._tcp.local", &delegate_printer); |
| 555 &delegate_printer); | |
| 556 | 488 |
| 557 ASSERT_TRUE(listener_printer->Start()); | 489 ASSERT_TRUE(listener_printer->Start()); |
| 558 | 490 |
| 559 EXPECT_CALL(delegate_printer, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) | 491 EXPECT_CALL(delegate_printer, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) |
| 560 .Times(Exactly(1)) | 492 .Times(Exactly(1)) |
| 561 .WillOnce(Invoke( | 493 .WillOnce( |
| 562 &record_printer, | 494 Invoke(&record_printer, &PtrRecordCopyContainer::SaveWithDummyArg)); |
| 563 &PtrRecordCopyContainer::SaveWithDummyArg)); | |
| 564 | 495 |
| 565 // First, send unsalvagable packet to ensure we can deal with it. | 496 // First, send unsalvagable packet to ensure we can deal with it. |
| 566 SimulatePacketReceive(kCorruptedPacketUnsalvagable, | 497 SimulatePacketReceive(kCorruptedPacketUnsalvagable, |
| 567 sizeof(kCorruptedPacketUnsalvagable)); | 498 sizeof(kCorruptedPacketUnsalvagable)); |
| 568 | 499 |
| 569 // Regression test: send a packet where the question cannot be read. | 500 // Regression test: send a packet where the question cannot be read. |
| 570 SimulatePacketReceive(kCorruptedPacketBadQuestion, | 501 SimulatePacketReceive(kCorruptedPacketBadQuestion, |
| 571 sizeof(kCorruptedPacketBadQuestion)); | 502 sizeof(kCorruptedPacketBadQuestion)); |
| 572 | 503 |
| 573 // Then send salvagable packet to ensure we can extract useful records. | 504 // Then send salvagable packet to ensure we can extract useful records. |
| 574 SimulatePacketReceive(kCorruptedPacketSalvagable, | 505 SimulatePacketReceive(kCorruptedPacketSalvagable, |
| 575 sizeof(kCorruptedPacketSalvagable)); | 506 sizeof(kCorruptedPacketSalvagable)); |
| 576 | 507 |
| 577 EXPECT_TRUE(record_printer.IsRecordWith("_printer._tcp.local", | 508 EXPECT_TRUE(record_printer.IsRecordWith("_printer._tcp.local", |
| 578 "hello._printer._tcp.local")); | 509 "hello._printer._tcp.local")); |
| 579 } | 510 } |
| 580 | 511 |
| 581 TEST_F(MDnsTest, TransactionWithEmptyCache) { | 512 TEST_F(MDnsTest, TransactionWithEmptyCache) { |
| 582 ExpectPacket(kQueryPacketPrivet, sizeof(kQueryPacketPrivet)); | 513 ExpectPacket(kQueryPacketPrivet, sizeof(kQueryPacketPrivet)); |
| 583 | 514 |
| 584 scoped_ptr<MDnsTransaction> transaction_privet = | 515 scoped_ptr<MDnsTransaction> transaction_privet = |
| 585 test_client_.CreateTransaction( | 516 test_client_.CreateTransaction( |
| 586 dns_protocol::kTypePTR, "_privet._tcp.local", | 517 dns_protocol::kTypePTR, |
| 587 MDnsTransaction::QUERY_NETWORK | | 518 "_privet._tcp.local", |
| 588 MDnsTransaction::QUERY_CACHE | | 519 MDnsTransaction::QUERY_NETWORK | MDnsTransaction::QUERY_CACHE | |
| 589 MDnsTransaction::SINGLE_RESULT, | 520 MDnsTransaction::SINGLE_RESULT, |
| 590 base::Bind(&MDnsTest::MockableRecordCallback, | 521 base::Bind(&MDnsTest::MockableRecordCallback, |
| 591 base::Unretained(this))); | 522 base::Unretained(this))); |
| 592 | 523 |
| 593 ASSERT_TRUE(transaction_privet->Start()); | 524 ASSERT_TRUE(transaction_privet->Start()); |
| 594 | 525 |
| 595 PtrRecordCopyContainer record_privet; | 526 PtrRecordCopyContainer record_privet; |
| 596 | 527 |
| 597 EXPECT_CALL(*this, MockableRecordCallback(MDnsTransaction::RESULT_RECORD, _)) | 528 EXPECT_CALL(*this, MockableRecordCallback(MDnsTransaction::RESULT_RECORD, _)) |
| 598 .Times(Exactly(1)) | 529 .Times(Exactly(1)) |
| 599 .WillOnce(Invoke(&record_privet, | 530 .WillOnce( |
| 600 &PtrRecordCopyContainer::SaveWithDummyArg)); | 531 Invoke(&record_privet, &PtrRecordCopyContainer::SaveWithDummyArg)); |
| 601 | 532 |
| 602 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); | 533 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); |
| 603 | 534 |
| 604 EXPECT_TRUE(record_privet.IsRecordWith("_privet._tcp.local", | 535 EXPECT_TRUE(record_privet.IsRecordWith("_privet._tcp.local", |
| 605 "hello._privet._tcp.local")); | 536 "hello._privet._tcp.local")); |
| 606 } | 537 } |
| 607 | 538 |
| 608 TEST_F(MDnsTest, TransactionCacheOnlyNoResult) { | 539 TEST_F(MDnsTest, TransactionCacheOnlyNoResult) { |
| 609 scoped_ptr<MDnsTransaction> transaction_privet = | 540 scoped_ptr<MDnsTransaction> transaction_privet = |
| 610 test_client_.CreateTransaction( | 541 test_client_.CreateTransaction( |
| 611 dns_protocol::kTypePTR, "_privet._tcp.local", | 542 dns_protocol::kTypePTR, |
| 612 MDnsTransaction::QUERY_CACHE | | 543 "_privet._tcp.local", |
| 613 MDnsTransaction::SINGLE_RESULT, | 544 MDnsTransaction::QUERY_CACHE | MDnsTransaction::SINGLE_RESULT, |
| 614 base::Bind(&MDnsTest::MockableRecordCallback, | 545 base::Bind(&MDnsTest::MockableRecordCallback, |
| 615 base::Unretained(this))); | 546 base::Unretained(this))); |
| 616 | 547 |
| 617 EXPECT_CALL(*this, | 548 EXPECT_CALL(*this, |
| 618 MockableRecordCallback(MDnsTransaction::RESULT_NO_RESULTS, _)) | 549 MockableRecordCallback(MDnsTransaction::RESULT_NO_RESULTS, _)) |
| 619 .Times(Exactly(1)); | 550 .Times(Exactly(1)); |
| 620 | 551 |
| 621 ASSERT_TRUE(transaction_privet->Start()); | 552 ASSERT_TRUE(transaction_privet->Start()); |
| 622 } | 553 } |
| 623 | 554 |
| 624 TEST_F(MDnsTest, TransactionWithCache) { | 555 TEST_F(MDnsTest, TransactionWithCache) { |
| 625 // Listener to force the client to listen | 556 // Listener to force the client to listen |
| 626 StrictMock<MockListenerDelegate> delegate_irrelevant; | 557 StrictMock<MockListenerDelegate> delegate_irrelevant; |
| 627 scoped_ptr<MDnsListener> listener_irrelevant = | 558 scoped_ptr<MDnsListener> listener_irrelevant = test_client_.CreateListener( |
| 628 test_client_.CreateListener(dns_protocol::kTypeA, | 559 dns_protocol::kTypeA, "codereview.chromium.local", &delegate_irrelevant); |
| 629 "codereview.chromium.local", | |
| 630 &delegate_irrelevant); | |
| 631 | 560 |
| 632 ASSERT_TRUE(listener_irrelevant->Start()); | 561 ASSERT_TRUE(listener_irrelevant->Start()); |
| 633 | 562 |
| 634 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); | 563 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); |
| 635 | 564 |
| 636 | |
| 637 PtrRecordCopyContainer record_privet; | 565 PtrRecordCopyContainer record_privet; |
| 638 | 566 |
| 639 EXPECT_CALL(*this, MockableRecordCallback(MDnsTransaction::RESULT_RECORD, _)) | 567 EXPECT_CALL(*this, MockableRecordCallback(MDnsTransaction::RESULT_RECORD, _)) |
| 640 .WillOnce(Invoke(&record_privet, | 568 .WillOnce( |
| 641 &PtrRecordCopyContainer::SaveWithDummyArg)); | 569 Invoke(&record_privet, &PtrRecordCopyContainer::SaveWithDummyArg)); |
| 642 | 570 |
| 643 scoped_ptr<MDnsTransaction> transaction_privet = | 571 scoped_ptr<MDnsTransaction> transaction_privet = |
| 644 test_client_.CreateTransaction( | 572 test_client_.CreateTransaction( |
| 645 dns_protocol::kTypePTR, "_privet._tcp.local", | 573 dns_protocol::kTypePTR, |
| 646 MDnsTransaction::QUERY_NETWORK | | 574 "_privet._tcp.local", |
| 647 MDnsTransaction::QUERY_CACHE | | 575 MDnsTransaction::QUERY_NETWORK | MDnsTransaction::QUERY_CACHE | |
| 648 MDnsTransaction::SINGLE_RESULT, | 576 MDnsTransaction::SINGLE_RESULT, |
| 649 base::Bind(&MDnsTest::MockableRecordCallback, | 577 base::Bind(&MDnsTest::MockableRecordCallback, |
| 650 base::Unretained(this))); | 578 base::Unretained(this))); |
| 651 | 579 |
| 652 ASSERT_TRUE(transaction_privet->Start()); | 580 ASSERT_TRUE(transaction_privet->Start()); |
| 653 | 581 |
| 654 EXPECT_TRUE(record_privet.IsRecordWith("_privet._tcp.local", | 582 EXPECT_TRUE(record_privet.IsRecordWith("_privet._tcp.local", |
| 655 "hello._privet._tcp.local")); | 583 "hello._privet._tcp.local")); |
| 656 } | 584 } |
| 657 | 585 |
| 658 TEST_F(MDnsTest, AdditionalRecords) { | 586 TEST_F(MDnsTest, AdditionalRecords) { |
| 659 StrictMock<MockListenerDelegate> delegate_privet; | 587 StrictMock<MockListenerDelegate> delegate_privet; |
| 660 | 588 |
| 661 PtrRecordCopyContainer record_privet; | 589 PtrRecordCopyContainer record_privet; |
| 662 | 590 |
| 663 scoped_ptr<MDnsListener> listener_privet = | 591 scoped_ptr<MDnsListener> listener_privet = test_client_.CreateListener( |
| 664 test_client_.CreateListener(dns_protocol::kTypePTR, "_privet._tcp.local", | 592 dns_protocol::kTypePTR, "_privet._tcp.local", &delegate_privet); |
| 665 &delegate_privet); | |
| 666 | 593 |
| 667 ASSERT_TRUE(listener_privet->Start()); | 594 ASSERT_TRUE(listener_privet->Start()); |
| 668 | 595 |
| 669 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) | 596 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) |
| 670 .Times(Exactly(1)) | 597 .Times(Exactly(1)) |
| 671 .WillOnce(Invoke( | 598 .WillOnce( |
| 672 &record_privet, | 599 Invoke(&record_privet, &PtrRecordCopyContainer::SaveWithDummyArg)); |
| 673 &PtrRecordCopyContainer::SaveWithDummyArg)); | |
| 674 | 600 |
| 675 SimulatePacketReceive(kSamplePacketAdditionalOnly, | 601 SimulatePacketReceive(kSamplePacketAdditionalOnly, |
| 676 sizeof(kSamplePacketAdditionalOnly)); | 602 sizeof(kSamplePacketAdditionalOnly)); |
| 677 | 603 |
| 678 EXPECT_TRUE(record_privet.IsRecordWith("_privet._tcp.local", | 604 EXPECT_TRUE(record_privet.IsRecordWith("_privet._tcp.local", |
| 679 "hello._privet._tcp.local")); | 605 "hello._privet._tcp.local")); |
| 680 } | 606 } |
| 681 | 607 |
| 682 TEST_F(MDnsTest, TransactionTimeout) { | 608 TEST_F(MDnsTest, TransactionTimeout) { |
| 683 ExpectPacket(kQueryPacketPrivet, sizeof(kQueryPacketPrivet)); | 609 ExpectPacket(kQueryPacketPrivet, sizeof(kQueryPacketPrivet)); |
| 684 | 610 |
| 685 scoped_ptr<MDnsTransaction> transaction_privet = | 611 scoped_ptr<MDnsTransaction> transaction_privet = |
| 686 test_client_.CreateTransaction( | 612 test_client_.CreateTransaction( |
| 687 dns_protocol::kTypePTR, "_privet._tcp.local", | 613 dns_protocol::kTypePTR, |
| 688 MDnsTransaction::QUERY_NETWORK | | 614 "_privet._tcp.local", |
| 689 MDnsTransaction::QUERY_CACHE | | 615 MDnsTransaction::QUERY_NETWORK | MDnsTransaction::QUERY_CACHE | |
| 690 MDnsTransaction::SINGLE_RESULT, | 616 MDnsTransaction::SINGLE_RESULT, |
| 691 base::Bind(&MDnsTest::MockableRecordCallback, | 617 base::Bind(&MDnsTest::MockableRecordCallback, |
| 692 base::Unretained(this))); | 618 base::Unretained(this))); |
| 693 | 619 |
| 694 ASSERT_TRUE(transaction_privet->Start()); | 620 ASSERT_TRUE(transaction_privet->Start()); |
| 695 | 621 |
| 696 EXPECT_CALL(*this, | 622 EXPECT_CALL(*this, |
| 697 MockableRecordCallback(MDnsTransaction::RESULT_NO_RESULTS, NULL)) | 623 MockableRecordCallback(MDnsTransaction::RESULT_NO_RESULTS, NULL)) |
| 698 .Times(Exactly(1)) | 624 .Times(Exactly(1)) |
| 699 .WillOnce(InvokeWithoutArgs(this, &MDnsTest::Stop)); | 625 .WillOnce(InvokeWithoutArgs(this, &MDnsTest::Stop)); |
| 700 | 626 |
| 701 RunFor(base::TimeDelta::FromSeconds(4)); | 627 RunFor(base::TimeDelta::FromSeconds(4)); |
| 702 } | 628 } |
| 703 | 629 |
| 704 TEST_F(MDnsTest, TransactionMultipleRecords) { | 630 TEST_F(MDnsTest, TransactionMultipleRecords) { |
| 705 ExpectPacket(kQueryPacketPrivet, sizeof(kQueryPacketPrivet)); | 631 ExpectPacket(kQueryPacketPrivet, sizeof(kQueryPacketPrivet)); |
| 706 | 632 |
| 707 scoped_ptr<MDnsTransaction> transaction_privet = | 633 scoped_ptr<MDnsTransaction> transaction_privet = |
| 708 test_client_.CreateTransaction( | 634 test_client_.CreateTransaction( |
| 709 dns_protocol::kTypePTR, "_privet._tcp.local", | 635 dns_protocol::kTypePTR, |
| 710 MDnsTransaction::QUERY_NETWORK | | 636 "_privet._tcp.local", |
| 711 MDnsTransaction::QUERY_CACHE , | 637 MDnsTransaction::QUERY_NETWORK | MDnsTransaction::QUERY_CACHE, |
| 712 base::Bind(&MDnsTest::MockableRecordCallback, | 638 base::Bind(&MDnsTest::MockableRecordCallback, |
| 713 base::Unretained(this))); | 639 base::Unretained(this))); |
| 714 | 640 |
| 715 ASSERT_TRUE(transaction_privet->Start()); | 641 ASSERT_TRUE(transaction_privet->Start()); |
| 716 | 642 |
| 717 PtrRecordCopyContainer record_privet; | 643 PtrRecordCopyContainer record_privet; |
| 718 PtrRecordCopyContainer record_privet2; | 644 PtrRecordCopyContainer record_privet2; |
| 719 | 645 |
| 720 EXPECT_CALL(*this, MockableRecordCallback(MDnsTransaction::RESULT_RECORD, _)) | 646 EXPECT_CALL(*this, MockableRecordCallback(MDnsTransaction::RESULT_RECORD, _)) |
| 721 .Times(Exactly(2)) | 647 .Times(Exactly(2)) |
| 722 .WillOnce(Invoke(&record_privet, | 648 .WillOnce( |
| 723 &PtrRecordCopyContainer::SaveWithDummyArg)) | 649 Invoke(&record_privet, &PtrRecordCopyContainer::SaveWithDummyArg)) |
| 724 .WillOnce(Invoke(&record_privet2, | 650 .WillOnce( |
| 725 &PtrRecordCopyContainer::SaveWithDummyArg)); | 651 Invoke(&record_privet2, &PtrRecordCopyContainer::SaveWithDummyArg)); |
| 726 | 652 |
| 727 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); | 653 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); |
| 728 SimulatePacketReceive(kSamplePacket2, sizeof(kSamplePacket2)); | 654 SimulatePacketReceive(kSamplePacket2, sizeof(kSamplePacket2)); |
| 729 | 655 |
| 730 EXPECT_TRUE(record_privet.IsRecordWith("_privet._tcp.local", | 656 EXPECT_TRUE(record_privet.IsRecordWith("_privet._tcp.local", |
| 731 "hello._privet._tcp.local")); | 657 "hello._privet._tcp.local")); |
| 732 | 658 |
| 733 EXPECT_TRUE(record_privet2.IsRecordWith("_privet._tcp.local", | 659 EXPECT_TRUE(record_privet2.IsRecordWith("_privet._tcp.local", |
| 734 "zzzzz._privet._tcp.local")); | 660 "zzzzz._privet._tcp.local")); |
| 735 | 661 |
| 736 EXPECT_CALL(*this, MockableRecordCallback(MDnsTransaction::RESULT_DONE, NULL)) | 662 EXPECT_CALL(*this, MockableRecordCallback(MDnsTransaction::RESULT_DONE, NULL)) |
| 737 .WillOnce(InvokeWithoutArgs(this, &MDnsTest::Stop)); | 663 .WillOnce(InvokeWithoutArgs(this, &MDnsTest::Stop)); |
| 738 | 664 |
| 739 RunFor(base::TimeDelta::FromSeconds(4)); | 665 RunFor(base::TimeDelta::FromSeconds(4)); |
| 740 } | 666 } |
| 741 | 667 |
| 742 TEST_F(MDnsTest, TransactionReentrantDelete) { | 668 TEST_F(MDnsTest, TransactionReentrantDelete) { |
| 743 ExpectPacket(kQueryPacketPrivet, sizeof(kQueryPacketPrivet)); | 669 ExpectPacket(kQueryPacketPrivet, sizeof(kQueryPacketPrivet)); |
| 744 | 670 |
| 745 transaction_ = test_client_.CreateTransaction( | 671 transaction_ = test_client_.CreateTransaction( |
| 746 dns_protocol::kTypePTR, "_privet._tcp.local", | 672 dns_protocol::kTypePTR, |
| 747 MDnsTransaction::QUERY_NETWORK | | 673 "_privet._tcp.local", |
| 748 MDnsTransaction::QUERY_CACHE | | 674 MDnsTransaction::QUERY_NETWORK | MDnsTransaction::QUERY_CACHE | |
| 749 MDnsTransaction::SINGLE_RESULT, | 675 MDnsTransaction::SINGLE_RESULT, |
| 750 base::Bind(&MDnsTest::MockableRecordCallback, | 676 base::Bind(&MDnsTest::MockableRecordCallback, base::Unretained(this))); |
| 751 base::Unretained(this))); | |
| 752 | 677 |
| 753 ASSERT_TRUE(transaction_->Start()); | 678 ASSERT_TRUE(transaction_->Start()); |
| 754 | 679 |
| 755 EXPECT_CALL(*this, MockableRecordCallback(MDnsTransaction::RESULT_NO_RESULTS, | 680 EXPECT_CALL(*this, |
| 756 NULL)) | 681 MockableRecordCallback(MDnsTransaction::RESULT_NO_RESULTS, NULL)) |
| 757 .Times(Exactly(1)) | 682 .Times(Exactly(1)) |
| 758 .WillOnce(DoAll(InvokeWithoutArgs(this, &MDnsTest::DeleteTransaction), | 683 .WillOnce(DoAll(InvokeWithoutArgs(this, &MDnsTest::DeleteTransaction), |
| 759 InvokeWithoutArgs(this, &MDnsTest::Stop))); | 684 InvokeWithoutArgs(this, &MDnsTest::Stop))); |
| 760 | 685 |
| 761 RunFor(base::TimeDelta::FromSeconds(4)); | 686 RunFor(base::TimeDelta::FromSeconds(4)); |
| 762 | 687 |
| 763 EXPECT_EQ(NULL, transaction_.get()); | 688 EXPECT_EQ(NULL, transaction_.get()); |
| 764 } | 689 } |
| 765 | 690 |
| 766 TEST_F(MDnsTest, TransactionReentrantDeleteFromCache) { | 691 TEST_F(MDnsTest, TransactionReentrantDeleteFromCache) { |
| 767 StrictMock<MockListenerDelegate> delegate_irrelevant; | 692 StrictMock<MockListenerDelegate> delegate_irrelevant; |
| 768 scoped_ptr<MDnsListener> listener_irrelevant = test_client_.CreateListener( | 693 scoped_ptr<MDnsListener> listener_irrelevant = test_client_.CreateListener( |
| 769 dns_protocol::kTypeA, "codereview.chromium.local", | 694 dns_protocol::kTypeA, "codereview.chromium.local", &delegate_irrelevant); |
| 770 &delegate_irrelevant); | |
| 771 ASSERT_TRUE(listener_irrelevant->Start()); | 695 ASSERT_TRUE(listener_irrelevant->Start()); |
| 772 | 696 |
| 773 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); | 697 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); |
| 774 | 698 |
| 775 transaction_ = test_client_.CreateTransaction( | 699 transaction_ = test_client_.CreateTransaction( |
| 776 dns_protocol::kTypePTR, "_privet._tcp.local", | 700 dns_protocol::kTypePTR, |
| 777 MDnsTransaction::QUERY_NETWORK | | 701 "_privet._tcp.local", |
| 778 MDnsTransaction::QUERY_CACHE, | 702 MDnsTransaction::QUERY_NETWORK | MDnsTransaction::QUERY_CACHE, |
| 779 base::Bind(&MDnsTest::MockableRecordCallback, | 703 base::Bind(&MDnsTest::MockableRecordCallback, base::Unretained(this))); |
| 780 base::Unretained(this))); | |
| 781 | 704 |
| 782 EXPECT_CALL(*this, MockableRecordCallback(MDnsTransaction::RESULT_RECORD, _)) | 705 EXPECT_CALL(*this, MockableRecordCallback(MDnsTransaction::RESULT_RECORD, _)) |
| 783 .Times(Exactly(1)) | 706 .Times(Exactly(1)) |
| 784 .WillOnce(InvokeWithoutArgs(this, &MDnsTest::DeleteTransaction)); | 707 .WillOnce(InvokeWithoutArgs(this, &MDnsTest::DeleteTransaction)); |
| 785 | 708 |
| 786 ASSERT_TRUE(transaction_->Start()); | 709 ASSERT_TRUE(transaction_->Start()); |
| 787 | 710 |
| 788 EXPECT_EQ(NULL, transaction_.get()); | 711 EXPECT_EQ(NULL, transaction_.get()); |
| 789 } | 712 } |
| 790 | 713 |
| 791 TEST_F(MDnsTest, TransactionReentrantCacheLookupStart) { | 714 TEST_F(MDnsTest, TransactionReentrantCacheLookupStart) { |
| 792 ExpectPacket(kQueryPacketPrivet, sizeof(kQueryPacketPrivet)); | 715 ExpectPacket(kQueryPacketPrivet, sizeof(kQueryPacketPrivet)); |
| 793 | 716 |
| 794 scoped_ptr<MDnsTransaction> transaction1 = | 717 scoped_ptr<MDnsTransaction> transaction1 = test_client_.CreateTransaction( |
| 795 test_client_.CreateTransaction( | 718 dns_protocol::kTypePTR, |
| 796 dns_protocol::kTypePTR, "_privet._tcp.local", | 719 "_privet._tcp.local", |
| 797 MDnsTransaction::QUERY_NETWORK | | 720 MDnsTransaction::QUERY_NETWORK | MDnsTransaction::QUERY_CACHE | |
| 798 MDnsTransaction::QUERY_CACHE | | |
| 799 MDnsTransaction::SINGLE_RESULT, | 721 MDnsTransaction::SINGLE_RESULT, |
| 800 base::Bind(&MDnsTest::MockableRecordCallback, | 722 base::Bind(&MDnsTest::MockableRecordCallback, base::Unretained(this))); |
| 801 base::Unretained(this))); | |
| 802 | 723 |
| 803 scoped_ptr<MDnsTransaction> transaction2 = | 724 scoped_ptr<MDnsTransaction> transaction2 = test_client_.CreateTransaction( |
| 804 test_client_.CreateTransaction( | 725 dns_protocol::kTypePTR, |
| 805 dns_protocol::kTypePTR, "_printer._tcp.local", | 726 "_printer._tcp.local", |
| 806 MDnsTransaction::QUERY_CACHE | | 727 MDnsTransaction::QUERY_CACHE | MDnsTransaction::SINGLE_RESULT, |
| 807 MDnsTransaction::SINGLE_RESULT, | 728 base::Bind(&MDnsTest::MockableRecordCallback2, base::Unretained(this))); |
| 808 base::Bind(&MDnsTest::MockableRecordCallback2, | |
| 809 base::Unretained(this))); | |
| 810 | 729 |
| 811 EXPECT_CALL(*this, MockableRecordCallback2(MDnsTransaction::RESULT_RECORD, | 730 EXPECT_CALL(*this, MockableRecordCallback2(MDnsTransaction::RESULT_RECORD, _)) |
| 812 _)) | |
| 813 .Times(Exactly(1)); | 731 .Times(Exactly(1)); |
| 814 | 732 |
| 815 EXPECT_CALL(*this, MockableRecordCallback(MDnsTransaction::RESULT_RECORD, | 733 EXPECT_CALL(*this, MockableRecordCallback(MDnsTransaction::RESULT_RECORD, _)) |
| 816 _)) | |
| 817 .Times(Exactly(1)) | 734 .Times(Exactly(1)) |
| 818 .WillOnce(IgnoreResult(InvokeWithoutArgs(transaction2.get(), | 735 .WillOnce(IgnoreResult( |
| 819 &MDnsTransaction::Start))); | 736 InvokeWithoutArgs(transaction2.get(), &MDnsTransaction::Start))); |
| 820 | 737 |
| 821 ASSERT_TRUE(transaction1->Start()); | 738 ASSERT_TRUE(transaction1->Start()); |
| 822 | 739 |
| 823 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); | 740 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); |
| 824 } | 741 } |
| 825 | 742 |
| 826 TEST_F(MDnsTest, GoodbyePacketNotification) { | 743 TEST_F(MDnsTest, GoodbyePacketNotification) { |
| 827 StrictMock<MockListenerDelegate> delegate_privet; | 744 StrictMock<MockListenerDelegate> delegate_privet; |
| 828 | 745 |
| 829 scoped_ptr<MDnsListener> listener_privet = test_client_.CreateListener( | 746 scoped_ptr<MDnsListener> listener_privet = test_client_.CreateListener( |
| 830 dns_protocol::kTypePTR, "_privet._tcp.local", &delegate_privet); | 747 dns_protocol::kTypePTR, "_privet._tcp.local", &delegate_privet); |
| 831 ASSERT_TRUE(listener_privet->Start()); | 748 ASSERT_TRUE(listener_privet->Start()); |
| 832 | 749 |
| 833 SimulatePacketReceive(kSamplePacketGoodbye, sizeof(kSamplePacketGoodbye)); | 750 SimulatePacketReceive(kSamplePacketGoodbye, sizeof(kSamplePacketGoodbye)); |
| 834 | 751 |
| 835 RunFor(base::TimeDelta::FromSeconds(2)); | 752 RunFor(base::TimeDelta::FromSeconds(2)); |
| 836 } | 753 } |
| 837 | 754 |
| 838 TEST_F(MDnsTest, GoodbyePacketRemoval) { | 755 TEST_F(MDnsTest, GoodbyePacketRemoval) { |
| 839 StrictMock<MockListenerDelegate> delegate_privet; | 756 StrictMock<MockListenerDelegate> delegate_privet; |
| 840 | 757 |
| 841 scoped_ptr<MDnsListener> listener_privet = | 758 scoped_ptr<MDnsListener> listener_privet = test_client_.CreateListener( |
| 842 test_client_.CreateListener(dns_protocol::kTypePTR, "_privet._tcp.local", | 759 dns_protocol::kTypePTR, "_privet._tcp.local", &delegate_privet); |
| 843 &delegate_privet); | |
| 844 ASSERT_TRUE(listener_privet->Start()); | 760 ASSERT_TRUE(listener_privet->Start()); |
| 845 | 761 |
| 846 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) | 762 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) |
| 847 .Times(Exactly(1)); | 763 .Times(Exactly(1)); |
| 848 | 764 |
| 849 SimulatePacketReceive(kSamplePacket2, sizeof(kSamplePacket2)); | 765 SimulatePacketReceive(kSamplePacket2, sizeof(kSamplePacket2)); |
| 850 | 766 |
| 851 SimulatePacketReceive(kSamplePacketGoodbye, sizeof(kSamplePacketGoodbye)); | 767 SimulatePacketReceive(kSamplePacketGoodbye, sizeof(kSamplePacketGoodbye)); |
| 852 | 768 |
| 853 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_REMOVED, _)) | 769 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_REMOVED, _)) |
| 854 .Times(Exactly(1)); | 770 .Times(Exactly(1)); |
| 855 | 771 |
| 856 RunFor(base::TimeDelta::FromSeconds(2)); | 772 RunFor(base::TimeDelta::FromSeconds(2)); |
| 857 } | 773 } |
| 858 | 774 |
| 859 // In order to reliably test reentrant listener deletes, we create two listeners | 775 // In order to reliably test reentrant listener deletes, we create two listeners |
| 860 // and have each of them delete both, so we're guaranteed to try and deliver a | 776 // and have each of them delete both, so we're guaranteed to try and deliver a |
| 861 // callback to at least one deleted listener. | 777 // callback to at least one deleted listener. |
| 862 | 778 |
| 863 TEST_F(MDnsTest, ListenerReentrantDelete) { | 779 TEST_F(MDnsTest, ListenerReentrantDelete) { |
| 864 StrictMock<MockListenerDelegate> delegate_privet; | 780 StrictMock<MockListenerDelegate> delegate_privet; |
| 865 | 781 |
| 866 listener1_ = test_client_.CreateListener(dns_protocol::kTypePTR, | 782 listener1_ = test_client_.CreateListener( |
| 867 "_privet._tcp.local", | 783 dns_protocol::kTypePTR, "_privet._tcp.local", &delegate_privet); |
| 868 &delegate_privet); | |
| 869 | 784 |
| 870 listener2_ = test_client_.CreateListener(dns_protocol::kTypePTR, | 785 listener2_ = test_client_.CreateListener( |
| 871 "_privet._tcp.local", | 786 dns_protocol::kTypePTR, "_privet._tcp.local", &delegate_privet); |
| 872 &delegate_privet); | |
| 873 | 787 |
| 874 ASSERT_TRUE(listener1_->Start()); | 788 ASSERT_TRUE(listener1_->Start()); |
| 875 | 789 |
| 876 ASSERT_TRUE(listener2_->Start()); | 790 ASSERT_TRUE(listener2_->Start()); |
| 877 | 791 |
| 878 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) | 792 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) |
| 879 .Times(Exactly(1)) | 793 .Times(Exactly(1)) |
| 880 .WillOnce(InvokeWithoutArgs(this, &MDnsTest::DeleteBothListeners)); | 794 .WillOnce(InvokeWithoutArgs(this, &MDnsTest::DeleteBothListeners)); |
| 881 | 795 |
| 882 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); | 796 SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1)); |
| 883 | 797 |
| 884 EXPECT_EQ(NULL, listener1_.get()); | 798 EXPECT_EQ(NULL, listener1_.get()); |
| 885 EXPECT_EQ(NULL, listener2_.get()); | 799 EXPECT_EQ(NULL, listener2_.get()); |
| 886 } | 800 } |
| 887 | 801 |
| 888 ACTION_P(SaveIPAddress, ip_container) { | 802 ACTION_P(SaveIPAddress, ip_container) { |
| 889 ::testing::StaticAssertTypeEq<const RecordParsed*, arg1_type>(); | 803 ::testing::StaticAssertTypeEq<const RecordParsed*, arg1_type>(); |
| 890 ::testing::StaticAssertTypeEq<IPAddressNumber*, ip_container_type>(); | 804 ::testing::StaticAssertTypeEq<IPAddressNumber*, ip_container_type>(); |
| 891 | 805 |
| 892 *ip_container = arg1->template rdata<ARecordRdata>()->address(); | 806 *ip_container = arg1->template rdata<ARecordRdata>()->address(); |
| 893 } | 807 } |
| 894 | 808 |
| 895 TEST_F(MDnsTest, DoubleRecordDisagreeing) { | 809 TEST_F(MDnsTest, DoubleRecordDisagreeing) { |
| 896 IPAddressNumber address; | 810 IPAddressNumber address; |
| 897 StrictMock<MockListenerDelegate> delegate_privet; | 811 StrictMock<MockListenerDelegate> delegate_privet; |
| 898 | 812 |
| 899 scoped_ptr<MDnsListener> listener_privet = | 813 scoped_ptr<MDnsListener> listener_privet = test_client_.CreateListener( |
| 900 test_client_.CreateListener(dns_protocol::kTypeA, "privet.local", | 814 dns_protocol::kTypeA, "privet.local", &delegate_privet); |
| 901 &delegate_privet); | |
| 902 | 815 |
| 903 ASSERT_TRUE(listener_privet->Start()); | 816 ASSERT_TRUE(listener_privet->Start()); |
| 904 | 817 |
| 905 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) | 818 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) |
| 906 .Times(Exactly(1)) | 819 .Times(Exactly(1)) |
| 907 .WillOnce(SaveIPAddress(&address)); | 820 .WillOnce(SaveIPAddress(&address)); |
| 908 | 821 |
| 909 SimulatePacketReceive(kCorruptedPacketDoubleRecord, | 822 SimulatePacketReceive(kCorruptedPacketDoubleRecord, |
| 910 sizeof(kCorruptedPacketDoubleRecord)); | 823 sizeof(kCorruptedPacketDoubleRecord)); |
| 911 | 824 |
| 912 EXPECT_EQ("2.3.4.5", IPAddressToString(address)); | 825 EXPECT_EQ("2.3.4.5", IPAddressToString(address)); |
| 913 } | 826 } |
| 914 | 827 |
| 915 TEST_F(MDnsTest, NsecWithListener) { | 828 TEST_F(MDnsTest, NsecWithListener) { |
| 916 StrictMock<MockListenerDelegate> delegate_privet; | 829 StrictMock<MockListenerDelegate> delegate_privet; |
| 917 scoped_ptr<MDnsListener> listener_privet = | 830 scoped_ptr<MDnsListener> listener_privet = test_client_.CreateListener( |
| 918 test_client_.CreateListener(dns_protocol::kTypeA, "_privet._tcp.local", | 831 dns_protocol::kTypeA, "_privet._tcp.local", &delegate_privet); |
| 919 &delegate_privet); | |
| 920 | 832 |
| 921 // Test to make sure nsec callback is NOT called for PTR | 833 // Test to make sure nsec callback is NOT called for PTR |
| 922 // (which is marked as existing). | 834 // (which is marked as existing). |
| 923 StrictMock<MockListenerDelegate> delegate_privet2; | 835 StrictMock<MockListenerDelegate> delegate_privet2; |
| 924 scoped_ptr<MDnsListener> listener_privet2 = | 836 scoped_ptr<MDnsListener> listener_privet2 = test_client_.CreateListener( |
| 925 test_client_.CreateListener(dns_protocol::kTypePTR, "_privet._tcp.local", | 837 dns_protocol::kTypePTR, "_privet._tcp.local", &delegate_privet2); |
| 926 &delegate_privet2); | |
| 927 | 838 |
| 928 ASSERT_TRUE(listener_privet->Start()); | 839 ASSERT_TRUE(listener_privet->Start()); |
| 929 | 840 |
| 930 EXPECT_CALL(delegate_privet, | 841 EXPECT_CALL(delegate_privet, |
| 931 OnNsecRecord("_privet._tcp.local", dns_protocol::kTypeA)); | 842 OnNsecRecord("_privet._tcp.local", dns_protocol::kTypeA)); |
| 932 | 843 |
| 933 SimulatePacketReceive(kSamplePacketNsec, | 844 SimulatePacketReceive(kSamplePacketNsec, sizeof(kSamplePacketNsec)); |
| 934 sizeof(kSamplePacketNsec)); | |
| 935 } | 845 } |
| 936 | 846 |
| 937 TEST_F(MDnsTest, NsecWithTransactionFromNetwork) { | 847 TEST_F(MDnsTest, NsecWithTransactionFromNetwork) { |
| 938 scoped_ptr<MDnsTransaction> transaction_privet = | 848 scoped_ptr<MDnsTransaction> transaction_privet = |
| 939 test_client_.CreateTransaction( | 849 test_client_.CreateTransaction( |
| 940 dns_protocol::kTypeA, "_privet._tcp.local", | 850 dns_protocol::kTypeA, |
| 941 MDnsTransaction::QUERY_NETWORK | | 851 "_privet._tcp.local", |
| 942 MDnsTransaction::QUERY_CACHE | | 852 MDnsTransaction::QUERY_NETWORK | MDnsTransaction::QUERY_CACHE | |
| 943 MDnsTransaction::SINGLE_RESULT, | 853 MDnsTransaction::SINGLE_RESULT, |
| 944 base::Bind(&MDnsTest::MockableRecordCallback, | 854 base::Bind(&MDnsTest::MockableRecordCallback, |
| 945 base::Unretained(this))); | 855 base::Unretained(this))); |
| 946 | 856 |
| 947 EXPECT_CALL(socket_factory_, OnSendTo(_)).Times(2); | 857 EXPECT_CALL(socket_factory_, OnSendTo(_)).Times(2); |
| 948 | 858 |
| 949 ASSERT_TRUE(transaction_privet->Start()); | 859 ASSERT_TRUE(transaction_privet->Start()); |
| 950 | 860 |
| 951 EXPECT_CALL(*this, | 861 EXPECT_CALL(*this, |
| 952 MockableRecordCallback(MDnsTransaction::RESULT_NSEC, NULL)); | 862 MockableRecordCallback(MDnsTransaction::RESULT_NSEC, NULL)); |
| 953 | 863 |
| 954 SimulatePacketReceive(kSamplePacketNsec, | 864 SimulatePacketReceive(kSamplePacketNsec, sizeof(kSamplePacketNsec)); |
| 955 sizeof(kSamplePacketNsec)); | |
| 956 } | 865 } |
| 957 | 866 |
| 958 TEST_F(MDnsTest, NsecWithTransactionFromCache) { | 867 TEST_F(MDnsTest, NsecWithTransactionFromCache) { |
| 959 // Force mDNS to listen. | 868 // Force mDNS to listen. |
| 960 StrictMock<MockListenerDelegate> delegate_irrelevant; | 869 StrictMock<MockListenerDelegate> delegate_irrelevant; |
| 961 scoped_ptr<MDnsListener> listener_irrelevant = | 870 scoped_ptr<MDnsListener> listener_irrelevant = test_client_.CreateListener( |
| 962 test_client_.CreateListener(dns_protocol::kTypePTR, "_privet._tcp.local", | 871 dns_protocol::kTypePTR, "_privet._tcp.local", &delegate_irrelevant); |
| 963 &delegate_irrelevant); | |
| 964 listener_irrelevant->Start(); | 872 listener_irrelevant->Start(); |
| 965 | 873 |
| 966 SimulatePacketReceive(kSamplePacketNsec, | 874 SimulatePacketReceive(kSamplePacketNsec, sizeof(kSamplePacketNsec)); |
| 967 sizeof(kSamplePacketNsec)); | |
| 968 | 875 |
| 969 EXPECT_CALL(*this, | 876 EXPECT_CALL(*this, |
| 970 MockableRecordCallback(MDnsTransaction::RESULT_NSEC, NULL)); | 877 MockableRecordCallback(MDnsTransaction::RESULT_NSEC, NULL)); |
| 971 | 878 |
| 972 scoped_ptr<MDnsTransaction> transaction_privet_a = | 879 scoped_ptr<MDnsTransaction> transaction_privet_a = |
| 973 test_client_.CreateTransaction( | 880 test_client_.CreateTransaction( |
| 974 dns_protocol::kTypeA, "_privet._tcp.local", | 881 dns_protocol::kTypeA, |
| 975 MDnsTransaction::QUERY_NETWORK | | 882 "_privet._tcp.local", |
| 976 MDnsTransaction::QUERY_CACHE | | 883 MDnsTransaction::QUERY_NETWORK | MDnsTransaction::QUERY_CACHE | |
| 977 MDnsTransaction::SINGLE_RESULT, | 884 MDnsTransaction::SINGLE_RESULT, |
| 978 base::Bind(&MDnsTest::MockableRecordCallback, | 885 base::Bind(&MDnsTest::MockableRecordCallback, |
| 979 base::Unretained(this))); | 886 base::Unretained(this))); |
| 980 | 887 |
| 981 ASSERT_TRUE(transaction_privet_a->Start()); | 888 ASSERT_TRUE(transaction_privet_a->Start()); |
| 982 | 889 |
| 983 // Test that a PTR transaction does NOT consider the same NSEC record to be a | 890 // Test that a PTR transaction does NOT consider the same NSEC record to be a |
| 984 // valid answer to the query | 891 // valid answer to the query |
| 985 | 892 |
| 986 scoped_ptr<MDnsTransaction> transaction_privet_ptr = | 893 scoped_ptr<MDnsTransaction> transaction_privet_ptr = |
| 987 test_client_.CreateTransaction( | 894 test_client_.CreateTransaction( |
| 988 dns_protocol::kTypePTR, "_privet._tcp.local", | 895 dns_protocol::kTypePTR, |
| 989 MDnsTransaction::QUERY_NETWORK | | 896 "_privet._tcp.local", |
| 990 MDnsTransaction::QUERY_CACHE | | 897 MDnsTransaction::QUERY_NETWORK | MDnsTransaction::QUERY_CACHE | |
| 991 MDnsTransaction::SINGLE_RESULT, | 898 MDnsTransaction::SINGLE_RESULT, |
| 992 base::Bind(&MDnsTest::MockableRecordCallback, | 899 base::Bind(&MDnsTest::MockableRecordCallback, |
| 993 base::Unretained(this))); | 900 base::Unretained(this))); |
| 994 | 901 |
| 995 EXPECT_CALL(socket_factory_, OnSendTo(_)).Times(2); | 902 EXPECT_CALL(socket_factory_, OnSendTo(_)).Times(2); |
| 996 | 903 |
| 997 ASSERT_TRUE(transaction_privet_ptr->Start()); | 904 ASSERT_TRUE(transaction_privet_ptr->Start()); |
| 998 } | 905 } |
| 999 | 906 |
| 1000 TEST_F(MDnsTest, NsecConflictRemoval) { | 907 TEST_F(MDnsTest, NsecConflictRemoval) { |
| 1001 StrictMock<MockListenerDelegate> delegate_privet; | 908 StrictMock<MockListenerDelegate> delegate_privet; |
| 1002 scoped_ptr<MDnsListener> listener_privet = | 909 scoped_ptr<MDnsListener> listener_privet = test_client_.CreateListener( |
| 1003 test_client_.CreateListener(dns_protocol::kTypeA, "_privet._tcp.local", | 910 dns_protocol::kTypeA, "_privet._tcp.local", &delegate_privet); |
| 1004 &delegate_privet); | |
| 1005 | 911 |
| 1006 ASSERT_TRUE(listener_privet->Start()); | 912 ASSERT_TRUE(listener_privet->Start()); |
| 1007 | 913 |
| 1008 const RecordParsed* record1; | 914 const RecordParsed* record1; |
| 1009 const RecordParsed* record2; | 915 const RecordParsed* record2; |
| 1010 | 916 |
| 1011 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) | 917 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)) |
| 1012 .WillOnce(SaveArg<1>(&record1)); | 918 .WillOnce(SaveArg<1>(&record1)); |
| 1013 | 919 |
| 1014 SimulatePacketReceive(kSamplePacketAPrivet, | 920 SimulatePacketReceive(kSamplePacketAPrivet, sizeof(kSamplePacketAPrivet)); |
| 1015 sizeof(kSamplePacketAPrivet)); | |
| 1016 | 921 |
| 1017 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_REMOVED, _)) | 922 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_REMOVED, _)) |
| 1018 .WillOnce(SaveArg<1>(&record2)); | 923 .WillOnce(SaveArg<1>(&record2)); |
| 1019 | 924 |
| 1020 EXPECT_CALL(delegate_privet, | 925 EXPECT_CALL(delegate_privet, |
| 1021 OnNsecRecord("_privet._tcp.local", dns_protocol::kTypeA)); | 926 OnNsecRecord("_privet._tcp.local", dns_protocol::kTypeA)); |
| 1022 | 927 |
| 1023 SimulatePacketReceive(kSamplePacketNsec, | 928 SimulatePacketReceive(kSamplePacketNsec, sizeof(kSamplePacketNsec)); |
| 1024 sizeof(kSamplePacketNsec)); | |
| 1025 | 929 |
| 1026 EXPECT_EQ(record1, record2); | 930 EXPECT_EQ(record1, record2); |
| 1027 } | 931 } |
| 1028 | 932 |
| 1029 | |
| 1030 TEST_F(MDnsTest, RefreshQuery) { | 933 TEST_F(MDnsTest, RefreshQuery) { |
| 1031 StrictMock<MockListenerDelegate> delegate_privet; | 934 StrictMock<MockListenerDelegate> delegate_privet; |
| 1032 scoped_ptr<MDnsListener> listener_privet = | 935 scoped_ptr<MDnsListener> listener_privet = test_client_.CreateListener( |
| 1033 test_client_.CreateListener(dns_protocol::kTypeA, "_privet._tcp.local", | 936 dns_protocol::kTypeA, "_privet._tcp.local", &delegate_privet); |
| 1034 &delegate_privet); | |
| 1035 | 937 |
| 1036 listener_privet->SetActiveRefresh(true); | 938 listener_privet->SetActiveRefresh(true); |
| 1037 ASSERT_TRUE(listener_privet->Start()); | 939 ASSERT_TRUE(listener_privet->Start()); |
| 1038 | 940 |
| 1039 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)); | 941 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _)); |
| 1040 | 942 |
| 1041 SimulatePacketReceive(kSamplePacketAPrivet, | 943 SimulatePacketReceive(kSamplePacketAPrivet, sizeof(kSamplePacketAPrivet)); |
| 1042 sizeof(kSamplePacketAPrivet)); | |
| 1043 | 944 |
| 1044 // Expecting 2 calls (one for ipv4 and one for ipv6) for each of the 2 | 945 // Expecting 2 calls (one for ipv4 and one for ipv6) for each of the 2 |
| 1045 // scheduled refresh queries. | 946 // scheduled refresh queries. |
| 1046 EXPECT_CALL(socket_factory_, OnSendTo( | 947 EXPECT_CALL(socket_factory_, |
| 1047 MakeString(kQueryPacketPrivetA, sizeof(kQueryPacketPrivetA)))) | 948 OnSendTo(MakeString(kQueryPacketPrivetA, |
| 1048 .Times(4); | 949 sizeof(kQueryPacketPrivetA)))).Times(4); |
| 1049 | 950 |
| 1050 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_REMOVED, _)); | 951 EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_REMOVED, _)); |
| 1051 | 952 |
| 1052 RunFor(base::TimeDelta::FromSeconds(6)); | 953 RunFor(base::TimeDelta::FromSeconds(6)); |
| 1053 } | 954 } |
| 1054 | 955 |
| 1055 // Note: These tests assume that the ipv4 socket will always be created first. | 956 // Note: These tests assume that the ipv4 socket will always be created first. |
| 1056 // This is a simplifying assumption based on the way the code works now. | 957 // This is a simplifying assumption based on the way the code works now. |
| 1057 class SimpleMockSocketFactory : public MDnsSocketFactory { | 958 class SimpleMockSocketFactory : public MDnsSocketFactory { |
| 1058 public: | 959 public: |
| 1059 virtual void CreateSockets( | 960 virtual void CreateSockets( |
| 1060 ScopedVector<DatagramServerSocket>* sockets) OVERRIDE { | 961 ScopedVector<DatagramServerSocket>* sockets) OVERRIDE { |
| 1061 sockets->clear(); | 962 sockets->clear(); |
| 1062 sockets->swap(sockets_); | 963 sockets->swap(sockets_); |
| 1063 } | 964 } |
| 1064 | 965 |
| 1065 void PushSocket(DatagramServerSocket* socket) { | 966 void PushSocket(DatagramServerSocket* socket) { sockets_.push_back(socket); } |
| 1066 sockets_.push_back(socket); | |
| 1067 } | |
| 1068 | 967 |
| 1069 private: | 968 private: |
| 1070 ScopedVector<DatagramServerSocket> sockets_; | 969 ScopedVector<DatagramServerSocket> sockets_; |
| 1071 }; | 970 }; |
| 1072 | 971 |
| 1073 class MockMDnsConnectionDelegate : public MDnsConnection::Delegate { | 972 class MockMDnsConnectionDelegate : public MDnsConnection::Delegate { |
| 1074 public: | 973 public: |
| 1075 virtual void HandlePacket(DnsResponse* response, int size) { | 974 virtual void HandlePacket(DnsResponse* response, int size) { |
| 1076 HandlePacketInternal(std::string(response->io_buffer()->data(), size)); | 975 HandlePacketInternal(std::string(response->io_buffer()->data(), size)); |
| 1077 } | 976 } |
| 1078 | 977 |
| 1079 MOCK_METHOD1(HandlePacketInternal, void(std::string packet)); | 978 MOCK_METHOD1(HandlePacketInternal, void(std::string packet)); |
| 1080 | 979 |
| 1081 MOCK_METHOD1(OnConnectionError, void(int error)); | 980 MOCK_METHOD1(OnConnectionError, void(int error)); |
| 1082 }; | 981 }; |
| 1083 | 982 |
| 1084 class MDnsConnectionTest : public ::testing::Test { | 983 class MDnsConnectionTest : public ::testing::Test { |
| 1085 public: | 984 public: |
| 1086 MDnsConnectionTest() : connection_(&delegate_) { | 985 MDnsConnectionTest() : connection_(&delegate_) {} |
| 1087 } | |
| 1088 | 986 |
| 1089 protected: | 987 protected: |
| 1090 // Follow successful connection initialization. | 988 // Follow successful connection initialization. |
| 1091 virtual void SetUp() OVERRIDE { | 989 virtual void SetUp() OVERRIDE { |
| 1092 socket_ipv4_ = new MockMDnsDatagramServerSocket(ADDRESS_FAMILY_IPV4); | 990 socket_ipv4_ = new MockMDnsDatagramServerSocket(ADDRESS_FAMILY_IPV4); |
| 1093 socket_ipv6_ = new MockMDnsDatagramServerSocket(ADDRESS_FAMILY_IPV6); | 991 socket_ipv6_ = new MockMDnsDatagramServerSocket(ADDRESS_FAMILY_IPV6); |
| 1094 factory_.PushSocket(socket_ipv6_); | 992 factory_.PushSocket(socket_ipv6_); |
| 1095 factory_.PushSocket(socket_ipv4_); | 993 factory_.PushSocket(socket_ipv4_); |
| 1096 } | 994 } |
| 1097 | 995 |
| 1098 bool InitConnection() { | 996 bool InitConnection() { return connection_.Init(&factory_); } |
| 1099 return connection_.Init(&factory_); | |
| 1100 } | |
| 1101 | 997 |
| 1102 StrictMock<MockMDnsConnectionDelegate> delegate_; | 998 StrictMock<MockMDnsConnectionDelegate> delegate_; |
| 1103 | 999 |
| 1104 MockMDnsDatagramServerSocket* socket_ipv4_; | 1000 MockMDnsDatagramServerSocket* socket_ipv4_; |
| 1105 MockMDnsDatagramServerSocket* socket_ipv6_; | 1001 MockMDnsDatagramServerSocket* socket_ipv6_; |
| 1106 SimpleMockSocketFactory factory_; | 1002 SimpleMockSocketFactory factory_; |
| 1107 MDnsConnection connection_; | 1003 MDnsConnection connection_; |
| 1108 TestCompletionCallback callback_; | 1004 TestCompletionCallback callback_; |
| 1109 }; | 1005 }; |
| 1110 | 1006 |
| 1111 TEST_F(MDnsConnectionTest, ReceiveSynchronous) { | 1007 TEST_F(MDnsConnectionTest, ReceiveSynchronous) { |
| 1112 std::string sample_packet = MakeString(kSamplePacket1, | 1008 std::string sample_packet = |
| 1113 sizeof(kSamplePacket1)); | 1009 MakeString(kSamplePacket1, sizeof(kSamplePacket1)); |
| 1114 | 1010 |
| 1115 socket_ipv6_->SetResponsePacket(sample_packet); | 1011 socket_ipv6_->SetResponsePacket(sample_packet); |
| 1116 EXPECT_CALL(*socket_ipv4_, RecvFrom(_, _, _, _)) | 1012 EXPECT_CALL(*socket_ipv4_, RecvFrom(_, _, _, _)) |
| 1117 .WillOnce(Return(ERR_IO_PENDING)); | 1013 .WillOnce(Return(ERR_IO_PENDING)); |
| 1118 EXPECT_CALL(*socket_ipv6_, RecvFrom(_, _, _, _)) | 1014 EXPECT_CALL(*socket_ipv6_, RecvFrom(_, _, _, _)) |
| 1119 .WillOnce( | 1015 .WillOnce( |
| 1120 Invoke(socket_ipv6_, &MockMDnsDatagramServerSocket::HandleRecvNow)) | 1016 Invoke(socket_ipv6_, &MockMDnsDatagramServerSocket::HandleRecvNow)) |
| 1121 .WillOnce(Return(ERR_IO_PENDING)); | 1017 .WillOnce(Return(ERR_IO_PENDING)); |
| 1122 | 1018 |
| 1123 EXPECT_CALL(delegate_, HandlePacketInternal(sample_packet)); | 1019 EXPECT_CALL(delegate_, HandlePacketInternal(sample_packet)); |
| 1124 | 1020 |
| 1125 ASSERT_TRUE(InitConnection()); | 1021 ASSERT_TRUE(InitConnection()); |
| 1126 } | 1022 } |
| 1127 | 1023 |
| 1128 TEST_F(MDnsConnectionTest, ReceiveAsynchronous) { | 1024 TEST_F(MDnsConnectionTest, ReceiveAsynchronous) { |
| 1129 std::string sample_packet = MakeString(kSamplePacket1, | 1025 std::string sample_packet = |
| 1130 sizeof(kSamplePacket1)); | 1026 MakeString(kSamplePacket1, sizeof(kSamplePacket1)); |
| 1131 socket_ipv6_->SetResponsePacket(sample_packet); | 1027 socket_ipv6_->SetResponsePacket(sample_packet); |
| 1132 EXPECT_CALL(*socket_ipv4_, RecvFrom(_, _, _, _)) | 1028 EXPECT_CALL(*socket_ipv4_, RecvFrom(_, _, _, _)) |
| 1133 .WillOnce(Return(ERR_IO_PENDING)); | 1029 .WillOnce(Return(ERR_IO_PENDING)); |
| 1134 EXPECT_CALL(*socket_ipv6_, RecvFrom(_, _, _, _)) | 1030 EXPECT_CALL(*socket_ipv6_, RecvFrom(_, _, _, _)) |
| 1135 .WillOnce( | 1031 .WillOnce( |
| 1136 Invoke(socket_ipv6_, &MockMDnsDatagramServerSocket::HandleRecvLater)) | 1032 Invoke(socket_ipv6_, &MockMDnsDatagramServerSocket::HandleRecvLater)) |
| 1137 .WillOnce(Return(ERR_IO_PENDING)); | 1033 .WillOnce(Return(ERR_IO_PENDING)); |
| 1138 | 1034 |
| 1139 ASSERT_TRUE(InitConnection()); | 1035 ASSERT_TRUE(InitConnection()); |
| 1140 | 1036 |
| 1141 EXPECT_CALL(delegate_, HandlePacketInternal(sample_packet)); | 1037 EXPECT_CALL(delegate_, HandlePacketInternal(sample_packet)); |
| 1142 | 1038 |
| 1143 base::MessageLoop::current()->RunUntilIdle(); | 1039 base::MessageLoop::current()->RunUntilIdle(); |
| 1144 } | 1040 } |
| 1145 | 1041 |
| 1146 TEST_F(MDnsConnectionTest, Send) { | 1042 TEST_F(MDnsConnectionTest, Send) { |
| 1147 std::string sample_packet = MakeString(kSamplePacket1, | 1043 std::string sample_packet = |
| 1148 sizeof(kSamplePacket1)); | 1044 MakeString(kSamplePacket1, sizeof(kSamplePacket1)); |
| 1149 | 1045 |
| 1150 scoped_refptr<IOBufferWithSize> buf( | 1046 scoped_refptr<IOBufferWithSize> buf( |
| 1151 new IOBufferWithSize(sizeof kSamplePacket1)); | 1047 new IOBufferWithSize(sizeof kSamplePacket1)); |
| 1152 memcpy(buf->data(), kSamplePacket1, sizeof(kSamplePacket1)); | 1048 memcpy(buf->data(), kSamplePacket1, sizeof(kSamplePacket1)); |
| 1153 | 1049 |
| 1154 EXPECT_CALL(*socket_ipv4_, RecvFrom(_, _, _, _)) | 1050 EXPECT_CALL(*socket_ipv4_, RecvFrom(_, _, _, _)) |
| 1155 .WillOnce(Return(ERR_IO_PENDING)); | 1051 .WillOnce(Return(ERR_IO_PENDING)); |
| 1156 EXPECT_CALL(*socket_ipv6_, RecvFrom(_, _, _, _)) | 1052 EXPECT_CALL(*socket_ipv6_, RecvFrom(_, _, _, _)) |
| 1157 .WillOnce(Return(ERR_IO_PENDING)); | 1053 .WillOnce(Return(ERR_IO_PENDING)); |
| 1158 | 1054 |
| 1159 ASSERT_TRUE(InitConnection()); | 1055 ASSERT_TRUE(InitConnection()); |
| 1160 | 1056 |
| 1161 EXPECT_CALL(*socket_ipv4_, | 1057 EXPECT_CALL(*socket_ipv4_, |
| 1162 SendToInternal(sample_packet, "224.0.0.251:5353", _)); | 1058 SendToInternal(sample_packet, "224.0.0.251:5353", _)); |
| 1163 EXPECT_CALL(*socket_ipv6_, | 1059 EXPECT_CALL(*socket_ipv6_, |
| 1164 SendToInternal(sample_packet, "[ff02::fb]:5353", _)); | 1060 SendToInternal(sample_packet, "[ff02::fb]:5353", _)); |
| 1165 | 1061 |
| 1166 connection_.Send(buf, buf->size()); | 1062 connection_.Send(buf, buf->size()); |
| 1167 } | 1063 } |
| 1168 | 1064 |
| 1169 TEST_F(MDnsConnectionTest, Error) { | 1065 TEST_F(MDnsConnectionTest, Error) { |
| 1170 CompletionCallback callback; | 1066 CompletionCallback callback; |
| 1171 | 1067 |
| 1172 EXPECT_CALL(*socket_ipv4_, RecvFrom(_, _, _, _)) | 1068 EXPECT_CALL(*socket_ipv4_, RecvFrom(_, _, _, _)) |
| 1173 .WillOnce(Return(ERR_IO_PENDING)); | 1069 .WillOnce(Return(ERR_IO_PENDING)); |
| 1174 EXPECT_CALL(*socket_ipv6_, RecvFrom(_, _, _, _)) | 1070 EXPECT_CALL(*socket_ipv6_, RecvFrom(_, _, _, _)) |
| 1175 .WillOnce(DoAll(SaveArg<3>(&callback), Return(ERR_IO_PENDING))); | 1071 .WillOnce(DoAll(SaveArg<3>(&callback), Return(ERR_IO_PENDING))); |
| 1176 | 1072 |
| 1177 ASSERT_TRUE(InitConnection()); | 1073 ASSERT_TRUE(InitConnection()); |
| 1178 | 1074 |
| 1179 EXPECT_CALL(delegate_, OnConnectionError(ERR_SOCKET_NOT_CONNECTED)); | 1075 EXPECT_CALL(delegate_, OnConnectionError(ERR_SOCKET_NOT_CONNECTED)); |
| 1180 callback.Run(ERR_SOCKET_NOT_CONNECTED); | 1076 callback.Run(ERR_SOCKET_NOT_CONNECTED); |
| 1181 } | 1077 } |
| 1182 | 1078 |
| 1183 } // namespace | 1079 } // namespace |
| 1184 | 1080 |
| 1185 } // namespace net | 1081 } // namespace net |
| OLD | NEW |