aboutsummaryrefslogtreecommitdiff
path: root/t/22-race.t
blob: 7ff39ca9b8d91ed54f78d9d82b48c5b3274dcd49 (plain)
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
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::Exit;
use FindBin '$RealBin';
use lib "$RealBin/../SBO-Lib/lib";
use SBO::Lib qw/ open_fh %config /;
use Capture::Tiny qw/ capture_merged /;
use File::Temp 'tempdir';
use Cwd;

plan tests => 8;

sub emulate_race {
	my ($file, $caller) = @_;
	$caller = "SBO::Lib::$caller";

	no warnings 'redefine';
	*_race::cond = sub { unlink $file if $caller eq (caller(1))[3]; };
}

# 1: emulate race condition for open_fh
{
	my $tempdir = tempdir(CLEANUP => 1);
	my $file = "$tempdir/foo";
	system('touch', $file);

	emulate_race($file, 'Util::open_fh');

	my ($fh, $exit) = open_fh $file, '<';
	is ($exit, 6, 'open_fh returned exit value 6');
}

# 2-3: emulate race in open_fh by get_slack_version
{
	my $sv_file = '/etc/slackware-version';
	capture_merged {
		system('mkdir', '-p', '/etc');
		system('mv', $sv_file, "$sv_file.bak");
		system('touch', $sv_file);
	};

	my $exit;
	emulate_race($sv_file, 'Util::open_fh');
	local $config{SLACKWARE_VERSION} = 'FALSE';
	my $out = capture_merged { $exit = exit_code { SBO::Lib::get_slack_version(); }; };

	is ($exit, 6, 'get_slackware_version() exited with correct exitcode');
	is ($out, "Unable to open $sv_file.\n", 'get_slackware_version output correct');

	system('mv', "$sv_file.bak", $sv_file) if -e "$sv_file.bak";
}

# 4-7: emulate races in git_sbo_tree
SKIP: {
	my $tempdir = tempdir(CLEANUP => 1);

	my $repo = '/usr/sbo/repo';
	system('mkdir', '-p', $repo);
	system('mv', $repo, "$repo.bak");

	capture_merged { system <<"GIT"; };
		cd $tempdir
		git init
		mkdir -p test
		cp -R "$RealBin/LO/nonexistentslackbuild" test
		git add test
		git commit -m 'added test/nonexistentslackbuild'

		cd /usr/sbo
		git clone file://$tempdir repo
GIT

	no warnings 'redefine';
	*_race::cond = sub { system('rm', '-rf', $repo) if $_[0] eq '$repo_path can be deleted after -d check' };

	my $res;
	my $out = capture_merged { $res = SBO::Lib::git_sbo_tree("file://$tempdir", ''); };
        note($out);

	is ($out, '', 'git_sbo_tree() no output');
	is ($res, 0, 'git_sbo_tree() returned 0');

	capture_merged { system('git', 'clone', "file://$tempdir", $repo); };
	my $cwd = getcwd();
	mkdir "$tempdir/bar";
	chdir "$tempdir/bar";
	*_race::cond = sub {
		system('rm', '-rf', "$repo/.git") if $_[0] eq 'git repo could be changed or deleted here';
		system('rmdir', "$tempdir/bar") if $_[0] eq '$cwd could be deleted here';
	};

	undef $res;
	$out = capture_merged { $res = SBO::Lib::git_sbo_tree("file://$tempdir", ''); };

	is ($out, "fatal: not a git repository (or any of the parent directories): .git\n", 'git_sbo_tree() gave correct output');
	is ($res, 0, 'git_sbo_tree() returned 0');

	chdir $cwd;
	system('rm', '-rf', $repo);
	system('mv', "$repo.bak", $repo);
}

# 8: emulate race in read_config
{
  my $conf_file = "/etc/sbotools/sbotools.conf";

  mkdir "/etc/sbotools";
  rename $conf_file, "$conf_file.bak";
  system touch => $conf_file;

  no warnings 'redefine';

  local *SBO::Lib::Util::open_read = sub { return undef, 1 };

  my $out = capture_merged { SBO::Lib::Util::read_config(); };

  is ($out, "Unable to open $conf_file.\n", "read_config() output correct");

  unlink $conf_file;
  rename "$conf_file.bak", $conf_file;
}