summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/io/OsTest.java
blob: 9b38ee9da9b47336de5ed6273cd9024b33a2830a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package libcore.io;

import android.system.ErrnoException;
import android.system.NetlinkSocketAddress;
import android.system.OsConstants;
import android.system.PacketSocketAddress;
import android.system.StructTimeval;
import android.system.StructUcred;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.InetUnixAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Locale;
import junit.framework.TestCase;
import static android.system.OsConstants.*;

public class OsTest extends TestCase {
  public void testIsSocket() throws Exception {
    File f = new File("/dev/null");
    FileInputStream fis = new FileInputStream(f);
    assertFalse(S_ISSOCK(Libcore.os.fstat(fis.getFD()).st_mode));
    fis.close();

    ServerSocket s = new ServerSocket();
    assertTrue(S_ISSOCK(Libcore.os.fstat(s.getImpl$().getFD$()).st_mode));
    s.close();
  }

  public void testFcntlInt() throws Exception {
    File f = File.createTempFile("OsTest", "tst");
    FileInputStream fis = null;
    try {
      fis = new FileInputStream(f);
      Libcore.os.fcntlInt(fis.getFD(), F_SETFD, FD_CLOEXEC);
      int flags = Libcore.os.fcntlVoid(fis.getFD(), F_GETFD);
      assertTrue((flags & FD_CLOEXEC) != 0);
    } finally {
      IoUtils.closeQuietly(fis);
      f.delete();
    }
  }

  public void testUnixDomainSockets_in_file_system() throws Exception {
    String path = System.getProperty("java.io.tmpdir") + "/test_unix_socket";
    new File(path).delete();
    checkUnixDomainSocket(new InetUnixAddress(path), false);
  }

  public void testUnixDomainSocket_abstract_name() throws Exception {
    // Linux treats a sun_path starting with a NUL byte as an abstract name. See unix(7).
    byte[] path = "/abstract_name_unix_socket".getBytes("UTF-8");
    path[0] = 0;
    checkUnixDomainSocket(new InetUnixAddress(path), true);
  }

  private void checkUnixDomainSocket(final InetUnixAddress address, final boolean isAbstract) throws Exception {
    final FileDescriptor serverFd = Libcore.os.socket(AF_UNIX, SOCK_STREAM, 0);
    Libcore.os.bind(serverFd, address, 0);
    Libcore.os.listen(serverFd, 5);

    checkSockName(serverFd, isAbstract, address);

    Thread server = new Thread(new Runnable() {
      public void run() {
        try {
          InetSocketAddress peerAddress = new InetSocketAddress();
          FileDescriptor clientFd = Libcore.os.accept(serverFd, peerAddress);
          checkSockName(clientFd, isAbstract, address);
          checkNoName(peerAddress);

          checkNoPeerName(clientFd);

          StructUcred credentials = Libcore.os.getsockoptUcred(clientFd, SOL_SOCKET, SO_PEERCRED);
          assertEquals(Libcore.os.getpid(), credentials.pid);
          assertEquals(Libcore.os.getuid(), credentials.uid);
          assertEquals(Libcore.os.getgid(), credentials.gid);

          byte[] request = new byte[256];
          Libcore.os.read(clientFd, request, 0, request.length);

          String s = new String(request, "UTF-8");
          byte[] response = s.toUpperCase(Locale.ROOT).getBytes("UTF-8");
          Libcore.os.write(clientFd, response, 0, response.length);

          Libcore.os.close(clientFd);
        } catch (Exception ex) {
          throw new RuntimeException(ex);
        }
      }
    });
    server.start();

    FileDescriptor clientFd = Libcore.os.socket(AF_UNIX, SOCK_STREAM, 0);

    Libcore.os.connect(clientFd, address, 0);
    checkNoSockName(clientFd);

    String string = "hello, world!";

    byte[] request = string.getBytes("UTF-8");
    assertEquals(request.length, Libcore.os.write(clientFd, request, 0, request.length));

    byte[] response = new byte[request.length];
    assertEquals(response.length, Libcore.os.read(clientFd, response, 0, response.length));

    assertEquals(string.toUpperCase(Locale.ROOT), new String(response, "UTF-8"));

    Libcore.os.close(clientFd);
  }

  private void checkSockName(FileDescriptor fd, boolean isAbstract, InetAddress address) throws Exception {
    InetSocketAddress isa = (InetSocketAddress) Libcore.os.getsockname(fd);
    if (isAbstract) {
      checkNoName(isa);
    } else {
      assertEquals(address, isa.getAddress());
    }
  }

  private void checkNoName(SocketAddress sa) {
    InetSocketAddress isa = (InetSocketAddress) sa;
    assertEquals(0, isa.getAddress().getAddress().length);
  }

  private void checkNoPeerName(FileDescriptor fd) throws Exception {
    checkNoName(Libcore.os.getpeername(fd));
  }

  private void checkNoSockName(FileDescriptor fd) throws Exception {
    checkNoName(Libcore.os.getsockname(fd));
  }

  public void test_strsignal() throws Exception {
    assertEquals("Killed", Libcore.os.strsignal(9));
    assertEquals("Unknown signal -1", Libcore.os.strsignal(-1));
  }

  public void test_byteBufferPositions_write_pwrite() throws Exception {
    FileOutputStream fos = new FileOutputStream(new File("/dev/null"));
    FileDescriptor fd = fos.getFD();
    final byte[] contents = new String("goodbye, cruel world").getBytes(StandardCharsets.US_ASCII);
    ByteBuffer byteBuffer = ByteBuffer.wrap(contents);

    byteBuffer.position(0);
    int written = Libcore.os.write(fd, byteBuffer);
    assertTrue(written > 0);
    assertEquals(written, byteBuffer.position());

    byteBuffer.position(4);
    written = Libcore.os.write(fd, byteBuffer);
    assertTrue(written > 0);
    assertEquals(written + 4, byteBuffer.position());

    byteBuffer.position(0);
    written = Libcore.os.pwrite(fd, byteBuffer, 64 /* offset */);
    assertTrue(written > 0);
    assertEquals(written, byteBuffer.position());

    byteBuffer.position(4);
    written = Libcore.os.pwrite(fd, byteBuffer, 64 /* offset */);
    assertTrue(written > 0);
    assertEquals(written + 4, byteBuffer.position());

    fos.close();
  }

  public void test_byteBufferPositions_read_pread() throws Exception {
    FileInputStream fis = new FileInputStream(new File("/dev/zero"));
    FileDescriptor fd = fis.getFD();
    ByteBuffer byteBuffer = ByteBuffer.allocate(64);

    byteBuffer.position(0);
    int read = Libcore.os.read(fd, byteBuffer);
    assertTrue(read > 0);
    assertEquals(read, byteBuffer.position());

    byteBuffer.position(4);
    read = Libcore.os.read(fd, byteBuffer);
    assertTrue(read > 0);
    assertEquals(read + 4, byteBuffer.position());

    byteBuffer.position(0);
    read = Libcore.os.pread(fd, byteBuffer, 64 /* offset */);
    assertTrue(read > 0);
    assertEquals(read, byteBuffer.position());

    byteBuffer.position(4);
    read = Libcore.os.pread(fd, byteBuffer, 64 /* offset */);
    assertTrue(read > 0);
    assertEquals(read + 4, byteBuffer.position());

    fis.close();
  }

  static void checkByteBufferPositions_sendto_recvfrom(
          int family, InetAddress loopback) throws Exception {
    final FileDescriptor serverFd = Libcore.os.socket(family, SOCK_STREAM, 0);
    Libcore.os.bind(serverFd, loopback, 0);
    Libcore.os.listen(serverFd, 5);

    InetSocketAddress address = (InetSocketAddress) Libcore.os.getsockname(serverFd);

    final Thread server = new Thread(new Runnable() {
      public void run() {
        try {
          InetSocketAddress peerAddress = new InetSocketAddress();
          FileDescriptor clientFd = Libcore.os.accept(serverFd, peerAddress);

          // Attempt to receive a maximum of 24 bytes from the client, and then
          // close the connection.
          ByteBuffer buffer = ByteBuffer.allocate(16);
          int received = Libcore.os.recvfrom(clientFd, buffer, 0, null);
          assertTrue(received > 0);
          assertEquals(received, buffer.position());

          ByteBuffer buffer2 = ByteBuffer.allocate(16);
          buffer2.position(8);
          received = Libcore.os.recvfrom(clientFd, buffer2, 0, null);
          assertTrue(received > 0);
          assertEquals(received + 8, buffer.position());

          Libcore.os.close(clientFd);
        } catch (Exception ex) {
          throw new RuntimeException(ex);
        }
      }
    });


    server.start();

    FileDescriptor clientFd = Libcore.os.socket(family, SOCK_STREAM, 0);
    Libcore.os.connect(clientFd, address.getAddress(), address.getPort());

    final byte[] bytes = "good bye, cruel black hole with fancy distortion".getBytes(StandardCharsets.US_ASCII);
    assertTrue(bytes.length > 24);

    ByteBuffer input = ByteBuffer.wrap(bytes);
    input.position(0);
    input.limit(16);

    int sent = Libcore.os.sendto(clientFd, input, 0, address.getAddress(), address.getPort());
    assertTrue(sent > 0);
    assertEquals(sent, input.position());

    input.position(16);
    input.limit(24);
    sent = Libcore.os.sendto(clientFd, input, 0, address.getAddress(), address.getPort());
    assertTrue(sent > 0);
    assertEquals(sent + 16, input.position());

    Libcore.os.close(clientFd);
  }

  public void test_NetlinkSocket() throws Exception {
    FileDescriptor nlSocket = Libcore.os.socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
    Libcore.os.bind(nlSocket, new NetlinkSocketAddress());
    NetlinkSocketAddress address = (NetlinkSocketAddress) Libcore.os.getsockname(nlSocket);
    assertTrue(address.getPortId() > 0);
    assertEquals(0, address.getGroupsMask());

    NetlinkSocketAddress nlKernel = new NetlinkSocketAddress();
    Libcore.os.connect(nlSocket, nlKernel);
    NetlinkSocketAddress nlPeer = (NetlinkSocketAddress) Libcore.os.getpeername(nlSocket);
    assertEquals(0, nlPeer.getPortId());
    assertEquals(0, nlPeer.getGroupsMask());
    Libcore.os.close(nlSocket);
  }

  public void test_PacketSocketAddress() throws Exception {
    NetworkInterface lo = NetworkInterface.getByName("lo");
    FileDescriptor fd = Libcore.os.socket(AF_PACKET, SOCK_DGRAM, ETH_P_IPV6);
    PacketSocketAddress addr = new PacketSocketAddress((short) ETH_P_IPV6, lo.getIndex());
    Libcore.os.bind(fd, addr);

    PacketSocketAddress bound = (PacketSocketAddress) Libcore.os.getsockname(fd);
    assertEquals((short) ETH_P_IPV6, bound.sll_protocol);  // ETH_P_IPV6 is an int.
    assertEquals(lo.getIndex(), bound.sll_ifindex);
    assertEquals(ARPHRD_LOOPBACK, bound.sll_hatype);
    assertEquals(0, bound.sll_pkttype);

    // The loopback address is ETH_ALEN bytes long and is all zeros.
    // http://lxr.free-electrons.com/source/drivers/net/loopback.c?v=3.10#L167
    assertEquals(6, bound.sll_addr.length);
    for (int i = 0; i < 6; i++) {
      assertEquals(0, bound.sll_addr[i]);
    }
  }

  public void test_byteBufferPositions_sendto_recvfrom_af_inet() throws Exception {
    checkByteBufferPositions_sendto_recvfrom(AF_INET, InetAddress.getByName("127.0.0.1"));
  }

  public void test_byteBufferPositions_sendto_recvfrom_af_inet6() throws Exception {
    checkByteBufferPositions_sendto_recvfrom(AF_INET6, InetAddress.getByName("::1"));
  }

  private void checkSendToSocketAddress(int family, InetAddress loopback) throws Exception {
    FileDescriptor recvFd = Libcore.os.socket(family, SOCK_DGRAM, 0);
    Libcore.os.bind(recvFd, loopback, 0);
    StructTimeval tv = StructTimeval.fromMillis(20);
    Libcore.os.setsockoptTimeval(recvFd, SOL_SOCKET, SO_RCVTIMEO, tv);

    InetSocketAddress to = ((InetSocketAddress) Libcore.os.getsockname(recvFd));
    FileDescriptor sendFd = Libcore.os.socket(family, SOCK_DGRAM, 0);
    byte[] msg = ("Hello, I'm going to a socket address: " + to.toString()).getBytes("UTF-8");
    int len = msg.length;

    assertEquals(len, Libcore.os.sendto(sendFd, msg, 0, len, 0, to));
    byte[] received = new byte[msg.length + 42];
    InetSocketAddress from = new InetSocketAddress();
    assertEquals(len, Libcore.os.recvfrom(recvFd, received, 0, received.length, 0, from));
    assertEquals(loopback, from.getAddress());
  }

  public void test_sendtoSocketAddress_af_inet() throws Exception {
      checkSendToSocketAddress(AF_INET, InetAddress.getByName("127.0.0.1"));
  }

  public void test_sendtoSocketAddress_af_inet6() throws Exception {
      checkSendToSocketAddress(AF_INET6, InetAddress.getByName("::1"));
  }

  public void test_socketFamilies() throws Exception {
    FileDescriptor fd = Libcore.os.socket(AF_INET6, SOCK_STREAM, 0);
    Libcore.os.bind(fd, InetAddress.getByName("::"), 0);
    InetSocketAddress localSocketAddress = (InetSocketAddress) Libcore.os.getsockname(fd);
    assertEquals(Inet6Address.ANY, localSocketAddress.getAddress());

    fd = Libcore.os.socket(AF_INET6, SOCK_STREAM, 0);
    Libcore.os.bind(fd, InetAddress.getByName("0.0.0.0"), 0);
    localSocketAddress = (InetSocketAddress) Libcore.os.getsockname(fd);
    assertEquals(Inet6Address.ANY, localSocketAddress.getAddress());

    fd = Libcore.os.socket(AF_INET, SOCK_STREAM, 0);
    Libcore.os.bind(fd, InetAddress.getByName("0.0.0.0"), 0);
    localSocketAddress = (InetSocketAddress) Libcore.os.getsockname(fd);
    assertEquals(Inet4Address.ANY, localSocketAddress.getAddress());
    try {
      Libcore.os.bind(fd, InetAddress.getByName("::"), 0);
      fail("Expected ErrnoException binding IPv4 socket to ::");
    } catch (ErrnoException expected) {
      assertEquals("Expected EAFNOSUPPORT binding IPv4 socket to ::", EAFNOSUPPORT, expected.errno);
    }
  }

  private static void assertArrayEquals(byte[] expected, byte[] actual) {
    assertTrue("Expected=" + Arrays.toString(expected) + ", actual=" + Arrays.toString(actual),
               Arrays.equals(expected, actual));
  }

  private static void checkSocketPing(FileDescriptor fd, InetAddress to, byte[] packet,
          byte type, byte responseType, boolean useSendto) throws Exception {
    int len = packet.length;
    packet[0] = type;
    if (useSendto) {
      assertEquals(len, Libcore.os.sendto(fd, packet, 0, len, 0, to, 0));
    } else {
      Libcore.os.connect(fd, to, 0);
      assertEquals(len, Libcore.os.sendto(fd, packet, 0, len, 0, null, 0));
    }

    int icmpId = ((InetSocketAddress) Libcore.os.getsockname(fd)).getPort();
    byte[] received = new byte[4096];
    InetSocketAddress srcAddress = new InetSocketAddress();
    assertEquals(len, Libcore.os.recvfrom(fd, received, 0, received.length, 0, srcAddress));
    assertEquals(to, srcAddress.getAddress());
    assertEquals(responseType, received[0]);
    assertEquals(received[4], (byte) (icmpId >> 8));
    assertEquals(received[5], (byte) (icmpId & 0xff));

    received = Arrays.copyOf(received, len);
    received[0] = (byte) type;
    received[2] = received[3] = 0;  // Checksum.
    received[4] = received[5] = 0;  // ICMP ID.
    assertArrayEquals(packet, received);
  }

  public void test_socketPing() throws Exception {
    final byte ICMP_ECHO = 8, ICMP_ECHOREPLY = 0;
    final byte ICMPV6_ECHO_REQUEST = (byte) 128, ICMPV6_ECHO_REPLY = (byte) 129;
    final byte[] packet = ("\000\000\000\000" +  // ICMP type, code.
                           "\000\000\000\003" +  // ICMP ID (== port), sequence number.
                           "Hello myself").getBytes(StandardCharsets.US_ASCII);

    FileDescriptor fd = Libcore.os.socket(AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6);
    InetAddress ipv6Loopback = InetAddress.getByName("::1");
    checkSocketPing(fd, ipv6Loopback, packet, ICMPV6_ECHO_REQUEST, ICMPV6_ECHO_REPLY, true);
    checkSocketPing(fd, ipv6Loopback, packet, ICMPV6_ECHO_REQUEST, ICMPV6_ECHO_REPLY, false);

    fd = Libcore.os.socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
    InetAddress ipv4Loopback = InetAddress.getByName("127.0.0.1");
    checkSocketPing(fd, ipv4Loopback, packet, ICMP_ECHO, ICMP_ECHOREPLY, true);
    checkSocketPing(fd, ipv4Loopback, packet, ICMP_ECHO, ICMP_ECHOREPLY, false);
  }

    private static void assertPartial(byte[] expected, byte[] actual) {
        for (int i = 0; i < expected.length; i++) {
            if (expected[i] != actual[i]) {
                fail("Expected " + Arrays.toString(expected) + " but found "
                        + Arrays.toString(actual));
            }
        }
    }

    public void test_xattr() throws Exception {
        final String NAME_TEST = "user.meow";

        final byte[] VALUE_CAKE = "cake cake cake".getBytes(StandardCharsets.UTF_8);
        final byte[] VALUE_PIE = "pie".getBytes(StandardCharsets.UTF_8);

        File file = File.createTempFile("xattr", "test");
        String path = file.getAbsolutePath();

        byte[] tmp = new byte[1024];
        try {
            try {
                Libcore.os.getxattr(path, NAME_TEST, tmp);
                fail("Expected ENODATA");
            } catch (ErrnoException e) {
                assertEquals(OsConstants.ENODATA, e.errno);
            }

            Libcore.os.setxattr(path, NAME_TEST, VALUE_CAKE, OsConstants.XATTR_CREATE);
            assertEquals(VALUE_CAKE.length, Libcore.os.getxattr(path, NAME_TEST, tmp));
            assertPartial(VALUE_CAKE, tmp);

            try {
                Libcore.os.setxattr(path, NAME_TEST, VALUE_PIE, OsConstants.XATTR_CREATE);
                fail("Expected EEXIST");
            } catch (ErrnoException e) {
                assertEquals(OsConstants.EEXIST, e.errno);
            }

            Libcore.os.setxattr(path, NAME_TEST, VALUE_PIE, OsConstants.XATTR_REPLACE);
            assertEquals(VALUE_PIE.length, Libcore.os.getxattr(path, NAME_TEST, tmp));
            assertPartial(VALUE_PIE, tmp);

            Libcore.os.removexattr(path, NAME_TEST);
            try {
                Libcore.os.getxattr(path, NAME_TEST, tmp);
                fail("Expected ENODATA");
            } catch (ErrnoException e) {
                assertEquals(OsConstants.ENODATA, e.errno);
            }

        } finally {
            file.delete();
        }
    }
}