⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 optionalattributeattributetest.java

📁 stun的java实现
💻 JAVA
字号:
/*
 * Stun4j, the OpenSource Java Solution for NAT and Firewall Traversal.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.stun4j.attribute;

import junit.framework.*;
import net.java.stun4j.*;
import java.util.Arrays;

/**
 * @author Emil Ivov
 * @version 1.0
 */
public class OptionalAttributeAttributeTest extends TestCase
{
    private OptionalAttribute optionalAttribute = null;
    private MsgFixture msgFixture = null;
    byte[] expectedAttributeValue = null;

    protected void setUp() throws Exception
    {
        super.setUp();

        msgFixture = new MsgFixture();
        int offset = Attribute.HEADER_LENGTH;

        //init a sample body
        expectedAttributeValue =
            new byte[msgFixture.unknownOptionalAttribute.length - offset];

        System.arraycopy(msgFixture.unknownOptionalAttribute, offset,
                         expectedAttributeValue, 0,
                         expectedAttributeValue.length);

        optionalAttribute = new OptionalAttribute(
                                        msgFixture.optionalAttributeType);
    }

    protected void tearDown() throws Exception
    {
        optionalAttribute = null;
        expectedAttributeValue = null;
        super.tearDown();
    }



    /**
     * Test whetner sample binary arrays are correctly decoded.
     * @throws StunException
     */
    public void testDecodeAttributeBody() throws StunException {

        char offset = Attribute.HEADER_LENGTH;
        char length = (char)(msgFixture.unknownOptionalAttribute.length - offset);

        optionalAttribute.decodeAttributeBody(msgFixture.unknownOptionalAttribute,
                                              offset, length);


        assertTrue("OptionalAttribute did not decode properly.",
                     Arrays.equals( expectedAttributeValue,
                                    optionalAttribute.getBody()));

        assertEquals("Lenght was not properly decoded", length,
                     optionalAttribute.getDataLength());

    }

    /**
     * Test whether attributes are properly encoded
     */
    public void testEncode()
    {
        optionalAttribute.setBody(expectedAttributeValue, 0,
                                  expectedAttributeValue.length);

        byte[] actualReturn = optionalAttribute.encode();

        assertTrue("encode failed",
                  Arrays.equals( msgFixture.unknownOptionalAttribute, actualReturn) );
    }

    /**
     * Test whether the equals method works ok
     */
    public void testEquals()
    {
        //null comparison
        Object obj = null;
        boolean expectedReturn = false;
        optionalAttribute.setBody( expectedAttributeValue, 0,
                                   expectedAttributeValue.length);

        boolean actualReturn = optionalAttribute.equals(obj);
        assertEquals("failed null comparison", expectedReturn, actualReturn);

        //wrong type comparison
        obj = new String("hehe :)");
        actualReturn = optionalAttribute.equals(obj);
        assertEquals("failed wrong type comparison", expectedReturn,
                     actualReturn);

        //succesful comparison
        obj = new OptionalAttribute(msgFixture.optionalAttributeType);

        ((OptionalAttribute)obj).setBody( expectedAttributeValue, 0,
                                          expectedAttributeValue.length);
        expectedReturn = true;
        actualReturn = optionalAttribute.equals(obj);
        assertEquals("failed null comparison", expectedReturn, actualReturn);
    }

    public void testGetDataLength()
    {
        char expectedReturn = (char)expectedAttributeValue.length;

        optionalAttribute.setBody( expectedAttributeValue, 0,
                                   expectedAttributeValue.length);

        char actualReturn = optionalAttribute.getDataLength();
        assertEquals("return value", expectedReturn, actualReturn);
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -