Clover Coverage Report - JmDNS 3.4.1
Coverage timestamp: Thu Aug 25 2011 13:06:33 CEST
43   104   8   10.75
8   80   0.19   4
4     2  
1    
 
  DNSMessageTest       Line # 28 43 0% 8 17 69.1% 0.6909091
 
  (2)
 
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  2 toggle @Before
31    public void setup() {
32    //
33    }
34   
 
35  1 toggle @Test
36    public void testCreateQuery() throws IOException {
37  1 String serviceName = "_00000000-0b44-f234-48c8-071c565644b3._sub._home-sharing._tcp.local.";
38  1 DNSOutgoing out = new DNSOutgoing(DNSConstants.FLAGS_QR_QUERY);
39  1 assertNotNull("Could not create the outgoing message", out);
40  1 out.addQuestion(DNSQuestion.newQuestion(serviceName, DNSRecordType.TYPE_ANY, DNSRecordClass.CLASS_IN, true));
41  1 byte[] data = out.data();
42  1 assertNotNull("Could not encode the outgoing message", data);
43  1 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  86 for (int i = 0; i < data.length; i++) {
47  85 assertEquals("the encoded message is not what is expected at index " + i, expected[i], data[i]);
48    }
49  1 DatagramPacket packet = new DatagramPacket(data, 0, data.length);
50  1 DNSIncoming in = new DNSIncoming(packet);
51  1 assertTrue("Wrong packet type.", in.isQuery());
52  1 assertEquals("Wrong number of questions.", 1, in.getNumberOfQuestions());
53  1 for (DNSQuestion question : in.getQuestions()) {
54  1 assertEquals("Wrong question name.", serviceName, question.getName());
55    }
56    }
57   
 
58  1 toggle @Test
59    public void testCreateAnswer() throws IOException {
60  1 String serviceType = "_home-sharing._tcp.local.";
61  1 String serviceName = "Pierre." + serviceType;
62  1 DNSOutgoing out = new DNSOutgoing(DNSConstants.FLAGS_QR_RESPONSE | DNSConstants.FLAGS_AA, false);
63  1 assertNotNull("Could not create the outgoing message", out);
64  1 out.addQuestion(DNSQuestion.newQuestion(serviceName, DNSRecordType.TYPE_ANY, DNSRecordClass.CLASS_IN, true));
65  1 long now = (new Date()).getTime();
66  1 out.addAnswer(new DNSRecord.Pointer(serviceType, DNSRecordClass.CLASS_IN, true, DNSConstants.DNS_TTL, serviceName), now);
67  1 out.addAuthorativeAnswer(new DNSRecord.Service(serviceType, DNSRecordClass.CLASS_IN, true, DNSConstants.DNS_TTL, 1, 20, 8080, "panoramix.local."));
68  1 byte[] data = out.data();
69  1 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  1 DatagramPacket packet = new DatagramPacket(data, 0, data.length);
79  1 DNSIncoming in = new DNSIncoming(packet);
80  1 assertTrue("Wrong packet type.", in.isResponse());
81  1 assertEquals("Wrong number of questions.", 1, in.getNumberOfQuestions());
82  1 assertEquals("Wrong number of answers.", 1, in.getNumberOfAnswers());
83  1 assertEquals("Wrong number of authorities.", 1, in.getNumberOfAuthorities());
84  1 for (DNSQuestion question : in.getQuestions()) {
85  1 assertEquals("Wrong question name.", serviceName, question.getName());
86    }
87    }
88   
 
89  0 toggle protected void print(byte[] data) {
90  0 System.out.print("{");
91  0 for (int i = 0; i < data.length; i++) {
92  0 int value = data[i] & 0xFF;
93  0 if (i > 0) {
94  0 System.out.print(",");
95    }
96  0 System.out.print(" 0x");
97  0 System.out.print(Integer.toHexString(value));
98  0 if (i % 20 == 0) {
99  0 System.out.print("\n");
100    }
101    }
102  0 System.out.print("}\n");
103    }
104    }