View Javadoc

1   /**
2    *
3    */
4   package javax.jmdns.test;
5   
6   import static junit.framework.Assert.assertEquals;
7   import static junit.framework.Assert.assertNotNull;
8   import static junit.framework.Assert.assertTrue;
9   
10  import java.io.IOException;
11  import java.net.DatagramPacket;
12  import java.util.Date;
13  
14  import javax.jmdns.impl.DNSIncoming;
15  import javax.jmdns.impl.DNSOutgoing;
16  import javax.jmdns.impl.DNSQuestion;
17  import javax.jmdns.impl.DNSRecord;
18  import javax.jmdns.impl.constants.DNSConstants;
19  import javax.jmdns.impl.constants.DNSRecordClass;
20  import javax.jmdns.impl.constants.DNSRecordType;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  /**
26   *
27   */
28  public class DNSMessageTest {
29  
30      @Before
31      public void setup() {
32          //
33      }
34  
35      @Test
36      public void testCreateQuery() throws IOException {
37          String serviceName = "_00000000-0b44-f234-48c8-071c565644b3._sub._home-sharing._tcp.local.";
38          DNSOutgoing out = new DNSOutgoing(DNSConstants.FLAGS_QR_QUERY);
39          assertNotNull("Could not create the outgoing message", out);
40          out.addQuestion(DNSQuestion.newQuestion(serviceName, DNSRecordType.TYPE_ANY, DNSRecordClass.CLASS_IN, true));
41          byte[] data = out.data();
42          assertNotNull("Could not encode the outgoing message", data);
43          byte[] expected = new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x25, 0x5f, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x2d, 0x30, 0x62, 0x34, 0x34, 0x2d, 0x66, 0x32, 0x33, 0x34, 0x2d, 0x34, 0x38, 0x63, 0x38,
44                  0x2d, 0x30, 0x37, 0x31, 0x63, 0x35, 0x36, 0x35, 0x36, 0x34, 0x34, 0x62, 0x33, 0x4, 0x5f, 0x73, 0x75, 0x62, 0xd, 0x5f, 0x68, 0x6f, 0x6d, 0x65, 0x2d, 0x73, 0x68, 0x61, 0x72, 0x69, 0x6e, 0x67, 0x4, 0x5f, 0x74, 0x63, 0x70, 0x5, 0x6c,
45                  0x6f, 0x63, 0x61, 0x6c, 0x0, 0x0, (byte) 0xff, 0x0, 0x1 };
46          for (int i = 0; i < data.length; i++) {
47              assertEquals("the encoded message is not what is expected at index " + i, expected[i], data[i]);
48          }
49          DatagramPacket packet = new DatagramPacket(data, 0, data.length);
50          DNSIncoming in = new DNSIncoming(packet);
51          assertTrue("Wrong packet type.", in.isQuery());
52          assertEquals("Wrong number of questions.", 1, in.getNumberOfQuestions());
53          for (DNSQuestion question : in.getQuestions()) {
54              assertEquals("Wrong question name.", serviceName, question.getName());
55          }
56      }
57  
58      @Test
59      public void testCreateAnswer() throws IOException {
60          String serviceType = "_home-sharing._tcp.local.";
61          String serviceName = "Pierre." + serviceType;
62          DNSOutgoing out = new DNSOutgoing(DNSConstants.FLAGS_QR_RESPONSE | DNSConstants.FLAGS_AA, false);
63          assertNotNull("Could not create the outgoing message", out);
64          out.addQuestion(DNSQuestion.newQuestion(serviceName, DNSRecordType.TYPE_ANY, DNSRecordClass.CLASS_IN, true));
65          long now = (new Date()).getTime();
66          out.addAnswer(new DNSRecord.Pointer(serviceType, DNSRecordClass.CLASS_IN, true, DNSConstants.DNS_TTL, serviceName), now);
67          out.addAuthorativeAnswer(new DNSRecord.Service(serviceType, DNSRecordClass.CLASS_IN, true, DNSConstants.DNS_TTL, 1, 20, 8080, "panoramix.local."));
68          byte[] data = out.data();
69          assertNotNull("Could not encode the outgoing message", data);
70          // byte[] expected = new byte[] { 0x0, 0x0, (byte) 0x84, 0x0, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x6, 0x50, 0x69, 0x65, 0x72, 0x72, 0x65, 0xd, 0x5f, 0x68, 0x6f, 0x6d, 0x65, 0x2d, 0x73, 0x68, 0x61, 0x72, 0x69, 0x6e, 0x67, 0x4, 0x5f, 0x74,
71          // 0x63, 0x70, 0x5, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x0, 0x0, (byte) 0xff, 0x0, 0x1, (byte) 0xc0, 0x13, 0x0, 0xc, 0x0, 0x1, 0x0, 0x0, 0xe, 0xf, 0x0, 0x21, 0x6, 0x50, 0x69, 0x65, 0x72, 0x72, 0x65, 0xd, 0x5f, 0x68, 0x6f, 0x6d, 0x65, 0x2d,
72          // 0x73, 0x68, 0x61, 0x72, 0x69, 0x6e, 0x67, 0x4, 0x5f, 0x74, 0x63, 0x70, 0x5, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x0, (byte) 0xc0, 0x13, 0x0, 0x21, 0x0, 0x1, 0x0, 0x0, 0xe, 0xf, 0x0, 0x17, 0x0, 0x1, 0x0, 0x14, 0x1f, (byte) 0x90, 0x9, 0x70,
73          // 0x61, 0x6e, 0x6f, 0x72, 0x61, 0x6d, 0x69, 0x78, 0x5, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x0 };
74          // for (int i = 0; i < data.length; i++)
75          // {
76          // assertEquals("the encoded message is not what is expected at index " + i, expected[i], data[i]);
77          // }
78          DatagramPacket packet = new DatagramPacket(data, 0, data.length);
79          DNSIncoming in = new DNSIncoming(packet);
80          assertTrue("Wrong packet type.", in.isResponse());
81          assertEquals("Wrong number of questions.", 1, in.getNumberOfQuestions());
82          assertEquals("Wrong number of answers.", 1, in.getNumberOfAnswers());
83          assertEquals("Wrong number of authorities.", 1, in.getNumberOfAuthorities());
84          for (DNSQuestion question : in.getQuestions()) {
85              assertEquals("Wrong question name.", serviceName, question.getName());
86          }
87      }
88  
89      protected void print(byte[] data) {
90          System.out.print("{");
91          for (int i = 0; i < data.length; i++) {
92              int value = data[i] & 0xFF;
93              if (i > 0) {
94                  System.out.print(",");
95              }
96              System.out.print(" 0x");
97              System.out.print(Integer.toHexString(value));
98              if (i % 20 == 0) {
99                  System.out.print("\n");
100             }
101         }
102         System.out.print("}\n");
103     }
104 }