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
26TEST_IMPL(pipe_set_chmod) {
27 uv_pipe_t pipe_handle;
28 uv_loop_t* loop;
29 int r;
30#ifndef _WIN32
31 struct stat stat_buf;
32#endif
33
34 loop = uv_default_loop();
35
36 r = uv_pipe_init(loop, &pipe_handle, 0);
37 ASSERT(r == 0);
38
39 r = uv_pipe_bind(&pipe_handle, TEST_PIPENAME);
40 ASSERT(r == 0);
41
42 /* No easy way to test if this works, we will only make sure that the call is
43 * successful. */
44 r = uv_pipe_chmod(&pipe_handle, UV_READABLE);
45 if (r == UV_EPERM) {
46 MAKE_VALGRIND_HAPPY();
47 RETURN_SKIP("Insufficient privileges to alter pipe fmode");
48 }
49 ASSERT(r == 0);
50#ifndef _WIN32
51 stat(TEST_PIPENAME, &stat_buf);
52 ASSERT(stat_buf.st_mode & S_IRUSR);
53 ASSERT(stat_buf.st_mode & S_IRGRP);
54 ASSERT(stat_buf.st_mode & S_IROTH);
55#endif
56
57 r = uv_pipe_chmod(&pipe_handle, UV_WRITABLE);
58 ASSERT(r == 0);
59#ifndef _WIN32
60 stat(TEST_PIPENAME, &stat_buf);
61 ASSERT(stat_buf.st_mode & S_IWUSR);
62 ASSERT(stat_buf.st_mode & S_IWGRP);
63 ASSERT(stat_buf.st_mode & S_IWOTH);
64#endif
65
66 r = uv_pipe_chmod(&pipe_handle, UV_WRITABLE | UV_READABLE);
67 ASSERT(r == 0);
68#ifndef _WIN32
69 stat(TEST_PIPENAME, &stat_buf);
70 ASSERT(stat_buf.st_mode & S_IRUSR);
71 ASSERT(stat_buf.st_mode & S_IRGRP);
72 ASSERT(stat_buf.st_mode & S_IROTH);
73 ASSERT(stat_buf.st_mode & S_IWUSR);
74 ASSERT(stat_buf.st_mode & S_IWGRP);
75 ASSERT(stat_buf.st_mode & S_IWOTH);
76#endif
77
78 r = uv_pipe_chmod(NULL, UV_WRITABLE | UV_READABLE);
79 ASSERT(r == UV_EBADF);
80
81 r = uv_pipe_chmod(&pipe_handle, 12345678);
82 ASSERT(r == UV_EINVAL);
83
84 uv_close((uv_handle_t*)&pipe_handle, NULL);
85 r = uv_pipe_chmod(&pipe_handle, UV_WRITABLE | UV_READABLE);
86 ASSERT(r == UV_EBADF);
87
88 MAKE_VALGRIND_HAPPY();
89 return 0;
90}
91