xref: /freebsd/sbin/devd/tests/client_test.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1cdfa64aaSAlan Somers /*-
2cdfa64aaSAlan Somers  * Copyright (c) 2014 Spectra Logic Corporation. All rights reserved.
3cdfa64aaSAlan Somers  * Redistribution and use in source and binary forms, with or without
4cdfa64aaSAlan Somers  * modification, are permitted provided that the following conditions
5cdfa64aaSAlan Somers  * are met:
6cdfa64aaSAlan Somers  * 1. Redistributions of source code must retain the above copyright
7cdfa64aaSAlan Somers  *    notice, this list of conditions and the following disclaimer.
8cdfa64aaSAlan Somers  * 2. Redistributions in binary form must reproduce the above copyright
9cdfa64aaSAlan Somers  *    notice, this list of conditions and the following disclaimer in the
10cdfa64aaSAlan Somers  *    documentation and/or other materials provided with the distribution.
11cdfa64aaSAlan Somers  *
12cdfa64aaSAlan Somers  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13cdfa64aaSAlan Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14cdfa64aaSAlan Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15cdfa64aaSAlan Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
16cdfa64aaSAlan Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17cdfa64aaSAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18cdfa64aaSAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19cdfa64aaSAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20cdfa64aaSAlan Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21cdfa64aaSAlan Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22cdfa64aaSAlan Somers  * SUCH DAMAGE.
23cdfa64aaSAlan Somers  */
24cdfa64aaSAlan Somers 
25cdfa64aaSAlan Somers #include <sys/cdefs.h>
26cdfa64aaSAlan Somers #include <stdbool.h>
27cdfa64aaSAlan Somers #include <stdio.h>
28cdfa64aaSAlan Somers 
29cdfa64aaSAlan Somers #include <sys/param.h>
30cdfa64aaSAlan Somers #include <sys/types.h>
31cdfa64aaSAlan Somers #include <sys/socket.h>
32cdfa64aaSAlan Somers #include <sys/un.h>
33cdfa64aaSAlan Somers 
34cdfa64aaSAlan Somers #include <atf-c.h>
352c6254c2SAlan Somers 
362c6254c2SAlan Somers const char create_pat[] = "!system=DEVFS subsystem=CDEV type=CREATE cdev=md";
372c6254c2SAlan Somers const char destroy_pat[] = "!system=DEVFS subsystem=CDEV type=DESTROY cdev=md";
382c6254c2SAlan Somers 
39cdfa64aaSAlan Somers /* Helper functions*/
40cdfa64aaSAlan Somers 
41cdfa64aaSAlan Somers /*
42cdfa64aaSAlan Somers  * Create two devd events.  The easiest way I know of, that requires no special
43cdfa64aaSAlan Somers  * hardware, is to create md(4) devices.
44cdfa64aaSAlan Somers  */
45cdfa64aaSAlan Somers static void
create_two_events(void)46cdfa64aaSAlan Somers create_two_events(void)
47cdfa64aaSAlan Somers {
48cdfa64aaSAlan Somers 	FILE *create_stdout;
49cdfa64aaSAlan Somers 	FILE *destroy_stdout;
50cdfa64aaSAlan Somers 	char mdname[80];
51*958160f3SEitan Adler 	char destroy_cmd[95];
52cdfa64aaSAlan Somers 	char *error;
53cdfa64aaSAlan Somers 
54cdfa64aaSAlan Somers 	create_stdout = popen("mdconfig -a -s 64 -t null", "r");
55cdfa64aaSAlan Somers 	ATF_REQUIRE(create_stdout != NULL);
56cdfa64aaSAlan Somers 	error = fgets(mdname, sizeof(mdname), create_stdout);
57cdfa64aaSAlan Somers 	ATF_REQUIRE(error != NULL);
58cdfa64aaSAlan Somers 	/* We only expect one line of output */
59cdfa64aaSAlan Somers 	ATF_REQUIRE_EQ(0, pclose(create_stdout));
60cdfa64aaSAlan Somers 
61cdfa64aaSAlan Somers 	snprintf(destroy_cmd, nitems(destroy_cmd), "mdconfig -d -u %s", mdname);
62cdfa64aaSAlan Somers 	destroy_stdout = popen(destroy_cmd, "r");
6381ade99cSAlan Somers 	ATF_REQUIRE(destroy_stdout != NULL);
64cdfa64aaSAlan Somers 	/* We expect no output */
65cdfa64aaSAlan Somers 	ATF_REQUIRE_EQ(0, pclose(destroy_stdout));
66cdfa64aaSAlan Somers }
67cdfa64aaSAlan Somers 
682c6254c2SAlan Somers /* Setup and return an open client socket */
692c6254c2SAlan Somers static int
common_setup(int socktype,const char * sockpath)702c6254c2SAlan Somers common_setup(int socktype, const char* sockpath) {
712c6254c2SAlan Somers 	struct sockaddr_un devd_addr;
722c6254c2SAlan Somers 	int s, error;
732c6254c2SAlan Somers 
742c6254c2SAlan Somers 	memset(&devd_addr, 0, sizeof(devd_addr));
752c6254c2SAlan Somers 	devd_addr.sun_family = PF_LOCAL;
762c6254c2SAlan Somers 	strlcpy(devd_addr.sun_path, sockpath, sizeof(devd_addr.sun_path));
772c6254c2SAlan Somers 	s = socket(PF_LOCAL, socktype, 0);
782c6254c2SAlan Somers 	ATF_REQUIRE(s >= 0);
792c6254c2SAlan Somers 	error = connect(s, (struct sockaddr*)&devd_addr, SUN_LEN(&devd_addr));
802c6254c2SAlan Somers 	ATF_REQUIRE_EQ(0, error);
812c6254c2SAlan Somers 
822c6254c2SAlan Somers 	create_two_events();
832c6254c2SAlan Somers 	return (s);
842c6254c2SAlan Somers }
852c6254c2SAlan Somers 
86cdfa64aaSAlan Somers /*
87cdfa64aaSAlan Somers  * Test Cases
88cdfa64aaSAlan Somers  */
89cdfa64aaSAlan Somers 
90cdfa64aaSAlan Somers /*
91cdfa64aaSAlan Somers  * Open a client connection to devd, create some events, and test that they can
92cdfa64aaSAlan Somers  * be read _whole_ and _one_at_a_time_ from the socket
93cdfa64aaSAlan Somers  */
94cdfa64aaSAlan Somers ATF_TC_WITHOUT_HEAD(seqpacket);
ATF_TC_BODY(seqpacket,tc)95cdfa64aaSAlan Somers ATF_TC_BODY(seqpacket, tc)
96cdfa64aaSAlan Somers {
97cdfa64aaSAlan Somers 	int s;
98cdfa64aaSAlan Somers 	bool got_create_event = false;
99cdfa64aaSAlan Somers 	bool got_destroy_event = false;
100cdfa64aaSAlan Somers 
1012c6254c2SAlan Somers 	s = common_setup(SOCK_SEQPACKET, "/var/run/devd.seqpacket.pipe");
102cdfa64aaSAlan Somers 	/*
103cdfa64aaSAlan Somers 	 * Loop until both events are detected on _different_ reads
104cdfa64aaSAlan Somers 	 * There may be extra events due to unrelated system activity
105cdfa64aaSAlan Somers 	 * If we never get both events, then the test will timeout.
106cdfa64aaSAlan Somers 	 */
107cdfa64aaSAlan Somers 	while (!(got_create_event && got_destroy_event)) {
108cdfa64aaSAlan Somers 		int cmp;
109cdfa64aaSAlan Somers 		ssize_t len;
110cdfa64aaSAlan Somers 		char event[1024];
111cdfa64aaSAlan Somers 
11281ade99cSAlan Somers 		/* Read 1 less than sizeof(event) to allow space for NULL */
11381ade99cSAlan Somers 		len = recv(s, event, sizeof(event) - 1, MSG_WAITALL);
114cdfa64aaSAlan Somers 		ATF_REQUIRE(len != -1);
115cdfa64aaSAlan Somers 		/* NULL terminate the result */
116cdfa64aaSAlan Somers 		event[len] = '\0';
117cdfa64aaSAlan Somers 		printf("%s", event);
118cdfa64aaSAlan Somers 		cmp = strncmp(event, create_pat, sizeof(create_pat) - 1);
119cdfa64aaSAlan Somers 		if (cmp == 0)
120cdfa64aaSAlan Somers 			got_create_event = true;
121cdfa64aaSAlan Somers 
122cdfa64aaSAlan Somers 		cmp = strncmp(event, destroy_pat, sizeof(destroy_pat) - 1);
123cdfa64aaSAlan Somers 		if (cmp == 0)
124cdfa64aaSAlan Somers 			got_destroy_event = true;
125cdfa64aaSAlan Somers 	}
12681ade99cSAlan Somers 
12781ade99cSAlan Somers 	close(s);
128cdfa64aaSAlan Somers }
129cdfa64aaSAlan Somers 
130cdfa64aaSAlan Somers /*
131cdfa64aaSAlan Somers  * Open a client connection to devd using the stream socket, create some
132cdfa64aaSAlan Somers  * events, and test that they can be read in any number of reads.
133cdfa64aaSAlan Somers  */
134cdfa64aaSAlan Somers ATF_TC_WITHOUT_HEAD(stream);
ATF_TC_BODY(stream,tc)135cdfa64aaSAlan Somers ATF_TC_BODY(stream, tc)
136cdfa64aaSAlan Somers {
137cdfa64aaSAlan Somers 	int s;
138cdfa64aaSAlan Somers 	bool got_create_event = false;
139cdfa64aaSAlan Somers 	bool got_destroy_event = false;
140cdfa64aaSAlan Somers 	ssize_t len = 0;
141cdfa64aaSAlan Somers 
1422c6254c2SAlan Somers 	s = common_setup(SOCK_STREAM, "/var/run/devd.pipe");
143cdfa64aaSAlan Somers 	/*
1442c6254c2SAlan Somers 	 * Loop until both events are detected on the same or different reads.
1452c6254c2SAlan Somers 	 * There may be extra events due to unrelated system activity.
146cdfa64aaSAlan Somers 	 * If we never get both events, then the test will timeout.
147cdfa64aaSAlan Somers 	 */
148cdfa64aaSAlan Somers 	while (!(got_create_event && got_destroy_event)) {
149cdfa64aaSAlan Somers 		char event[1024];
150cdfa64aaSAlan Somers 		ssize_t newlen;
151cdfa64aaSAlan Somers 		char *create_pos, *destroy_pos;
152cdfa64aaSAlan Somers 
15381ade99cSAlan Somers 		/* Read 1 less than sizeof(event) to allow space for NULL */
15481ade99cSAlan Somers 		newlen = read(s, &event[len], sizeof(event) - len - 1);
155cdfa64aaSAlan Somers 		ATF_REQUIRE(newlen != -1);
156cdfa64aaSAlan Somers 		len += newlen;
157cdfa64aaSAlan Somers 		/* NULL terminate the result */
1582c6254c2SAlan Somers 		event[len] = '\0';
159cdfa64aaSAlan Somers 		printf("%s", event);
160cdfa64aaSAlan Somers 
161cdfa64aaSAlan Somers 		create_pos = strstr(event, create_pat);
162d8e198e6SBjoern A. Zeeb 		if (create_pos != NULL)
163cdfa64aaSAlan Somers 			got_create_event = true;
164cdfa64aaSAlan Somers 
165cdfa64aaSAlan Somers 		destroy_pos = strstr(event, destroy_pat);
166d8e198e6SBjoern A. Zeeb 		if (destroy_pos != NULL)
167cdfa64aaSAlan Somers 			got_destroy_event = true;
168cdfa64aaSAlan Somers 	}
16981ade99cSAlan Somers 
17081ade99cSAlan Somers 	close(s);
171cdfa64aaSAlan Somers }
172cdfa64aaSAlan Somers 
173cdfa64aaSAlan Somers /*
174cdfa64aaSAlan Somers  * Main.
175cdfa64aaSAlan Somers  */
176cdfa64aaSAlan Somers 
ATF_TP_ADD_TCS(tp)177cdfa64aaSAlan Somers ATF_TP_ADD_TCS(tp)
178cdfa64aaSAlan Somers {
179cdfa64aaSAlan Somers 	ATF_TP_ADD_TC(tp, seqpacket);
180cdfa64aaSAlan Somers 	ATF_TP_ADD_TC(tp, stream);
181cdfa64aaSAlan Somers 
182cdfa64aaSAlan Somers 	return (atf_no_error());
183cdfa64aaSAlan Somers }
184cdfa64aaSAlan Somers 
185