summaryrefslogtreecommitdiff
path: root/common/tests/unit/src/com/android/net/module/util/DnsPacketTest.java
blob: 28e183a95e4f02777d762420ee1339dd1dbefc2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.net.module.util;

import static android.net.DnsResolver.CLASS_IN;
import static android.net.DnsResolver.TYPE_A;
import static android.net.DnsResolver.TYPE_AAAA;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.annotation.NonNull;
import android.annotation.Nullable;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import libcore.net.InetAddressUtils;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class DnsPacketTest {
    private static final int TEST_DNS_PACKET_ID = 0x7722;
    private static final int TEST_DNS_PACKET_FLAGS = 0x8180;

    private void assertHeaderParses(DnsPacket.DnsHeader header, int id, int flag,
            int qCount, int aCount, int nsCount, int arCount) {
        assertEquals(header.getId(), id);
        assertEquals(header.getFlags(), flag);
        assertEquals(header.getRecordCount(DnsPacket.QDSECTION), qCount);
        assertEquals(header.getRecordCount(DnsPacket.ANSECTION), aCount);
        assertEquals(header.getRecordCount(DnsPacket.NSSECTION), nsCount);
        assertEquals(header.getRecordCount(DnsPacket.ARSECTION), arCount);
    }

    private void assertRecordParses(DnsPacket.DnsRecord record, String dname,
            int dtype, int dclass, int ttl, byte[] rr) {
        assertEquals(record.dName, dname);
        assertEquals(record.nsType, dtype);
        assertEquals(record.nsClass, dclass);
        assertEquals(record.ttl, ttl);
        assertTrue(Arrays.equals(record.getRR(), rr));
    }

    static class TestDnsPacket extends DnsPacket {
        TestDnsPacket(byte[] data) throws DnsPacket.ParseException {
            super(data);
        }

        TestDnsPacket(@NonNull DnsHeader header, @Nullable ArrayList<DnsRecord> qd,
                @Nullable ArrayList<DnsRecord> an) {
            super(header, qd, an);
        }

        public DnsHeader getHeader() {
            return mHeader;
        }
        public List<DnsRecord> getRecordList(int secType) {
            return mRecords[secType];
        }
    }

    @Test
    public void testNullDisallowed() {
        try {
            new TestDnsPacket(null);
            fail("Exception not thrown for null byte array");
        } catch (DnsPacket.ParseException e) {
        }
    }

    @Test
    public void testV4Answer() throws Exception {
        final byte[] v4blob = new byte[] {
            /* Header */
            0x55, 0x66, /* Transaction ID */
            (byte) 0x81, (byte) 0x80, /* Flags */
            0x00, 0x01, /* Questions */
            0x00, 0x01, /* Answer RRs */
            0x00, 0x00, /* Authority RRs */
            0x00, 0x00, /* Additional RRs */
            /* Queries */
            0x03, 0x77, 0x77, 0x77, 0x06, 0x67, 0x6F, 0x6F, 0x67, 0x6c, 0x65,
            0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name */
            0x00, 0x01, /* Type */
            0x00, 0x01, /* Class */
            /* Answers */
            (byte) 0xc0, 0x0c, /* Name */
            0x00, 0x01, /* Type */
            0x00, 0x01, /* Class */
            0x00, 0x00, 0x01, 0x2b, /* TTL */
            0x00, 0x04, /* Data length */
            (byte) 0xac, (byte) 0xd9, (byte) 0xa1, (byte) 0x84 /* Address */
        };
        TestDnsPacket packet = new TestDnsPacket(v4blob);

        // Header part
        assertHeaderParses(packet.getHeader(), 0x5566, 0x8180, 1, 1, 0, 0);

        // Record part
        List<DnsPacket.DnsRecord> qdRecordList =
                packet.getRecordList(DnsPacket.QDSECTION);
        assertEquals(qdRecordList.size(), 1);
        assertRecordParses(qdRecordList.get(0), "www.google.com", 1, 1, 0, null);

        List<DnsPacket.DnsRecord> anRecordList =
                packet.getRecordList(DnsPacket.ANSECTION);
        assertEquals(anRecordList.size(), 1);
        assertRecordParses(anRecordList.get(0), "www.google.com", 1, 1, 0x12b,
                new byte[]{ (byte) 0xac, (byte) 0xd9, (byte) 0xa1, (byte) 0x84 });
    }

    @Test
    public void testV6Answer() throws Exception {
        final byte[] v6blob = new byte[] {
            /* Header */
            0x77, 0x22, /* Transaction ID */
            (byte) 0x81, (byte) 0x80, /* Flags */
            0x00, 0x01, /* Questions */
            0x00, 0x01, /* Answer RRs */
            0x00, 0x00, /* Authority RRs */
            0x00, 0x00, /* Additional RRs */
            /* Queries */
            0x03, 0x77, 0x77, 0x77, 0x06, 0x67, 0x6F, 0x6F, 0x67, 0x6c, 0x65,
            0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name */
            0x00, 0x1c, /* Type */
            0x00, 0x01, /* Class */
            /* Answers */
            (byte) 0xc0, 0x0c, /* Name */
            0x00, 0x1c, /* Type */
            0x00, 0x01, /* Class */
            0x00, 0x00, 0x00, 0x37, /* TTL */
            0x00, 0x10, /* Data length */
            0x24, 0x04, 0x68, 0x00, 0x40, 0x05, 0x08, 0x0d,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x04 /* Address */
        };
        TestDnsPacket packet = new TestDnsPacket(v6blob);

        // Header part
        assertHeaderParses(packet.getHeader(), 0x7722, 0x8180, 1, 1, 0, 0);

        // Record part
        List<DnsPacket.DnsRecord> qdRecordList =
                packet.getRecordList(DnsPacket.QDSECTION);
        assertEquals(qdRecordList.size(), 1);
        assertRecordParses(qdRecordList.get(0), "www.google.com", 28, 1, 0, null);

        List<DnsPacket.DnsRecord> anRecordList =
                packet.getRecordList(DnsPacket.ANSECTION);
        assertEquals(anRecordList.size(), 1);
        assertRecordParses(anRecordList.get(0), "www.google.com", 28, 1, 0x37,
                new byte[]{ 0x24, 0x04, 0x68, 0x00, 0x40, 0x05, 0x08, 0x0d,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x04 });
    }

    /** Verifies that the synthesized {@link DnsPacket.DnsHeader} can be parsed correctly. */
    @Test
    public void testDnsHeaderSynthesize() {
        final DnsPacket.DnsHeader testHeader = new DnsPacket.DnsHeader(TEST_DNS_PACKET_ID,
                TEST_DNS_PACKET_FLAGS, 3 /* qcount */, 5 /* ancount */);
        final DnsPacket.DnsHeader actualHeader = new DnsPacket.DnsHeader(
                ByteBuffer.wrap(testHeader.getBytes()));
        assertEquals(testHeader, actualHeader);
    }

    /** Verifies that the synthesized {@link DnsPacket.DnsRecord} can be parsed correctly. */
    @Test
    public void testDnsRecordSynthesize() throws IOException {
        assertDnsRecordRoundTrip(
                DnsPacket.DnsRecord.makeAOrAAAARecord(DnsPacket.ANSECTION,
                        "test.com", CLASS_IN, 5 /* ttl */,
                        InetAddressUtils.parseNumericAddress("abcd::fedc")));
        assertDnsRecordRoundTrip(DnsPacket.DnsRecord.makeQuestion("test.com", TYPE_AAAA, CLASS_IN));
        assertDnsRecordRoundTrip(DnsPacket.DnsRecord.makeCNameRecord(DnsPacket.ANSECTION,
                "test.com", CLASS_IN, 0 /* ttl */, "example.com"));
    }

    /**
     * Verifies ttl/rData error handling when parsing
     * {@link DnsPacket.DnsRecord} from bytes.
     */
    @Test
    public void testDnsRecordTTLRDataErrorHandling() throws IOException {
        // Verify the constructor ignore ttl/rData of questions even if they are supplied.
        final byte[] qdWithTTLRData = new byte[]{
                0x03, 0x77, 0x77, 0x77, 0x06, 0x67, 0x6F, 0x6F, 0x67, 0x6c, 0x65,
                0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name */
                0x00, 0x00, /* Type */
                0x00, 0x01, /* Class */
                0x00, 0x00, 0x01, 0x2b, /* TTL */
                0x00, 0x04, /* Data length */
                (byte) 0xac, (byte) 0xd9, (byte) 0xa1, (byte) 0x84 /* Address */};
        final DnsPacket.DnsRecord questionsFromBytes =
                DnsPacket.DnsRecord.parse(DnsPacket.QDSECTION, ByteBuffer.wrap(qdWithTTLRData));
        assertEquals(0, questionsFromBytes.ttl);
        assertNull(questionsFromBytes.getRR());

        // Verify ANSECTION must have rData when constructing.
        final byte[] anWithoutTTLRData = new byte[]{
                0x03, 0x77, 0x77, 0x77, 0x06, 0x67, 0x6F, 0x6F, 0x67, 0x6c, 0x65,
                0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name */
                0x00, 0x01, /* Type */
                0x00, 0x01, /* Class */};
        assertThrows(BufferUnderflowException.class, () ->
                DnsPacket.DnsRecord.parse(DnsPacket.ANSECTION, ByteBuffer.wrap(anWithoutTTLRData)));
    }

    private void assertDnsRecordRoundTrip(DnsPacket.DnsRecord before)
            throws IOException {
        final DnsPacket.DnsRecord after = DnsPacket.DnsRecord.parse(before.rType,
                ByteBuffer.wrap(before.getBytes()));
        assertEquals(after, before);
    }

    /** Verifies that the synthesized {@link DnsPacket} can be parsed correctly. */
    @Test
    public void testDnsPacketSynthesize() throws IOException {
        // Ipv4 dns response packet generated by scapy:
        //   dns_r = scapy.DNS(
        //      id=0xbeef,
        //      qr=1,
        //      qd=scapy.DNSQR(qname="hello.example.com"),
        //      an=scapy.DNSRR(rrname="hello.example.com", type="CNAME", rdata='test.com') /
        //      scapy.DNSRR(rrname="hello.example.com", rdata='1.2.3.4'))
        //   scapy.hexdump(dns_r)
        //   dns_r.show2()
        // Note that since the synthesizing does not support name compression yet, the domain
        // name of the sample need to be uncompressed when generating.
        final byte[] v4BlobUncompressed = new byte[]{
                /* Header */
                (byte) 0xbe, (byte) 0xef, /* Transaction ID */
                (byte) 0x81, 0x00, /* Flags */
                0x00, 0x01, /* Questions */
                0x00, 0x02, /* Answer RRs */
                0x00, 0x00, /* Authority RRs */
                0x00, 0x00, /* Additional RRs */
                /* Queries */
                0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x07, 0x65, 0x78, 0x61,
                0x6D, 0x70, 0x6C, 0x65, 0x03, 0x63, 0x6F, 0x6D, 0x00, /* Name: hello.example.com */
                0x00, 0x01, /* Type */
                0x00, 0x01, /* Class */
                /* Answers */
                0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x07, 0x65, 0x78, 0x61,
                0x6D, 0x70, 0x6C, 0x65, 0x03, 0x63, 0x6F, 0x6D, 0x00, /* Name: hello.example.com */
                0x00, 0x05, /* Type */
                0x00, 0x01, /* Class */
                0x00, 0x00, 0x00, 0x00, /* TTL */
                0x00, 0x0A, /* Data length */
                0x04, 0x74, 0x65, 0x73, 0x74, 0x03, 0x63, 0x6F, 0x6D, 0x00, /* Alias: test.com */
                0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x07, 0x65, 0x78, 0x61,
                0x6D, 0x70, 0x6C, 0x65, 0x03, 0x63, 0x6F, 0x6D, 0x00, /* Name: hello.example.com */
                0x00, 0x01, /* Type */
                0x00, 0x01, /* Class */
                0x00, 0x00, 0x00, 0x00, /* TTL */
                0x00, 0x04, /* Data length */
                0x01, 0x02, 0x03, 0x04, /* Address: 1.2.3.4 */
        };

        // Forge one via constructors.
        final DnsPacket.DnsHeader testHeader = new DnsPacket.DnsHeader(0xbeef,
                0x8100, 1 /* qcount */, 2 /* ancount */);
        final ArrayList<DnsPacket.DnsRecord> qlist = new ArrayList<>();
        final ArrayList<DnsPacket.DnsRecord> alist = new ArrayList<>();
        qlist.add(DnsPacket.DnsRecord.makeQuestion(
                "hello.example.com", TYPE_A, CLASS_IN));
        alist.add(DnsPacket.DnsRecord.makeCNameRecord(
                DnsPacket.ANSECTION, "hello.example.com", CLASS_IN, 0 /* ttl */, "test.com"));
        alist.add(DnsPacket.DnsRecord.makeAOrAAAARecord(
                DnsPacket.ANSECTION, "hello.example.com", CLASS_IN, 0 /* ttl */,
                InetAddressUtils.parseNumericAddress("1.2.3.4")));
        final TestDnsPacket testPacket = new TestDnsPacket(testHeader, qlist, alist);

        // Assert content equals in both ways.
        assertTrue(Arrays.equals(v4BlobUncompressed, testPacket.getBytes()));
        assertEquals(new TestDnsPacket(v4BlobUncompressed), testPacket);
    }

    @Test
    public void testDnsPacketSynthesize_recordCountMismatch() throws IOException {
        final DnsPacket.DnsHeader testHeader = new DnsPacket.DnsHeader(0xbeef,
                0x8100, 1 /* qcount */, 1 /* ancount */);
        final ArrayList<DnsPacket.DnsRecord> qlist = new ArrayList<>();
        final ArrayList<DnsPacket.DnsRecord> alist = new ArrayList<>();
        qlist.add(DnsPacket.DnsRecord.makeQuestion(
                "hello.example.com", TYPE_A, CLASS_IN));

        // Assert throws if the supplied answer records fewer than the declared count.
        assertThrows(IllegalArgumentException.class, () ->
                new TestDnsPacket(testHeader, qlist, alist));

        // Assert throws if the supplied answer records more than the declared count.
        alist.add(DnsPacket.DnsRecord.makeCNameRecord(
                DnsPacket.ANSECTION, "hello.example.com", CLASS_IN, 0 /* ttl */, "test.com"));
        alist.add(DnsPacket.DnsRecord.makeAOrAAAARecord(
                DnsPacket.ANSECTION, "hello.example.com", CLASS_IN, 0 /* ttl */,
                InetAddressUtils.parseNumericAddress("1.2.3.4")));
        assertThrows(IllegalArgumentException.class, () ->
                new TestDnsPacket(testHeader, qlist, alist));

        // Assert counts matched if the byte buffer still has data when parsing ended.
        final byte[] blobTooMuchData = new byte[]{
                /* Header */
                (byte) 0xbe, (byte) 0xef, /* Transaction ID */
                (byte) 0x81, 0x00, /* Flags */
                0x00, 0x00, /* Questions */
                0x00, 0x00, /* Answer RRs */
                0x00, 0x00, /* Authority RRs */
                0x00, 0x00, /* Additional RRs */
                /* Queries */
                0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x07, 0x65, 0x78, 0x61,
                0x6D, 0x70, 0x6C, 0x65, 0x03, 0x63, 0x6F, 0x6D, 0x00, /* Name */
                0x00, 0x01, /* Type */
                0x00, 0x01, /* Class */
        };
        final TestDnsPacket packetFromTooMuchData = new TestDnsPacket(blobTooMuchData);
        for (int i = 0; i < DnsPacket.NUM_SECTIONS; i++) {
            assertEquals(0, packetFromTooMuchData.getRecordList(i).size());
            assertEquals(0, packetFromTooMuchData.getHeader().getRecordCount(i));
        }

        // Assert throws if the byte buffer ended when expecting more records.
        final byte[] blobNotEnoughData = new byte[]{
                /* Header */
                (byte) 0xbe, (byte) 0xef, /* Transaction ID */
                (byte) 0x81, 0x00, /* Flags */
                0x00, 0x01, /* Questions */
                0x00, 0x02, /* Answer RRs */
                0x00, 0x00, /* Authority RRs */
                0x00, 0x00, /* Additional RRs */
                /* Queries */
                0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x07, 0x65, 0x78, 0x61,
                0x6D, 0x70, 0x6C, 0x65, 0x03, 0x63, 0x6F, 0x6D, 0x00, /* Name */
                0x00, 0x01, /* Type */
                0x00, 0x01, /* Class */
                /* Answers */
                0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x07, 0x65, 0x78, 0x61,
                0x6D, 0x70, 0x6C, 0x65, 0x03, 0x63, 0x6F, 0x6D, 0x00, /* Name */
                0x00, 0x01, /* Type */
                0x00, 0x01, /* Class */
                0x00, 0x00, 0x00, 0x00, /* TTL */
                0x00, 0x04, /* Data length */
                0x01, 0x02, 0x03, 0x04, /* Address */
        };
        assertThrows(DnsPacket.ParseException.class, () -> new TestDnsPacket(blobNotEnoughData));
    }

    @Test
    public void testEqualsAndHashCode() throws IOException {
        // Verify DnsHeader equals and hashCode.
        final DnsPacket.DnsHeader testHeader = new DnsPacket.DnsHeader(TEST_DNS_PACKET_ID,
                TEST_DNS_PACKET_FLAGS, 1 /* qcount */, 1 /* ancount */);
        final DnsPacket.DnsHeader emptyHeader = new DnsPacket.DnsHeader(TEST_DNS_PACKET_ID + 1,
                TEST_DNS_PACKET_FLAGS + 0x08, 0 /* qcount */, 0 /* ancount */);
        final DnsPacket.DnsHeader headerFromBytes =
                new DnsPacket.DnsHeader(ByteBuffer.wrap(testHeader.getBytes()));
        assertEquals(testHeader, headerFromBytes);
        assertEquals(testHeader.hashCode(), headerFromBytes.hashCode());
        assertNotEquals(testHeader, emptyHeader);
        assertNotEquals(testHeader.hashCode(), emptyHeader.hashCode());
        assertNotEquals(headerFromBytes, emptyHeader);
        assertNotEquals(headerFromBytes.hashCode(), emptyHeader.hashCode());

        // Verify DnsRecord equals and hashCode.
        final DnsPacket.DnsRecord testQuestion = DnsPacket.DnsRecord.makeQuestion(
                "test.com", TYPE_AAAA, CLASS_IN);
        final DnsPacket.DnsRecord testAnswer = DnsPacket.DnsRecord.makeCNameRecord(
                DnsPacket.ANSECTION, "test.com", CLASS_IN, 9, "www.test.com");
        final DnsPacket.DnsRecord questionFromBytes = DnsPacket.DnsRecord.parse(DnsPacket.QDSECTION,
                ByteBuffer.wrap(testQuestion.getBytes()));
        assertEquals(testQuestion, questionFromBytes);
        assertEquals(testQuestion.hashCode(), questionFromBytes.hashCode());
        assertNotEquals(testQuestion, testAnswer);
        assertNotEquals(testQuestion.hashCode(), testAnswer.hashCode());
        assertNotEquals(questionFromBytes, testAnswer);
        assertNotEquals(questionFromBytes.hashCode(), testAnswer.hashCode());

        // Verify DnsPacket equals and hashCode.
        final ArrayList<DnsPacket.DnsRecord> qlist = new ArrayList<>();
        final ArrayList<DnsPacket.DnsRecord> alist = new ArrayList<>();
        qlist.add(testQuestion);
        alist.add(testAnswer);
        final TestDnsPacket testPacket = new TestDnsPacket(testHeader, qlist, alist);
        final TestDnsPacket emptyPacket = new TestDnsPacket(
                emptyHeader, new ArrayList<>(), new ArrayList<>());
        final TestDnsPacket packetFromBytes = new TestDnsPacket(testPacket.getBytes());
        assertEquals(testPacket, packetFromBytes);
        assertEquals(testPacket.hashCode(), packetFromBytes.hashCode());
        assertNotEquals(testPacket, emptyPacket);
        assertNotEquals(testPacket.hashCode(), emptyPacket.hashCode());
        assertNotEquals(packetFromBytes, emptyPacket);
        assertNotEquals(packetFromBytes.hashCode(), emptyPacket.hashCode());

        // Verify DnsPacket with empty list.
        final TestDnsPacket emptyPacketFromBytes = new TestDnsPacket(emptyPacket.getBytes());
        assertEquals(emptyPacket, emptyPacketFromBytes);
        assertEquals(emptyPacket.hashCode(), emptyPacketFromBytes.hashCode());
    }
}