View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.net.telnet;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * JUnit test class for TerminalTypeOptionHandler
27   */
28  class WindowSizeOptionHandlerTest extends AbstractTelnetOptionHandlerTest {
29  
30      /**
31       * compares two arrays of int
32       */
33      private void equalInts(final int a1[], final int a2[]) {
34          assertEquals(a1.length, a2.length, "Arrays should be the same length");
35          for (int ii = 0; ii < a1.length; ii++) {
36              assertEquals(a1[ii], a2[ii], "Array entry " + ii + " should match");
37          }
38      }
39  
40      /**
41       * setUp for the test.
42       */
43      @BeforeEach
44      protected void setUp() {
45          opthand1 = new WindowSizeOptionHandler(80, 24);
46          opthand2 = new WindowSizeOptionHandler(255, 255, true, true, true, true);
47          opthand3 = new WindowSizeOptionHandler(0xFFFF, 0x00FF, false, false, false, false);
48      }
49  
50      /**
51       * test of client-driven subnegotiation. Checks that nothing is sent
52       */
53      @Test
54      void testAnswerSubnegotiation() {
55          final int[] subn = { TelnetOption.WINDOW_SIZE, 24, 80 };
56  
57          final int[] resp1 = opthand1.answerSubnegotiation(subn, subn.length);
58          final int[] resp2 = opthand2.answerSubnegotiation(subn, subn.length);
59          final int[] resp3 = opthand3.answerSubnegotiation(subn, subn.length);
60  
61          assertNull(resp1);
62          assertNull(resp2);
63          assertNull(resp3);
64      }
65  
66      /**
67       * test of the constructors.
68       */
69      @Override
70      @Test
71      void testConstructors() {
72          assertEquals(TelnetOption.WINDOW_SIZE, opthand1.getOptionCode());
73          super.testConstructors();
74      }
75  
76      /**
77       * test of client-driven subnegotiation. Checks that no subnegotiation is made.
78       */
79      @Test
80      void testStartSubnegotiation() {
81          assertNull(opthand1.startSubnegotiationRemote());
82          assertNull(opthand2.startSubnegotiationRemote());
83          assertNull(opthand3.startSubnegotiationRemote());
84      }
85  
86      /**
87       * test of client-driven subnegotiation.
88       */
89      @Test
90      void testStartSubnegotiationLocal() {
91          final int[] exp1 = { 31, 0, 80, 0, 24 };
92          final int[] start1 = opthand1.startSubnegotiationLocal();
93          assertEquals(5, start1.length);
94          equalInts(exp1, start1);
95  
96          final int[] exp2 = { 31, 0, 255, 255, 0, 255, 255 };
97          final int[] start2 = opthand2.startSubnegotiationLocal();
98          equalInts(exp2, start2);
99  
100         final int[] exp3 = { 31, 255, 255, 255, 255, 0, 255, 255 };
101         final int[] start3 = opthand3.startSubnegotiationLocal();
102         equalInts(exp3, start3);
103     }
104 }