📄 addressattributetest.java
字号:
new byte[]{0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0}));
assertTrue("Port was not xorred",
testAddress.getPort() != xorredAddr.getPort());
//Test xor-ing the original with the xored - should get the xor code
addressAttribute.setAddress(testAddress);
xorredAddr =
addressAttribute.applyXor(new byte[]{21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36});
xorredAddr =
addressAttribute.applyXor(xorredAddr.getAddressBytes());
assertTrue("Xorring the original with the xor-ed didn't return the code..",
Arrays.equals(
xorredAddr.getAddressBytes(),
new byte[]{21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36}));
assertTrue("Port was not xorred",
testAddress.getPort() != 0xFFFF);
//Test double xor-ing - should get the original
addressAttribute.setAddress(testAddress);
xorredAddr =
addressAttribute.applyXor(new byte[]{21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36});
addressAttribute.setAddress(xorredAddr);
xorredAddr =
addressAttribute.applyXor(new byte[]{21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36});
assertEquals("Double xorring didn't give the original ...",
testAddress, xorredAddr);
}
/**
* Test whetner sample binary arrays are correctly decoded.
* @throws StunException
*/
public void testDecodeAttributeBody() throws StunException {
byte[] attributeValue = msgFixture.mappedAddress;
char offset = Attribute.HEADER_LENGTH;
char length = (char)(attributeValue.length - offset);
addressAttribute.decodeAttributeBody(attributeValue, offset, length);
assertEquals("AddressAttribute.decode() did not properly decode the port field.",
msgFixture.ADDRESS_ATTRIBUTE_PORT,
addressAttribute.getPort());
assertTrue("AddressAttribute.decode() did not properly decode the address field.",
Arrays.equals( msgFixture.ADDRESS_ATTRIBUTE_ADDRESS,
addressAttribute.getAddressBytes()));
}
/**
* Test whetner sample binary arrays are correctly decoded.
* @throws StunException
*/
public void testDecodeAttributeBodyv6() throws StunException {
byte[] attributeValue = msgFixture.mappedAddressv6;
char offset = Attribute.HEADER_LENGTH;
char length = (char)(attributeValue.length - offset);
addressAttribute.decodeAttributeBody(attributeValue, offset, length);
assertEquals("decode() failed for an IPv6 Addr's port.",
msgFixture.ADDRESS_ATTRIBUTE_PORT,
addressAttribute.getPort());
assertTrue("AddressAttribute.decode() failed for an IPv6 address.",
Arrays.equals( msgFixture.ADDRESS_ATTRIBUTE_ADDRESS_V6,
addressAttribute.getAddressBytes()));
}
/**
* Test whether attributes are properly encoded.
*
* @throws StunException java.lang.Exception if we fail
*/
public void testEncode()
throws StunException
{
byte[] expectedReturn = msgFixture.mappedAddress;
addressAttribute.setAddress(
new StunAddress(msgFixture.ADDRESS_ATTRIBUTE_ADDRESS,
msgFixture.ADDRESS_ATTRIBUTE_PORT));
byte[] actualReturn = addressAttribute.encode();
assertTrue("AddressAttribute.encode() did not properly encode a sample attribute",
Arrays.equals( expectedReturn, actualReturn));
}
/**
* Test whether attributes are properly encoded.
*
* @throws StunException java.lang.Exception if we fail
*/
public void testEncodev6()
throws StunException
{
byte[] expectedReturn = msgFixture.mappedAddressv6;
addressAttribute.setAddress(
new StunAddress(msgFixture.ADDRESS_ATTRIBUTE_ADDRESS_V6,
msgFixture.ADDRESS_ATTRIBUTE_PORT));
byte[] actualReturn = addressAttribute.encode();
assertTrue("An AddressAttribute did not properly encode an IPv6 addr.",
Arrays.equals( expectedReturn, actualReturn));
}
/**
* Tests the equals method against a null, a different and an identical
* object.
*
* @throws StunException java.lang.Exception if we fail
*/
public void testEquals()
throws StunException
{
//null test
AddressAttribute target = null;
boolean expectedReturn = false;
boolean actualReturn = addressAttribute.equals(target);
assertEquals("AddressAttribute.equals() failed against a null target.",
expectedReturn, actualReturn);
//difference test
target = new MappedAddressAttribute();
char port = (char)(msgFixture.ADDRESS_ATTRIBUTE_PORT + 1 );
target.setAddress( new StunAddress(msgFixture.ADDRESS_ATTRIBUTE_ADDRESS,
port));
addressAttribute.setAddress(
new StunAddress(msgFixture.ADDRESS_ATTRIBUTE_ADDRESS,
(char)msgFixture.ADDRESS_ATTRIBUTE_PORT ));
expectedReturn = false;
actualReturn = addressAttribute.equals(target);
assertEquals("AddressAttribute.equals() failed against a different target.",
expectedReturn, actualReturn);
//equality test
target.setAddress( new StunAddress( msgFixture.ADDRESS_ATTRIBUTE_ADDRESS,
(char)msgFixture.ADDRESS_ATTRIBUTE_PORT ));
expectedReturn = true;
actualReturn = addressAttribute.equals(target);
assertEquals("AddressAttribute.equals() failed against an equal target.",
expectedReturn, actualReturn);
//ipv6 equality test
target.setAddress( new StunAddress(msgFixture.ADDRESS_ATTRIBUTE_ADDRESS_V6,
(char)msgFixture.ADDRESS_ATTRIBUTE_PORT));
addressAttribute.setAddress(
new StunAddress(msgFixture.ADDRESS_ATTRIBUTE_ADDRESS_V6,
(char)msgFixture.ADDRESS_ATTRIBUTE_PORT ));
expectedReturn = true;
actualReturn = addressAttribute.equals(target);
assertEquals("AddressAttribute.equals() failed for IPv6 addresses.",
expectedReturn, actualReturn);
}
/**
* Tests whether data length is properly calculated.
*
* @throws StunException java.lang.Exception if we fail
*/
public void testGetDataLength()
throws StunException
{
char expectedReturn = 8;//1-padding + 1-family + 2-port + 4-address
addressAttribute.setAddress(
new StunAddress( msgFixture.ADDRESS_ATTRIBUTE_ADDRESS,
msgFixture.ADDRESS_ATTRIBUTE_PORT));
char actualReturn = addressAttribute.getDataLength();
assertEquals("Datalength is not propoerly calculated",
expectedReturn, actualReturn);
expectedReturn = 20;//1-padding + 1-family + 2-port + 16-address
addressAttribute.setAddress(
new StunAddress( msgFixture.ADDRESS_ATTRIBUTE_ADDRESS_V6,
msgFixture.ADDRESS_ATTRIBUTE_PORT));
actualReturn = addressAttribute.getDataLength();
assertEquals("Datalength is not propoerly calculated",
expectedReturn, actualReturn);
}
/**
* Tests that the address family is always 1.
*/
public void testGetFamily() {
byte expectedReturn = 1;
addressAttribute.setAddress(
new StunAddress( msgFixture.ADDRESS_ATTRIBUTE_ADDRESS,
msgFixture.ADDRESS_ATTRIBUTE_PORT));
byte actualReturn = addressAttribute.getFamily();
assertEquals("Address family was not 1 for an IPv4",
expectedReturn, actualReturn);
//ipv6
expectedReturn = 2;
addressAttribute.setAddress(
new StunAddress( msgFixture.ADDRESS_ATTRIBUTE_ADDRESS_V6,
msgFixture.ADDRESS_ATTRIBUTE_PORT));
actualReturn = addressAttribute.getFamily();
assertEquals("Address family was not 2 for an IPv6 address",
expectedReturn, actualReturn);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -