1/*
2 * Copyright (C) 2007 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
17package android.net;
18
19import android.net.Credentials;
20import android.net.LocalServerSocket;
21import android.net.LocalSocket;
22import android.net.LocalSocketAddress;
23import android.test.MoreAsserts;
24import android.test.suitebuilder.annotation.SmallTest;
25import junit.framework.TestCase;
26
27import java.io.FileDescriptor;
28import java.io.IOException;
29
30public class LocalSocketTest extends TestCase {
31
32    @SmallTest
33    public void testBasic() throws Exception {
34        LocalServerSocket ss;
35        LocalSocket ls;
36        LocalSocket ls1;
37
38        ss = new LocalServerSocket("android.net.LocalSocketTest");
39
40        ls = new LocalSocket();
41
42        ls.connect(new LocalSocketAddress("android.net.LocalSocketTest"));
43
44        ls1 = ss.accept();
45
46        // Test trivial read and write
47        ls.getOutputStream().write(42);
48
49        assertEquals(42, ls1.getInputStream().read());
50
51        // Test getting credentials
52        Credentials c = ls1.getPeerCredentials();
53
54        MoreAsserts.assertNotEqual(0, c.getPid());
55
56        // Test sending and receiving file descriptors
57        ls.setFileDescriptorsForSend(
58                new FileDescriptor[]{FileDescriptor.in});
59
60        ls.getOutputStream().write(42);
61
62        assertEquals(42, ls1.getInputStream().read());
63
64        FileDescriptor[] out = ls1.getAncillaryFileDescriptors();
65
66        assertEquals(1, out.length);
67
68        // Test multible byte write and available()
69        ls1.getOutputStream().write(new byte[]{0, 1, 2, 3, 4, 5}, 1, 5);
70
71        assertEquals(1, ls.getInputStream().read());
72        assertEquals(4, ls.getInputStream().available());
73
74        byte[] buffer = new byte[16];
75        int countRead;
76
77        countRead = ls.getInputStream().read(buffer, 1, 15);
78
79        assertEquals(4, countRead);
80        assertEquals(2, buffer[1]);
81        assertEquals(3, buffer[2]);
82        assertEquals(4, buffer[3]);
83        assertEquals(5, buffer[4]);
84
85        // Try various array-out-of-bound cases
86        try {
87            ls.getInputStream().read(buffer, 1, 16);
88            fail("expected exception");
89        } catch (ArrayIndexOutOfBoundsException ex) {
90            // excpected
91        }
92
93        try {
94            ls.getOutputStream().write(buffer, 1, 16);
95            fail("expected exception");
96        } catch (ArrayIndexOutOfBoundsException ex) {
97            // excpected
98        }
99
100        try {
101            ls.getOutputStream().write(buffer, -1, 15);
102            fail("expected exception");
103        } catch (ArrayIndexOutOfBoundsException ex) {
104            // excpected
105        }
106
107        try {
108            ls.getOutputStream().write(buffer, 0, -1);
109            fail("expected exception");
110        } catch (ArrayIndexOutOfBoundsException ex) {
111            // excpected
112        }
113
114        try {
115            ls.getInputStream().read(buffer, -1, 15);
116            fail("expected exception");
117        } catch (ArrayIndexOutOfBoundsException ex) {
118            // excpected
119        }
120
121        try {
122            ls.getInputStream().read(buffer, 0, -1);
123            fail("expected exception");
124        } catch (ArrayIndexOutOfBoundsException ex) {
125            // excpected
126        }
127
128        // Try read of length 0
129        ls.getOutputStream().write(42);
130        countRead = ls1.getInputStream().read(buffer, 0, 0);
131        assertEquals(0, countRead);
132        assertEquals(42, ls1.getInputStream().read());
133
134        ss.close();
135
136        ls.close();
137
138        // Try write on closed socket
139
140        try {
141            ls.getOutputStream().write(42);
142            fail("expected exception");
143        } catch (IOException ex) {
144            // Expected
145        }
146
147        // Try read on closed socket
148
149        try {
150            ls.getInputStream().read();
151            fail("expected exception");
152        } catch (IOException ex) {
153            // Expected
154        }
155
156        // Try write on socket whose peer has closed
157
158        try {
159            ls1.getOutputStream().write(42);
160            fail("expected exception");
161        } catch (IOException ex) {
162            // Expected
163        }
164
165        // Try read on socket whose peer has closed
166
167        assertEquals(-1, ls1.getInputStream().read());
168
169        ls1.close();
170    }
171}
172