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