tails-persistence-setup 2.2.1
[tails-persistence-setup.git] / t / specs / Utils.t
blob14eb11b3f41fda7fe5c05c04d2422afcb06d2639
1 use Test::Spec;
3 use 5.10.1;
4 use strictures 2;
6 use Carp;
7 use File::stat;
8 use File::Temp qw{tempdir tempfile};
9 use Path::Tiny;
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 {
15     my ($fh, $file);
16     before sub {
17         ($fh, $file) = tempfile();
18         print $fh '';
19         close $fh;
20     };
21     describe 'that contains "a=b" on the first line' => sub {
22         before sub {
23             my $fh = path($file)->openw;
24             print $fh "a=b\n";
25         };
26         it 'has value "b" for variable "a"' => sub {
27             is(get_variable_from_file($file, "a"), "b");
28         };
29     };
30     describe 'that contains "  a=b" on the first line' => sub {
31         before sub {
32             my $fh = path($file)->openw;
33             print $fh "  a=b\n";
34         };
35         it 'has value "b" for variable "a"' => sub {
36             is(get_variable_from_file($file, "a"), "b");
37         };
38     };
39     describe 'that contains "a=b" on the second line' => sub {
40         before sub {
41             my $fh = path($file)->openw;
42             print $fh "bla\na=b\n";
43         };
44         it 'has value "b" for variable "a"' => sub {
45             is(get_variable_from_file($file, "a"), "b");
46         };
47     };
48     describe 'that contains "a = b"' => sub {
49         before sub {
50             my $fh = path($file)->openw;
51             print $fh "a = b\n";
52         };
53         it 'has no value for variable "a"' => sub {
54             ok(! defined(get_variable_from_file($file, "a")));
55         };
56     };
59 # check_config_file_permissions
60 describe 'A configuration file' => sub {
61     my ($file, $tempdir);
62     my $expected = {
63         mode => oct(600),
64         uid  => '4242',
65         gid  => '2424',
66         acl  => '',
67     };
68     before 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();
74         print $test_fh '';
75         close $test_fh;
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";
80     };
81     describe 'that has correct ownership, permissions and ACL' => sub {
82         before sub {
83             $file->touch;
84             chown $expected->{uid}, $expected->{gid}, $file;
85             chmod $expected->{mode}, $file;
86         };
87         it 'is accepted' => sub {
88             lives_ok { check_config_file_permissions($file, $expected) };
89         };
90     };
91     describe 'that is a directory' => sub {
92         before sub {
93             mkdir $file;
94             chown $expected->{uid}, $expected->{gid}, $file;
95             chmod $expected->{mode}, $file;
96         };
97         it 'is rejected' => sub {
98             dies_ok { check_config_file_permissions($file, $expected) };
99         };
100     };
101     describe 'that is a broken symlink' => sub {
102         before sub {
103             my $destination = path($tempdir, 'destination');
104             link $destination, $file;
105         };
106         it 'is rejected' => sub {
107             dies_ok { check_config_file_permissions($file, $expected) };
108         };
109     };
110     describe 'that is a symlink to a file with correct ownership, permissions and ACL' => sub {
111         before sub {
112             my $destination = path($tempdir, 'destination');
113             $destination->touch;
114             chown $expected->{uid}, $expected->{gid}, $destination;
115             chmod $expected->{mode}, $destination;
116             symlink $destination, $file or croak "Could not link '$file' to '$destination'";
117         };
118         it 'is rejected' => sub {
119             dies_ok { check_config_file_permissions($file, $expected) };
120         };
121     };
122     describe 'that has wrong owner' => sub {
123         before sub {
124             $file->touch;
125             chown 0, $expected->{gid}, $file;
126             chmod $expected->{mode}, $file;
127         };
128         it 'is rejected' => sub {
129             dies_ok { check_config_file_permissions($file, $expected) };
130         };
131     };
132     describe 'that has wrong owning group' => sub {
133         before sub {
134             $file->touch;
135             chown $expected->{uid}, 0, $file;
136             chmod $expected->{mode}, $file;
137         };
138         it 'is rejected' => sub {
139             dies_ok { check_config_file_permissions($file, $expected) };
140         };
141     };
142     describe 'that has wrong permissions' => sub {
143         before sub {
144             $file->touch;
145             chown $expected->{uid}, $expected->{gid}, $file;
146             chmod 0644, $file;
147         };
148         it 'is rejected' => sub {
149             dies_ok { check_config_file_permissions($file, $expected) };
150         };
151     };
152     describe 'that has wrong ACL' => sub {
153         before sub {
154             $file->touch;
155             chown $expected->{uid}, $expected->{gid}, $file;
156             chmod $expected->{mode}, $file;
157             systemx('/bin/setfacl', '-m', 'other:r', $file);
158         };
159         it 'is rejected' => sub {
160             dies_ok { check_config_file_permissions($file, $expected) };
161         };
162     };
165 runtests unless caller;