1 // Copyright 2017 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <fcntl.h>
6 #include <limits.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12
13 #include <lib/fdio/limits.h>
14 #include <lib/fdio/util.h>
15 #include <unittest/unittest.h>
16 #include <zircon/syscalls.h>
17
18 #include "filesystems.h"
19
TestCloneSimple(void)20 bool TestCloneSimple(void) {
21 BEGIN_TEST;
22
23 int fd = open("::file", O_RDWR | O_CREAT, 0644);
24 ASSERT_GT(fd, 0);
25
26 zx_handle_t handle[FDIO_MAX_HANDLES];
27 uint32_t type[FDIO_MAX_HANDLES];
28 zx_status_t r = fdio_clone_fd(fd, 0, handle, type);
29 ASSERT_GT(r, 0);
30
31 int fd2;
32 ASSERT_EQ(fdio_create_fd(handle, type, r, &fd2), ZX_OK);
33
34 // Output from one fd...
35 char output[5];
36 memset(output, 'a', sizeof(output));
37 ASSERT_EQ(write(fd, output, sizeof(output)), sizeof(output));
38
39 // TODO(ZX-510): Make seek behavior across dup consistent
40 // among filesystems. Currently, this seek is necessary
41 // for thinfs, but not for minfs/memfs.
42 ASSERT_EQ(lseek(fd, 0, SEEK_SET), 0);
43
44 // ... Should be visible to the other fd.
45 char input[5];
46 ASSERT_EQ(read(fd2, input, sizeof(input)), sizeof(input));
47 ASSERT_EQ(memcmp(input, output, sizeof(input)), 0);
48
49 // Clean up
50 ASSERT_EQ(close(fd), 0);
51 ASSERT_EQ(close(fd2), 0);
52 ASSERT_EQ(unlink("::file"), 0);
53 END_TEST;
54 }
55
56 RUN_FOR_ALL_FILESYSTEMS(clone_tests,
57 RUN_TEST_MEDIUM(TestCloneSimple)
58 )
59