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 * http://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 18 package org.apache.harmony.tests.java.net; 19 20 import java.io.FileDescriptor; 21 import java.io.IOException; 22 import java.net.DatagramPacket; 23 import java.net.DatagramSocketImpl; 24 import java.net.InetAddress; 25 import java.net.NetworkInterface; 26 import java.net.SocketAddress; 27 import java.net.SocketException; 28 import java.net.SocketOption; 29 import java.net.SocketOptions; 30 import java.net.StandardSocketOptions; 31 import libcore.junit.junit3.TestCaseWithRules; 32 import libcore.junit.util.ResourceLeakageDetector; 33 import org.junit.Rule; 34 import org.junit.rules.TestRule; 35 36 public class DatagramSocketImplTest extends TestCaseWithRules { 37 @Rule 38 public TestRule guardRule = ResourceLeakageDetector.getRule(); 39 40 /** 41 * java.net.DatagramSocketImpl#DatagramSocketImpl() 42 */ test_Constructor()43 public void test_Constructor() throws Exception { 44 // regression test for Harmony-1117 45 MockDatagramSocketImpl impl = new MockDatagramSocketImpl(); 46 assertNull(impl.getFileDescriptor()); 47 } 48 49 test_connect()50 public void test_connect() throws Exception { 51 MockDatagramSocketImpl impl = new MockDatagramSocketImpl(); 52 InetAddress localhost = InetAddress.getByName("localhost"); //$NON-NLS-1$ 53 // connect do nothing, so will not throw exception 54 impl.test_connect(localhost, 0); 55 impl.test_connect(localhost, -1); 56 impl.test_connect(null, -1); 57 // disconnect 58 impl.test_disconnect(); 59 } 60 61 /** 62 * API test for {@link DatagramSocketImpl#setOption(SocketOption, Object)}. 63 */ testSetOption()64 public void testSetOption() throws IOException { 65 // The value isn't sent to the kernel, because the mock intercepts the value in this test. 66 MockDatagramSocketImpl impl = new MockDatagramSocketImpl(); 67 setAndAssertOption(impl, StandardSocketOptions.SO_SNDBUF, 68 SocketOptions.SO_SNDBUF, 1); 69 setAndAssertOption(impl, StandardSocketOptions.SO_RCVBUF, 70 SocketOptions.SO_RCVBUF, 2); 71 setAndAssertOption(impl, StandardSocketOptions.SO_REUSEADDR, 72 SocketOptions.SO_REUSEADDR, true); 73 setAndAssertOption(impl, StandardSocketOptions.IP_TOS, 74 SocketOptions.IP_TOS, 3); 75 } 76 setAndAssertOption(MockDatagramSocketImpl sockImpl, SocketOption option, int optionInt, Object value)77 private static void setAndAssertOption(MockDatagramSocketImpl sockImpl, SocketOption option, 78 int optionInt, Object value) throws IOException { 79 sockImpl.setSuperOption(option, value); 80 assertEquals(sockImpl.optionInt, optionInt); 81 assertEquals(sockImpl.optionValue, value); 82 } 83 } 84 85 class MockDatagramSocketImpl extends DatagramSocketImpl { 86 int optionInt; 87 Object optionValue; 88 89 @Override getFileDescriptor()90 public FileDescriptor getFileDescriptor() { 91 return super.getFileDescriptor(); 92 } 93 94 @Override bind(int port, InetAddress addr)95 protected void bind(int port, InetAddress addr) throws SocketException { 96 // empty 97 } 98 99 @Override close()100 protected void close() { 101 // empty 102 } 103 104 @Override create()105 protected void create() throws SocketException { 106 // empty 107 } 108 getOption(int optID)109 public Object getOption(int optID) throws SocketException { 110 return null; 111 } 112 113 @Override getTTL()114 protected byte getTTL() throws IOException { 115 return 0; 116 } 117 118 @Override getTimeToLive()119 protected int getTimeToLive() throws IOException { 120 return 0; 121 } 122 123 @Override join(InetAddress addr)124 protected void join(InetAddress addr) throws IOException { 125 // empty 126 } 127 128 @Override joinGroup(SocketAddress addr, NetworkInterface netInterface)129 protected void joinGroup(SocketAddress addr, NetworkInterface netInterface) 130 throws IOException { 131 // empty 132 } 133 134 @Override leave(InetAddress addr)135 protected void leave(InetAddress addr) throws IOException { 136 // empty 137 } 138 139 @Override leaveGroup(SocketAddress addr, NetworkInterface netInterface)140 protected void leaveGroup(SocketAddress addr, NetworkInterface netInterface) 141 throws IOException { 142 // empty 143 } 144 145 @Override peek(InetAddress sender)146 protected int peek(InetAddress sender) throws IOException { 147 return 0; 148 } 149 150 @Override peekData(DatagramPacket pack)151 protected int peekData(DatagramPacket pack) throws IOException { 152 return 0; 153 } 154 155 @Override receive(DatagramPacket pack)156 protected void receive(DatagramPacket pack) throws IOException { 157 // empty 158 } 159 160 @Override send(DatagramPacket pack)161 protected void send(DatagramPacket pack) throws IOException { 162 // empty 163 164 } 165 166 @Override setOption(int optID, Object val)167 public void setOption(int optID, Object val) throws SocketException { 168 this.optionInt = optID; 169 this.optionValue = val; 170 } 171 setSuperOption(SocketOption option, Object value)172 public void setSuperOption(SocketOption option, Object value) throws IOException { 173 super.setOption(option, value); 174 } 175 176 @Override setTTL(byte ttl)177 protected void setTTL(byte ttl) throws IOException { 178 // empty 179 } 180 181 @Override setTimeToLive(int ttl)182 protected void setTimeToLive(int ttl) throws IOException { 183 // empty 184 } 185 test_connect(InetAddress inetAddr, int port)186 public void test_connect(InetAddress inetAddr, int port) throws SocketException { 187 super.connect(inetAddr, port); 188 } 189 test_disconnect()190 public void test_disconnect() { 191 super.disconnect(); 192 } 193 } 194