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