Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package android.net.cts;
     17 
     18 import junit.framework.TestCase;
     19 
     20 import java.io.IOException;
     21 import java.io.InputStream;
     22 import java.io.OutputStream;
     23 import android.net.LocalServerSocket;
     24 import android.net.LocalSocket;
     25 import android.net.LocalSocketAddress;
     26 
     27 public class LocalServerSocketTest extends TestCase {
     28 
     29     public void testLocalServerSocket() throws IOException {
     30         String address = "com.android.net.LocalServerSocketTest_testLocalServerSocket";
     31         LocalServerSocket localServerSocket = new LocalServerSocket(address);
     32         assertNotNull(localServerSocket.getLocalSocketAddress());
     33 
     34         // create client socket
     35         LocalSocket clientSocket = new LocalSocket();
     36 
     37         // establish connection between client and server
     38         clientSocket.connect(new LocalSocketAddress(address));
     39         LocalSocket serverSocket = localServerSocket.accept();
     40 
     41         assertTrue(serverSocket.isConnected());
     42         assertTrue(serverSocket.isBound());
     43 
     44         // send data from client to server
     45         OutputStream clientOutStream = clientSocket.getOutputStream();
     46         clientOutStream.write(12);
     47         InputStream serverInStream = serverSocket.getInputStream();
     48         assertEquals(12, serverInStream.read());
     49 
     50         // send data from server to client
     51         OutputStream serverOutStream = serverSocket.getOutputStream();
     52         serverOutStream.write(3);
     53         InputStream clientInStream = clientSocket.getInputStream();
     54         assertEquals(3, clientInStream.read());
     55 
     56         // close server socket
     57         assertNotNull(localServerSocket.getFileDescriptor());
     58         localServerSocket.close();
     59         assertNull(localServerSocket.getFileDescriptor());
     60     }
     61 }
     62