xref: /libuv/test/test-pipe-set-fchmod.c (revision 011a1ac1)
1 /* Copyright libuv project contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21 
22 
23 #include "uv.h"
24 #include "task.h"
25 #include <string.h>
26 
TEST_IMPL(pipe_set_chmod)27 TEST_IMPL(pipe_set_chmod) {
28   uv_pipe_t pipe_handle;
29   uv_loop_t* loop;
30   int r;
31 #ifndef _WIN32
32   struct stat stat_buf;
33 #endif
34 
35   loop = uv_default_loop();
36 
37   r = uv_pipe_init(loop, &pipe_handle, 0);
38   ASSERT_OK(r);
39 
40   r = uv_pipe_bind(&pipe_handle, TEST_PIPENAME);
41   ASSERT_OK(r);
42 
43   /* No easy way to test if this works, we will only make sure that the call is
44    * successful. */
45   r = uv_pipe_chmod(&pipe_handle, UV_READABLE);
46   if (r == UV_EPERM) {
47     MAKE_VALGRIND_HAPPY(loop);
48     RETURN_SKIP("Insufficient privileges to alter pipe fmode");
49   }
50   ASSERT_OK(r);
51 #ifndef _WIN32
52   memset(&stat_buf, 0, sizeof(stat_buf));
53   ASSERT_OK(stat(TEST_PIPENAME, &stat_buf));
54   ASSERT(stat_buf.st_mode & S_IRUSR);
55   ASSERT(stat_buf.st_mode & S_IRGRP);
56   ASSERT(stat_buf.st_mode & S_IROTH);
57 #endif
58 
59   r = uv_pipe_chmod(&pipe_handle, UV_WRITABLE);
60   ASSERT_OK(r);
61 #ifndef _WIN32
62   stat(TEST_PIPENAME, &stat_buf);
63   ASSERT(stat_buf.st_mode & S_IWUSR);
64   ASSERT(stat_buf.st_mode & S_IWGRP);
65   ASSERT(stat_buf.st_mode & S_IWOTH);
66 #endif
67 
68   r = uv_pipe_chmod(&pipe_handle, UV_WRITABLE | UV_READABLE);
69   ASSERT_OK(r);
70 #ifndef _WIN32
71   stat(TEST_PIPENAME, &stat_buf);
72   ASSERT(stat_buf.st_mode & S_IRUSR);
73   ASSERT(stat_buf.st_mode & S_IRGRP);
74   ASSERT(stat_buf.st_mode & S_IROTH);
75   ASSERT(stat_buf.st_mode & S_IWUSR);
76   ASSERT(stat_buf.st_mode & S_IWGRP);
77   ASSERT(stat_buf.st_mode & S_IWOTH);
78 #endif
79 
80   r = uv_pipe_chmod(NULL, UV_WRITABLE | UV_READABLE);
81   ASSERT_EQ(r, UV_EBADF);
82 
83   r = uv_pipe_chmod(&pipe_handle, 12345678);
84   ASSERT_EQ(r, UV_EINVAL);
85 
86   uv_close((uv_handle_t*)&pipe_handle, NULL);
87   r = uv_pipe_chmod(&pipe_handle, UV_WRITABLE | UV_READABLE);
88   ASSERT_EQ(r, UV_EBADF);
89 
90   MAKE_VALGRIND_HAPPY(loop);
91   return 0;
92 }
93