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