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