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 tests.api.java.net; 19 20 import dalvik.annotation.TestTargetClass; 21 22 @TestTargetClass(java.net.Socket.class) 23 public abstract class SocketTestCase extends junit.framework.TestCase { 24 25 public static final int SO_MULTICAST = 0; 26 27 public static final int SO_MULTICAST_INTERFACE = 1; 28 29 public static final int SO_LINGER = 2; 30 31 public static final int SO_RCVBUF = 3; 32 33 public static final int SO_TIMEOUT = 4; 34 35 public static final int SO_SNDBUF = 5; 36 37 public static final int TCP_NODELAY = 6; 38 39 public static final int SO_KEEPALIVE = 7; 40 41 public static final int SO_REUSEADDR = 8; 42 43 public static final int SO_OOBINLINE = 9; 44 45 public static final int IP_TOS = 10; 46 47 public static final int SO_BROADCAST = 11; 48 49 public static final int SO_USELOOPBACK = 12; 50 51 public static final String LINUX = "Linux"; 52 53 private static final String osDoesNotSupportOperationString = "The socket does not support the operation"; 54 55 private static final String osDoesNotSupportOptionString = "The socket option is not supported"; 56 57 private static final String osDoesNotSupportOptionArgumentString = "The socket option arguments are invalid"; 58 59 public SocketTestCase() { 60 } 61 62 public SocketTestCase(String name) { 63 super(name); 64 } 65 66 /** 67 * Answer whether the OS supports the given socket option. 68 */ 69 public boolean getOptionIsSupported(int option) { 70 switch (option) { 71 case SO_RCVBUF: 72 case SO_SNDBUF: 73 return true; 74 case SO_MULTICAST: 75 case SO_MULTICAST_INTERFACE: 76 case SO_LINGER: 77 return true; 78 case TCP_NODELAY: 79 case SO_TIMEOUT: 80 return true; 81 case SO_KEEPALIVE: 82 case SO_REUSEADDR: 83 return true; 84 case SO_OOBINLINE: 85 return true; 86 case IP_TOS: 87 return true; 88 case SO_BROADCAST: 89 return true; 90 case SO_USELOOPBACK: 91 return true; 92 } 93 return false; 94 } 95 96 /** 97 * If the exception is "socket does not support the operation" exception and 98 * it is expected on the current platform, do nothing. Otherwise, fail the 99 * test. 100 */ 101 public void handleException(Exception e, int option) { 102 if (!getOptionIsSupported(option)) { 103 String message = e.getMessage(); 104 if (message != null 105 && (message.equals(osDoesNotSupportOperationString) 106 || message.equals(osDoesNotSupportOptionString) || message 107 .equals(osDoesNotSupportOptionArgumentString))) { 108 /* 109 * This exception is the correct behavior for platforms which do 110 * not support the option 111 */ 112 } else { 113 fail("Threw \"" 114 + e 115 + "\" instead of correct exception for unsupported socket option: " 116 + getSocketOptionString(option)); 117 } 118 } else { 119 fail("Exception during test : " + e.getMessage()); 120 } 121 } 122 123 /** 124 * This method should be called at the end of a socket option test's code 125 * but before the exception catch statements. It throws a failure if the 126 * option given is not supported on the current platform but the VM failed 127 * to throw an exception. So, on platforms which do not support the option, 128 * the execution should never get to this method. 129 */ 130 public void ensureExceptionThrownIfOptionIsUnsupportedOnOS(int option) { 131 if (!getOptionIsSupported(option)) { 132 String platform = System.getProperty("os.name"); 133 String version = System.getProperty("os.version"); 134 fail("Failed to throw exception for unsupported socket option: " 135 + getSocketOptionString(option)); 136 } 137 } 138 139 /** 140 * Answer a string for the socket option given. 141 */ 142 private String getSocketOptionString(int option) { 143 switch (option) { 144 case SO_MULTICAST: 145 return "Multicast"; 146 case SO_LINGER: 147 return "Linger"; 148 case SO_RCVBUF: 149 return "Receive buffer size"; 150 case SO_TIMEOUT: 151 return "Socket timeout"; 152 case SO_SNDBUF: 153 return "Send buffer size"; 154 case TCP_NODELAY: 155 return "TCP no delay"; 156 case SO_KEEPALIVE: 157 return "Keepalive"; 158 case SO_REUSEADDR: 159 return "Reuse address"; 160 case SO_OOBINLINE: 161 return "out of band data inline"; 162 case IP_TOS: 163 return "Traffic class"; 164 case SO_BROADCAST: 165 return "broadcast"; 166 case SO_USELOOPBACK: 167 return "loopback"; 168 } 169 return "Unknown socket option"; 170 } 171 172 } 173