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