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