1 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 ; 13 import java.util.zip.Adler32 ; 14 15 import junit.framework.TestCase; 16 17 20 public class WireProtocolHeaderTest extends TestCase { 21 22 private static byte[] goodHeader = { (byte) 0x17, (byte) 2, (byte) 3, (byte) 1, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0, (byte) 0, (byte) 0, (byte) 0xFF, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0xFF, (byte) 1, (byte) 0xFF, (byte) 1, (byte) 1, (byte) 0xFF, (byte) 1, (byte) 0xFF, (byte) 0xAA, (byte) 0x55, (byte) 0x55, (byte) 0xAA, }; 34 35 static { 36 38 Adler32 adler = new Adler32 (); 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 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 e) { 85 exception = true; 86 } 87 assertTrue(exception); 88 89 exception = false; 90 try { 91 header.setVersion((byte) 16); 92 } catch (Exception e) { 93 exception = true; 94 } 95 assertTrue(exception); 96 97 exception = false; 98 try { 99 header.setVersion((byte) -1); 100 } catch (Exception 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 header.setVersion((byte) (header.getVersion() + 1)); 141 assertFalse(header.isChecksumValid()); 142 143 header.computeChecksum(); 145 assertTrue(header.isChecksumValid()); 146 } 147 148 } | Popular Tags |