Package org.cipango.sip

Source Code of org.cipango.sip.StreamParserTest$Handler

// ========================================================================
// Copyright 2007-2008 NEXCOM Systems
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========================================================================

package org.cipango.sip;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;

import java.io.IOException;

import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.bio.StringEndPoint;
import org.junit.Test;

public class StreamParserTest
{
  int seq = -1;

  @Test
  public void testTcp() throws Exception
 
    StringEndPoint ep = new StringEndPoint();
    ep.setInput(_msg);
    SipParser parser = new SipParser(new ByteArrayBuffer(1024), ep, new Handler());
   
    try
    {
      parser.parse();
      fail("expected buffer overflow");
    }
    catch (BufferOverflowException e)
    {
     
    }
  }

  @Test
  public void testSeveral() throws Exception
  {
    StringBuffer msgs = new StringBuffer();
    for (int i = 0; i < 1000; i++)
    {
      msgs.append("INVITE sip:foo SIP/2.0\r\nX-Seq: " + i + "\r\nContent-Length:0\r\n\r\n");
    }
    StringEndPoint ep = new StringEndPoint();
    ep.setInput(msgs.toString());
   
    SipParser parser = new SipParser(new ByteArrayBuffer(55889), ep, new Handler());
    for (int i = 0; i < 1000; i++)
    {
      seq = -1;
      parser.parse();
      assertEquals(i, seq);
    }
   
    try
    {
      parser.parse();
      fail("expected buffer overflow");
    }
    catch (BufferOverflowException e)
    {
     
    }
  }

  @Test
  public void testOverflow() throws Exception
  {
    StringEndPoint ep = new StringEndPoint();
    ep.setInput(_msg);
    SipParser parser = new SipParser(new ByteArrayBuffer(1), ep, new Handler());
   
    int size = 1;
    boolean overflow = false;
    do
    {
      parser.setBuffer(new ByteArrayBuffer(size), true);
      try
      {
        parser.parse();
        overflow = false;
      }
      catch (BufferOverflowException e)
      {
        size = size + 500;
        overflow = true;
      }
    }
    while (overflow);
    System.out.println(size);
    System.out.println(_msg.getBytes().length);
  }
 
  class Handler extends SipParser.EventHandler
    {

    public void header(Buffer name, Buffer value) throws IOException
    {
      if (name.toString().equals("X-Seq"))
        seq = Integer.parseInt(value.toString());
    }
     
    }
   
  String _msg =
        "REGISTER sip:oahu:5070 SIP/2.0\r\n"
        + "Call-ID: c117fdfda2ffd6f4a859a2d504aedb25@127.0.0.1\r\n"
        + "CSeq: 1 REGISTER\r\n"
        + "From: <sip:foo@bar>;tag=9Aaz+gQAAA\r\n"
        + "To: <sip:foo@bar>\r\n"
        + "Via: SIP/2.0/UDP 127.0.0.1:6010\r\n"
        + "Max-Forwards: 70\r\n"
        + "User-Agent: Test Script\r\n"
        + "Contact: \"Foo Bar\" <sip:127.0.0.1:6010;transport=udp>\r\n"
        + "Allow: INVITE, ACK, BYE, CANCEL, PRACK, REFER, MESSAGE, SUBSCRIBE\r\n"
        + "X-large: start ----------------------------------------------------------------------------------------------------------------------------------------------------  \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "\t---------------------------------------------------------------------------------------------------------------------------------------------------- \r\n"
        + "Content-Length: 0\r\n\r\n";
}
TOP

Related Classes of org.cipango.sip.StreamParserTest$Handler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.