xref: /xnu-12377.1.9/tests/host_notifications.c (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
1*f6217f89SApple OSS Distributions #include <sys/time.h>
2*f6217f89SApple OSS Distributions #include <mach/mach.h>
3*f6217f89SApple OSS Distributions #include <mach/mach_host.h>
4*f6217f89SApple OSS Distributions 
5*f6217f89SApple OSS Distributions #include <darwintest.h>
6*f6217f89SApple OSS Distributions 
7*f6217f89SApple OSS Distributions T_GLOBAL_META(
8*f6217f89SApple OSS Distributions 	T_META_CHECK_LEAKS(false),
9*f6217f89SApple OSS Distributions 	T_META_LTEPHASE(LTE_POSTINIT));
10*f6217f89SApple OSS Distributions 
11*f6217f89SApple OSS Distributions static void
12*f6217f89SApple OSS Distributions do_test(int notify_type, void (^trigger_block)(void))
13*f6217f89SApple OSS Distributions {
14*f6217f89SApple OSS Distributions 	mach_port_t port;
15*f6217f89SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port), NULL);
16*f6217f89SApple OSS Distributions 
17*f6217f89SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(host_request_notification(mach_host_self(), notify_type, port), NULL);
18*f6217f89SApple OSS Distributions 
19*f6217f89SApple OSS Distributions 	trigger_block();
20*f6217f89SApple OSS Distributions 
21*f6217f89SApple OSS Distributions 	struct {
22*f6217f89SApple OSS Distributions 		mach_msg_header_t hdr;
23*f6217f89SApple OSS Distributions 		mach_msg_trailer_t trailer;
24*f6217f89SApple OSS Distributions 	} message = { .hdr = {
25*f6217f89SApple OSS Distributions 			      .msgh_bits = 0,
26*f6217f89SApple OSS Distributions 			      .msgh_size = sizeof(mach_msg_header_t),
27*f6217f89SApple OSS Distributions 			      .msgh_remote_port = MACH_PORT_NULL,
28*f6217f89SApple OSS Distributions 			      .msgh_local_port = port,
29*f6217f89SApple OSS Distributions 			      .msgh_voucher_port = MACH_PORT_NULL,
30*f6217f89SApple OSS Distributions 			      .msgh_id = 0,
31*f6217f89SApple OSS Distributions 		      }};
32*f6217f89SApple OSS Distributions 
33*f6217f89SApple OSS Distributions 	T_ASSERT_EQ(MACH_RCV_TOO_LARGE, mach_msg_receive(&message.hdr), NULL);
34*f6217f89SApple OSS Distributions 	mach_msg_destroy(&message.hdr);
35*f6217f89SApple OSS Distributions }
36*f6217f89SApple OSS Distributions 
37*f6217f89SApple OSS Distributions T_DECL(host_notify_calendar_change, "host_request_notification(HOST_NOTIFY_CALENDAR_CHANGE)")
38*f6217f89SApple OSS Distributions {
39*f6217f89SApple OSS Distributions 	do_test(HOST_NOTIFY_CALENDAR_CHANGE, ^{
40*f6217f89SApple OSS Distributions 		struct timeval tm;
41*f6217f89SApple OSS Distributions 		if (gettimeofday(&tm, NULL) != 0 || settimeofday(&tm, NULL) != 0) {
42*f6217f89SApple OSS Distributions 		        T_SKIP("Unable to settimeofday()");
43*f6217f89SApple OSS Distributions 		}
44*f6217f89SApple OSS Distributions 	});
45*f6217f89SApple OSS Distributions }
46*f6217f89SApple OSS Distributions 
47*f6217f89SApple OSS Distributions T_DECL(host_notify_calendar_set, "host_request_notification(HOST_NOTIFY_CALENDAR_SET)")
48*f6217f89SApple OSS Distributions {
49*f6217f89SApple OSS Distributions 	do_test(HOST_NOTIFY_CALENDAR_SET, ^{
50*f6217f89SApple OSS Distributions 		struct timeval tm;
51*f6217f89SApple OSS Distributions 		if (gettimeofday(&tm, NULL) != 0 || settimeofday(&tm, NULL) != 0) {
52*f6217f89SApple OSS Distributions 		        T_SKIP("Unable to settimeofday()");
53*f6217f89SApple OSS Distributions 		}
54*f6217f89SApple OSS Distributions 	});
55*f6217f89SApple OSS Distributions }
56*f6217f89SApple OSS Distributions 
57*f6217f89SApple OSS Distributions 
58*f6217f89SApple OSS Distributions T_DECL(host_notify_twice, "host_request_notification(HOST_NOTIFY_CALENDAR_SET)")
59*f6217f89SApple OSS Distributions {
60*f6217f89SApple OSS Distributions 	mach_port_t port;
61*f6217f89SApple OSS Distributions 
62*f6217f89SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port), NULL);
63*f6217f89SApple OSS Distributions 
64*f6217f89SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(host_request_notification(mach_host_self(), HOST_NOTIFY_CALENDAR_SET, port),
65*f6217f89SApple OSS Distributions 	    "first registration succeeds");
66*f6217f89SApple OSS Distributions 	T_ASSERT_MACH_ERROR(host_request_notification(mach_host_self(), HOST_NOTIFY_CALENDAR_CHANGE, port),
67*f6217f89SApple OSS Distributions 	    KERN_INVALID_CAPABILITY, "second registration fails");
68*f6217f89SApple OSS Distributions }
69