KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > transport > WireProtocolHeaderTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.net.protocol.transport;
5
6 import com.tc.bytes.TCByteBuffer;
7 import com.tc.bytes.TCByteBufferFactory;
8 import com.tc.net.protocol.transport.WireProtocolHeader;
9 import com.tc.net.protocol.transport.WireProtocolHeaderFormatException;
10 import com.tc.util.Conversion;
11
12 import java.util.Arrays JavaDoc;
13 import java.util.zip.Adler32 JavaDoc;
14
15 import junit.framework.TestCase;
16
17 /**
18  * @author teck
19  */

20 public class WireProtocolHeaderTest extends TestCase {
21
22   private static byte[] goodHeader = { // make formatter pretty
23
(byte) 0x17, // version == 1, length = 28
24
(byte) 2, // TOS == 2
25
(byte) 3, // TTL == 3
26
(byte) 1, // Protocol == 1
27
(byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, // Magic num
28
(byte) 0, (byte) 0, (byte) 0, (byte) 0xFF, // totalLen == 255
29
(byte) 0, (byte) 0, (byte) 0, (byte) 0, // adler initially zero
30
(byte) 0xFF, (byte) 1, (byte) 0xFF, (byte) 1, // source addr
31
(byte) 1, (byte) 0xFF, (byte) 1, (byte) 0xFF, // dest addr
32
(byte) 0xAA, (byte) 0x55, (byte) 0x55, (byte) 0xAA, // src/dest ports
33
};
34
35   static {
36     //IOFlavor.forceJDK13();
37

38     Adler32 JavaDoc adler = new Adler32 JavaDoc();
39     adler.update(goodHeader);
40     System.arraycopy(Conversion.uint2bytes(adler.getValue()), 0, goodHeader, 12, 4);
41   }
42
43   private byte[] getGoodHeader() {
44     byte[] rv = new byte[goodHeader.length];
45     System.arraycopy(goodHeader, 0, rv, 0, rv.length);
46     return rv;
47   }
48
49   // XXX: Finish any and all other tests!
50

51   public void testOptions() {
52     WireProtocolHeader header = new WireProtocolHeader();
53
54     byte oldLength = header.getHeaderLength();
55     header.setOptions(new byte[] {});
56     byte newLength = header.getHeaderLength();
57     assertTrue(newLength >= oldLength);
58     assertTrue(newLength == (WireProtocolHeader.MIN_LENGTH / 4));
59
60     byte[] maxOptions = new byte[WireProtocolHeader.MAX_LENGTH - WireProtocolHeader.MIN_LENGTH];
61     Arrays.fill(maxOptions, (byte) 0xFF);
62     header.setOptions(maxOptions);
63     assertTrue(header.getHeaderLength() * 4 == WireProtocolHeader.MAX_LENGTH);
64     Arrays.equals(maxOptions, header.getOptions());
65
66     header.setOptions(null);
67     assertTrue(header.getHeaderLength() * 4 == WireProtocolHeader.MIN_LENGTH);
68     assertTrue(header.getOptions().length == 0);
69   }
70
71   public void testVersion() {
72     WireProtocolHeader header = new WireProtocolHeader();
73
74     header.setVersion((byte) 15);
75     assertTrue(15 == header.getVersion());
76
77     header.setVersion(WireProtocolHeader.VERSION_1);
78     assertTrue(WireProtocolHeader.VERSION_1 == header.getVersion());
79
80     boolean exception = false;
81
82     try {
83       header.setVersion((byte) 0);
84     } catch (Exception JavaDoc e) {
85       exception = true;
86     }
87     assertTrue(exception);
88
89     exception = false;
90     try {
91       header.setVersion((byte) 16);
92     } catch (Exception JavaDoc e) {
93       exception = true;
94     }
95     assertTrue(exception);
96
97     exception = false;
98     try {
99       header.setVersion((byte) -1);
100     } catch (Exception JavaDoc e) {
101       exception = true;
102     }
103     assertTrue(exception);
104   }
105
106   public void testGoodHeader() {
107     byte[] data = getGoodHeader();
108     TCByteBuffer buffer = TCByteBufferFactory.getInstance(false, WireProtocolHeader.MAX_LENGTH);
109     buffer.put(data);
110     buffer.flip();
111
112     WireProtocolHeader header = new WireProtocolHeader(buffer);
113
114     System.out.println(header);
115
116     assertTrue(header.isChecksumValid());
117
118     assertTrue(header.getVersion() == 1);
119     assertTrue(header.getHeaderLength() == 7);
120     assertTrue(header.getTypeOfService() == 2);
121     assertTrue(header.getTimeToLive() == 3);
122
123     assertTrue(header.getTotalPacketLength() == 255);
124     assertTrue(header.getChecksum() == 1995114947);
125     assertTrue(Arrays.equals(header.getSourceAddress(), new byte[] { (byte) 0xFF, (byte) 1, (byte) 0xFF, (byte) 1 }));
126     assertTrue(Arrays.equals(header.getDestinationAddress(),
127                              new byte[] { (byte) 1, (byte) 0xFF, (byte) 1, (byte) 0xFF }));
128     assertTrue(header.getSourcePort() == 43605);
129     assertTrue(header.getDestinationPort() == 21930);
130
131     assertTrue(header.getOptions().length == 0);
132
133     try {
134       header.validate();
135     } catch (WireProtocolHeaderFormatException e) {
136       fail(e.getMessage());
137     }
138
139     // changing data in the header should cause the checksum to need to be recomputed
140
header.setVersion((byte) (header.getVersion() + 1));
141     assertFalse(header.isChecksumValid());
142
143     // Fix and validate the checksum
144
header.computeChecksum();
145     assertTrue(header.isChecksumValid());
146   }
147
148 }
Popular Tags