xref: /xnu-10002.1.13/tests/nvram_tests/nvram_system.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions #include <darwintest.h>
2*1031c584SApple OSS Distributions #include "nvram_helper.h"
3*1031c584SApple OSS Distributions 
4*1031c584SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.nvram"),
5*1031c584SApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("xnu"),
6*1031c584SApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("nvram"));
7*1031c584SApple OSS Distributions 
8*1031c584SApple OSS Distributions static io_registry_entry_t optionsRef = IO_OBJECT_NULL;
9*1031c584SApple OSS Distributions 
10*1031c584SApple OSS Distributions // xcrun -sdk macosx.internal make -C tests nvram_system && sudo ./tests/build/sym/nvram_system
11*1031c584SApple OSS Distributions 
12*1031c584SApple OSS Distributions // Test that writing, reading, and deleting of system variables with system entitlement should succeed
13*1031c584SApple OSS Distributions T_DECL(TestSystemEntitled, "Test system guids with entitlement")
14*1031c584SApple OSS Distributions {
15*1031c584SApple OSS Distributions #if ((TARGET_OS_OSX) && !(__x86_64__))
16*1031c584SApple OSS Distributions 	const char *varToTest = SystemNVRAMGuidString ":" "varToTest2";
17*1031c584SApple OSS Distributions 
18*1031c584SApple OSS Distributions 	optionsRef = CreateOptionsRef();
19*1031c584SApple OSS Distributions 
20*1031c584SApple OSS Distributions 	TestVarOp(OP_SET, varToTest, DefaultSetVal, KERN_SUCCESS, optionsRef);
21*1031c584SApple OSS Distributions 	TestVarOp(OP_GET, varToTest, NULL, KERN_SUCCESS, optionsRef);
22*1031c584SApple OSS Distributions 	TestVarOp(OP_DEL, varToTest, NULL, KERN_SUCCESS, optionsRef);
23*1031c584SApple OSS Distributions 
24*1031c584SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
25*1031c584SApple OSS Distributions #else
26*1031c584SApple OSS Distributions 	T_PASS("Test not supported");
27*1031c584SApple OSS Distributions #endif /* ((TARGET_OS_OSX) && !(__x86_64__)) */
28*1031c584SApple OSS Distributions }
29*1031c584SApple OSS Distributions 
30*1031c584SApple OSS Distributions 
31*1031c584SApple OSS Distributions #if !(TARGET_CPU_ARM64 && TARGET_OS_OSX)
32*1031c584SApple OSS Distributions // Test that system guid is translated to common guid on devices without system namespace support
33*1031c584SApple OSS Distributions T_DECL(TestSysNonAS, "Test system guids for devices without system namespace")
34*1031c584SApple OSS Distributions {
35*1031c584SApple OSS Distributions 	const char *varWithSys = SystemNVRAMGuidString ":" "varToTestSys";
36*1031c584SApple OSS Distributions 	const char *varWOSys = "varToTestSys";
37*1031c584SApple OSS Distributions 
38*1031c584SApple OSS Distributions 	optionsRef = CreateOptionsRef();
39*1031c584SApple OSS Distributions 
40*1031c584SApple OSS Distributions 	TestVarOp(OP_SET, varWithSys, DefaultSetVal, KERN_SUCCESS, optionsRef);
41*1031c584SApple OSS Distributions 	TestVarOp(OP_GET, varWOSys, NULL, KERN_SUCCESS, optionsRef);
42*1031c584SApple OSS Distributions 	TestVarOp(OP_DEL, varWOSys, NULL, KERN_SUCCESS, optionsRef);
43*1031c584SApple OSS Distributions 
44*1031c584SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
45*1031c584SApple OSS Distributions }
46*1031c584SApple OSS Distributions #endif
47*1031c584SApple OSS Distributions 
48*1031c584SApple OSS Distributions // Variable entitlement tests for system variables
49*1031c584SApple OSS Distributions #if ((TARGET_OS_OSX) && !(__x86_64__))
50*1031c584SApple OSS Distributions // Test that read of entitled system variables without entitlement should fail
51*1031c584SApple OSS Distributions T_DECL(TestEntRdSys, "Test read entitled system variables")
52*1031c584SApple OSS Distributions {
53*1031c584SApple OSS Distributions 	char * varToTest = SystemNVRAMGuidString ":" "testEntRdSys";
54*1031c584SApple OSS Distributions 
55*1031c584SApple OSS Distributions 	optionsRef = CreateOptionsRef();
56*1031c584SApple OSS Distributions 
57*1031c584SApple OSS Distributions 	TestVarOp(OP_SET, varToTest, DefaultSetVal, KERN_SUCCESS, optionsRef);
58*1031c584SApple OSS Distributions 	TestVarOp(OP_GET, varToTest, NULL, KERN_FAILURE, optionsRef);
59*1031c584SApple OSS Distributions 
60*1031c584SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
61*1031c584SApple OSS Distributions }
62*1031c584SApple OSS Distributions 
63*1031c584SApple OSS Distributions // Test that writing of entitled system variables without entitlement should fail
64*1031c584SApple OSS Distributions T_DECL(TestEntModRstSys, "Test write entitled system variables")
65*1031c584SApple OSS Distributions {
66*1031c584SApple OSS Distributions 	char * varToTest = SystemNVRAMGuidString ":" "testEntModRstSys";
67*1031c584SApple OSS Distributions 
68*1031c584SApple OSS Distributions 	optionsRef = CreateOptionsRef();
69*1031c584SApple OSS Distributions 
70*1031c584SApple OSS Distributions 	TestVarOp(OP_SET, varToTest, DefaultSetVal, kIOReturnNotPrivileged, optionsRef);
71*1031c584SApple OSS Distributions 
72*1031c584SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
73*1031c584SApple OSS Distributions }
74*1031c584SApple OSS Distributions 
75*1031c584SApple OSS Distributions // Test that deleting of entitled system variables without entitlement should fail
76*1031c584SApple OSS Distributions T_DECL(TestEntDelSys, "Test delete entitled system variables")
77*1031c584SApple OSS Distributions {
78*1031c584SApple OSS Distributions 	char * varToTest = SystemNVRAMGuidString ":" "testEntDelSys";
79*1031c584SApple OSS Distributions 
80*1031c584SApple OSS Distributions 	optionsRef = CreateOptionsRef();
81*1031c584SApple OSS Distributions 
82*1031c584SApple OSS Distributions 	TestVarOp(OP_SET, varToTest, DefaultSetVal, KERN_SUCCESS, optionsRef);
83*1031c584SApple OSS Distributions 	TestVarOp(OP_DEL, varToTest, NULL, KERN_FAILURE, optionsRef);
84*1031c584SApple OSS Distributions 
85*1031c584SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
86*1031c584SApple OSS Distributions }
87*1031c584SApple OSS Distributions 
88*1031c584SApple OSS Distributions // Test resetting of nvram with entitlement should not erase testEntRstSys
89*1031c584SApple OSS Distributions // To reset nvram, call the test with -r argument:
90*1031c584SApple OSS Distributions // sudo ./tests/build/sym/nvram_system -n xnu.nvram.TestEntRstSys -- -r
91*1031c584SApple OSS Distributions T_DECL(TestEntRstSys, "Test reset entitled system variables")
92*1031c584SApple OSS Distributions {
93*1031c584SApple OSS Distributions 	opterr = 0;
94*1031c584SApple OSS Distributions 	optind = 0;
95*1031c584SApple OSS Distributions 	char * varToTest = SystemNVRAMGuidString ":" "testEntRstSys";
96*1031c584SApple OSS Distributions 
97*1031c584SApple OSS Distributions 	optionsRef = CreateOptionsRef();
98*1031c584SApple OSS Distributions 
99*1031c584SApple OSS Distributions 	TestVarOp(OP_SET, varToTest, DefaultSetVal, KERN_SUCCESS, optionsRef);
100*1031c584SApple OSS Distributions 
101*1031c584SApple OSS Distributions 	if (getopt(argc, argv, "r") == 'r') {
102*1031c584SApple OSS Distributions 		TestVarOp(OP_RES, NULL, NULL, KERN_SUCCESS, optionsRef);
103*1031c584SApple OSS Distributions 		TestVarOp(OP_GET, varToTest, NULL, KERN_SUCCESS, optionsRef);
104*1031c584SApple OSS Distributions 	} else {
105*1031c584SApple OSS Distributions 		TestVarOp(OP_DEL, varToTest, NULL, KERN_SUCCESS, optionsRef);
106*1031c584SApple OSS Distributions 		T_PASS("NVram reset not invoked");
107*1031c584SApple OSS Distributions 	}
108*1031c584SApple OSS Distributions 
109*1031c584SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
110*1031c584SApple OSS Distributions }
111*1031c584SApple OSS Distributions 
112*1031c584SApple OSS Distributions // Test NVRAM Reset
113*1031c584SApple OSS Distributions // To reset nvram, call the test with -r argument:
114*1031c584SApple OSS Distributions // sudo ./tests/build/sym/nvram_system -n xnu.nvram.TestNVRAMResetSys -- -r
115*1031c584SApple OSS Distributions T_DECL(TestNVRAMResetSys, "Test NVRAM Reset for system region variables")
116*1031c584SApple OSS Distributions {
117*1031c584SApple OSS Distributions 	opterr = 0;
118*1031c584SApple OSS Distributions 	optind = 0;
119*1031c584SApple OSS Distributions 	const char * varToTest = "testVar1";
120*1031c584SApple OSS Distributions 	const char * varToTestWSys = SystemNVRAMGuidString ":" "testVar2";
121*1031c584SApple OSS Distributions 
122*1031c584SApple OSS Distributions 	optionsRef = CreateOptionsRef();
123*1031c584SApple OSS Distributions 
124*1031c584SApple OSS Distributions 	if (getopt(argc, argv, "r") == 'r') {
125*1031c584SApple OSS Distributions 		TestVarOp(OP_SET, varToTest, DefaultSetVal, KERN_SUCCESS, optionsRef);
126*1031c584SApple OSS Distributions 		TestVarOp(OP_SET, varToTestWSys, DefaultSetVal, KERN_SUCCESS, optionsRef);
127*1031c584SApple OSS Distributions 
128*1031c584SApple OSS Distributions 		TestVarOp(OP_RES, NULL, NULL, KERN_SUCCESS, optionsRef);
129*1031c584SApple OSS Distributions 		TestVarOp(OP_GET, varToTest, NULL, KERN_FAILURE, optionsRef);
130*1031c584SApple OSS Distributions 
131*1031c584SApple OSS Distributions 		TestVarOp(OP_GET, varToTestWSys, NULL, KERN_FAILURE, optionsRef);
132*1031c584SApple OSS Distributions 	} else {
133*1031c584SApple OSS Distributions 		T_PASS("NVram reset not invoked");
134*1031c584SApple OSS Distributions 	}
135*1031c584SApple OSS Distributions 
136*1031c584SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
137*1031c584SApple OSS Distributions }
138*1031c584SApple OSS Distributions 
139*1031c584SApple OSS Distributions // Test NVRAM Obliterate
140*1031c584SApple OSS Distributions // To obliterate nvram, call the test with -r argument:
141*1031c584SApple OSS Distributions // sudo ./tests/build/sym/nvram_system -n xnu.nvram.TestNVRAMOblitSys -- -r
142*1031c584SApple OSS Distributions T_DECL(TestNVRAMOblitSys, "Test NVRAM Obliterate for system region")
143*1031c584SApple OSS Distributions {
144*1031c584SApple OSS Distributions 	opterr = 0;
145*1031c584SApple OSS Distributions 	optind = 0;
146*1031c584SApple OSS Distributions 	const char * varToTest = "testVar1";
147*1031c584SApple OSS Distributions 	const char * varToTestWSys = SystemNVRAMGuidString ":" "testVar2";
148*1031c584SApple OSS Distributions 	const char * varToTestWRand = RandomNVRAMGuidString ":" "testVar3";
149*1031c584SApple OSS Distributions 	const char * oblitSys = SystemNVRAMGuidString ":" "ObliterateNVRam";
150*1031c584SApple OSS Distributions 	const char * oblitNonSys = CommonNVRAMGuidString ":" "ObliterateNVRam";
151*1031c584SApple OSS Distributions 
152*1031c584SApple OSS Distributions 	optionsRef = CreateOptionsRef();
153*1031c584SApple OSS Distributions 
154*1031c584SApple OSS Distributions 	if (getopt(argc, argv, "r") == 'r') {
155*1031c584SApple OSS Distributions 		// Set variables with common namespace, random namespace and system namespace
156*1031c584SApple OSS Distributions 		TestVarOp(OP_SET, varToTest, DefaultSetVal, KERN_SUCCESS, optionsRef);
157*1031c584SApple OSS Distributions 		TestVarOp(OP_SET, varToTestWSys, DefaultSetVal, KERN_SUCCESS, optionsRef);
158*1031c584SApple OSS Distributions 		TestVarOp(OP_SET, varToTestWRand, DefaultSetVal, KERN_SUCCESS, optionsRef);
159*1031c584SApple OSS Distributions 
160*1031c584SApple OSS Distributions 		// Obliterate sys first and make sure non-sys variables aren't deleted
161*1031c584SApple OSS Distributions 		TestVarOp(OP_OBL, oblitSys, NULL, KERN_SUCCESS, optionsRef);
162*1031c584SApple OSS Distributions 
163*1031c584SApple OSS Distributions 		TestVarOp(OP_GET, varToTestWSys, NULL, KERN_FAILURE, optionsRef);
164*1031c584SApple OSS Distributions 		TestVarOp(OP_GET, varToTest, NULL, KERN_SUCCESS, optionsRef);
165*1031c584SApple OSS Distributions 		TestVarOp(OP_GET, varToTestWRand, NULL, KERN_SUCCESS, optionsRef);
166*1031c584SApple OSS Distributions 
167*1031c584SApple OSS Distributions 		// Now, obliterate common region and make sure all non-sys variables are deleted
168*1031c584SApple OSS Distributions 		TestVarOp(OP_SET, varToTestWSys, DefaultSetVal, KERN_SUCCESS, optionsRef);
169*1031c584SApple OSS Distributions 
170*1031c584SApple OSS Distributions 		TestVarOp(OP_OBL, oblitNonSys, NULL, KERN_SUCCESS, optionsRef);
171*1031c584SApple OSS Distributions 
172*1031c584SApple OSS Distributions 		TestVarOp(OP_GET, varToTestWSys, NULL, KERN_SUCCESS, optionsRef);
173*1031c584SApple OSS Distributions 		TestVarOp(OP_GET, varToTest, NULL, KERN_FAILURE, optionsRef);
174*1031c584SApple OSS Distributions 		TestVarOp(OP_GET, varToTestWRand, NULL, KERN_FAILURE, optionsRef);
175*1031c584SApple OSS Distributions 		TestVarOp(OP_DEL, varToTestWSys, NULL, KERN_SUCCESS, optionsRef);
176*1031c584SApple OSS Distributions 	} else {
177*1031c584SApple OSS Distributions 		T_PASS("NVram obliterate not invoked");
178*1031c584SApple OSS Distributions 	}
179*1031c584SApple OSS Distributions 
180*1031c584SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
181*1031c584SApple OSS Distributions }
182*1031c584SApple OSS Distributions #endif /* ((TARGET_OS_OSX) && !(__x86_64__)) */
183