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