001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.net.protocol.transport;
005:
006: import com.tc.bytes.TCByteBuffer;
007: import com.tc.bytes.TCByteBufferFactory;
008: import com.tc.net.protocol.transport.WireProtocolHeader;
009: import com.tc.net.protocol.transport.WireProtocolHeaderFormatException;
010: import com.tc.util.Conversion;
011:
012: import java.util.Arrays;
013: import java.util.zip.Adler32;
014:
015: import junit.framework.TestCase;
016:
017: /**
018: * @author teck
019: */
020: public class WireProtocolHeaderTest extends TestCase {
021:
022: private static byte[] goodHeader = { // make formatter pretty
023: (byte) 0x17, // version == 1, length = 28
024: (byte) 2, // TOS == 2
025: (byte) 3, // TTL == 3
026: (byte) 1, // Protocol == 1
027: (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, // Magic num
028: (byte) 0, (byte) 0, (byte) 0, (byte) 0xFF, // totalLen == 255
029: (byte) 0, (byte) 0, (byte) 0, (byte) 0, // adler initially zero
030: (byte) 0xFF, (byte) 1, (byte) 0xFF, (byte) 1, // source addr
031: (byte) 1, (byte) 0xFF, (byte) 1, (byte) 0xFF, // dest addr
032: (byte) 0xAA, (byte) 0x55, (byte) 0x55, (byte) 0xAA, // src/dest ports
033: };
034:
035: static {
036: //IOFlavor.forceJDK13();
037:
038: Adler32 adler = new Adler32();
039: adler.update(goodHeader);
040: System.arraycopy(Conversion.uint2bytes(adler.getValue()), 0,
041: goodHeader, 12, 4);
042: }
043:
044: private byte[] getGoodHeader() {
045: byte[] rv = new byte[goodHeader.length];
046: System.arraycopy(goodHeader, 0, rv, 0, rv.length);
047: return rv;
048: }
049:
050: // XXX: Finish any and all other tests!
051:
052: public void testOptions() {
053: WireProtocolHeader header = new WireProtocolHeader();
054:
055: byte oldLength = header.getHeaderLength();
056: header.setOptions(new byte[] {});
057: byte newLength = header.getHeaderLength();
058: assertTrue(newLength >= oldLength);
059: assertTrue(newLength == (WireProtocolHeader.MIN_LENGTH / 4));
060:
061: byte[] maxOptions = new byte[WireProtocolHeader.MAX_LENGTH
062: - WireProtocolHeader.MIN_LENGTH];
063: Arrays.fill(maxOptions, (byte) 0xFF);
064: header.setOptions(maxOptions);
065: assertTrue(header.getHeaderLength() * 4 == WireProtocolHeader.MAX_LENGTH);
066: Arrays.equals(maxOptions, header.getOptions());
067:
068: header.setOptions(null);
069: assertTrue(header.getHeaderLength() * 4 == WireProtocolHeader.MIN_LENGTH);
070: assertTrue(header.getOptions().length == 0);
071: }
072:
073: public void testVersion() {
074: WireProtocolHeader header = new WireProtocolHeader();
075:
076: header.setVersion((byte) 15);
077: assertTrue(15 == header.getVersion());
078:
079: header.setVersion(WireProtocolHeader.VERSION_1);
080: assertTrue(WireProtocolHeader.VERSION_1 == header.getVersion());
081:
082: boolean exception = false;
083:
084: try {
085: header.setVersion((byte) 0);
086: } catch (Exception e) {
087: exception = true;
088: }
089: assertTrue(exception);
090:
091: exception = false;
092: try {
093: header.setVersion((byte) 16);
094: } catch (Exception e) {
095: exception = true;
096: }
097: assertTrue(exception);
098:
099: exception = false;
100: try {
101: header.setVersion((byte) -1);
102: } catch (Exception e) {
103: exception = true;
104: }
105: assertTrue(exception);
106: }
107:
108: public void testGoodHeader() {
109: byte[] data = getGoodHeader();
110: TCByteBuffer buffer = TCByteBufferFactory.getInstance(false,
111: WireProtocolHeader.MAX_LENGTH);
112: buffer.put(data);
113: buffer.flip();
114:
115: WireProtocolHeader header = new WireProtocolHeader(buffer);
116:
117: System.out.println(header);
118:
119: assertTrue(header.isChecksumValid());
120:
121: assertTrue(header.getVersion() == 1);
122: assertTrue(header.getHeaderLength() == 7);
123: assertTrue(header.getTypeOfService() == 2);
124: assertTrue(header.getTimeToLive() == 3);
125:
126: assertTrue(header.getTotalPacketLength() == 255);
127: assertTrue(header.getChecksum() == 1995114947);
128: assertTrue(Arrays.equals(header.getSourceAddress(), new byte[] {
129: (byte) 0xFF, (byte) 1, (byte) 0xFF, (byte) 1 }));
130: assertTrue(Arrays.equals(header.getDestinationAddress(),
131: new byte[] { (byte) 1, (byte) 0xFF, (byte) 1,
132: (byte) 0xFF }));
133: assertTrue(header.getSourcePort() == 43605);
134: assertTrue(header.getDestinationPort() == 21930);
135:
136: assertTrue(header.getOptions().length == 0);
137:
138: try {
139: header.validate();
140: } catch (WireProtocolHeaderFormatException e) {
141: fail(e.getMessage());
142: }
143:
144: // changing data in the header should cause the checksum to need to be recomputed
145: header.setVersion((byte) (header.getVersion() + 1));
146: assertFalse(header.isChecksumValid());
147:
148: // Fix and validate the checksum
149: header.computeChecksum();
150: assertTrue(header.isChecksumValid());
151: }
152:
153: }
|