xref: /libuv/test/test-getters-setters.c (revision 8a499e13)
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 #include "uv.h"
23 #include "task.h"
24 #include <string.h>
25 #include <sys/stat.h>
26 
27 #ifdef _WIN32
28 # define S_IFDIR _S_IFDIR
29 #endif
30 
31 int cookie1;
32 int cookie2;
33 int cookie3;
34 
35 
TEST_IMPL(handle_type_name)36 TEST_IMPL(handle_type_name) {
37   ASSERT_OK(strcmp(uv_handle_type_name(UV_NAMED_PIPE), "pipe"));
38   ASSERT_OK(strcmp(uv_handle_type_name(UV_UDP), "udp"));
39   ASSERT_OK(strcmp(uv_handle_type_name(UV_FILE), "file"));
40   ASSERT_NULL(uv_handle_type_name(UV_HANDLE_TYPE_MAX));
41   ASSERT_NULL(uv_handle_type_name(UV_HANDLE_TYPE_MAX + 1));
42   ASSERT_NULL(uv_handle_type_name(UV_UNKNOWN_HANDLE));
43   return 0;
44 }
45 
46 
TEST_IMPL(req_type_name)47 TEST_IMPL(req_type_name) {
48   ASSERT_OK(strcmp(uv_req_type_name(UV_REQ), "req"));
49   ASSERT_OK(strcmp(uv_req_type_name(UV_UDP_SEND), "udp_send"));
50   ASSERT_OK(strcmp(uv_req_type_name(UV_WORK), "work"));
51   ASSERT_NULL(uv_req_type_name(UV_REQ_TYPE_MAX));
52   ASSERT_NULL(uv_req_type_name(UV_REQ_TYPE_MAX + 1));
53   ASSERT_NULL(uv_req_type_name(UV_UNKNOWN_REQ));
54   return 0;
55 }
56 
57 
TEST_IMPL(getters_setters)58 TEST_IMPL(getters_setters) {
59   uv_loop_t* loop;
60   uv_pipe_t* pipe;
61   uv_fs_t* fs;
62   int r;
63 
64   loop = malloc(uv_loop_size());
65   ASSERT_NOT_NULL(loop);
66   r = uv_loop_init(loop);
67   ASSERT_OK(r);
68 
69   uv_loop_set_data(loop, &cookie1);
70   ASSERT_PTR_EQ(loop->data, &cookie1);
71   ASSERT_PTR_EQ(uv_loop_get_data(loop), &cookie1);
72 
73   pipe = malloc(uv_handle_size(UV_NAMED_PIPE));
74   r = uv_pipe_init(loop, pipe, 0);
75   ASSERT_OK(r);
76   ASSERT_EQ(uv_handle_get_type((uv_handle_t*)pipe), UV_NAMED_PIPE);
77 
78   ASSERT_PTR_EQ(uv_handle_get_loop((uv_handle_t*)pipe), loop);
79   pipe->data = &cookie2;
80   ASSERT_PTR_EQ(uv_handle_get_data((uv_handle_t*)pipe), &cookie2);
81   uv_handle_set_data((uv_handle_t*)pipe, &cookie1);
82   ASSERT_PTR_EQ(uv_handle_get_data((uv_handle_t*)pipe), &cookie1);
83   ASSERT_PTR_EQ(pipe->data, &cookie1);
84 
85   ASSERT_OK(uv_stream_get_write_queue_size((uv_stream_t*)pipe));
86   pipe->write_queue_size++;
87   ASSERT_EQ(1, uv_stream_get_write_queue_size((uv_stream_t*)pipe));
88   pipe->write_queue_size--;
89   uv_close((uv_handle_t*)pipe, NULL);
90 
91   r = uv_run(loop, UV_RUN_DEFAULT);
92   ASSERT_OK(r);
93 
94   fs = malloc(uv_req_size(UV_FS));
95   uv_fs_stat(loop, fs, ".", NULL);
96 
97   r = uv_run(loop, UV_RUN_DEFAULT);
98   ASSERT_OK(r);
99 
100   ASSERT_EQ(uv_fs_get_type(fs), UV_FS_STAT);
101   ASSERT_OK(uv_fs_get_result(fs));
102   ASSERT_PTR_EQ(uv_fs_get_ptr(fs), uv_fs_get_statbuf(fs));
103   ASSERT(uv_fs_get_statbuf(fs)->st_mode & S_IFDIR);
104   ASSERT_OK(strcmp(uv_fs_get_path(fs), "."));
105   uv_fs_req_cleanup(fs);
106 
107   r = uv_loop_close(loop);
108   ASSERT_OK(r);
109 
110   free(pipe);
111   free(fs);
112   free(loop);
113   return 0;
114 }
115