8 use File::Temp qw{tempdir tempfile};
10 use IPC::System::Simple qw{systemx};
11 use Tails::Persistence::Utils qw{check_config_file_permissions get_variable_from_file};
12 use Test::Fatal qw{dies_ok lives_ok};
14 describe 'A file' => sub {
17 ($fh, $file) = tempfile();
21 describe 'that contains "a=b" on the first line' => sub {
23 my $fh = path($file)->openw;
26 it 'has value "b" for variable "a"' => sub {
27 is(get_variable_from_file($file, "a"), "b");
30 describe 'that contains " a=b" on the first line' => sub {
32 my $fh = path($file)->openw;
35 it 'has value "b" for variable "a"' => sub {
36 is(get_variable_from_file($file, "a"), "b");
39 describe 'that contains "a=b" on the second line' => sub {
41 my $fh = path($file)->openw;
42 print $fh "bla\na=b\n";
44 it 'has value "b" for variable "a"' => sub {
45 is(get_variable_from_file($file, "a"), "b");
48 describe 'that contains "a = b"' => sub {
50 my $fh = path($file)->openw;
53 it 'has no value for variable "a"' => sub {
54 ok(! defined(get_variable_from_file($file, "a")));
59 # check_config_file_permissions
60 describe 'A configuration file' => sub {
69 # Set up test environment
70 $tempdir = tempdir(CLEANUP => 1);
71 $file = path($tempdir, 'persistence.conf');
72 # Check that we're running under fakeroot
73 my ($test_fh, $test_file) = tempfile();
76 chown 0, 0, $test_file or croak "Please run this test under fakeroot";
77 my $st = stat($test_file);
78 $st->uid eq 0 or croak "Please run this test under fakeroot";
79 $st->gid eq 0 or croak "Please run this test under fakeroot";
81 describe 'that has correct ownership, permissions and ACL' => sub {
84 chown $expected->{uid}, $expected->{gid}, $file;
85 chmod $expected->{mode}, $file;
87 it 'is accepted' => sub {
88 lives_ok { check_config_file_permissions($file, $expected) };
91 describe 'that is a directory' => sub {
94 chown $expected->{uid}, $expected->{gid}, $file;
95 chmod $expected->{mode}, $file;
97 it 'is rejected' => sub {
98 dies_ok { check_config_file_permissions($file, $expected) };
101 describe 'that is a broken symlink' => sub {
103 my $destination = path($tempdir, 'destination');
104 link $destination, $file;
106 it 'is rejected' => sub {
107 dies_ok { check_config_file_permissions($file, $expected) };
110 describe 'that is a symlink to a file with correct ownership, permissions and ACL' => sub {
112 my $destination = path($tempdir, 'destination');
114 chown $expected->{uid}, $expected->{gid}, $destination;
115 chmod $expected->{mode}, $destination;
116 symlink $destination, $file or croak "Could not link '$file' to '$destination'";
118 it 'is rejected' => sub {
119 dies_ok { check_config_file_permissions($file, $expected) };
122 describe 'that has wrong owner' => sub {
125 chown 0, $expected->{gid}, $file;
126 chmod $expected->{mode}, $file;
128 it 'is rejected' => sub {
129 dies_ok { check_config_file_permissions($file, $expected) };
132 describe 'that has wrong owning group' => sub {
135 chown $expected->{uid}, 0, $file;
136 chmod $expected->{mode}, $file;
138 it 'is rejected' => sub {
139 dies_ok { check_config_file_permissions($file, $expected) };
142 describe 'that has wrong permissions' => sub {
145 chown $expected->{uid}, $expected->{gid}, $file;
148 it 'is rejected' => sub {
149 dies_ok { check_config_file_permissions($file, $expected) };
152 describe 'that has wrong ACL' => sub {
155 chown $expected->{uid}, $expected->{gid}, $file;
156 chmod $expected->{mode}, $file;
157 systemx('/bin/setfacl', '-m', 'other:r', $file);
159 it 'is rejected' => sub {
160 dies_ok { check_config_file_permissions($file, $expected) };
165 runtests unless caller;