1*b29f2fbfSchristos /* Copyright libuv project contributors. All rights reserved.
2*b29f2fbfSchristos *
3*b29f2fbfSchristos * Permission is hereby granted, free of charge, to any person obtaining a copy
4*b29f2fbfSchristos * of this software and associated documentation files (the "Software"), to
5*b29f2fbfSchristos * deal in the Software without restriction, including without limitation the
6*b29f2fbfSchristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7*b29f2fbfSchristos * sell copies of the Software, and to permit persons to whom the Software is
8*b29f2fbfSchristos * furnished to do so, subject to the following conditions:
9*b29f2fbfSchristos *
10*b29f2fbfSchristos * The above copyright notice and this permission notice shall be included in
11*b29f2fbfSchristos * all copies or substantial portions of the Software.
12*b29f2fbfSchristos *
13*b29f2fbfSchristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*b29f2fbfSchristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*b29f2fbfSchristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16*b29f2fbfSchristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17*b29f2fbfSchristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18*b29f2fbfSchristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19*b29f2fbfSchristos * IN THE SOFTWARE.
20*b29f2fbfSchristos */
21*b29f2fbfSchristos
22fbb2e0a3Schristos #include "uv.h"
23fbb2e0a3Schristos #include "task.h"
24fbb2e0a3Schristos #include <string.h>
25fbb2e0a3Schristos #include <sys/stat.h>
26fbb2e0a3Schristos
27fbb2e0a3Schristos int cookie1;
28fbb2e0a3Schristos int cookie2;
29fbb2e0a3Schristos int cookie3;
30fbb2e0a3Schristos
31fbb2e0a3Schristos
TEST_IMPL(handle_type_name)32fbb2e0a3Schristos TEST_IMPL(handle_type_name) {
33fbb2e0a3Schristos ASSERT(strcmp(uv_handle_type_name(UV_NAMED_PIPE), "pipe") == 0);
34fbb2e0a3Schristos ASSERT(strcmp(uv_handle_type_name(UV_UDP), "udp") == 0);
35fbb2e0a3Schristos ASSERT(strcmp(uv_handle_type_name(UV_FILE), "file") == 0);
36*b29f2fbfSchristos ASSERT_NULL(uv_handle_type_name(UV_HANDLE_TYPE_MAX));
37*b29f2fbfSchristos ASSERT_NULL(uv_handle_type_name(UV_HANDLE_TYPE_MAX + 1));
38*b29f2fbfSchristos ASSERT_NULL(uv_handle_type_name(UV_UNKNOWN_HANDLE));
39fbb2e0a3Schristos return 0;
40fbb2e0a3Schristos }
41fbb2e0a3Schristos
42fbb2e0a3Schristos
TEST_IMPL(req_type_name)43fbb2e0a3Schristos TEST_IMPL(req_type_name) {
44fbb2e0a3Schristos ASSERT(strcmp(uv_req_type_name(UV_REQ), "req") == 0);
45fbb2e0a3Schristos ASSERT(strcmp(uv_req_type_name(UV_UDP_SEND), "udp_send") == 0);
46fbb2e0a3Schristos ASSERT(strcmp(uv_req_type_name(UV_WORK), "work") == 0);
47*b29f2fbfSchristos ASSERT_NULL(uv_req_type_name(UV_REQ_TYPE_MAX));
48*b29f2fbfSchristos ASSERT_NULL(uv_req_type_name(UV_REQ_TYPE_MAX + 1));
49*b29f2fbfSchristos ASSERT_NULL(uv_req_type_name(UV_UNKNOWN_REQ));
50fbb2e0a3Schristos return 0;
51fbb2e0a3Schristos }
52fbb2e0a3Schristos
53fbb2e0a3Schristos
TEST_IMPL(getters_setters)54fbb2e0a3Schristos TEST_IMPL(getters_setters) {
55fbb2e0a3Schristos uv_loop_t* loop;
56fbb2e0a3Schristos uv_pipe_t* pipe;
57fbb2e0a3Schristos uv_fs_t* fs;
58fbb2e0a3Schristos int r;
59fbb2e0a3Schristos
60fbb2e0a3Schristos loop = malloc(uv_loop_size());
61*b29f2fbfSchristos ASSERT_NOT_NULL(loop);
62fbb2e0a3Schristos r = uv_loop_init(loop);
63fbb2e0a3Schristos ASSERT(r == 0);
64fbb2e0a3Schristos
65fbb2e0a3Schristos uv_loop_set_data(loop, &cookie1);
66fbb2e0a3Schristos ASSERT(loop->data == &cookie1);
67fbb2e0a3Schristos ASSERT(uv_loop_get_data(loop) == &cookie1);
68fbb2e0a3Schristos
69fbb2e0a3Schristos pipe = malloc(uv_handle_size(UV_NAMED_PIPE));
70fbb2e0a3Schristos r = uv_pipe_init(loop, pipe, 0);
71fbb2e0a3Schristos ASSERT(uv_handle_get_type((uv_handle_t*)pipe) == UV_NAMED_PIPE);
72fbb2e0a3Schristos
73fbb2e0a3Schristos ASSERT(uv_handle_get_loop((uv_handle_t*)pipe) == loop);
74fbb2e0a3Schristos pipe->data = &cookie2;
75fbb2e0a3Schristos ASSERT(uv_handle_get_data((uv_handle_t*)pipe) == &cookie2);
76fbb2e0a3Schristos uv_handle_set_data((uv_handle_t*)pipe, &cookie1);
77fbb2e0a3Schristos ASSERT(uv_handle_get_data((uv_handle_t*)pipe) == &cookie1);
78fbb2e0a3Schristos ASSERT(pipe->data == &cookie1);
79fbb2e0a3Schristos
80fbb2e0a3Schristos ASSERT(uv_stream_get_write_queue_size((uv_stream_t*)pipe) == 0);
81fbb2e0a3Schristos pipe->write_queue_size++;
82fbb2e0a3Schristos ASSERT(uv_stream_get_write_queue_size((uv_stream_t*)pipe) == 1);
83fbb2e0a3Schristos pipe->write_queue_size--;
84fbb2e0a3Schristos uv_close((uv_handle_t*)pipe, NULL);
85fbb2e0a3Schristos
86fbb2e0a3Schristos r = uv_run(loop, UV_RUN_DEFAULT);
87fbb2e0a3Schristos ASSERT(r == 0);
88fbb2e0a3Schristos
89fbb2e0a3Schristos fs = malloc(uv_req_size(UV_FS));
90fbb2e0a3Schristos uv_fs_stat(loop, fs, ".", NULL);
91fbb2e0a3Schristos
92fbb2e0a3Schristos r = uv_run(loop, UV_RUN_DEFAULT);
93fbb2e0a3Schristos ASSERT(r == 0);
94fbb2e0a3Schristos
95fbb2e0a3Schristos ASSERT(uv_fs_get_type(fs) == UV_FS_STAT);
96fbb2e0a3Schristos ASSERT(uv_fs_get_result(fs) == 0);
97fbb2e0a3Schristos ASSERT(uv_fs_get_ptr(fs) == uv_fs_get_statbuf(fs));
98fbb2e0a3Schristos ASSERT(uv_fs_get_statbuf(fs)->st_mode & S_IFDIR);
99fbb2e0a3Schristos ASSERT(strcmp(uv_fs_get_path(fs), ".") == 0);
100fbb2e0a3Schristos uv_fs_req_cleanup(fs);
101fbb2e0a3Schristos
102fbb2e0a3Schristos r = uv_loop_close(loop);
103fbb2e0a3Schristos ASSERT(r == 0);
104fbb2e0a3Schristos
105fbb2e0a3Schristos free(pipe);
106fbb2e0a3Schristos free(fs);
107fbb2e0a3Schristos free(loop);
108fbb2e0a3Schristos return 0;
109fbb2e0a3Schristos }
110