Home | History | Annotate | Line # | Download | only in kernel
t_epoll.c revision 1.1
      1 /*	$NetBSD: t_epoll.c,v 1.1 2023/07/28 18:19:01 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2023 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Theodore Preduta.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: t_epoll.c,v 1.1 2023/07/28 18:19:01 christos Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/types.h>
     36 #include <sys/epoll.h>
     37 #include <errno.h>
     38 
     39 #include <atf-c.h>
     40 
     41 #include "h_macros.h"
     42 
     43 ATF_TC(create_size);
     44 ATF_TC_HEAD(create_size, tc)
     45 {
     46 
     47 	atf_tc_set_md_var(tc, "descr",
     48 	    "Checks that epoll_create requires a non-positive size");
     49 }
     50 ATF_TC_BODY(create_size, tc)
     51 {
     52 	ATF_REQUIRE_EQ_MSG(epoll_create(-1), -1,
     53 	    "epoll_create succeeded unexpectedly");
     54 	ATF_REQUIRE_ERRNO(EINVAL, true);
     55 
     56 	ATF_REQUIRE_EQ_MSG(epoll_create(0), -1,
     57 	    "epoll_create succeeded unexpectedly");
     58 	ATF_REQUIRE_ERRNO(EINVAL, true);
     59 
     60 	RL(epoll_create(1));
     61 }
     62 
     63 ATF_TC(bad_epfd);
     64 ATF_TC_HEAD(bad_epfd, tc)
     65 {
     66 
     67 	atf_tc_set_md_var(tc, "descr",
     68 	    "Checks that epoll_ctl detects an invalid epfd");
     69 }
     70 ATF_TC_BODY(bad_epfd, tc)
     71 {
     72 	int fd;
     73 	struct epoll_event event;
     74 
     75 	RL(fd = epoll_create1(0));
     76 	event.events = EPOLLIN;
     77 
     78 	ATF_REQUIRE_EQ_MSG(epoll_ctl(-1, EPOLL_CTL_ADD, fd, &event), -1,
     79 	    "epoll_ctl succeeded unexpectedly");
     80 	ATF_REQUIRE_ERRNO(EBADF, true);
     81 }
     82 
     83 ATF_TC(bad_fd);
     84 ATF_TC_HEAD(bad_fd, tc)
     85 {
     86 
     87 	atf_tc_set_md_var(tc, "descr",
     88 	    "Checks that epoll_ctl detects an invalid fd");
     89 }
     90 ATF_TC_BODY(bad_fd, tc)
     91 {
     92 	int epfd;
     93 	struct epoll_event event;
     94 
     95 	RL(epfd = epoll_create1(0));
     96 	event.events = EPOLLIN;
     97 
     98 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_ADD, -1, &event), -1,
     99 	    "epoll_ctl succeeded unexpectedly");
    100 	ATF_REQUIRE_ERRNO(EBADF, true);
    101 }
    102 
    103 ATF_TC(double_add);
    104 ATF_TC_HEAD(double_add, tc)
    105 {
    106 
    107 	atf_tc_set_md_var(tc, "descr",
    108 	    "Checks that epoll_ctl detects if a fd has already been added");
    109 }
    110 ATF_TC_BODY(double_add, tc)
    111 {
    112 	int epfd, fd;
    113 	struct epoll_event event;
    114 
    115 	RL(epfd = epoll_create1(0));
    116 	RL(fd = epoll_create1(0));
    117 	event.events = EPOLLIN;
    118 
    119 	RL(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event));
    120 
    121 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event), -1,
    122 	    "epoll_ctl succeeded unexpectedly");
    123 	ATF_REQUIRE_ERRNO(EEXIST, true);
    124 }
    125 
    126 ATF_TC(not_added);
    127 ATF_TC_HEAD(not_added, tc)
    128 {
    129 
    130 	atf_tc_set_md_var(tc, "descr",
    131 	    "Checks that epoll_ctl detects if a fd has not been added");
    132 }
    133 ATF_TC_BODY(not_added, tc)
    134 {
    135 	int epfd, fd;
    136 	struct epoll_event event;
    137 
    138 	RL(epfd = epoll_create1(0));
    139 	RL(fd = epoll_create1(0));
    140 	event.events = EPOLLIN;
    141 
    142 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &event), -1,
    143 	    "epoll_ctl succeeded unexpectedly");
    144 	ATF_REQUIRE_ERRNO(ENOENT, true);
    145 
    146 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL), -1,
    147 	    "epoll_ctl succeeded unexpectedly");
    148 	ATF_REQUIRE_ERRNO(ENOENT, true);
    149 }
    150 
    151 ATF_TC(watching_self);
    152 ATF_TC_HEAD(watching_self, tc)
    153 {
    154 
    155 	atf_tc_set_md_var(tc, "descr",
    156 	    "Checks that epoll disallows watching itself");
    157 }
    158 ATF_TC_BODY(watching_self, tc)
    159 {
    160 	int epfd;
    161 	struct epoll_event event;
    162 
    163 	RL(epfd = epoll_create1(0));
    164 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_ADD, epfd, &event), -1,
    165 	    "epoll_ctl succeeded unexpectedly");
    166 	ATF_REQUIRE_ERRNO(EINVAL, true);
    167 }
    168 
    169 ATF_TC(watch_loops);
    170 ATF_TC_HEAD(watch_loops, tc)
    171 {
    172 
    173 	atf_tc_set_md_var(tc, "descr", "Checks that epoll disallows loops");
    174 }
    175 ATF_TC_BODY(watch_loops, tc)
    176 {
    177         int epfd1, epfd2;
    178 	struct epoll_event event;
    179 
    180 	event.events = EPOLLIN;
    181 	RL(epfd1 = epoll_create1(0));
    182 	RL(epfd2 = epoll_create1(0));
    183 	RL(epoll_ctl(epfd1, EPOLL_CTL_ADD, epfd2, &event));
    184 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd2, EPOLL_CTL_ADD, epfd1, &event), -1,
    185 	    "epoll_ctl succeeded unexpectedly");
    186 	ATF_REQUIRE_ERRNO(ELOOP, true);
    187 }
    188 
    189 ATF_TC(watch_depth);
    190 ATF_TC_HEAD(watch_depth, tc)
    191 {
    192 
    193 	atf_tc_set_md_var(tc, "descr",
    194 	    "Checks that epoll fails when the watch depth exceeds 5");
    195 }
    196 ATF_TC_BODY(watch_depth, tc)
    197 {
    198 	int epfd, tmp;
    199 	struct epoll_event event;
    200 
    201 	event.events = EPOLLIN;
    202 	RL(epfd = epoll_create1(0));
    203 	for (size_t i = 0; i < 4; i++) {
    204 		RL(tmp = epoll_create1(0));
    205 		RL(epoll_ctl(tmp, EPOLL_CTL_ADD, epfd, &event));
    206 		epfd = tmp;
    207 	}
    208 	RL(tmp = epoll_create1(0));
    209 	ATF_REQUIRE_EQ_MSG(epoll_ctl(tmp, EPOLL_CTL_ADD, epfd, &event), -1,
    210 	    "epoll_ctl succeeded unexpectedly");
    211 	ATF_REQUIRE_ERRNO(EINVAL, true);
    212 }
    213 
    214 ATF_TP_ADD_TCS(tp)
    215 {
    216 	ATF_TP_ADD_TC(tp, create_size);
    217 	ATF_TP_ADD_TC(tp, bad_epfd);
    218 	ATF_TP_ADD_TC(tp, bad_fd);
    219 	ATF_TP_ADD_TC(tp, not_added);
    220 	ATF_TP_ADD_TC(tp, watching_self);
    221 	ATF_TP_ADD_TC(tp, watch_loops);
    222 	ATF_TP_ADD_TC(tp, watch_depth);
    223 
    224 	return atf_no_error();
    225 }
    226