1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.channel.unix.tests;
17
18 import io.netty5.channel.unix.Buffer;
19 import io.netty5.channel.unix.Socket;
20 import org.junit.jupiter.api.AfterEach;
21 import org.junit.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.Test;
23 import org.opentest4j.TestAbortedException;
24
25 import java.io.IOException;
26 import java.nio.ByteBuffer;
27
28 import static org.junit.jupiter.api.Assertions.assertEquals;
29 import static org.junit.jupiter.api.Assertions.assertFalse;
30 import static org.junit.jupiter.api.Assertions.assertNotEquals;
31 import static org.junit.jupiter.api.Assertions.assertTrue;
32
33 public abstract class SocketTest<T extends Socket> {
34 protected T socket;
35
36 protected abstract T newSocket();
37
38 @BeforeEach
39 public void setup() {
40 socket = newSocket();
41 }
42
43 @AfterEach
44 public void tearDown() throws IOException {
45 socket.close();
46 }
47
48 @Test
49 public void testKeepAlive() throws Exception {
50 assertFalse(socket.isKeepAlive());
51 socket.setKeepAlive(true);
52 assertTrue(socket.isKeepAlive());
53 }
54
55 @Test
56 public void testTcpNoDelay() throws Exception {
57 assertFalse(socket.isTcpNoDelay());
58 socket.setTcpNoDelay(true);
59 assertTrue(socket.isTcpNoDelay());
60 }
61
62 @Test
63 public void testReceivedBufferSize() throws Exception {
64 int size = socket.getReceiveBufferSize();
65 int newSize = 65535;
66 assertTrue(size > 0);
67 socket.setReceiveBufferSize(newSize);
68
69 assertTrue(newSize <= socket.getReceiveBufferSize());
70 }
71
72 @Test
73 public void testSendBufferSize() throws Exception {
74 int size = socket.getSendBufferSize();
75 int newSize = 65535;
76 assertTrue(size > 0);
77 socket.setSendBufferSize(newSize);
78
79 assertTrue(newSize <= socket.getSendBufferSize());
80 }
81
82 @Test
83 public void testSoLinger() throws Exception {
84 assertEquals(-1, socket.getSoLinger());
85 socket.setSoLinger(10);
86 assertEquals(10, socket.getSoLinger());
87 }
88
89 @Test
90 public void testDoubleCloseDoesNotThrow() throws IOException {
91 Socket socket = Socket.newSocketStream();
92 socket.close();
93 socket.close();
94 }
95
96 @Test
97 public void testTrafficClass() throws IOException {
98
99 final int value = 0x08;
100 socket.setTrafficClass(value);
101 assertEquals(value, socket.getTrafficClass());
102 }
103
104 @Test
105 public void testIntOpt() throws IOException {
106 socket.setReuseAddress(false);
107 socket.setIntOpt(level(), optname(), 1);
108
109 assertNotEquals(0, socket.getIntOpt(level(), optname()));
110 socket.setIntOpt(level(), optname(), 0);
111
112 assertEquals(0, socket.getIntOpt(level(), optname()));
113 }
114
115 @Test
116 public void testRawOpt() throws IOException {
117 ByteBuffer buffer = Buffer.allocateDirectWithNativeOrder(4);
118 buffer.putInt(1).flip();
119 socket.setRawOpt(level(), optname(), buffer);
120
121 ByteBuffer out = ByteBuffer.allocate(4);
122 socket.getRawOpt(level(), optname(), out);
123 assertFalse(out.hasRemaining());
124
125 out.flip();
126 assertNotEquals(ByteBuffer.allocate(0), out);
127 }
128
129 protected int level() {
130 throw new TestAbortedException("Not supported");
131 }
132
133 protected int optname() {
134 throw new TestAbortedException("Not supported");
135 }
136 }