• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 
23 import androidx.test.filters.SmallTest;
24 import androidx.test.runner.AndroidJUnit4;
25 
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 
29 import java.util.Arrays;
30 import java.util.List;
31 
32 @RunWith(AndroidJUnit4.class)
33 @SmallTest
34 public class DnsPacketTest {
assertHeaderParses(DnsPacket.DnsHeader header, int id, int flag, int qCount, int aCount, int nsCount, int arCount)35     private void assertHeaderParses(DnsPacket.DnsHeader header, int id, int flag,
36             int qCount, int aCount, int nsCount, int arCount) {
37         assertEquals(header.id, id);
38         assertEquals(header.flags, flag);
39         assertEquals(header.getRecordCount(DnsPacket.QDSECTION), qCount);
40         assertEquals(header.getRecordCount(DnsPacket.ANSECTION), aCount);
41         assertEquals(header.getRecordCount(DnsPacket.NSSECTION), nsCount);
42         assertEquals(header.getRecordCount(DnsPacket.ARSECTION), arCount);
43     }
44 
assertRecordParses(DnsPacket.DnsRecord record, String dname, int dtype, int dclass, int ttl, byte[] rr)45     private void assertRecordParses(DnsPacket.DnsRecord record, String dname,
46             int dtype, int dclass, int ttl, byte[] rr) {
47         assertEquals(record.dName, dname);
48         assertEquals(record.nsType, dtype);
49         assertEquals(record.nsClass, dclass);
50         assertEquals(record.ttl, ttl);
51         assertTrue(Arrays.equals(record.getRR(), rr));
52     }
53 
54     class TestDnsPacket extends DnsPacket {
TestDnsPacket(byte[] data)55         TestDnsPacket(byte[] data) throws ParseException {
56             super(data);
57         }
58 
getHeader()59         public DnsHeader getHeader() {
60             return mHeader;
61         }
getRecordList(int secType)62         public List<DnsRecord> getRecordList(int secType) {
63             return mRecords[secType];
64         }
65     }
66 
67     @Test
testNullDisallowed()68     public void testNullDisallowed() {
69         try {
70             new TestDnsPacket(null);
71             fail("Exception not thrown for null byte array");
72         } catch (ParseException e) {
73         }
74     }
75 
76     @Test
testV4Answer()77     public void testV4Answer() throws Exception {
78         final byte[] v4blob = new byte[] {
79             /* Header */
80             0x55, 0x66, /* Transaction ID */
81             (byte) 0x81, (byte) 0x80, /* Flags */
82             0x00, 0x01, /* Questions */
83             0x00, 0x01, /* Answer RRs */
84             0x00, 0x00, /* Authority RRs */
85             0x00, 0x00, /* Additional RRs */
86             /* Queries */
87             0x03, 0x77, 0x77, 0x77, 0x06, 0x67, 0x6F, 0x6F, 0x67, 0x6c, 0x65,
88             0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name */
89             0x00, 0x01, /* Type */
90             0x00, 0x01, /* Class */
91             /* Answers */
92             (byte) 0xc0, 0x0c, /* Name */
93             0x00, 0x01, /* Type */
94             0x00, 0x01, /* Class */
95             0x00, 0x00, 0x01, 0x2b, /* TTL */
96             0x00, 0x04, /* Data length */
97             (byte) 0xac, (byte) 0xd9, (byte) 0xa1, (byte) 0x84 /* Address */
98         };
99         TestDnsPacket packet = new TestDnsPacket(v4blob);
100 
101         // Header part
102         assertHeaderParses(packet.getHeader(), 0x5566, 0x8180, 1, 1, 0, 0);
103 
104         // Record part
105         List<DnsPacket.DnsRecord> qdRecordList =
106                 packet.getRecordList(DnsPacket.QDSECTION);
107         assertEquals(qdRecordList.size(), 1);
108         assertRecordParses(qdRecordList.get(0), "www.google.com", 1, 1, 0, null);
109 
110         List<DnsPacket.DnsRecord> anRecordList =
111                 packet.getRecordList(DnsPacket.ANSECTION);
112         assertEquals(anRecordList.size(), 1);
113         assertRecordParses(anRecordList.get(0), "www.google.com", 1, 1, 0x12b,
114                 new byte[]{ (byte) 0xac, (byte) 0xd9, (byte) 0xa1, (byte) 0x84 });
115     }
116 
117     @Test
testV6Answer()118     public void testV6Answer() throws Exception {
119         final byte[] v6blob = new byte[] {
120             /* Header */
121             0x77, 0x22, /* Transaction ID */
122             (byte) 0x81, (byte) 0x80, /* Flags */
123             0x00, 0x01, /* Questions */
124             0x00, 0x01, /* Answer RRs */
125             0x00, 0x00, /* Authority RRs */
126             0x00, 0x00, /* Additional RRs */
127             /* Queries */
128             0x03, 0x77, 0x77, 0x77, 0x06, 0x67, 0x6F, 0x6F, 0x67, 0x6c, 0x65,
129             0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name */
130             0x00, 0x1c, /* Type */
131             0x00, 0x01, /* Class */
132             /* Answers */
133             (byte) 0xc0, 0x0c, /* Name */
134             0x00, 0x1c, /* Type */
135             0x00, 0x01, /* Class */
136             0x00, 0x00, 0x00, 0x37, /* TTL */
137             0x00, 0x10, /* Data length */
138             0x24, 0x04, 0x68, 0x00, 0x40, 0x05, 0x08, 0x0d,
139             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x04 /* Address */
140         };
141         TestDnsPacket packet = new TestDnsPacket(v6blob);
142 
143         // Header part
144         assertHeaderParses(packet.getHeader(), 0x7722, 0x8180, 1, 1, 0, 0);
145 
146         // Record part
147         List<DnsPacket.DnsRecord> qdRecordList =
148                 packet.getRecordList(DnsPacket.QDSECTION);
149         assertEquals(qdRecordList.size(), 1);
150         assertRecordParses(qdRecordList.get(0), "www.google.com", 28, 1, 0, null);
151 
152         List<DnsPacket.DnsRecord> anRecordList =
153                 packet.getRecordList(DnsPacket.ANSECTION);
154         assertEquals(anRecordList.size(), 1);
155         assertRecordParses(anRecordList.get(0), "www.google.com", 28, 1, 0x37,
156                 new byte[]{ 0x24, 0x04, 0x68, 0x00, 0x40, 0x05, 0x08, 0x0d,
157                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x04 });
158     }
159 }
160