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