1 /*
2 * Copyright (c) 2016-2024 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <assert.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <uuid/uuid.h>
34 #include <unistd.h>
35 #include "skywalk_test_driver.h"
36 #include "skywalk_test_common.h"
37 #include "skywalk_test_utils.h"
38
39 static int
skt_closecfd_main(int argc,char * argv[])40 skt_closecfd_main(int argc, char *argv[])
41 {
42 int error;
43 channel_t channel;
44 uuid_t channel_uuid;
45 int channelfd;
46
47 error = uuid_parse(argv[3], channel_uuid);
48 SKTC_ASSERT_ERR(!error);
49
50 channel = sktu_channel_create_extended(channel_uuid, 0,
51 CHANNEL_DIR_TX_RX, CHANNEL_RING_ID_ANY, NULL,
52 -1, -1, -1, -1, -1, -1, -1, 1, -1, -1);
53 assert(channel);
54
55 channelfd = os_channel_get_fd(channel);
56 assert(channelfd != -1);
57
58 error = close(channelfd); // expected guarded fd fail.
59 SKTC_ASSERT_ERR(!error);
60
61 return 1; // should not reach
62 }
63
64 struct skywalk_test skt_closecfd = {
65 "closecfd", "test closing guarded channel fd",
66 SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE,
67 skt_closecfd_main, SKTC_GENERIC_UPIPE_ARGV,
68 sktc_generic_upipe_null_init, sktc_generic_upipe_fini,
69 0x4000000100000000, 0xFFFFFFFF,
70 };
71
72 /****************************************************************/
73
74 static int
skt_writecfd_main(int argc,char * argv[])75 skt_writecfd_main(int argc, char *argv[])
76 {
77 int error;
78 ssize_t ret;
79 channel_t channel;
80 uuid_t channel_uuid;
81 int channelfd;
82 char buf[100];
83
84 error = uuid_parse(argv[3], channel_uuid);
85 SKTC_ASSERT_ERR(!error);
86
87 channel = sktu_channel_create_extended(channel_uuid, 0,
88 CHANNEL_DIR_TX_RX, CHANNEL_RING_ID_ANY, NULL,
89 -1, -1, -1, -1, -1, -1, -1, 1, -1, -1);
90 assert(channel);
91
92 channelfd = os_channel_get_fd(channel);
93 assert(channelfd != -1);
94
95 ret = write(channelfd, buf, sizeof(buf));
96 assert(ret == -1);
97 SKTC_ASSERT_ERR(errno == EBADF);
98
99 return 0;
100 }
101
102 struct skywalk_test skt_writecfd = {
103 "writecfd", "test writing to channel fd",
104 SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE,
105 skt_writecfd_main, SKTC_GENERIC_UPIPE_ARGV,
106 sktc_generic_upipe_null_init, sktc_generic_upipe_fini,
107 };
108
109
110 /****************************************************************/
111
112 static int
skt_readcfd_main(int argc,char * argv[])113 skt_readcfd_main(int argc, char *argv[])
114 {
115 int error;
116 ssize_t ret;
117 channel_t channel;
118 uuid_t channel_uuid;
119 int channelfd;
120 char buf[100];
121
122 error = uuid_parse(argv[3], channel_uuid);
123 SKTC_ASSERT_ERR(!error);
124
125 channel = sktu_channel_create_extended(channel_uuid, 0,
126 CHANNEL_DIR_TX_RX, CHANNEL_RING_ID_ANY, NULL,
127 -1, -1, -1, -1, -1, -1, -1, 1, -1, -1);
128 assert(channel);
129
130 channelfd = os_channel_get_fd(channel);
131 assert(channelfd != -1);
132
133 ret = read(channelfd, buf, sizeof(buf));
134 assert(ret == -1);
135 SKTC_ASSERT_ERR(errno == EBADF);
136
137 return 0;
138 }
139
140 struct skywalk_test skt_readcfd = {
141 "readcfd", "test reading from channel fd",
142 SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE,
143 skt_readcfd_main, SKTC_GENERIC_UPIPE_ARGV,
144 sktc_generic_upipe_null_init, sktc_generic_upipe_fini,
145 };
146
147 /****************************************************************/
148