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