1 /* $NetBSD: t_ksem.c,v 1.1 2019/02/03 03:20:24 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __COPYRIGHT("@(#) Copyright (c) 2019\
31 The NetBSD Foundation, inc. All rights reserved.");
32 __RCSID("$NetBSD: t_ksem.c,v 1.1 2019/02/03 03:20:24 thorpej Exp $");
33
34 #include <sys/mman.h>
35 #include <sys/wait.h>
36
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <unistd.h>
41
42 #include <atf-c.h>
43
44 #define _LIBC
45 #include <sys/ksem.h>
46
47 ATF_TC(close_on_unnamed);
ATF_TC_HEAD(close_on_unnamed,tc)48 ATF_TC_HEAD(close_on_unnamed, tc)
49 {
50 atf_tc_set_md_var(tc, "descr",
51 "test for correct error on close of unnamed semaphore");
52 }
ATF_TC_BODY(close_on_unnamed,tc)53 ATF_TC_BODY(close_on_unnamed, tc)
54 {
55 intptr_t ksem;
56
57 /* _ksem_close() is invalid on unnamed semaphore. */
58 ksem = 0;
59 ATF_REQUIRE_EQ(_ksem_init(0, &ksem), 0);
60 ATF_REQUIRE(_ksem_close(ksem) == -1 && errno == EINVAL);
61 ATF_REQUIRE_EQ(_ksem_destroy(ksem), 0);
62 }
63
64 ATF_TC(close_on_unnamed_pshared);
ATF_TC_HEAD(close_on_unnamed_pshared,tc)65 ATF_TC_HEAD(close_on_unnamed_pshared, tc)
66 {
67 atf_tc_set_md_var(tc, "descr",
68 "test for correct error on close of unnamed pshared semaphore");
69 }
ATF_TC_BODY(close_on_unnamed_pshared,tc)70 ATF_TC_BODY(close_on_unnamed_pshared, tc)
71 {
72 intptr_t ksem;
73
74 /* Similar, but lifecycle of pshared is slightly different. */
75 ksem = KSEM_PSHARED;
76 ATF_REQUIRE_EQ(_ksem_init(0, &ksem), 0);
77 ATF_REQUIRE(_ksem_close(ksem) == -1 && errno == EINVAL);
78 ATF_REQUIRE_EQ(_ksem_destroy(ksem), 0);
79 }
80
81 ATF_TC_WITH_CLEANUP(destroy_on_named);
ATF_TC_HEAD(destroy_on_named,tc)82 ATF_TC_HEAD(destroy_on_named, tc)
83 {
84 atf_tc_set_md_var(tc, "descr",
85 "test for correct error on destroy of named semaphore");
86 }
ATF_TC_BODY(destroy_on_named,tc)87 ATF_TC_BODY(destroy_on_named, tc)
88 {
89 intptr_t ksem;
90
91 /* Exercise open-unlinked semaphore lifecycle. */
92 ATF_REQUIRE_EQ(_ksem_open("/ksem_x", O_CREAT | O_EXCL, 0644, 0, &ksem),
93 0);
94 ATF_REQUIRE(_ksem_destroy(ksem) == -1 && errno == EINVAL);
95 ATF_REQUIRE_EQ(_ksem_close(ksem), 0);
96 ATF_REQUIRE_EQ(_ksem_unlink("/ksem_x"), 0);
97 }
ATF_TC_CLEANUP(destroy_on_named,tc)98 ATF_TC_CLEANUP(destroy_on_named, tc)
99 {
100 (void)_ksem_unlink("/ksem_x");
101 }
102
103 ATF_TC_WITH_CLEANUP(open_unlinked_lifecycle);
ATF_TC_HEAD(open_unlinked_lifecycle,tc)104 ATF_TC_HEAD(open_unlinked_lifecycle, tc)
105 {
106 atf_tc_set_md_var(tc, "descr",
107 "Exercises lifecycle of open-unlined semaphores");
108 }
ATF_TC_BODY(open_unlinked_lifecycle,tc)109 ATF_TC_BODY(open_unlinked_lifecycle, tc)
110 {
111 intptr_t ksem, ksem1;
112 int val;
113
114 /* Exercise open-unlinked semaphore lifecycle. */
115 ATF_REQUIRE_EQ(_ksem_open("/ksem_b", O_CREAT | O_EXCL, 0644, 0, &ksem),
116 0);
117 ATF_REQUIRE_EQ(_ksem_unlink("/ksem_b"), 0);
118 val = 255;
119 ATF_REQUIRE(_ksem_getvalue(ksem, &val) == 0 && val == 0);
120
121 ATF_REQUIRE_EQ(_ksem_open("/ksem_b", O_CREAT | O_EXCL, 0644, 1, &ksem1),
122 0);
123 ATF_REQUIRE_EQ(_ksem_unlink("/ksem_b"), 0);
124 val = 255;
125 ATF_REQUIRE(_ksem_getvalue(ksem1, &val) == 0 && val == 1);
126
127 ATF_REQUIRE_EQ(_ksem_close(ksem), 0);
128 ATF_REQUIRE_EQ(_ksem_close(ksem1), 0);
129 }
ATF_TC_CLEANUP(open_unlinked_lifecycle,tc)130 ATF_TC_CLEANUP(open_unlinked_lifecycle, tc)
131 {
132 (void)_ksem_unlink("/ksem_a");
133 (void)_ksem_unlink("/ksem_b");
134 }
135
ATF_TP_ADD_TCS(tp)136 ATF_TP_ADD_TCS(tp)
137 {
138
139 ATF_TP_ADD_TC(tp, close_on_unnamed);
140 ATF_TP_ADD_TC(tp, close_on_unnamed_pshared);
141 ATF_TP_ADD_TC(tp, destroy_on_named);
142 ATF_TP_ADD_TC(tp, open_unlinked_lifecycle);
143
144 return atf_no_error();
145 }
146