View Javadoc

1   // Copyright (C) 2005 - 2011 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.tools.tcpproxy;
23  
24  import static net.grinder.testutility.AssertUtilities.assertContains;
25  import static org.junit.Assert.assertEquals;
26  
27  import java.io.PrintWriter;
28  import java.io.StringWriter;
29  
30  import org.junit.Test;
31  
32  
33  /**
34   * Unit test case for {@link EchoFilter}.
35   *
36   * @author Philip Aston
37   */
38  public class TestEchoFilter {
39  
40    private static final String LINE_SEPARATOR =
41        System.getProperty("line.separator");
42  
43    private final EndPoint m_endPoint1 = new EndPoint("host1", 1234);
44    private final EndPoint m_endPoint2 = new EndPoint("host2", 99);
45  
46    private final ConnectionDetails m_connectionDetails =
47        new ConnectionDetails(m_endPoint1, m_endPoint2, false);
48  
49    private StringWriter m_outString = new StringWriter();
50    private PrintWriter m_out = new PrintWriter(m_outString);
51  
52    @Test public void testHandle() throws Exception {
53      final EchoFilter echoFilter = new EchoFilter(m_out);
54  
55      assertEquals(0, m_outString.toString().length());
56  
57      echoFilter.handle(m_connectionDetails, "This is a campaign.".getBytes(), 5);
58  
59      final String output = m_outString.toString();
60      assertContains(output, m_connectionDetails.toString());
61      assertContains(output, "This " + LINE_SEPARATOR);
62  
63      final String lines = "Some\nlines\rblah";
64      echoFilter.handle(m_connectionDetails, lines.getBytes(), lines.length());
65  
66      final String output2 = m_outString.toString().substring(output.length());
67      assertContains(output2, m_connectionDetails.toString());
68      assertContains(output2, "Some\nlines\rblah" + LINE_SEPARATOR);
69  
70      final byte[] binary = { 0x01, (byte)0xE7, 'a', 'b', 'c', (byte)0x89,
71                              'd', 'a', 'h', '\n', 'b', 'a', 'h' };
72      echoFilter.handle(m_connectionDetails, binary, binary.length);
73      final String output3 =
74        m_outString.toString().substring(output.length() + output2.length());
75      assertContains(output3, m_connectionDetails.toString());
76      assertContains(output3, "[01E7]abc[89]dah\nbah" + LINE_SEPARATOR);
77    }
78  
79    @Test public void testConnectionOpened() throws Exception {
80      final EchoFilter echoFilter = new EchoFilter(m_out);
81  
82      echoFilter.connectionOpened(m_connectionDetails);
83      final String output = m_outString.toString();
84      assertContains(output, m_connectionDetails.toString());
85      assertContains(output, "opened");
86    }
87  
88    @Test public void testConnectionClosed() throws Exception {
89      final EchoFilter echoFilter = new EchoFilter(m_out);
90  
91      echoFilter.connectionClosed(m_connectionDetails);
92      final String output = m_outString.toString();
93      assertContains(output, m_connectionDetails.toString());
94      assertContains(output, "closed");
95    }
96  }