| 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 char kSamplePacket1[] = { | 31 const uint8 kSamplePacket1[] = { |
| 32 // Header | 32 // Header |
| 33 '\x00', '\x00', // ID is zeroed out | 33 0x00, 0x00, // ID is zeroed out |
| 34 '\x81', '\x80', // Standard query response, RA, no error | 34 0x81, 0x80, // Standard query response, RA, no error |
| 35 '\x00', '\x00', // No questions (for simplicity) | 35 0x00, 0x00, // No questions (for simplicity) |
| 36 '\x00', '\x02', // 2 RRs (answers) | 36 0x00, 0x02, // 2 RRs (answers) |
| 37 '\x00', '\x00', // 0 authority RRs | 37 0x00, 0x00, // 0 authority RRs |
| 38 '\x00', '\x00', // 0 additional RRs | 38 0x00, 0x00, // 0 additional RRs |
| 39 | 39 |
| 40 // Answer 1 | 40 // Answer 1 |
| 41 '\x07', '_', 'p', 'r', 'i', 'v', 'e', 't', | 41 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
| 42 '\x04', '_', 't', 'c', 'p', | 42 0x04, '_', 't', 'c', 'p', |
| 43 '\x05', 'l', 'o', 'c', 'a', 'l', | 43 0x05, 'l', 'o', 'c', 'a', 'l', |
| 44 '\x00', | 44 0x00, |
| 45 '\x00', '\x0c', // TYPE is PTR. | 45 0x00, 0x0c, // TYPE is PTR. |
| 46 '\x00', '\x01', // CLASS is IN. | 46 0x00, 0x01, // CLASS is IN. |
| 47 '\x00', '\x00', // TTL (4 bytes) is 1 second; | 47 0x00, 0x00, // TTL (4 bytes) is 1 second; |
| 48 '\x00', '\x01', | 48 0x00, 0x01, |
| 49 '\x00', '\x08', // RDLENGTH is 8 bytes. | 49 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 50 '\x05', 'h', 'e', 'l', 'l', 'o', | 50 0x05, 'h', 'e', 'l', 'l', 'o', |
| 51 '\xc0', '\x0c', | 51 0xc0, 0x0c, |
| 52 | 52 |
| 53 // Answer 2 | 53 // Answer 2 |
| 54 '\x08', '_', 'p', 'r', 'i', 'n', 't', 'e', 'r', | 54 0x08, '_', 'p', 'r', 'i', 'n', 't', 'e', 'r', |
| 55 '\xc0', '\x14', // Pointer to "._tcp.local" | 55 0xc0, 0x14, // Pointer to "._tcp.local" |
| 56 '\x00', '\x0c', // TYPE is PTR. | 56 0x00, 0x0c, // TYPE is PTR. |
| 57 '\x00', '\x01', // CLASS is IN. | 57 0x00, 0x01, // CLASS is IN. |
| 58 '\x00', '\x01', // TTL (4 bytes) is 20 hours, 47 minutes, 49 seconds. | 58 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 49 seconds. |
| 59 '\x24', '\x75', | 59 0x24, 0x75, |
| 60 '\x00', '\x08', // RDLENGTH is 8 bytes. | 60 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 61 '\x05', 'h', 'e', 'l', 'l', 'o', | 61 0x05, 'h', 'e', 'l', 'l', 'o', |
| 62 '\xc0', '\x32' | 62 0xc0, 0x32 |
| 63 }; | 63 }; |
| 64 | 64 |
| 65 const char kCorruptedPacketBadQuestion[] = { | 65 const uint8 kCorruptedPacketBadQuestion[] = { |
| 66 // Header | 66 // Header |
| 67 '\x00', '\x00', // ID is zeroed out | 67 0x00, 0x00, // ID is zeroed out |
| 68 '\x81', '\x80', // Standard query response, RA, no error | 68 0x81, 0x80, // Standard query response, RA, no error |
| 69 '\x00', '\x01', // One question | 69 0x00, 0x01, // One question |
| 70 '\x00', '\x02', // 2 RRs (answers) | 70 0x00, 0x02, // 2 RRs (answers) |
| 71 '\x00', '\x00', // 0 authority RRs | 71 0x00, 0x00, // 0 authority RRs |
| 72 '\x00', '\x00', // 0 additional RRs | 72 0x00, 0x00, // 0 additional RRs |
| 73 | 73 |
| 74 // Question is corrupted and cannot be read. | 74 // Question is corrupted and cannot be read. |
| 75 '\x99', 'h', 'e', 'l', 'l', 'o', | 75 0x99, 'h', 'e', 'l', 'l', 'o', |
| 76 '\x00', | 76 0x00, |
| 77 '\x00', '\x00', | 77 0x00, 0x00, |
| 78 '\x00', '\x00', | 78 0x00, 0x00, |
| 79 | 79 |
| 80 // Answer 1 | 80 // Answer 1 |
| 81 '\x07', '_', 'p', 'r', 'i', 'v', 'e', 't', | 81 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
| 82 '\x04', '_', 't', 'c', 'p', | 82 0x04, '_', 't', 'c', 'p', |
| 83 '\x05', 'l', 'o', 'c', 'a', 'l', | 83 0x05, 'l', 'o', 'c', 'a', 'l', |
| 84 '\x00', | 84 0x00, |
| 85 '\x00', '\x0c', // TYPE is PTR. | 85 0x00, 0x0c, // TYPE is PTR. |
| 86 '\x00', '\x01', // CLASS is IN. | 86 0x00, 0x01, // CLASS is IN. |
| 87 '\x00', '\x01', // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 87 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 88 '\x24', '\x74', | 88 0x24, 0x74, |
| 89 '\x00', '\x99', // RDLENGTH is impossible | 89 0x00, 0x99, // RDLENGTH is impossible |
| 90 '\x05', 'h', 'e', 'l', 'l', 'o', | 90 0x05, 'h', 'e', 'l', 'l', 'o', |
| 91 '\xc0', '\x0c', | 91 0xc0, 0x0c, |
| 92 | 92 |
| 93 // Answer 2 | 93 // Answer 2 |
| 94 '\x08', '_', 'p', 'r', // Useless trailing data. | 94 0x08, '_', 'p', 'r', // Useless trailing data. |
| 95 }; | 95 }; |
| 96 | 96 |
| 97 const char kCorruptedPacketUnsalvagable[] = { | 97 const uint8 kCorruptedPacketUnsalvagable[] = { |
| 98 // Header | 98 // Header |
| 99 '\x00', '\x00', // ID is zeroed out | 99 0x00, 0x00, // ID is zeroed out |
| 100 '\x81', '\x80', // Standard query response, RA, no error | 100 0x81, 0x80, // Standard query response, RA, no error |
| 101 '\x00', '\x00', // No questions (for simplicity) | 101 0x00, 0x00, // No questions (for simplicity) |
| 102 '\x00', '\x02', // 2 RRs (answers) | 102 0x00, 0x02, // 2 RRs (answers) |
| 103 '\x00', '\x00', // 0 authority RRs | 103 0x00, 0x00, // 0 authority RRs |
| 104 '\x00', '\x00', // 0 additional RRs | 104 0x00, 0x00, // 0 additional RRs |
| 105 | 105 |
| 106 // Answer 1 | 106 // Answer 1 |
| 107 '\x07', '_', 'p', 'r', 'i', 'v', 'e', 't', | 107 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
| 108 '\x04', '_', 't', 'c', 'p', | 108 0x04, '_', 't', 'c', 'p', |
| 109 '\x05', 'l', 'o', 'c', 'a', 'l', | 109 0x05, 'l', 'o', 'c', 'a', 'l', |
| 110 '\x00', | 110 0x00, |
| 111 '\x00', '\x0c', // TYPE is PTR. | 111 0x00, 0x0c, // TYPE is PTR. |
| 112 '\x00', '\x01', // CLASS is IN. | 112 0x00, 0x01, // CLASS is IN. |
| 113 '\x00', '\x01', // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 113 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 114 '\x24', '\x74', | 114 0x24, 0x74, |
| 115 '\x00', '\x99', // RDLENGTH is impossible | 115 0x00, 0x99, // RDLENGTH is impossible |
| 116 '\x05', 'h', 'e', 'l', 'l', 'o', | 116 0x05, 'h', 'e', 'l', 'l', 'o', |
| 117 '\xc0', '\x0c', | 117 0xc0, 0x0c, |
| 118 | 118 |
| 119 // Answer 2 | 119 // Answer 2 |
| 120 '\x08', '_', 'p', 'r', // Useless trailing data. | 120 0x08, '_', 'p', 'r', // Useless trailing data. |
| 121 }; | 121 }; |
| 122 | 122 |
| 123 const char kCorruptedPacketDoubleRecord[] = { | 123 const uint8 kCorruptedPacketDoubleRecord[] = { |
| 124 // Header | 124 // Header |
| 125 '\x00', '\x00', // ID is zeroed out | 125 0x00, 0x00, // ID is zeroed out |
| 126 '\x81', '\x80', // Standard query response, RA, no error | 126 0x81, 0x80, // Standard query response, RA, no error |
| 127 '\x00', '\x00', // No questions (for simplicity) | 127 0x00, 0x00, // No questions (for simplicity) |
| 128 '\x00', '\x02', // 2 RRs (answers) | 128 0x00, 0x02, // 2 RRs (answers) |
| 129 '\x00', '\x00', // 0 authority RRs | 129 0x00, 0x00, // 0 authority RRs |
| 130 '\x00', '\x00', // 0 additional RRs | 130 0x00, 0x00, // 0 additional RRs |
| 131 | 131 |
| 132 // Answer 1 | 132 // Answer 1 |
| 133 '\x06', 'p', 'r', 'i', 'v', 'e', 't', | 133 0x06, 'p', 'r', 'i', 'v', 'e', 't', |
| 134 '\x05', 'l', 'o', 'c', 'a', 'l', | 134 0x05, 'l', 'o', 'c', 'a', 'l', |
| 135 '\x00', | 135 0x00, |
| 136 '\x00', '\x01', // TYPE is A. | 136 0x00, 0x01, // TYPE is A. |
| 137 '\x00', '\x01', // CLASS is IN. | 137 0x00, 0x01, // CLASS is IN. |
| 138 '\x00', '\x01', // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 138 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 139 '\x24', '\x74', | 139 0x24, 0x74, |
| 140 '\x00', '\x04', // RDLENGTH is 4 | 140 0x00, 0x04, // RDLENGTH is 4 |
| 141 '\x05', '\x03', | 141 0x05, 0x03, |
| 142 '\xc0', '\x0c', | 142 0xc0, 0x0c, |
| 143 | 143 |
| 144 // Answer 2 -- Same key | 144 // Answer 2 -- Same key |
| 145 '\x06', 'p', 'r', 'i', 'v', 'e', 't', | 145 0x06, 'p', 'r', 'i', 'v', 'e', 't', |
| 146 '\x05', 'l', 'o', 'c', 'a', 'l', | 146 0x05, 'l', 'o', 'c', 'a', 'l', |
| 147 '\x00', | 147 0x00, |
| 148 '\x00', '\x01', // TYPE is A. | 148 0x00, 0x01, // TYPE is A. |
| 149 '\x00', '\x01', // CLASS is IN. | 149 0x00, 0x01, // CLASS is IN. |
| 150 '\x00', '\x01', // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 150 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 151 '\x24', '\x74', | 151 0x24, 0x74, |
| 152 '\x00', '\x04', // RDLENGTH is 4 | 152 0x00, 0x04, // RDLENGTH is 4 |
| 153 '\x02', '\x03', | 153 0x02, 0x03, |
| 154 '\x04', '\x05', | 154 0x04, 0x05, |
| 155 }; | 155 }; |
| 156 | 156 |
| 157 const char kCorruptedPacketSalvagable[] = { | 157 const uint8 kCorruptedPacketSalvagable[] = { |
| 158 // Header | 158 // Header |
| 159 '\x00', '\x00', // ID is zeroed out | 159 0x00, 0x00, // ID is zeroed out |
| 160 '\x81', '\x80', // Standard query response, RA, no error | 160 0x81, 0x80, // Standard query response, RA, no error |
| 161 '\x00', '\x00', // No questions (for simplicity) | 161 0x00, 0x00, // No questions (for simplicity) |
| 162 '\x00', '\x02', // 2 RRs (answers) | 162 0x00, 0x02, // 2 RRs (answers) |
| 163 '\x00', '\x00', // 0 authority RRs | 163 0x00, 0x00, // 0 authority RRs |
| 164 '\x00', '\x00', // 0 additional RRs | 164 0x00, 0x00, // 0 additional RRs |
| 165 | 165 |
| 166 // Answer 1 | 166 // Answer 1 |
| 167 '\x07', '_', 'p', 'r', 'i', 'v', 'e', 't', | 167 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
| 168 '\x04', '_', 't', 'c', 'p', | 168 0x04, '_', 't', 'c', 'p', |
| 169 '\x05', 'l', 'o', 'c', 'a', 'l', | 169 0x05, 'l', 'o', 'c', 'a', 'l', |
| 170 '\x00', | 170 0x00, |
| 171 '\x00', '\x0c', // TYPE is PTR. | 171 0x00, 0x0c, // TYPE is PTR. |
| 172 '\x00', '\x01', // CLASS is IN. | 172 0x00, 0x01, // CLASS is IN. |
| 173 '\x00', '\x01', // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 173 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 174 '\x24', '\x74', | 174 0x24, 0x74, |
| 175 '\x00', '\x08', // RDLENGTH is 8 bytes. | 175 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 176 '\x99', 'h', 'e', 'l', 'l', 'o', // Bad RDATA format. | 176 0x99, 'h', 'e', 'l', 'l', 'o', // Bad RDATA format. |
| 177 '\xc0', '\x0c', | 177 0xc0, 0x0c, |
| 178 | 178 |
| 179 // Answer 2 | 179 // Answer 2 |
| 180 '\x08', '_', 'p', 'r', 'i', 'n', 't', 'e', 'r', | 180 0x08, '_', 'p', 'r', 'i', 'n', 't', 'e', 'r', |
| 181 '\xc0', '\x14', // Pointer to "._tcp.local" | 181 0xc0, 0x14, // Pointer to "._tcp.local" |
| 182 '\x00', '\x0c', // TYPE is PTR. | 182 0x00, 0x0c, // TYPE is PTR. |
| 183 '\x00', '\x01', // CLASS is IN. | 183 0x00, 0x01, // CLASS is IN. |
| 184 '\x00', '\x01', // TTL (4 bytes) is 20 hours, 47 minutes, 49 seconds. | 184 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 49 seconds. |
| 185 '\x24', '\x75', | 185 0x24, 0x75, |
| 186 '\x00', '\x08', // RDLENGTH is 8 bytes. | 186 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 187 '\x05', 'h', 'e', 'l', 'l', 'o', | 187 0x05, 'h', 'e', 'l', 'l', 'o', |
| 188 '\xc0', '\x32' | 188 0xc0, 0x32 |
| 189 }; | 189 }; |
| 190 | 190 |
| 191 const char kSamplePacket2[] = { | 191 const uint8 kSamplePacket2[] = { |
| 192 // Header | 192 // Header |
| 193 '\x00', '\x00', // ID is zeroed out | 193 0x00, 0x00, // ID is zeroed out |
| 194 '\x81', '\x80', // Standard query response, RA, no error | 194 0x81, 0x80, // Standard query response, RA, no error |
| 195 '\x00', '\x00', // No questions (for simplicity) | 195 0x00, 0x00, // No questions (for simplicity) |
| 196 '\x00', '\x02', // 2 RRs (answers) | 196 0x00, 0x02, // 2 RRs (answers) |
| 197 '\x00', '\x00', // 0 authority RRs | 197 0x00, 0x00, // 0 authority RRs |
| 198 '\x00', '\x00', // 0 additional RRs | 198 0x00, 0x00, // 0 additional RRs |
| 199 | 199 |
| 200 // Answer 1 | 200 // Answer 1 |
| 201 '\x07', '_', 'p', 'r', 'i', 'v', 'e', 't', | 201 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
| 202 '\x04', '_', 't', 'c', 'p', | 202 0x04, '_', 't', 'c', 'p', |
| 203 '\x05', 'l', 'o', 'c', 'a', 'l', | 203 0x05, 'l', 'o', 'c', 'a', 'l', |
| 204 '\x00', | 204 0x00, |
| 205 '\x00', '\x0c', // TYPE is PTR. | 205 0x00, 0x0c, // TYPE is PTR. |
| 206 '\x00', '\x01', // CLASS is IN. | 206 0x00, 0x01, // CLASS is IN. |
| 207 '\x00', '\x01', // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 207 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 208 '\x24', '\x74', | 208 0x24, 0x74, |
| 209 '\x00', '\x08', // RDLENGTH is 8 bytes. | 209 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 210 '\x05', 'z', 'z', 'z', 'z', 'z', | 210 0x05, 'z', 'z', 'z', 'z', 'z', |
| 211 '\xc0', '\x0c', | 211 0xc0, 0x0c, |
| 212 | 212 |
| 213 // Answer 2 | 213 // Answer 2 |
| 214 '\x08', '_', 'p', 'r', 'i', 'n', 't', 'e', 'r', | 214 0x08, '_', 'p', 'r', 'i', 'n', 't', 'e', 'r', |
| 215 '\xc0', '\x14', // Pointer to "._tcp.local" | 215 0xc0, 0x14, // Pointer to "._tcp.local" |
| 216 '\x00', '\x0c', // TYPE is PTR. | 216 0x00, 0x0c, // TYPE is PTR. |
| 217 '\x00', '\x01', // CLASS is IN. | 217 0x00, 0x01, // CLASS is IN. |
| 218 '\x00', '\x01', // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 218 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 219 '\x24', '\x74', | 219 0x24, 0x74, |
| 220 '\x00', '\x08', // RDLENGTH is 8 bytes. | 220 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 221 '\x05', 'z', 'z', 'z', 'z', 'z', | 221 0x05, 'z', 'z', 'z', 'z', 'z', |
| 222 '\xc0', '\x32' | 222 0xc0, 0x32 |
| 223 }; | 223 }; |
| 224 | 224 |
| 225 const char kQueryPacketPrivet[] = { | 225 const uint8 kQueryPacketPrivet[] = { |
| 226 // Header | 226 // Header |
| 227 '\x00', '\x00', // ID is zeroed out | 227 0x00, 0x00, // ID is zeroed out |
| 228 '\x00', '\x00', // No flags. | 228 0x00, 0x00, // No flags. |
| 229 '\x00', '\x01', // One question. | 229 0x00, 0x01, // One question. |
| 230 '\x00', '\x00', // 0 RRs (answers) | 230 0x00, 0x00, // 0 RRs (answers) |
| 231 '\x00', '\x00', // 0 authority RRs | 231 0x00, 0x00, // 0 authority RRs |
| 232 '\x00', '\x00', // 0 additional RRs | 232 0x00, 0x00, // 0 additional RRs |
| 233 | 233 |
| 234 // Question | 234 // Question |
| 235 // This part is echoed back from the respective query. | 235 // This part is echoed back from the respective query. |
| 236 '\x07', '_', 'p', 'r', 'i', 'v', 'e', 't', | 236 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
| 237 '\x04', '_', 't', 'c', 'p', | 237 0x04, '_', 't', 'c', 'p', |
| 238 '\x05', 'l', 'o', 'c', 'a', 'l', | 238 0x05, 'l', 'o', 'c', 'a', 'l', |
| 239 '\x00', | 239 0x00, |
| 240 '\x00', '\x0c', // TYPE is PTR. | 240 0x00, 0x0c, // TYPE is PTR. |
| 241 '\x00', '\x01', // CLASS is IN. | 241 0x00, 0x01, // CLASS is IN. |
| 242 }; | 242 }; |
| 243 | 243 |
| 244 const char kSamplePacketAdditionalOnly[] = { | 244 const uint8 kSamplePacketAdditionalOnly[] = { |
| 245 // Header | 245 // Header |
| 246 '\x00', '\x00', // ID is zeroed out | 246 0x00, 0x00, // ID is zeroed out |
| 247 '\x81', '\x80', // Standard query response, RA, no error | 247 0x81, 0x80, // Standard query response, RA, no error |
| 248 '\x00', '\x00', // No questions (for simplicity) | 248 0x00, 0x00, // No questions (for simplicity) |
| 249 '\x00', '\x00', // 2 RRs (answers) | 249 0x00, 0x00, // 2 RRs (answers) |
| 250 '\x00', '\x00', // 0 authority RRs | 250 0x00, 0x00, // 0 authority RRs |
| 251 '\x00', '\x01', // 0 additional RRs | 251 0x00, 0x01, // 0 additional RRs |
| 252 | 252 |
| 253 // Answer 1 | 253 // Answer 1 |
| 254 '\x07', '_', 'p', 'r', 'i', 'v', 'e', 't', | 254 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
| 255 '\x04', '_', 't', 'c', 'p', | 255 0x04, '_', 't', 'c', 'p', |
| 256 '\x05', 'l', 'o', 'c', 'a', 'l', | 256 0x05, 'l', 'o', 'c', 'a', 'l', |
| 257 '\x00', | 257 0x00, |
| 258 '\x00', '\x0c', // TYPE is PTR. | 258 0x00, 0x0c, // TYPE is PTR. |
| 259 '\x00', '\x01', // CLASS is IN. | 259 0x00, 0x01, // CLASS is IN. |
| 260 '\x00', '\x01', // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 260 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 261 '\x24', '\x74', | 261 0x24, 0x74, |
| 262 '\x00', '\x08', // RDLENGTH is 8 bytes. | 262 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 263 '\x05', 'h', 'e', 'l', 'l', 'o', | 263 0x05, 'h', 'e', 'l', 'l', 'o', |
| 264 '\xc0', '\x0c', | 264 0xc0, 0x0c, |
| 265 }; | 265 }; |
| 266 | 266 |
| 267 const char kSamplePacketNsec[] = { | 267 const uint8 kSamplePacketNsec[] = { |
| 268 // Header | 268 // Header |
| 269 '\x00', '\x00', // ID is zeroed out | 269 0x00, 0x00, // ID is zeroed out |
| 270 '\x81', '\x80', // Standard query response, RA, no error | 270 0x81, 0x80, // Standard query response, RA, no error |
| 271 '\x00', '\x00', // No questions (for simplicity) | 271 0x00, 0x00, // No questions (for simplicity) |
| 272 '\x00', '\x01', // 1 RR (answers) | 272 0x00, 0x01, // 1 RR (answers) |
| 273 '\x00', '\x00', // 0 authority RRs | 273 0x00, 0x00, // 0 authority RRs |
| 274 '\x00', '\x00', // 0 additional RRs | 274 0x00, 0x00, // 0 additional RRs |
| 275 | 275 |
| 276 // Answer 1 | 276 // Answer 1 |
| 277 '\x07', '_', 'p', 'r', 'i', 'v', 'e', 't', | 277 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
| 278 '\x04', '_', 't', 'c', 'p', | 278 0x04, '_', 't', 'c', 'p', |
| 279 '\x05', 'l', 'o', 'c', 'a', 'l', | 279 0x05, 'l', 'o', 'c', 'a', 'l', |
| 280 '\x00', | 280 0x00, |
| 281 '\x00', '\x2f', // TYPE is NSEC. | 281 0x00, 0x2f, // TYPE is NSEC. |
| 282 '\x00', '\x01', // CLASS is IN. | 282 0x00, 0x01, // CLASS is IN. |
| 283 '\x00', '\x01', // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 283 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 284 '\x24', '\x74', | 284 0x24, 0x74, |
| 285 '\x00', '\x06', // RDLENGTH is 6 bytes. | 285 0x00, 0x06, // RDLENGTH is 6 bytes. |
| 286 '\xc0', '\x0c', | 286 0xc0, 0x0c, |
| 287 '\x00', '\x02', '\x00', '\x08' // Only A record present | 287 0x00, 0x02, 0x00, 0x08 // Only A record present |
| 288 }; | 288 }; |
| 289 | 289 |
| 290 const char kSamplePacketAPrivet[] = { | 290 const uint8 kSamplePacketAPrivet[] = { |
| 291 // Header | 291 // Header |
| 292 '\x00', '\x00', // ID is zeroed out | 292 0x00, 0x00, // ID is zeroed out |
| 293 '\x81', '\x80', // Standard query response, RA, no error | 293 0x81, 0x80, // Standard query response, RA, no error |
| 294 '\x00', '\x00', // No questions (for simplicity) | 294 0x00, 0x00, // No questions (for simplicity) |
| 295 '\x00', '\x01', // 1 RR (answers) | 295 0x00, 0x01, // 1 RR (answers) |
| 296 '\x00', '\x00', // 0 authority RRs | 296 0x00, 0x00, // 0 authority RRs |
| 297 '\x00', '\x00', // 0 additional RRs | 297 0x00, 0x00, // 0 additional RRs |
| 298 | 298 |
| 299 // Answer 1 | 299 // Answer 1 |
| 300 '\x07', '_', 'p', 'r', 'i', 'v', 'e', 't', | 300 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
| 301 '\x04', '_', 't', 'c', 'p', | 301 0x04, '_', 't', 'c', 'p', |
| 302 '\x05', 'l', 'o', 'c', 'a', 'l', | 302 0x05, 'l', 'o', 'c', 'a', 'l', |
| 303 '\x00', | 303 0x00, |
| 304 '\x00', '\x01', // TYPE is A. | 304 0x00, 0x01, // TYPE is A. |
| 305 '\x00', '\x01', // CLASS is IN. | 305 0x00, 0x01, // CLASS is IN. |
| 306 '\x00', '\x01', // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. | 306 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds. |
| 307 '\x24', '\x74', | 307 0x24, 0x74, |
| 308 '\x00', '\x04', // RDLENGTH is 4 bytes. | 308 0x00, 0x04, // RDLENGTH is 4 bytes. |
| 309 '\xc0', '\x0c', | 309 0xc0, 0x0c, |
| 310 '\x00', '\x02', | 310 0x00, 0x02, |
| 311 }; | 311 }; |
| 312 | 312 |
| 313 const char kSamplePacketGoodbye[] = { | 313 const uint8 kSamplePacketGoodbye[] = { |
| 314 // Header | 314 // Header |
| 315 '\x00', '\x00', // ID is zeroed out | 315 0x00, 0x00, // ID is zeroed out |
| 316 '\x81', '\x80', // Standard query response, RA, no error | 316 0x81, 0x80, // Standard query response, RA, no error |
| 317 '\x00', '\x00', // No questions (for simplicity) | 317 0x00, 0x00, // No questions (for simplicity) |
| 318 '\x00', '\x01', // 2 RRs (answers) | 318 0x00, 0x01, // 2 RRs (answers) |
| 319 '\x00', '\x00', // 0 authority RRs | 319 0x00, 0x00, // 0 authority RRs |
| 320 '\x00', '\x00', // 0 additional RRs | 320 0x00, 0x00, // 0 additional RRs |
| 321 | 321 |
| 322 // Answer 1 | 322 // Answer 1 |
| 323 '\x07', '_', 'p', 'r', 'i', 'v', 'e', 't', | 323 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't', |
| 324 '\x04', '_', 't', 'c', 'p', | 324 0x04, '_', 't', 'c', 'p', |
| 325 '\x05', 'l', 'o', 'c', 'a', 'l', | 325 0x05, 'l', 'o', 'c', 'a', 'l', |
| 326 '\x00', | 326 0x00, |
| 327 '\x00', '\x0c', // TYPE is PTR. | 327 0x00, 0x0c, // TYPE is PTR. |
| 328 '\x00', '\x01', // CLASS is IN. | 328 0x00, 0x01, // CLASS is IN. |
| 329 '\x00', '\x00', // TTL (4 bytes) is zero; | 329 0x00, 0x00, // TTL (4 bytes) is zero; |
| 330 '\x00', '\x00', | 330 0x00, 0x00, |
| 331 '\x00', '\x08', // RDLENGTH is 8 bytes. | 331 0x00, 0x08, // RDLENGTH is 8 bytes. |
| 332 '\x05', 'z', 'z', 'z', 'z', 'z', | 332 0x05, 'z', 'z', 'z', 'z', 'z', |
| 333 '\xc0', '\x0c', | 333 0xc0, 0x0c, |
| 334 }; | 334 }; |
| 335 |
| 336 std::string MakeString(const uint8* data, unsigned size) { |
| 337 return std::string(reinterpret_cast<const char*>(data), size); |
| 338 } |
| 335 | 339 |
| 336 class PtrRecordCopyContainer { | 340 class PtrRecordCopyContainer { |
| 337 public: | 341 public: |
| 338 PtrRecordCopyContainer() {} | 342 PtrRecordCopyContainer() {} |
| 339 ~PtrRecordCopyContainer() {} | 343 ~PtrRecordCopyContainer() {} |
| 340 | 344 |
| 341 bool is_set() const { return set_; } | 345 bool is_set() const { return set_; } |
| 342 | 346 |
| 343 void SaveWithDummyArg(int unused, const RecordParsed* value) { | 347 void SaveWithDummyArg(int unused, const RecordParsed* value) { |
| 344 Save(value); | 348 Save(value); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 void Stop(); | 382 void Stop(); |
| 379 | 383 |
| 380 MOCK_METHOD2(MockableRecordCallback, void(MDnsTransaction::Result result, | 384 MOCK_METHOD2(MockableRecordCallback, void(MDnsTransaction::Result result, |
| 381 const RecordParsed* record)); | 385 const RecordParsed* record)); |
| 382 | 386 |
| 383 MOCK_METHOD2(MockableRecordCallback2, void(MDnsTransaction::Result result, | 387 MOCK_METHOD2(MockableRecordCallback2, void(MDnsTransaction::Result result, |
| 384 const RecordParsed* record)); | 388 const RecordParsed* record)); |
| 385 | 389 |
| 386 | 390 |
| 387 protected: | 391 protected: |
| 388 void ExpectPacket(const char* packet, unsigned size); | 392 void ExpectPacket(const uint8* packet, unsigned size); |
| 389 void SimulatePacketReceive(const char* packet, unsigned size); | 393 void SimulatePacketReceive(const uint8* packet, unsigned size); |
| 390 | 394 |
| 391 scoped_ptr<MDnsClientImpl> test_client_; | 395 scoped_ptr<MDnsClientImpl> test_client_; |
| 392 IPEndPoint mdns_ipv4_endpoint_; | 396 IPEndPoint mdns_ipv4_endpoint_; |
| 393 StrictMock<MockMDnsSocketFactory>* socket_factory_; | 397 StrictMock<MockMDnsSocketFactory>* socket_factory_; |
| 394 | 398 |
| 395 // Transactions and listeners that can be deleted by class methods for | 399 // Transactions and listeners that can be deleted by class methods for |
| 396 // reentrancy tests. | 400 // reentrancy tests. |
| 397 scoped_ptr<MDnsTransaction> transaction_; | 401 scoped_ptr<MDnsTransaction> transaction_; |
| 398 scoped_ptr<MDnsListener> listener1_; | 402 scoped_ptr<MDnsListener> listener1_; |
| 399 scoped_ptr<MDnsListener> listener2_; | 403 scoped_ptr<MDnsListener> listener2_; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 417 MDnsTest::~MDnsTest() { | 421 MDnsTest::~MDnsTest() { |
| 418 } | 422 } |
| 419 | 423 |
| 420 void MDnsTest::SetUp() { | 424 void MDnsTest::SetUp() { |
| 421 test_client_->StartListening(); | 425 test_client_->StartListening(); |
| 422 } | 426 } |
| 423 | 427 |
| 424 void MDnsTest::TearDown() { | 428 void MDnsTest::TearDown() { |
| 425 } | 429 } |
| 426 | 430 |
| 427 void MDnsTest::SimulatePacketReceive(const char* packet, unsigned size) { | 431 void MDnsTest::SimulatePacketReceive(const uint8* packet, unsigned size) { |
| 428 socket_factory_->SimulateReceive(packet, size); | 432 socket_factory_->SimulateReceive(packet, size); |
| 429 } | 433 } |
| 430 | 434 |
| 431 void MDnsTest::ExpectPacket(const char* packet, unsigned size) { | 435 void MDnsTest::ExpectPacket(const uint8* packet, unsigned size) { |
| 432 EXPECT_CALL(*socket_factory_, OnSendTo(std::string(packet, size))) | 436 EXPECT_CALL(*socket_factory_, OnSendTo(MakeString(packet, size))) |
| 433 .Times(2); | 437 .Times(2); |
| 434 } | 438 } |
| 435 | 439 |
| 436 void MDnsTest::DeleteTransaction() { | 440 void MDnsTest::DeleteTransaction() { |
| 437 transaction_.reset(); | 441 transaction_.reset(); |
| 438 } | 442 } |
| 439 | 443 |
| 440 void MDnsTest::DeleteBothListeners() { | 444 void MDnsTest::DeleteBothListeners() { |
| 441 listener1_.reset(); | 445 listener1_.reset(); |
| 442 listener2_.reset(); | 446 listener2_.reset(); |
| (...skipping 646 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1089 StrictMock<MockMDnsConnectionDelegate> delegate_; | 1093 StrictMock<MockMDnsConnectionDelegate> delegate_; |
| 1090 | 1094 |
| 1091 MockMDnsDatagramServerSocket* socket_ipv4_; | 1095 MockMDnsDatagramServerSocket* socket_ipv4_; |
| 1092 MockMDnsDatagramServerSocket* socket_ipv6_; | 1096 MockMDnsDatagramServerSocket* socket_ipv6_; |
| 1093 SimpleMockSocketFactory factory_; | 1097 SimpleMockSocketFactory factory_; |
| 1094 MDnsConnection connection_; | 1098 MDnsConnection connection_; |
| 1095 TestCompletionCallback callback_; | 1099 TestCompletionCallback callback_; |
| 1096 }; | 1100 }; |
| 1097 | 1101 |
| 1098 TEST_F(MDnsConnectionTest, ReceiveSynchronous) { | 1102 TEST_F(MDnsConnectionTest, ReceiveSynchronous) { |
| 1099 std::string sample_packet = | 1103 std::string sample_packet = MakeString(kSamplePacket1, |
| 1100 std::string(kSamplePacket1, sizeof(kSamplePacket1)); | 1104 sizeof(kSamplePacket1)); |
| 1101 | 1105 |
| 1102 socket_ipv6_->SetResponsePacket(sample_packet); | 1106 socket_ipv6_->SetResponsePacket(sample_packet); |
| 1103 EXPECT_CALL(*socket_ipv4_, RecvFrom(_, _, _, _)) | 1107 EXPECT_CALL(*socket_ipv4_, RecvFrom(_, _, _, _)) |
| 1104 .WillOnce(Return(ERR_IO_PENDING)); | 1108 .WillOnce(Return(ERR_IO_PENDING)); |
| 1105 EXPECT_CALL(*socket_ipv6_, RecvFrom(_, _, _, _)) | 1109 EXPECT_CALL(*socket_ipv6_, RecvFrom(_, _, _, _)) |
| 1106 .WillOnce( | 1110 .WillOnce( |
| 1107 Invoke(socket_ipv6_, &MockMDnsDatagramServerSocket::HandleRecvNow)) | 1111 Invoke(socket_ipv6_, &MockMDnsDatagramServerSocket::HandleRecvNow)) |
| 1108 .WillOnce(Return(ERR_IO_PENDING)); | 1112 .WillOnce(Return(ERR_IO_PENDING)); |
| 1109 | 1113 |
| 1110 EXPECT_CALL(delegate_, HandlePacketInternal(sample_packet)); | 1114 EXPECT_CALL(delegate_, HandlePacketInternal(sample_packet)); |
| 1111 | 1115 |
| 1112 ASSERT_TRUE(InitConnection()); | 1116 ASSERT_TRUE(InitConnection()); |
| 1113 } | 1117 } |
| 1114 | 1118 |
| 1115 TEST_F(MDnsConnectionTest, ReceiveAsynchronous) { | 1119 TEST_F(MDnsConnectionTest, ReceiveAsynchronous) { |
| 1116 std::string sample_packet = | 1120 std::string sample_packet = MakeString(kSamplePacket1, |
| 1117 std::string(kSamplePacket1, sizeof(kSamplePacket1)); | 1121 sizeof(kSamplePacket1)); |
| 1118 socket_ipv6_->SetResponsePacket(sample_packet); | 1122 socket_ipv6_->SetResponsePacket(sample_packet); |
| 1119 EXPECT_CALL(*socket_ipv4_, RecvFrom(_, _, _, _)) | 1123 EXPECT_CALL(*socket_ipv4_, RecvFrom(_, _, _, _)) |
| 1120 .WillOnce(Return(ERR_IO_PENDING)); | 1124 .WillOnce(Return(ERR_IO_PENDING)); |
| 1121 EXPECT_CALL(*socket_ipv6_, RecvFrom(_, _, _, _)) | 1125 EXPECT_CALL(*socket_ipv6_, RecvFrom(_, _, _, _)) |
| 1122 .WillOnce( | 1126 .WillOnce( |
| 1123 Invoke(socket_ipv6_, &MockMDnsDatagramServerSocket::HandleRecvLater)) | 1127 Invoke(socket_ipv6_, &MockMDnsDatagramServerSocket::HandleRecvLater)) |
| 1124 .WillOnce(Return(ERR_IO_PENDING)); | 1128 .WillOnce(Return(ERR_IO_PENDING)); |
| 1125 | 1129 |
| 1126 ASSERT_TRUE(InitConnection()); | 1130 ASSERT_TRUE(InitConnection()); |
| 1127 | 1131 |
| 1128 EXPECT_CALL(delegate_, HandlePacketInternal(sample_packet)); | 1132 EXPECT_CALL(delegate_, HandlePacketInternal(sample_packet)); |
| 1129 | 1133 |
| 1130 base::MessageLoop::current()->RunUntilIdle(); | 1134 base::MessageLoop::current()->RunUntilIdle(); |
| 1131 } | 1135 } |
| 1132 | 1136 |
| 1133 TEST_F(MDnsConnectionTest, Send) { | 1137 TEST_F(MDnsConnectionTest, Send) { |
| 1134 std::string sample_packet = | 1138 std::string sample_packet = MakeString(kSamplePacket1, |
| 1135 std::string(kSamplePacket1, sizeof(kSamplePacket1)); | 1139 sizeof(kSamplePacket1)); |
| 1136 | 1140 |
| 1137 scoped_refptr<IOBufferWithSize> buf( | 1141 scoped_refptr<IOBufferWithSize> buf( |
| 1138 new IOBufferWithSize(sizeof kSamplePacket1)); | 1142 new IOBufferWithSize(sizeof kSamplePacket1)); |
| 1139 memcpy(buf->data(), kSamplePacket1, sizeof(kSamplePacket1)); | 1143 memcpy(buf->data(), kSamplePacket1, sizeof(kSamplePacket1)); |
| 1140 | 1144 |
| 1141 EXPECT_CALL(*socket_ipv4_, RecvFrom(_, _, _, _)) | 1145 EXPECT_CALL(*socket_ipv4_, RecvFrom(_, _, _, _)) |
| 1142 .WillOnce(Return(ERR_IO_PENDING)); | 1146 .WillOnce(Return(ERR_IO_PENDING)); |
| 1143 EXPECT_CALL(*socket_ipv6_, RecvFrom(_, _, _, _)) | 1147 EXPECT_CALL(*socket_ipv6_, RecvFrom(_, _, _, _)) |
| 1144 .WillOnce(Return(ERR_IO_PENDING)); | 1148 .WillOnce(Return(ERR_IO_PENDING)); |
| 1145 | 1149 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1163 | 1167 |
| 1164 ASSERT_TRUE(InitConnection()); | 1168 ASSERT_TRUE(InitConnection()); |
| 1165 | 1169 |
| 1166 EXPECT_CALL(delegate_, OnConnectionError(ERR_SOCKET_NOT_CONNECTED)); | 1170 EXPECT_CALL(delegate_, OnConnectionError(ERR_SOCKET_NOT_CONNECTED)); |
| 1167 callback.Run(ERR_SOCKET_NOT_CONNECTED); | 1171 callback.Run(ERR_SOCKET_NOT_CONNECTED); |
| 1168 } | 1172 } |
| 1169 | 1173 |
| 1170 } // namespace | 1174 } // namespace |
| 1171 | 1175 |
| 1172 } // namespace net | 1176 } // namespace net |
| OLD | NEW |