xref: /xnu-12377.1.9/tests/unit/mocks/dt_proxy.c (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
1 /*
2  * Copyright (c) 2000-2024 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include "std_safe.h"
30 #include "dt_proxy.h"
31 #include <darwintest.h>
32 
33 static void
pt_assert_true(bool cond,const char * msg)34 pt_assert_true(bool cond, const char *msg)
35 {
36 	T_ASSERT_TRUE(cond, "%s", msg);
37 }
38 static void
pt_assert_notnull(void * ptr,const char * msg)39 pt_assert_notnull(void *ptr, const char *msg)
40 {
41 	T_ASSERT_NOTNULL(ptr, "%s", msg);
42 }
43 static void
pt_assert_posix_zero(int v,const char * msg)44 pt_assert_posix_zero(int v, const char *msg)
45 {
46 	T_ASSERT_POSIX_ZERO(v, "%s", msg);
47 }
48 static void
pt_log(const char * msg)49 pt_log(const char *msg)
50 {
51 	T_LOG("%s", msg);
52 }
53 static void
pt_log_fmtstr(const char * fmt,const char * msg)54 pt_log_fmtstr(const char* fmt, const char *msg)
55 {
56 	T_LOG(fmt, msg);
57 }
58 static void
pt_fail(const char * msg)59 pt_fail(const char *msg)
60 {
61 	T_FAIL("%s", msg);
62 }
63 static void
pt_quiet(void)64 pt_quiet(void)
65 {
66 	T_QUIET;
67 }
68 
69 static struct dt_proxy_callbacks dt_callbacks = {
70 	.t_assert_true = &pt_assert_true,
71 	.t_assert_notnull = &pt_assert_notnull,
72 	.t_assert_posix_zero = &pt_assert_posix_zero,
73 	.t_log = &pt_log,
74 	.t_log_fmtstr = &pt_log_fmtstr,
75 	.t_fail = &pt_fail,
76 	.t_quiet = &pt_quiet
77 };
78 
79 // This code is linked into every test executable to allow the XNU and mocks .dylibs access to some
80 // darwintest functionality.  libdarwintest.a is only linked to the executable so code in the XNU and
81 // mocks .dylibs can't call into it directly
82 // due to how dyld works, this constructor is going to be called after the fake_kinit() constructor
83 // so during fake_kinit() dt_proxy is going to stay NULL and any output to darwintest asserts is lost.
84 __attribute__((constructor)) void
dt_init(void)85 dt_init(void)
86 {
87 	set_dt_proxy_attached(&dt_callbacks);
88 	set_dt_proxy_mock(&dt_callbacks);
89 }
90