|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 Fritz Elfert |
| 3 | + * |
| 4 | + * (ported from https://github.com/softprops/unisockets/blob/master/unisockets-core/src/main/scala/Socket.scala) |
| 5 | + * |
| 6 | + * This file is part of the JNR project. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + * |
| 20 | + */ |
| 21 | +package jnr.unixsocket; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | +import java.net.SocketAddress; |
| 26 | +import java.net.SocketException; |
| 27 | +import java.nio.ByteBuffer; |
| 28 | +import java.nio.CharBuffer; |
| 29 | +import java.nio.file.Files; |
| 30 | +import java.nio.channels.AlreadyBoundException; |
| 31 | +import java.nio.channels.DatagramChannel; |
| 32 | +import java.nio.charset.StandardCharsets; |
| 33 | + |
| 34 | +import java.util.Set; |
| 35 | + |
| 36 | +import org.junit.Assume; |
| 37 | +import org.junit.Ignore; |
| 38 | +import org.junit.Test; |
| 39 | + |
| 40 | +import jnr.ffi.Platform; |
| 41 | +import jnr.ffi.Platform.OS; |
| 42 | + |
| 43 | +import static junit.framework.Assert.*; |
| 44 | + |
| 45 | +public class ChannelOptionsTest { |
| 46 | + |
| 47 | + @Test |
| 48 | + public void readonlyDatagramChannelOptionTest() throws Exception { |
| 49 | + UnixDatagramChannel[] sp = UnixDatagramChannel.pair(); |
| 50 | + UnixDatagramChannel ch = sp[0]; |
| 51 | + Credentials c = ch.socket().getCredentials(); |
| 52 | + try { |
| 53 | + // SO_PEERCRED is readonly |
| 54 | + ch.setOption(UnixSocketOptions.SO_PEERCRED, c); |
| 55 | + fail("Should have thrown AssertionError"); |
| 56 | + } catch (AssertionError ae) { |
| 57 | + assertEquals("exception message", ae.getMessage(), "Option not found or not writable"); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void readonlySocketChannelOptionTest() throws Exception { |
| 63 | + UnixSocketChannel[] sp = UnixSocketChannel.pair(); |
| 64 | + UnixSocketChannel ch = sp[0]; |
| 65 | + Credentials c = ch.socket().getCredentials(); |
| 66 | + try { |
| 67 | + // SO_PEERCRED is readonly |
| 68 | + ch.setOption(UnixSocketOptions.SO_PEERCRED, c); |
| 69 | + fail("Should have thrown AssertionError"); |
| 70 | + } catch (AssertionError ae) { |
| 71 | + assertEquals("exception message", ae.getMessage(), "Option not found or not writable"); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void unsupportedChannelOptionTest() throws Exception { |
| 77 | + UnixDatagramChannel ch = UnixDatagramChannel.open(); |
| 78 | + try { |
| 79 | + // SO_KEEPALIVE is suitable only for SOCK_STREAM sockets |
| 80 | + ch.getOption(UnixSocketOptions.SO_KEEPALIVE); |
| 81 | + fail("Should have thrown UnsupportedOperationException"); |
| 82 | + } catch (UnsupportedOperationException uoe) { |
| 83 | + assertEquals("exception message", uoe.getMessage(), "'SO_KEEPALIVE' not supported"); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void keepaliveOptionTest() throws Exception { |
| 89 | + UnixSocketChannel ch = UnixSocketChannel.open(); |
| 90 | + boolean origValue = ch.getOption(UnixSocketOptions.SO_KEEPALIVE).booleanValue(); |
| 91 | + assertEquals("Initial value of SO_KEEPALIVE", origValue, false); |
| 92 | + ch.setOption(UnixSocketOptions.SO_KEEPALIVE, Boolean.TRUE); |
| 93 | + boolean changedValue = ch.getOption(UnixSocketOptions.SO_KEEPALIVE).booleanValue(); |
| 94 | + assertEquals("Changed value of SO_KEEPALIVE", changedValue, true); |
| 95 | + ch.setOption(UnixSocketOptions.SO_KEEPALIVE, Boolean.FALSE); |
| 96 | + changedValue = ch.getOption(UnixSocketOptions.SO_KEEPALIVE).booleanValue(); |
| 97 | + assertEquals("Changed value of SO_KEEPALIVE", changedValue, origValue); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void invalidOptionValueTest() throws Exception { |
| 102 | + UnixSocketChannel ch = UnixSocketChannel.open(); |
| 103 | + try { |
| 104 | + ch.setOption(UnixSocketOptions.SO_RCVTIMEO, Integer.valueOf(-1)); |
| 105 | + fail("Should have thrown IllegalArgumentException"); |
| 106 | + } catch (IllegalArgumentException iae) { |
| 107 | + assertEquals("exception message", iae.getMessage(), "Invalid send/receive timeout"); |
| 108 | + } |
| 109 | + try { |
| 110 | + ch.setOption(UnixSocketOptions.SO_SNDTIMEO, Integer.valueOf(-1)); |
| 111 | + fail("Should have thrown IllegalArgumentException"); |
| 112 | + } catch (IllegalArgumentException iae) { |
| 113 | + assertEquals("exception message", iae.getMessage(), "Invalid send/receive timeout"); |
| 114 | + } |
| 115 | + try { |
| 116 | + ch.setOption(UnixSocketOptions.SO_RCVBUF, Integer.valueOf(-1)); |
| 117 | + fail("Should have thrown IllegalArgumentException"); |
| 118 | + } catch (IllegalArgumentException iae) { |
| 119 | + assertEquals("exception message", iae.getMessage(), "Invalid send/receive buffer size"); |
| 120 | + } |
| 121 | + try { |
| 122 | + ch.setOption(UnixSocketOptions.SO_SNDBUF, Integer.valueOf(-1)); |
| 123 | + fail("Should have thrown IllegalArgumentException"); |
| 124 | + } catch (IllegalArgumentException iae) { |
| 125 | + assertEquals("exception message", iae.getMessage(), "Invalid send/receive buffer size"); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + @Ignore |
| 131 | + // Linux doubles the values when setting. Check what other platforms do. |
| 132 | + public void socketBufferTest() throws Exception { |
| 133 | + UnixDatagramChannel ch = UnixDatagramChannel.open(); |
| 134 | + int rxs = ch.getOption(UnixSocketOptions.SO_RCVBUF); |
| 135 | + int txs = ch.getOption(UnixSocketOptions.SO_SNDBUF); |
| 136 | + System.out.println(String.format("rxbuf=%d, txbuf=%d", rxs, txs)); |
| 137 | + ch.setOption(UnixSocketOptions.SO_RCVBUF, rxs - 100); |
| 138 | + ch.setOption(UnixSocketOptions.SO_SNDBUF, txs - 100); |
| 139 | + rxs = ch.getOption(UnixSocketOptions.SO_RCVBUF); |
| 140 | + txs = ch.getOption(UnixSocketOptions.SO_SNDBUF); |
| 141 | + System.out.println(String.format("rxbuf=%d, txbuf=%d", rxs, txs)); |
| 142 | + } |
| 143 | +} |
0 commit comments