xref: /xnu-8796.121.2/tests/persona.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions #include <darwintest.h>
2*c54f35caSApple OSS Distributions 
3*c54f35caSApple OSS Distributions #include <sys/kauth.h>
4*c54f35caSApple OSS Distributions #include <sys/persona.h>
5*c54f35caSApple OSS Distributions #include <uuid/uuid.h>
6*c54f35caSApple OSS Distributions 
7*c54f35caSApple OSS Distributions T_GLOBAL_META(
8*c54f35caSApple OSS Distributions 	T_META_NAMESPACE("xnu.persona"),
9*c54f35caSApple OSS Distributions 	T_META_CHECK_LEAKS(false),
10*c54f35caSApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true),
11*c54f35caSApple OSS Distributions 	T_META_ENABLED(!TARGET_OS_WATCH) // rdar://81809878
12*c54f35caSApple OSS Distributions 	);
13*c54f35caSApple OSS Distributions 
14*c54f35caSApple OSS Distributions static uid_t
_create_persona(int persona_type,uint32_t persona_info_version)15*c54f35caSApple OSS Distributions _create_persona(int persona_type, uint32_t persona_info_version)
16*c54f35caSApple OSS Distributions {
17*c54f35caSApple OSS Distributions 	struct kpersona_info pinfo = {
18*c54f35caSApple OSS Distributions 		.persona_info_version = persona_info_version,
19*c54f35caSApple OSS Distributions 		.persona_type = persona_type,
20*c54f35caSApple OSS Distributions 	};
21*c54f35caSApple OSS Distributions 
22*c54f35caSApple OSS Distributions 	uuid_t uuid;
23*c54f35caSApple OSS Distributions 	uuid_generate(uuid);
24*c54f35caSApple OSS Distributions 	uuid_string_t uuid_string;
25*c54f35caSApple OSS Distributions 	uuid_unparse(uuid, uuid_string);
26*c54f35caSApple OSS Distributions 	snprintf(pinfo.persona_name, MAXLOGNAME, "persona_test.%s", uuid_string);
27*c54f35caSApple OSS Distributions 
28*c54f35caSApple OSS Distributions 	uid_t persona_id = 0;
29*c54f35caSApple OSS Distributions 	int ret = kpersona_alloc(&pinfo, &persona_id);
30*c54f35caSApple OSS Distributions 	T_WITH_ERRNO; T_ASSERT_EQ(ret, 0, NULL);
31*c54f35caSApple OSS Distributions 	T_ASSERT_GT(persona_id, 0, NULL);
32*c54f35caSApple OSS Distributions 
33*c54f35caSApple OSS Distributions 	return persona_id;
34*c54f35caSApple OSS Distributions }
35*c54f35caSApple OSS Distributions 
36*c54f35caSApple OSS Distributions T_DECL(mutlipe_system_personas, "create multiple PERSONA_SYSTEM")
37*c54f35caSApple OSS Distributions {
38*c54f35caSApple OSS Distributions 	uid_t first = _create_persona(PERSONA_SYSTEM, PERSONA_INFO_V1);
39*c54f35caSApple OSS Distributions 	uid_t second = _create_persona(PERSONA_SYSTEM, PERSONA_INFO_V1);
40*c54f35caSApple OSS Distributions 
41*c54f35caSApple OSS Distributions 	T_ASSERT_NE(first, second, NULL);
42*c54f35caSApple OSS Distributions 
43*c54f35caSApple OSS Distributions 	T_ASSERT_EQ(kpersona_dealloc(first), 0, NULL);
44*c54f35caSApple OSS Distributions 	T_ASSERT_EQ(kpersona_dealloc(second), 0, NULL);
45*c54f35caSApple OSS Distributions }
46*c54f35caSApple OSS Distributions 
47*c54f35caSApple OSS Distributions T_DECL(mutlipe_system_proxy_personas, "create multiple PERSONA_SYSTEM_PROXY")
48*c54f35caSApple OSS Distributions {
49*c54f35caSApple OSS Distributions 	uid_t first = _create_persona(PERSONA_SYSTEM_PROXY, PERSONA_INFO_V1);
50*c54f35caSApple OSS Distributions 	uid_t second = _create_persona(PERSONA_SYSTEM_PROXY, PERSONA_INFO_V1);
51*c54f35caSApple OSS Distributions 
52*c54f35caSApple OSS Distributions 	T_ASSERT_NE(first, second, NULL);
53*c54f35caSApple OSS Distributions 
54*c54f35caSApple OSS Distributions 	T_ASSERT_EQ(kpersona_dealloc(first), 0, NULL);
55*c54f35caSApple OSS Distributions 	T_ASSERT_EQ(kpersona_dealloc(second), 0, NULL);
56*c54f35caSApple OSS Distributions }
57*c54f35caSApple OSS Distributions 
58*c54f35caSApple OSS Distributions T_DECL(persona_info_v2, "create and query persona PERSONA_INFO_V2")
59*c54f35caSApple OSS Distributions {
60*c54f35caSApple OSS Distributions 	uid_t persona = _create_persona(PERSONA_MANAGED, PERSONA_INFO_V2);
61*c54f35caSApple OSS Distributions 
62*c54f35caSApple OSS Distributions 	for (uint32_t version = PERSONA_INFO_V1; version <= PERSONA_INFO_V2; version++) {
63*c54f35caSApple OSS Distributions 		struct kpersona_info info = {
64*c54f35caSApple OSS Distributions 			.persona_info_version = version,
65*c54f35caSApple OSS Distributions 		};
66*c54f35caSApple OSS Distributions 		int error = kpersona_info(persona, &info);
67*c54f35caSApple OSS Distributions 		T_ASSERT_EQ(error, 0, "kpersona_info(v%d) error", version);
68*c54f35caSApple OSS Distributions 		T_ASSERT_EQ(info.persona_type, PERSONA_MANAGED, "kpersona_info(v%d) type", version);
69*c54f35caSApple OSS Distributions 		T_ASSERT_EQ(info.persona_info_version, version, "kpersona_info(v%d) version", version);
70*c54f35caSApple OSS Distributions 	}
71*c54f35caSApple OSS Distributions 
72*c54f35caSApple OSS Distributions 	T_ASSERT_EQ(kpersona_dealloc(persona), 0, NULL);
73*c54f35caSApple OSS Distributions }
74*c54f35caSApple OSS Distributions 
75*c54f35caSApple OSS Distributions T_DECL(persona_uid, "create a persona with a uid and fetch it")
76*c54f35caSApple OSS Distributions {
77*c54f35caSApple OSS Distributions 	uid_t persona_uid = 501;
78*c54f35caSApple OSS Distributions 	struct kpersona_info pinfo = {
79*c54f35caSApple OSS Distributions 		.persona_info_version = PERSONA_INFO_V2,
80*c54f35caSApple OSS Distributions 		.persona_type = PERSONA_MANAGED,
81*c54f35caSApple OSS Distributions 		.persona_uid = persona_uid,
82*c54f35caSApple OSS Distributions 	};
83*c54f35caSApple OSS Distributions 
84*c54f35caSApple OSS Distributions 	uuid_t uuid;
85*c54f35caSApple OSS Distributions 	uuid_generate(uuid);
86*c54f35caSApple OSS Distributions 	uuid_string_t uuid_string;
87*c54f35caSApple OSS Distributions 	uuid_unparse(uuid, uuid_string);
88*c54f35caSApple OSS Distributions 	snprintf(pinfo.persona_name, MAXLOGNAME, "persona_test.%s", uuid_string);
89*c54f35caSApple OSS Distributions 
90*c54f35caSApple OSS Distributions 	uid_t persona_id = 0;
91*c54f35caSApple OSS Distributions 	int ret = kpersona_alloc(&pinfo, &persona_id);
92*c54f35caSApple OSS Distributions 	T_WITH_ERRNO; T_ASSERT_EQ(ret, 0, NULL);
93*c54f35caSApple OSS Distributions 	T_ASSERT_GT(persona_id, 0, NULL);
94*c54f35caSApple OSS Distributions 
95*c54f35caSApple OSS Distributions 	struct kpersona_info fetched_persona = {
96*c54f35caSApple OSS Distributions 		.persona_info_version = PERSONA_INFO_V2,
97*c54f35caSApple OSS Distributions 	};
98*c54f35caSApple OSS Distributions 	int error = kpersona_info(persona_id, &fetched_persona);
99*c54f35caSApple OSS Distributions 	T_ASSERT_EQ(error, 0, NULL);
100*c54f35caSApple OSS Distributions 	T_ASSERT_EQ(fetched_persona.persona_uid, persona_uid, NULL);
101*c54f35caSApple OSS Distributions 
102*c54f35caSApple OSS Distributions 	T_ASSERT_EQ(kpersona_dealloc(persona_id), 0, NULL);
103*c54f35caSApple OSS Distributions }
104*c54f35caSApple OSS Distributions 
105*c54f35caSApple OSS Distributions T_DECL(persona_v1_uid_is_unset, "create PERSONA_INFO_V1 and make sure its UID is unset")
106*c54f35caSApple OSS Distributions {
107*c54f35caSApple OSS Distributions 	uid_t persona = _create_persona(PERSONA_MANAGED, PERSONA_INFO_V1);
108*c54f35caSApple OSS Distributions 
109*c54f35caSApple OSS Distributions 	struct kpersona_info info = {
110*c54f35caSApple OSS Distributions 		.persona_info_version = PERSONA_INFO_V2,
111*c54f35caSApple OSS Distributions 	};
112*c54f35caSApple OSS Distributions 	int error = kpersona_info(persona, &info);
113*c54f35caSApple OSS Distributions 
114*c54f35caSApple OSS Distributions 	T_ASSERT_EQ(error, 0, NULL);
115*c54f35caSApple OSS Distributions 	T_ASSERT_EQ(info.persona_uid, KAUTH_UID_NONE, NULL);
116*c54f35caSApple OSS Distributions 	T_ASSERT_EQ(kpersona_dealloc(persona), 0, NULL);
117*c54f35caSApple OSS Distributions }
118