View Javadoc
1   /*
2    * Copyright 2015 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.unix.tests;
17  
18  import io.netty.channel.unix.Buffer;
19  import io.netty.channel.unix.Socket;
20  import io.netty.util.internal.CleanableDirectBuffer;
21  import org.junit.jupiter.api.AfterEach;
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.Test;
24  import org.opentest4j.TestAbortedException;
25  
26  import java.io.IOException;
27  import java.nio.ByteBuffer;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  import static org.junit.jupiter.api.Assertions.assertFalse;
31  import static org.junit.jupiter.api.Assertions.assertNotEquals;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  
34  public abstract class SocketTest<T extends Socket> {
35      protected T socket;
36  
37      protected abstract T newSocket();
38  
39      @BeforeEach
40      public void setup() {
41          socket = newSocket();
42      }
43  
44      @AfterEach
45      public void tearDown() throws IOException {
46          socket.close();
47      }
48  
49      @Test
50      public void testKeepAlive() throws Exception {
51          assertFalse(socket.isKeepAlive());
52          socket.setKeepAlive(true);
53          assertTrue(socket.isKeepAlive());
54      }
55  
56      @Test
57      public void testTcpNoDelay() throws Exception {
58          assertFalse(socket.isTcpNoDelay());
59          socket.setTcpNoDelay(true);
60          assertTrue(socket.isTcpNoDelay());
61      }
62  
63      @Test
64      public void testReceivedBufferSize() throws Exception {
65          int size = socket.getReceiveBufferSize();
66          int newSize = 65535;
67          assertTrue(size > 0);
68          socket.setReceiveBufferSize(newSize);
69          // Linux usually set it to double what is specified
70          assertTrue(newSize <= socket.getReceiveBufferSize());
71      }
72  
73      @Test
74      public void testSendBufferSize() throws Exception {
75          int size = socket.getSendBufferSize();
76          int newSize = 65535;
77          assertTrue(size > 0);
78          socket.setSendBufferSize(newSize);
79          // Linux usually set it to double what is specified
80          assertTrue(newSize <= socket.getSendBufferSize());
81      }
82  
83      @Test
84      public void testSoLinger() throws Exception {
85          assertEquals(-1, socket.getSoLinger());
86          socket.setSoLinger(10);
87          assertEquals(10, socket.getSoLinger());
88      }
89  
90      @Test
91      public void testDoubleCloseDoesNotThrow() throws IOException {
92          Socket socket = Socket.newSocketStream();
93          socket.close();
94          socket.close();
95      }
96  
97      @Test
98      public void testTrafficClass() throws IOException {
99          // IPTOS_THROUGHPUT
100         final int value = 0x08;
101         socket.setTrafficClass(value);
102         assertEquals(value, socket.getTrafficClass());
103     }
104 
105     @Test
106     public void testIntOpt() throws IOException {
107         socket.setReuseAddress(false);
108         socket.setIntOpt(level(), optname(), 1);
109         // Anything which is != 0 is considered enabled
110         assertNotEquals(0, socket.getIntOpt(level(), optname()));
111         socket.setIntOpt(level(), optname(), 0);
112         // This should be disabled again
113         assertEquals(0, socket.getIntOpt(level(), optname()));
114     }
115 
116     @Test
117     public void testRawOpt() throws IOException {
118         CleanableDirectBuffer cleanableDirectBuffer = Buffer.allocateDirectBufferWithNativeOrder(4);
119         ByteBuffer buffer = cleanableDirectBuffer.buffer();
120         buffer.putInt(1).flip();
121         socket.setRawOpt(level(), optname(), buffer);
122 
123         ByteBuffer out = ByteBuffer.allocate(4);
124         socket.getRawOpt(level(), optname(), out);
125         assertFalse(out.hasRemaining());
126 
127         out.flip();
128         assertNotEquals(ByteBuffer.allocate(0), out);
129         cleanableDirectBuffer.clean();
130     }
131 
132     protected int level() {
133         throw new TestAbortedException("Not supported");
134     }
135 
136     protected int optname() {
137         throw new TestAbortedException("Not supported");
138     }
139 }