File: SpawnerTestCases.cpp

package info (click to toggle)
ruby-passenger 4.0.53-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,668 kB
  • ctags: 70,512
  • sloc: cpp: 264,280; ruby: 25,606; sh: 22,815; ansic: 18,277; python: 767; makefile: 99; perl: 20
file content (825 lines) | stat: -rw-r--r-- 27,558 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
// Included in DirectSpawnerTest.cpp and SmartSpawnerTest.cpp.

	#define SETUP_USER_SWITCHING_TEST(code) \
		if (geteuid() != 0) { \
			return; \
		} \
		TempDirCopy copy("stub/wsgi", "tmp.wsgi"); \
		addUserSwitchingCode(); \
		\
		DeleteFileEventually info1("/tmp/info.txt"); \
		DeleteFileEventually info2("/tmp/info2.txt"); \
		\
		SpawnerPtr spawner; \
		Options options; \
		options = createOptions(); \
		options.appRoot = "tmp.wsgi"; \
		options.appType = "wsgi"; \
		options.defaultUser = testConfig["default_user"].asCString(); \
		options.defaultGroup = testConfig["default_group"].asCString(); \
		code \
		spawner = createSpawner(options)

	#define RUN_USER_SWITCHING_TEST() \
		process = spawner->spawn(options); \
		process->requiresShutdown = false; \
		BufferedIO io(FileDescriptor(open("/tmp/info.txt", O_RDONLY))); \
		uid_t uid = (uid_t) atol(io.readLine().c_str()); \
		gid_t gid = (gid_t) atol(io.readLine().c_str()); \
		string groups = strip(io.readLine()); \
		/* Avoid compiler warning. */ \
		(void) uid; (void) gid; (void) groups

	typedef boost::shared_ptr<Spawner> SpawnerPtr;

	static void addUserSwitchingCode() {
		FILE *f = fopen("tmp.wsgi/passenger_wsgi.py", "a");
		fputs(
			"\n"
			"import os\n"
			"f = open('/tmp/info.txt', 'w')\n"
			"f.write(str(os.getuid()) + '\\n')\n"
			"f.write(str(os.getgid()) + '\\n')\n"
			"f.write(os.popen('groups').read() + '\\n')\n"
			"f.close()\n",
			f);
		fclose(f);

		rename("tmp.wsgi/passenger_wsgi.py", "tmp.wsgi/passenger_wsgi.py.real");
		symlink("passenger_wsgi.py.real", "tmp.wsgi/passenger_wsgi.py");
	}

	static void checkin(ProcessPtr process, Connection *conn) {
		process->sockets->front().checkinConnection(*conn);
	}

	static string userNameForUid(uid_t uid) {
		return getpwuid(uid)->pw_name;
	}

	static string groupNameForGid(gid_t gid) {
		return getgrgid(gid)->gr_name;
	}

	static uid_t uidFor(const string &userName) {
		return getpwnam(userName.c_str())->pw_uid;
	}

	static gid_t gidFor(const string &groupName) {
		return getgrnam(groupName.c_str())->gr_gid;
	}

	static string primaryGroupFor(const string &userName) {
		gid_t gid = getpwnam(userName.c_str())->pw_gid;
		return getgrgid(gid)->gr_name;
	}

	TEST_METHOD(1) {
		// Basic spawning test.
		Options options = createOptions();
		options.appRoot      = "stub/rack";
		options.startCommand = "ruby\t" "start.rb";
		options.startupFile  = "start.rb";
		SpawnerPtr spawner = createSpawner(options);
		process = spawner->spawn(options);
		process->requiresShutdown = false;
		ensure_equals(process->sockets->size(), 1u);

		Connection conn = process->sockets->front().checkoutConnection();
		ScopeGuard guard(boost::bind(checkin, process, &conn));
		writeExact(conn.fd, "ping\n");
		ensure_equals(readAll(conn.fd), "pong\n");
	}

	TEST_METHOD(2) {
		// It enforces the given start timeout.
		Options options = createOptions();
		options.appRoot      = "stub";
		options.startCommand = "sleep\t" "60";
		options.startupFile  = ".";
		options.startTimeout = 300;
		SpawnerPtr spawner = createSpawner(options);
		setLogLevel(LVL_CRIT);
		try {
			process = spawner->spawn(options);
			process->requiresShutdown = false;
			fail("Timeout expected");
		} catch (const SpawnException &e) {
			ensure_equals(e.getErrorKind(),
				SpawnException::APP_STARTUP_TIMEOUT);
		}
	}

	TEST_METHOD(3) {
		// Any protocol errors during startup are caught and result
		// in exceptions.
		Options options = createOptions();
		options.appRoot      = "stub";
		options.startCommand = "echo\t" "!> hello world";
		options.startupFile  = ".";
		SpawnerPtr spawner = createSpawner(options);
		setLogLevel(LVL_CRIT);
		try {
			process = spawner->spawn(options);
			process->requiresShutdown = false;
			fail("Exception expected");
		} catch (const SpawnException &e) {
			ensure_equals(e.getErrorKind(),
				SpawnException::APP_STARTUP_PROTOCOL_ERROR);
		}
	}

	TEST_METHOD(4) {
		// The application may respond with a special Error response,
		// which will result in a SpawnException with the content.
		Options options = createOptions();
		options.appRoot      = "stub";
		options.startCommand = "perl\t" "start_error.pl";
		options.startupFile  = "start_error.pl";
		SpawnerPtr spawner = createSpawner(options);
		setLogLevel(LVL_CRIT);
		try {
			process = spawner->spawn(options);
			process->requiresShutdown = false;
			fail("SpawnException expected");
		} catch (const SpawnException &e) {
			ensure_equals(e.getErrorKind(),
				SpawnException::APP_STARTUP_EXPLAINABLE_ERROR);
			ensure_equals(e.getErrorPage(),
				"He's dead, Jim!\n"
				"Relax, I'm a doctor.\n");
		}
	}

	TEST_METHOD(5) {
		// The start timeout is enforced even while reading the error
		// response.
		Options options = createOptions();
		options.appRoot      = "stub";
		options.startCommand = "perl\t" "start_error.pl\t" "freeze";
		options.startupFile  = "start_error.pl";
		options.startTimeout = 300;
		SpawnerPtr spawner = createSpawner(options);
		setLogLevel(LVL_CRIT);
		try {
			process = spawner->spawn(options);
			process->requiresShutdown = false;
			fail("Timeout expected");
		} catch (const SpawnException &e) {
			ensure_equals(e.getErrorKind(),
				SpawnException::APP_STARTUP_TIMEOUT);
		}
	}

	TEST_METHOD(6) {
		// The reported PID is correct.
		Options options = createOptions();
		options.appRoot      = "stub/rack";
		options.startCommand = "ruby\t" "start.rb";
		options.startupFile  = "start.rb";
		SpawnerPtr spawner = createSpawner(options);
		process = spawner->spawn(options);
		process->requiresShutdown = false;
		ensure_equals(process->sockets->size(), 1u);

		Connection conn = process->sockets->front().checkoutConnection();
		ScopeGuard guard(boost::bind(checkin, process, &conn));
		writeExact(conn.fd, "pid\n");
		ensure_equals(readAll(conn.fd), toString(process->pid) + "\n");
	}

	TEST_METHOD(7) {
		// Custom environment variables can be passed.
		Options options = createOptions();
		options.appRoot = "stub/rack";
		options.startCommand = "ruby\t" "start.rb";
		options.startupFile  = "start.rb";
		options.environmentVariables.push_back(make_pair("PASSENGER_FOO", "foo"));
		options.environmentVariables.push_back(make_pair("PASSENGER_BAR", "bar"));
		SpawnerPtr spawner = createSpawner(options);
		process = spawner->spawn(options);
		process->requiresShutdown = false;
		ensure_equals(process->sockets->size(), 1u);

		Connection conn = process->sockets->front().checkoutConnection();
		ScopeGuard guard(boost::bind(checkin, process, &conn));
		writeExact(conn.fd, "envvars\n");
		string envvars = readAll(conn.fd);
		ensure("(1)", envvars.find("PASSENGER_FOO = foo\n") != string::npos);
		ensure("(2)", envvars.find("PASSENGER_BAR = bar\n") != string::npos);
	}

	TEST_METHOD(8) {
		// Any raised SpawnExceptions take note of the process's environment variables.
		Options options = createOptions();
		options.appRoot      = "stub";
		options.startCommand = "echo\t" "!> hello world";
		options.startupFile  = ".";
		options.environmentVariables.push_back(make_pair("PASSENGER_FOO", "foo"));
		SpawnerPtr spawner = createSpawner(options);
		setLogLevel(LVL_CRIT);
		try {
			process = spawner->spawn(options);
			process->requiresShutdown = false;
			fail("Exception expected");
		} catch (const SpawnException &e) {
			ensure(containsSubstring(e["envvars"], "PASSENGER_FOO=foo\n"));
		}
	}

	TEST_METHOD(9) {
		// It raises an exception if the user does not have a access to one
		// of the app root's parent directories, or the app root itself.
		runShellCommand("mkdir -p tmp.check/a/b/c");
		TempDirCopy dir("stub/rack", "tmp.check/a/b/c/d");
		TempDir dir2("tmp.check");

		char buffer[PATH_MAX];
		string cwd = getcwd(buffer, sizeof(buffer));

		Options options = createOptions();
		options.appRoot = "tmp.check/a/b/c/d";
		options.appType = "rack";
		SpawnerPtr spawner = createSpawner(options);
		setLogLevel(LVL_CRIT);

		if (getuid() != 0) {
			// TODO: implement this test for root too
			runShellCommand("chmod 000 tmp.check/a/b/c/d");
			runShellCommand("chmod 600 tmp.check/a/b/c");
			runShellCommand("chmod 600 tmp.check/a");

			try {
				process = spawner->spawn(options);
				process->requiresShutdown = false;
				fail("SpawnException expected");
			} catch (const SpawnException &e) {
				ensure("(1)", containsSubstring(e.getErrorPage(),
					"the parent directory '" + cwd + "/tmp.check/a' has wrong permissions"));
			}

			runShellCommand("chmod 700 tmp.check/a");
			try {
				process = spawner->spawn(options);
				process->requiresShutdown = false;
				fail("SpawnException expected");
			} catch (const SpawnException &e) {
				ensure("(2)", containsSubstring(e.getErrorPage(),
					"the parent directory '" + cwd + "/tmp.check/a/b/c' has wrong permissions"));
			}

			runShellCommand("chmod 700 tmp.check/a/b/c");
			try {
				process = spawner->spawn(options);
				process->requiresShutdown = false;
				fail("SpawnException expected");
			} catch (const SpawnException &e) {
				ensure("(3)", containsSubstring(e.getErrorPage(),
					"However this directory is not accessible because it has wrong permissions."));
			}

			runShellCommand("chmod 700 tmp.check/a/b/c/d");
			process = spawner->spawn(options); // Should not throw.
			process->requiresShutdown = false;
		}
	}

	TEST_METHOD(10) {
		// It forwards all stdout and stderr output, even after the corresponding
		// Process object has been destroyed.
		DeleteFileEventually d("tmp.output");
		PipeWatcher::onData = gatherOutput;

		Options options = createOptions();
		options.appRoot = "stub/rack";
		options.appType = "rack";
		SpawnerPtr spawner = createSpawner(options);
		process = spawner->spawn(options);
		process->requiresShutdown = false;

		SessionPtr session = process->newSession();
		session->initiate();

		setLogLevel(LVL_ERROR); // TODO: should be LVL_WARN
		const char header[] =
			"REQUEST_METHOD\0GET\0"
			"PATH_INFO\0/print_stdout_and_stderr\0";
		string data(header, sizeof(header) - 1);
		data.append("PASSENGER_CONNECT_PASSWORD");
		data.append(1, '\0');
		data.append(process->connectPassword);
		data.append(1, '\0');

		writeScalarMessage(session->fd(), data);
		shutdown(session->fd(), SHUT_WR);
		readAll(session->fd());
		session->close(true);
		session.reset();
		process.reset();

		EVENTUALLY(2,
			boost::lock_guard<boost::mutex> l(gatheredOutputSyncher);
			result = gatheredOutput.find("hello stdout!\n") != string::npos
				&& gatheredOutput.find("hello stderr!\n") != string::npos;
		);
	}

	TEST_METHOD(11) {
		// It infers the code revision from the REVISION file.
		TempDirCopy dir("stub/rack", "tmp.rack");
		createFile("tmp.rack/REVISION", "hello\n");

		Options options = createOptions();
		options.appRoot      = "tmp.rack";
		options.startCommand = "ruby\t" "start.rb";
		options.startupFile  = "start.rb";
		SpawnerPtr spawner = createSpawner(options);
		process = spawner->spawn(options);
		process->requiresShutdown = false;

		ensure_equals(process->codeRevision, "hello");
	}

	TEST_METHOD(12) {
		// It infers the code revision from the app root symlink,
		// if the app root is called "current".
		TempDir dir1("tmp.rack");
		TempDirCopy dir2("stub/rack", "tmp.rack/today");
		symlink("today", "tmp.rack/current");

		Options options = createOptions();
		options.appRoot      = "tmp.rack/current";
		options.startCommand = "ruby\t" "start.rb";
		options.startupFile  = "start.rb";
		SpawnerPtr spawner = createSpawner(options);
		process = spawner->spawn(options);
		process->requiresShutdown = false;

		ensure_equals(process->codeRevision, "today");
	}

	// It raises an exception if getStartupCommand() is empty.

	/******* User switching tests *******/

	// If 'user' is set
		// and 'user' is 'root'
			TEST_METHOD(20) {
				// It changes the user to the value of 'defaultUser'.
				SETUP_USER_SWITCHING_TEST(
					options.user = "root";
				);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(userNameForUid(uid), testConfig["default_user"]);
			}

			TEST_METHOD(21) {
				// If 'group' is given, it changes group to the given group name.
				SETUP_USER_SWITCHING_TEST(
					options.user = "root";
					options.group = testConfig["normal_group_1"].asCString();
				);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(groupNameForGid(gid), testConfig["normal_group_1"].asString());
			}

			TEST_METHOD(22) {
				// If 'group' is set to the root group, it changes group to defaultGroup.
				string rootGroup = groupNameForGid(0);
				SETUP_USER_SWITCHING_TEST(
					options.user = "root";
					options.group = rootGroup.c_str();
				);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(groupNameForGid(gid), testConfig["default_group"].asString());
			}

			// and 'group' is set to '!STARTUP_FILE!'"
				TEST_METHOD(23) {
					// It changes the group to the startup file's group.
					SETUP_USER_SWITCHING_TEST(
						options.user = "root";
						options.group = "!STARTUP_FILE!";
					);
					lchown("tmp.wsgi/passenger_wsgi.py",
						(uid_t) -1,
						gidFor(testConfig["normal_group_1"].asString()));
					RUN_USER_SWITCHING_TEST();
					ensure_equals(groupNameForGid(gid), testConfig["normal_group_1"].asString());
				}

				TEST_METHOD(24) {
					// If the startup file is a symlink, then it uses the symlink's group, not the target's group
					SETUP_USER_SWITCHING_TEST(
						options.user = "root";
						options.group = "!STARTUP_FILE!";
					);
					lchown("tmp.wsgi/passenger_wsgi.py",
						(uid_t) -1,
						gidFor(testConfig["normal_group_2"].asString()));
					chown("tmp.wsgi/passenger_wsgi.py.real",
						(uid_t) -1,
						gidFor(testConfig["normal_group_1"].asString()));
					RUN_USER_SWITCHING_TEST();
					ensure_equals(groupNameForGid(gid), testConfig["normal_group_2"].asString());
				}

			TEST_METHOD(25) {
				// If 'group' is not given, it changes the group to defaultUser's primary group.
				SETUP_USER_SWITCHING_TEST(
					options.user = "root";
				);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(groupNameForGid(gid),
					primaryGroupFor(testConfig["default_user"].asString()));
			}

		// and 'user' is not 'root'
			TEST_METHOD(29) {
				// It changes the user to the given username.
				SETUP_USER_SWITCHING_TEST(
					options.user = testConfig["normal_user_1"].asCString();
				);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(userNameForUid(uid), testConfig["normal_user_1"].asString());
			}

			TEST_METHOD(30) {
				// If 'group' is given, it changes group to the given group name.
				// It changes the user to the given username.
				SETUP_USER_SWITCHING_TEST(
					options.user = testConfig["normal_user_1"].asCString();
					options.group = testConfig["normal_group_1"].asCString();
				);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(groupNameForGid(gid), testConfig["normal_group_1"].asString());
			}

			TEST_METHOD(31) {
				// If 'group' is set to the root group, it changes group to defaultGroup.
				// It changes the user to the given username.
				string rootGroup = groupNameForGid(0);
				SETUP_USER_SWITCHING_TEST(
					options.user = testConfig["normal_user_1"].asCString();
					options.group = rootGroup.c_str();
				);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(groupNameForGid(gid), testConfig["default_group"].asString());
			}

			// and 'group' is set to '!STARTUP_FILE!'
				TEST_METHOD(32) {
					// It changes the group to the startup file's group.
					SETUP_USER_SWITCHING_TEST(
						options.user = testConfig["normal_user_1"].asCString();
						options.group = "!STARTUP_FILE!";
					);
					lchown("tmp.wsgi/passenger_wsgi.py",
						(uid_t) -1,
						gidFor(testConfig["normal_group_1"].asString()));
					RUN_USER_SWITCHING_TEST();
					ensure_equals(groupNameForGid(gid),
						testConfig["normal_group_1"].asString());
				}

				TEST_METHOD(33) {
					// If the startup file is a symlink, then it uses the
					// symlink's group, not the target's group.
					SETUP_USER_SWITCHING_TEST(
						options.user = testConfig["normal_user_1"].asCString();
						options.group = "!STARTUP_FILE!";
					);
					lchown("tmp.wsgi/passenger_wsgi.py",
						(uid_t) -1,
						gidFor(testConfig["normal_group_2"].asString()));
					chown("tmp.wsgi/passenger_wsgi.py.real",
						(uid_t) -1,
						gidFor(testConfig["normal_group_1"].asString()));
					RUN_USER_SWITCHING_TEST();
					ensure_equals(groupNameForGid(gid),
						testConfig["normal_group_2"].asString());
				}

			TEST_METHOD(34) {
				// If 'group' is not given, it changes the group to the user's primary group.
				SETUP_USER_SWITCHING_TEST(
					options.user = testConfig["normal_user_1"].asCString();
				);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(groupNameForGid(gid),
					primaryGroupFor(testConfig["normal_user_1"].asString()));
			}

		// and the given username does not exist
			TEST_METHOD(38) {
				// It changes the user to the value of defaultUser.
				SETUP_USER_SWITCHING_TEST(
					options.user = testConfig["nonexistant_user"].asCString();
				);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(userNameForUid(uid), testConfig["default_user"].asString());
			}

			TEST_METHOD(39) {
				// If 'group' is given, it changes group to the given group name.
				SETUP_USER_SWITCHING_TEST(
					options.user = testConfig["nonexistant_user"].asCString();
					options.group = testConfig["normal_group_1"].asCString();
				);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(groupNameForGid(gid), testConfig["normal_group_1"].asString());
			}

			TEST_METHOD(40) {
				// If 'group' is set to the root group, it changes group to defaultGroup.
				string rootGroup = groupNameForGid(0);
				SETUP_USER_SWITCHING_TEST(
					options.user = testConfig["nonexistant_user"].asCString();
					options.group = rootGroup;
				);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(groupNameForGid(gid), testConfig["default_group"].asString());
			}

			// and 'group' is set to '!STARTUP_FILE!'
				TEST_METHOD(41) {
					// It changes the group to the startup file's group.
					SETUP_USER_SWITCHING_TEST(
						options.user = testConfig["nonexistant_user"].asCString();
						options.group = "!STARTUP_FILE!";
					);
					lchown("tmp.wsgi/passenger_wsgi.py",
						(uid_t) -1,
						gidFor(testConfig["normal_group_1"].asString()));
					RUN_USER_SWITCHING_TEST();
					ensure_equals(groupNameForGid(gid), testConfig["normal_group_1"].asString());
				}

				TEST_METHOD(42) {
					// If the startup file is a symlink, then it uses the
					// symlink's group, not the target's group.
					SETUP_USER_SWITCHING_TEST(
						options.user = testConfig["nonexistant_user"].asCString();
						options.group = "!STARTUP_FILE!";
					);
					lchown("tmp.wsgi/passenger_wsgi.py",
						(uid_t) -1,
						gidFor(testConfig["normal_group_2"].asString()));
					chown("tmp.wsgi/passenger_wsgi.py.real",
						(uid_t) -1,
						gidFor(testConfig["normal_group_1"].asString()));
					RUN_USER_SWITCHING_TEST();
					ensure_equals(groupNameForGid(gid), testConfig["normal_group_2"].asString());
				}

			TEST_METHOD(43) {
				// If 'group' is not given, it changes the group to defaultUser's primary group.
				SETUP_USER_SWITCHING_TEST(
					options.user = testConfig["nonexistant_user"].asCString();
				);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(groupNameForGid(gid),
					primaryGroupFor(testConfig["default_user"].asString()));
			}

	// If 'user' is not set
		// and the startup file's owner exists
			TEST_METHOD(47) {
				// It changes the user to the owner of the startup file.
				SETUP_USER_SWITCHING_TEST(
					(void) 0;
				);
				lchown("tmp.wsgi/passenger_wsgi.py",
					uidFor(testConfig["normal_user_1"].asString()),
					(gid_t) -1);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(userNameForUid(uid), testConfig["normal_user_1"].asString());
			}

			TEST_METHOD(48) {
				// If the startup file is a symlink, then it uses the symlink's owner, not the target's owner.
				SETUP_USER_SWITCHING_TEST(
					(void) 0;
				);
				lchown("tmp.wsgi/passenger_wsgi.py",
					uidFor(testConfig["normal_user_2"].asString()),
					(gid_t) -1);
				chown("tmp.wsgi/passenger_wsgi.py.real",
					uidFor(testConfig["normal_user_1"].asString()),
					(gid_t) -1);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(userNameForUid(uid), testConfig["normal_user_2"].asString());
			}

			TEST_METHOD(49) {
				// If 'group' is given, it changes group to the given group name.
				SETUP_USER_SWITCHING_TEST(
					options.group = testConfig["normal_group_1"].asCString();
				);
				lchown("tmp.wsgi/passenger_wsgi.py",
					uidFor(testConfig["normal_user_1"].asString()),
					(gid_t) -1);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(groupNameForGid(gid), testConfig["normal_group_1"].asString());
			}

			TEST_METHOD(50) {
				// If 'group' is set to the root group, it changes group to defaultGroup.
				string rootGroup = groupNameForGid(0);
				SETUP_USER_SWITCHING_TEST(
					options.group = rootGroup;
				);
				lchown("tmp.wsgi/passenger_wsgi.py",
					uidFor(testConfig["normal_user_1"].asString()),
					(gid_t) -1);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(groupNameForGid(gid), testConfig["default_group"].asString());
			}

			// and 'group' is set to '!STARTUP_FILE!'
				TEST_METHOD(51) {
					// It changes the group to the startup file's group.
					SETUP_USER_SWITCHING_TEST(
						options.group = "!STARTUP_FILE!";
					);
					lchown("tmp.wsgi/passenger_wsgi.py",
						(uid_t) -1,
						gidFor(testConfig["normal_group_1"].asString()));
					RUN_USER_SWITCHING_TEST();
					ensure_equals(groupNameForGid(gid), testConfig["normal_group_1"].asString());
				}

				TEST_METHOD(52) {
					// If the startup file is a symlink, then it uses the symlink's
					// group, not the target's group.
					SETUP_USER_SWITCHING_TEST(
						options.group = "!STARTUP_FILE!";
					);
					lchown("tmp.wsgi/passenger_wsgi.py",
						(uid_t) -1,
						gidFor(testConfig["normal_group_2"].asString()));
					chown("tmp.wsgi/passenger_wsgi.py.real",
						(uid_t) -1,
						gidFor(testConfig["normal_group_1"].asString()));
					RUN_USER_SWITCHING_TEST();
					ensure_equals(groupNameForGid(gid), testConfig["normal_group_2"].asString());
				}

			TEST_METHOD(53) {
				// If 'group' is not given, it changes the group to the startup file's owner's primary group.
				SETUP_USER_SWITCHING_TEST(
					(void) 0;
				);
				lchown("tmp.wsgi/passenger_wsgi.py",
					uidFor(testConfig["normal_user_1"].asString()),
					(gid_t) -1);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(groupNameForGid(gid),
					primaryGroupFor(testConfig["normal_user_1"].asString()));
			}

		// and the startup file's owner doesn't exist
			TEST_METHOD(57) {
				// It changes the user to the value of defaultUser.
				SETUP_USER_SWITCHING_TEST(
					(void) 0;
				);
				lchown("tmp.wsgi/passenger_wsgi.py",
					(uid_t) testConfig["nonexistant_uid"].asInt64(),
					(gid_t) -1);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(userNameForUid(uid), testConfig["default_user"].asString());
			}

			TEST_METHOD(58) {
				// If 'group' is given, it changes group to the given group name.
				SETUP_USER_SWITCHING_TEST(
					options.group = testConfig["normal_group_1"].asCString();
				);
				lchown("tmp.wsgi/passenger_wsgi.py",
					(uid_t) testConfig["nonexistant_uid"].asInt64(),
					(gid_t) -1);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(groupNameForGid(gid), testConfig["normal_group_1"].asString());
			}

			TEST_METHOD(59) {
				// If 'group' is set to the root group, it changes group to defaultGroup.
				string rootGroup = groupNameForGid(0);
				SETUP_USER_SWITCHING_TEST(
					options.group = rootGroup;
				);
				lchown("tmp.wsgi/passenger_wsgi.py",
					(uid_t) testConfig["nonexistant_uid"].asInt64(),
					(gid_t) -1);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(groupNameForGid(gid), testConfig["default_group"].asString());
			}

			// and 'group' is set to '!STARTUP_FILE!'
				// and the startup file's group doesn't exist
					TEST_METHOD(60) {
						// It changes the group to the value given by defaultGroup.
						SETUP_USER_SWITCHING_TEST(
							options.group = "!STARTUP_FILE!";
						);
						lchown("tmp.wsgi/passenger_wsgi.py",
							(uid_t) testConfig["nonexistant_uid"].asInt64(),
							(gid_t) testConfig["nonexistant_gid"].asInt64());
						RUN_USER_SWITCHING_TEST();
						ensure_equals(groupNameForGid(gid), testConfig["default_group"].asString());
					}

				// and the startup file's group exists
					TEST_METHOD(61) {
						// It changes the group to the startup file's group.
						SETUP_USER_SWITCHING_TEST(
							options.group = "!STARTUP_FILE!";
						);
						lchown("tmp.wsgi/passenger_wsgi.py",
							(uid_t) testConfig["nonexistant_uid"].asInt64(),
							gidFor(testConfig["normal_group_1"].asString()));
						RUN_USER_SWITCHING_TEST();
						ensure_equals(groupNameForGid(gid), testConfig["normal_group_1"].asString());
					}

					TEST_METHOD(62) {
						// If the startup file is a symlink, then it uses the symlink's group, not the target's group.
						SETUP_USER_SWITCHING_TEST(
							options.group = "!STARTUP_FILE!";
						);
						lchown("tmp.wsgi/passenger_wsgi.py",
							(uid_t) testConfig["nonexistant_uid"].asInt64(),
							gidFor(testConfig["normal_group_2"].asString()));
						chown("tmp.wsgi/passenger_wsgi.py.real",
							(uid_t) -1,
							gidFor(testConfig["normal_group_1"].asString()));
						RUN_USER_SWITCHING_TEST();
						ensure_equals(groupNameForGid(gid), testConfig["normal_group_2"].asString());
					}

			TEST_METHOD(63) {
				// If 'group' is not given, it changes the group to defaultUser's primary group.
				SETUP_USER_SWITCHING_TEST(
					(void) 0;
				);
				lchown("tmp.wsgi/passenger_wsgi.py",
					(uid_t) testConfig["nonexistant_uid"].asInt64(),
					(gid_t) -1);
				RUN_USER_SWITCHING_TEST();
				ensure_equals(groupNameForGid(gid), primaryGroupFor(testConfig["default_user"].asString()));
			}

	TEST_METHOD(67) {
		// It raises an error if it tries to lower to 'defaultUser',
		// but that user doesn't exist.
		SETUP_USER_SWITCHING_TEST(
			options.user = "root";
			options.defaultUser = testConfig["nonexistant_user"].asCString();
		);
		try {
			RUN_USER_SWITCHING_TEST();
			fail();
		} catch (const RuntimeException &e) {
			ensure(containsSubstring(e.what(), "Cannot determine a user to lower privilege to"));
		}
	}

	TEST_METHOD(68) {
		// It raises an error if it tries to lower to 'default_group',
		// but that group doesn't exist.
		SETUP_USER_SWITCHING_TEST(
			options.user = testConfig["normal_user_1"].asCString();
			options.group = groupNameForGid(0);
			options.defaultGroup = testConfig["nonexistant_group"].asCString();
		);
		try {
			RUN_USER_SWITCHING_TEST();
			fail();
		} catch (const RuntimeException &e) {
			ensure(containsSubstring(e.what(), "Cannot determine a group to lower privilege to"));
		}
	}

	TEST_METHOD(69) {
		// Changes supplementary groups to the owner's default supplementary groups.
		SETUP_USER_SWITCHING_TEST(
			options.user = testConfig["normal_user_1"].asCString();
		);
		RUN_USER_SWITCHING_TEST();
		runShellCommand(("groups " + testConfig["normal_user_1"].asString() + " > /tmp/info2.txt").c_str());
		string defaultGroups = strip(readAll("/tmp/info2.txt"));

		// On Linux, the 'groups' output is prepended by the group name so
		// get rid of that.
		string::size_type pos = defaultGroups.find(':');
		if (pos != string::npos) {
			pos++;
			while (pos < defaultGroups.size() && defaultGroups[pos] == ' ') {
				pos++;
			}
			defaultGroups.erase(0, pos);
		}

		ensure_equals(groups, defaultGroups);
	}