Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / native_client / tests / nonsfi / fcntl_test.cc
1 /*
2  * Copyright 2014 The Native Client Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 #include <fcntl.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10
11 #include "native_client/src/include/nacl_assert.h"
12
13 void test_getsetfl(const char *test_file) {
14   puts("test for F_GETFL and F_SETFL");
15
16   int fd = open(test_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
17   ASSERT_GE(fd, 0);
18
19   int rc = fcntl(fd, F_SETFL, O_NONBLOCK);
20   ASSERT_EQ(rc, 0);
21   rc = fcntl(fd, F_GETFL);
22   ASSERT_GE(rc, 0);
23   /*
24    * We only test the O_NONBLOCK bit because the user mode qemu does
25    * not return O_WRONLY with O_NONBLOCK.
26    */
27   ASSERT_EQ(rc & O_NONBLOCK, O_NONBLOCK);
28
29   rc = close(fd);
30   ASSERT_EQ(rc, 0);
31 }
32
33 void test_getsetfd(const char *test_file) {
34   puts("test for F_GETFD and F_SETFD");
35
36   int fd = open(test_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
37   ASSERT_GE(fd, 0);
38
39   int rc = fcntl(fd, F_GETFD);
40   ASSERT_EQ(rc, 0);
41   rc = fcntl(fd, F_SETFD, FD_CLOEXEC);
42   ASSERT_EQ(rc, 0);
43   rc = fcntl(fd, F_GETFD);
44   ASSERT_EQ(rc, FD_CLOEXEC);
45
46   rc = close(fd);
47   ASSERT_EQ(rc, 0);
48 }
49
50 int main(int argc, char *argv[]) {
51   if (argc != 2) {
52     printf("Please specify the test file name\n");
53     exit(-1);
54   }
55
56   const char *test_file = argv[1];
57   test_getsetfl(test_file);
58   test_getsetfd(test_file);
59
60   puts("PASSED");
61   return 0;
62 }