From e48e56303c6dcfd02499a9c5ebacf5daec66a51c Mon Sep 17 00:00:00 2001 From: paweldomas Date: Tue, 13 May 2014 19:49:45 +0000 Subject: [PATCH] Adds unit test for TURN attributes and minor fixes. Patch by Aakash Garg. --- .../attribute/ConnectionIdAttribute.java | 18 +- .../RequestedAddressFamilyAttribute.java | 8 +- test/org/ice4j/MsgFixture.java | 19 ++ test/org/ice4j/StunTestSuite.java | 4 + .../attribute/ConnectionIdAttributeTest.java | 169 +++++++++++++ .../RequestedAddressFamilyAttributeTest.java | 239 ++++++++++++++++++ 6 files changed, 447 insertions(+), 10 deletions(-) create mode 100644 test/org/ice4j/attribute/ConnectionIdAttributeTest.java create mode 100644 test/org/ice4j/attribute/RequestedAddressFamilyAttributeTest.java diff --git a/src/org/ice4j/attribute/ConnectionIdAttribute.java b/src/org/ice4j/attribute/ConnectionIdAttribute.java index 2d860cd..222fb8d 100644 --- a/src/org/ice4j/attribute/ConnectionIdAttribute.java +++ b/src/org/ice4j/attribute/ConnectionIdAttribute.java @@ -126,15 +126,21 @@ public class ConnectionIdAttribute void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws StunException { - if(length != 4) + if(length != DATA_LENGTH) { - throw new StunException("length invalid"); + throw new StunException("length invalid: " + length); } - connectionIdValue = attributeValue[0] & 0xff; - connectionIdValue = connectionIdValue*256 + (attributeValue[1] & 0xff); - connectionIdValue = connectionIdValue*256 + (attributeValue[2] & 0xff); - connectionIdValue = connectionIdValue*256 + (attributeValue[3] & 0xff); + connectionIdValue = attributeValue[offset] & 0xff; + + connectionIdValue = connectionIdValue << 8 | + (attributeValue[offset+1] & 0xff); + + connectionIdValue = connectionIdValue << 8 | + (attributeValue[offset+2] & 0xff); + + connectionIdValue = connectionIdValue << 8 | + (attributeValue[offset+3] & 0xff); } /** diff --git a/src/org/ice4j/attribute/RequestedAddressFamilyAttribute.java b/src/org/ice4j/attribute/RequestedAddressFamilyAttribute.java index 759d8e3..8e2b4d2 100644 --- a/src/org/ice4j/attribute/RequestedAddressFamilyAttribute.java +++ b/src/org/ice4j/attribute/RequestedAddressFamilyAttribute.java @@ -133,17 +133,17 @@ public class RequestedAddressFamilyAttribute extends Attribute void decodeAttributeBody(byte[] attributeValue, char offset, char length) throws StunException { - if(length != 4) + if(length != DATA_LENGTH) { - throw new StunException("length invalid"); + throw new StunException("length invalid: " + length); } - family = (char)(attributeValue[0] & 0xff); + family = (char)(attributeValue[offset] & 0xff); if(family != IPv4 && family != IPv6) { // instead throw TurnException - throw new StunException("invalid family value"); + throw new StunException("invalid family value: " + family); } } diff --git a/test/org/ice4j/MsgFixture.java b/test/org/ice4j/MsgFixture.java index 5aaaf27..4c56ed9 100644 --- a/test/org/ice4j/MsgFixture.java +++ b/test/org/ice4j/MsgFixture.java @@ -119,6 +119,25 @@ public class MsgFixture }; + public static final char REQUESTED_ADDRESS_FAMILY_ATTRIBUTE_V4 = 0X01; + public static final char REQUESTED_ADDRESS_FAMILY_ATTRIBUTE_V6 = 0X02; + + public byte[] requestedAddressFamilyV4 = new byte[] + {0x00, 0x17, 0x00, 0x01, //Type, Length + 0x01}; //family + + public byte[] requestedAddressFamilyV6 = new byte[] + {0x00, 0x17, 0x00, 0x01, //Type, Length + 0x02}; //family + + public static final int CONNECTION_ID = 0x5555; + public static final int CONNECTION_ID_2 = 0x2222; + + public byte[] connectionId = new byte[] + {0x00, 0x2a, 0x00, 0x04, //Type, Length + 0x00, 0x00, 0x55, 0x55}; //connectionId + + //--------------------------- Messages ---------------------------------------- public static final byte[] TRANSACTION_ID = new byte[]{ diff --git a/test/org/ice4j/StunTestSuite.java b/test/org/ice4j/StunTestSuite.java index 96f247f..a64b5a5 100644 --- a/test/org/ice4j/StunTestSuite.java +++ b/test/org/ice4j/StunTestSuite.java @@ -52,6 +52,10 @@ public class StunTestSuite SoftwareAttributeTest.class); suite.addTestSuite(org.ice4j.attribute. OptionalAttributeAttributeTest.class); + suite.addTestSuite(org.ice4j.attribute. + ConnectionIdAttributeTest.class); + suite.addTestSuite(org.ice4j.attribute. + RequestedAddressFamilyAttributeTest.class); suite.addTestSuite(org.ice4j.attribute.UsernameAttributeTest.class); suite.addTestSuite(org.ice4j.attribute.NonceAttributeTest.class); suite.addTestSuite(org.ice4j.attribute.RealmAttributeTest.class); diff --git a/test/org/ice4j/attribute/ConnectionIdAttributeTest.java b/test/org/ice4j/attribute/ConnectionIdAttributeTest.java new file mode 100644 index 0000000..4b29ca3 --- /dev/null +++ b/test/org/ice4j/attribute/ConnectionIdAttributeTest.java @@ -0,0 +1,169 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the SIP Communicator community (http://sip-communicator.org). + * + * Distributable under LGPL license. See terms of license at gnu.org. + */ +package org.ice4j.attribute; + +import org.ice4j.*; + +import java.util.*; + +import junit.framework.*; + +/** + * Class to test the ConnectionIdAttribute class. + * + * @author Aakash Garg + * + */ +public class ConnectionIdAttributeTest + extends TestCase +{ + private ConnectionIdAttribute connectionIdAttribute = null; + + private MsgFixture msgFixture; + + @Override + protected void setUp() throws Exception + { + super.setUp(); + this.connectionIdAttribute = new ConnectionIdAttribute(); + this.msgFixture = new MsgFixture(); + + msgFixture.setUp(); + } + + @Override + protected void tearDown() throws Exception + { + this.connectionIdAttribute = null; + this.msgFixture.tearDown(); + + this.msgFixture = null; + super.tearDown(); + } + + /** + * Tests whether data length is properly calculated. + */ + public void testGetDataLength() + { + char expectedReturn = 4; + this.connectionIdAttribute + .setConnectionIdValue(MsgFixture.CONNECTION_ID); + char actualReturn = this.connectionIdAttribute.getDataLength(); + assertEquals( + "Datalength is not properly calculated", expectedReturn, + actualReturn); + } + + /** + * Tests getting the name. + */ + public void testGetName() + { + String expectedReturn = "CONNECTION-ID"; + String actualReturn = connectionIdAttribute.getName(); + assertEquals( + "getting name failed", expectedReturn, actualReturn); + } + + /** + * Tests the equals method against a null, a different and an identical + * object. + */ + public void testEqualsObject() + { + // null test + ConnectionIdAttribute target = null; + boolean expectedReturn = false; + boolean actualReturn = connectionIdAttribute.equals(target); + + assertEquals( + "ConnectionIdAttribute.equals() failed against a null target.", + expectedReturn, actualReturn); + + // difference test + target = new ConnectionIdAttribute(); + + int connectionId = MsgFixture.CONNECTION_ID_2; + target.setConnectionIdValue(connectionId); + + connectionIdAttribute.setConnectionIdValue(MsgFixture.CONNECTION_ID); + expectedReturn = false; + actualReturn = connectionIdAttribute.equals(target); + assertEquals( + "ConnectionIdAttribute.equals() failed against a different target.", + expectedReturn, actualReturn); + + // equality test + target.setConnectionIdValue(MsgFixture.CONNECTION_ID); + + expectedReturn = true; + actualReturn = connectionIdAttribute.equals(target); + assertEquals( + "ConnectionIdAttribute.equals() failed against an equal target.", + expectedReturn, actualReturn); + } + + /** + * Test whether attributes are properly encoded. + */ + public void testEncode() + { + byte[] expectedReturn = msgFixture.connectionId; + connectionIdAttribute.setConnectionIdValue(MsgFixture.CONNECTION_ID); + byte[] actualReturn = connectionIdAttribute.encode(); + + assertTrue( + "ConnectionIdAttribute.encode() did not " + + "properly encode a sample attribute", Arrays.equals( + expectedReturn, actualReturn)); + } + + /** + * Test whether sample binary arrays are correctly decoded. + * + * @throws StunException if something goes wrong while decoding + * Attribute Body. + */ + public void testDecodeAttributeBody() throws StunException + { + byte[] attributeValue = msgFixture.connectionId; + char offset = Attribute.HEADER_LENGTH; + char length = (char) (attributeValue.length - offset); + + connectionIdAttribute.decodeAttributeBody( + attributeValue, offset, length); + + assertEquals( + "ConnectionIdAttribute.decode() did not properly decode the " + + "connection id field.", MsgFixture.CONNECTION_ID, + connectionIdAttribute.getConnectionIdValue()); + } + + /** + * Tests that the connection Id is always integer. + */ + public void testGetConnectionIdValue() + { + int expectedReturn = 0x5555; + this.connectionIdAttribute + .setConnectionIdValue(MsgFixture.CONNECTION_ID); + int actualReturn = this.connectionIdAttribute.getConnectionIdValue(); + assertEquals( + "ConnectionId is not properly calculated", expectedReturn, + actualReturn); + + expectedReturn = 0x2222; + this.connectionIdAttribute + .setConnectionIdValue(MsgFixture.CONNECTION_ID_2); + actualReturn = this.connectionIdAttribute.getConnectionIdValue(); + assertEquals( + "ConnectionId is not properly calculated", expectedReturn, + actualReturn); + } + +} diff --git a/test/org/ice4j/attribute/RequestedAddressFamilyAttributeTest.java b/test/org/ice4j/attribute/RequestedAddressFamilyAttributeTest.java new file mode 100644 index 0000000..25c063f --- /dev/null +++ b/test/org/ice4j/attribute/RequestedAddressFamilyAttributeTest.java @@ -0,0 +1,239 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the SIP Communicator community (http://sip-communicator.org). + * + * Distributable under LGPL license. See terms of license at gnu.org. + */ +package org.ice4j.attribute; + +import java.util.*; + +import junit.framework.*; + +import org.ice4j.*; + +/** + * Class to test the RequestedAddressFamilyAttribute class. + * + * @author Aakash Garg + * + */ +public class RequestedAddressFamilyAttributeTest + extends TestCase +{ + private RequestedAddressFamilyAttribute requestedAddressFamilyAttribute = + null; + + private MsgFixture msgFixture; + + @Override + protected void setUp() throws Exception + { + super.setUp(); + this.requestedAddressFamilyAttribute = + new RequestedAddressFamilyAttribute(); + this.msgFixture = new MsgFixture(); + + msgFixture.setUp(); + } + + public RequestedAddressFamilyAttributeTest(String name) + { + super(name); + } + + @Override + protected void tearDown() throws Exception + { + this.requestedAddressFamilyAttribute = null; + this.msgFixture.tearDown(); + + this.msgFixture = null; + super.tearDown(); + } + + /** + * Tests whether data length is properly calculated. + */ + public void testGetDataLength() + { + char expectedReturn = 1; + this.requestedAddressFamilyAttribute + .setFamily(MsgFixture.REQUESTED_ADDRESS_FAMILY_ATTRIBUTE_V4); + char actualReturn = + this.requestedAddressFamilyAttribute.getDataLength(); + assertEquals( + "Datalength is not properly calculated", expectedReturn, + actualReturn); + + expectedReturn = 1; + this.requestedAddressFamilyAttribute + .setFamily(MsgFixture.REQUESTED_ADDRESS_FAMILY_ATTRIBUTE_V6); + actualReturn = this.requestedAddressFamilyAttribute.getDataLength(); + assertEquals( + "Datalength is not properly calculated", expectedReturn, + actualReturn); + + } + + /** + * Tests getting the name. + */ + public void testGetName() + { + String expectedReturn = "REQUESTED-ADDRESS-FAMILY"; + String actualReturn = requestedAddressFamilyAttribute.getName(); + assertEquals( + "getting name failed", expectedReturn, actualReturn); + } + + /** + * Tests the equals method against a null, a different and an identical + * object. + */ + public void testEqualsObject() + { + // null test + RequestedAddressFamilyAttribute target = null; + boolean expectedReturn = false; + boolean actualReturn = requestedAddressFamilyAttribute.equals(target); + + assertEquals( + "RequestedAddressFamilyAttribute.equals() failed against a null " + + "target.", expectedReturn, actualReturn); + + // difference test + target = new RequestedAddressFamilyAttribute(); + + char family = MsgFixture.REQUESTED_ADDRESS_FAMILY_ATTRIBUTE_V6; + target.setFamily(family); + + requestedAddressFamilyAttribute + .setFamily(MsgFixture.REQUESTED_ADDRESS_FAMILY_ATTRIBUTE_V4); + + expectedReturn = false; + actualReturn = requestedAddressFamilyAttribute.equals(target); + assertEquals( + "RequestedAddressFamilyAttribute.equals() failed against a " + + "different target.", expectedReturn, actualReturn); + + // equality test + target.setFamily(MsgFixture.REQUESTED_ADDRESS_FAMILY_ATTRIBUTE_V4); + + expectedReturn = true; + actualReturn = requestedAddressFamilyAttribute.equals(target); + assertEquals( + "RequestedAddressFamilyAttribute.equals() failed against an equal " + + "target.", expectedReturn, actualReturn); + + // ipv6 equality test + target.setFamily(MsgFixture.REQUESTED_ADDRESS_FAMILY_ATTRIBUTE_V6); + + requestedAddressFamilyAttribute + .setFamily(MsgFixture.REQUESTED_ADDRESS_FAMILY_ATTRIBUTE_V6); + + expectedReturn = true; + actualReturn = requestedAddressFamilyAttribute.equals(target); + assertEquals( + "RequestedAddressFamilyAttribute.equals() failed for IPv6 address.", + expectedReturn, actualReturn); + } + + /** + * Test whether sample binary arrays are correctly decoded. + * + * @throws StunException if something goes wrong while decoding + * Attribute Body. + */ + public void testDecodeAttributeBodyV4() throws StunException + { + byte[] attributeValue = msgFixture.requestedAddressFamilyV4; + char offset = Attribute.HEADER_LENGTH; + char length = (char) (attributeValue.length - offset); + + requestedAddressFamilyAttribute.decodeAttributeBody( + attributeValue, offset, length); + + assertEquals( + "RequestedAddressFamilyAttribute.decode() did not properly decode " + + "the family field.", + MsgFixture.REQUESTED_ADDRESS_FAMILY_ATTRIBUTE_V4, + requestedAddressFamilyAttribute.getFamily()); + } + + /** + * Test whether sample binary arrays are correctly decoded. + * + * @throws StunException if something goes wrong while decoding + * Attribute Body. + */ + public void testDecodeAttributeBodyV6() throws StunException + { + byte[] attributeValue = msgFixture.requestedAddressFamilyV6; + char offset = Attribute.HEADER_LENGTH; + char length = (char) (attributeValue.length - offset); + + requestedAddressFamilyAttribute.decodeAttributeBody( + attributeValue, offset, length); + + assertEquals( + "RequestedAddressFamilyAttribute.decode() did not properly decode.", + MsgFixture.REQUESTED_ADDRESS_FAMILY_ATTRIBUTE_V6, + requestedAddressFamilyAttribute.getFamily()); + } + + /** + * Test whether attributes are properly encoded. + */ + public void testEncodeV4() + { + byte[] expectedReturn = msgFixture.requestedAddressFamilyV4; + requestedAddressFamilyAttribute + .setFamily(MsgFixture.REQUESTED_ADDRESS_FAMILY_ATTRIBUTE_V4); + byte[] actualReturn = requestedAddressFamilyAttribute.encode(); + assertTrue( + "RequestedAddressFamilyAttribute.encode() did not " + + "properly encode a sample attribute for IPv4 family", + Arrays.equals( + expectedReturn, actualReturn)); + } + + /** + * Test whether attributes are properly encoded. + */ + public void testEncodeV6() + { + byte[] expectedReturn = msgFixture.requestedAddressFamilyV6; + requestedAddressFamilyAttribute + .setFamily(MsgFixture.REQUESTED_ADDRESS_FAMILY_ATTRIBUTE_V6); + byte[] actualReturn = requestedAddressFamilyAttribute.encode(); + assertTrue( + "RequestedAddressFamilyAttribute.encode() did not " + + "properly encode a sample attribute for IPv6 family", + Arrays.equals( + expectedReturn, actualReturn)); + } + + /** + * Tests that the address family is always 0x01 or 0x02. + */ + public void testGetFamily() + { + char expectedReturn = 0x01; + this.requestedAddressFamilyAttribute + .setFamily(MsgFixture.REQUESTED_ADDRESS_FAMILY_ATTRIBUTE_V4); + char actualReturn = this.requestedAddressFamilyAttribute.getFamily(); + assertEquals( + "Family is not properly calculated", expectedReturn, + actualReturn); + + expectedReturn = 0x02; + this.requestedAddressFamilyAttribute + .setFamily(MsgFixture.REQUESTED_ADDRESS_FAMILY_ATTRIBUTE_V6); + actualReturn = this.requestedAddressFamilyAttribute.getFamily(); + assertEquals( + "Family is not properly calculated", expectedReturn, + actualReturn); + } + +}