App-FileSync

 view release on metacpan or  search on metacpan

t/regressions.t  view on Meta::CPAN

	no warnings 'redefine';
	local *App::FileSync::Trash::remove_tree = sub { return 0 };
	use warnings 'redefine';
	my $stuck = tempdir(CLEANUP => 1);
	make_path(File::Spec->catdir($stuck, qw(2020 01 01)));
	is_deeply(App::FileSync::Trash::prune(root => $stuck, days => 1), [], 'a bucket that stays is not reported as removed');
};

subtest 'Settings: a bare settings file name finds universal beside it' => sub {
	# Old: splitpath gave an empty directory, so it looked for /universal.json.
	my $dir = tempdir(CLEANUP => 1);
	for my $name (qw(app.json universal.json)) {
		open my $fh, '>', File::Spec->catfile($dir, $name) or die $!;
		print {$fh} "{}\n";
		close $fh;
	}
	my $cwd = Cwd::getcwd();
	chdir $dir or die $!;
	my $found = App::FileSync::Settings::_universal_file('app.json');
	chdir $cwd or die $!;
	is($found, File::Spec->catfile('.', 'universal.json'), 'found');
};

subtest 'Admin: restore into the master refuses a symlinked directory' => sub {
	# Old: the destination was built with catfile, so a symlinked directory
	# in the master let the restored file land outside it.
	my $env = TestSync->new_env();
	my $outside = tempdir(CLEANUP => 1);
	my $store = App::FileSync::Store->new(root => $env->{server}, repo => 'docs');
	plan skip_all => 'no symlinks' unless eval { symlink $outside, File::Spec->catfile($store->master_dir(), 'linked') };
	my $trashed = File::Spec->catfile($store->trash_dir(), qw(2026 01 01 linked b.txt));
	make_path(File::Basename::dirname($trashed));
	open my $fh, '>', $trashed or die $!;
	close $fh;
	my $admin = App::FileSync::Admin->new(root => $env->{server});
	throws_ok { $admin->restore(repo => 'docs', path => 'linked/b.txt') } qr/\ARefusing to write through symlinked directory /, 'refused';
	ok(!-e File::Spec->catfile($outside, 'b.txt'), 'nothing outside the master');
};

subtest 'Server: put and delta check the fields that reach chmod and utime' => sub {
	# Old: only the manifest's m and mode were checked; a put could carry
	# any string to chmod, utime and the index every client pulls from.
	my $env = TestSync->new_env();
	$env->write_file(client => 'a.txt', 'a');
	my $hash = Digest::SHA::sha256_hex('a');
	# session() has sent the manifest and the plan pushes a.txt.
	my $conn = session($env);
	my $reply = call($conn, { cmd => 'put', p => 'a.txt', s => 1, h => $hash, m => 1, mode => 'rwx' });
	is($reply->{error}, "Bad 'mode' for 'a.txt'", 'a junk mode is refused');
};

subtest 'Server: a symlink in the master is never sent' => sub {
	# Old: _file_stat followed links, so a link appearing between the plan
	# and the read sent whatever it pointed at.
	my $env = TestSync->new_env();
	my $store = App::FileSync::Store->new(root => $env->{server}, repo => 'docs');
	my $server = App::FileSync::Server->new(root => $env->{server}, repo => 'docs', client => 'test-1', role => 'client',
		connection => App::FileSync::Protocol->new(in => \*STDIN, out => \*STDOUT), logger => Log::Abstraction->new(level => 'error', logger => []));
	my $link = File::Spec->catfile($store->master_dir(), 'evil');
	plan skip_all => 'no symlinks' unless eval { symlink $INC{'App/FileSync.pm'}, $link };
	throws_ok { $server->_file_stat($link, 'evil') } qr/\ACannot send 'evil': not a regular file/, 'refused';
};

subtest 'Client: a pull never goes through a symlinked directory' => sub {
	# Old: _abs only checked the path's text, so another client's file under
	# a name that is a symlink here was written wherever the link points.
	my $env = TestSync->new_env();
	my $outside = tempdir(CLEANUP => 1);
	plan skip_all => 'no symlinks' unless eval { symlink $outside, File::Spec->catfile($env->{client}, 'linked') };
	$env->write_file(server => 'linked/planted.txt', 'x');
	App::FileSync::Admin->new(root => $env->{server})->reindex(repo => 'docs');
	open my $saved, '>&', \*STDERR or die $!;
	open STDERR, '>', File::Spec->devnull() or die $!;
	my $ok = eval { $env->sync(quiet => 1); 1 };
	my $error = $@;
	open STDERR, '>&', $saved or die $!;
	ok(!$ok, 'the sync fails');
	like(strip_location($error), qr/\ARefusing to go through symlinked directory /, 'saying why');
	ok(!-e File::Spec->catfile($outside, 'planted.txt'), 'nothing outside the root');
};

subtest 'CLI: commands refuse arguments they do not take' => sub {
	# Old: sync, status, init and several admin commands ignored them, so
	# "filesync sync docs" synced every repo and "client forget pc laptop"
	# forgot only pc.
	throws_ok { App::FileSync::CLI::_client_sync('docs') } qr/\Async: unexpected arguments: docs/, 'filesync sync NAME';
	throws_ok { App::FileSync::CLI::_client_status('docs') } qr/\Astatus: unexpected arguments: docs/, 'filesync status NAME';
	my $admin = App::FileSync::Admin->new(root => tempdir(CLEANUP => 1), logger => Log::Abstraction->new(level => 'error', logger => []));
	throws_ok { App::FileSync::CLI::_admin_client_forget($admin, 'pc', 'laptop', '--repo', 'docs') } qr/\Aclient forget: unexpected arguments: laptop/, 'client forget A B';
	throws_ok { App::FileSync::CLI::_admin_repo_list($admin, 'x') } qr/\Arepo list: unexpected arguments: x/, 'repo list X';
};

# --- 0.003.0 follow-up: set-id bits, upload fields, unreadable trees, -------
# --- line length and wide characters ----------------------------------------

subtest 'Modes: setuid and setgid are never carried' => sub {
	# Old: MODE_MASK was 07777, so a client could push a set-id program that
	# every other client then installed with those bits.
	plan skip_all => 'no POSIX modes' if $^O eq 'MSWin32';
	my $dir = tempdir(CLEANUP => 1);
	my $temp = File::Spec->catfile($dir, 'temp');
	open my $fh, '>', $temp or die $!;
	close $fh;
	my $target = App::FileSync::IO::install_file($temp, File::Spec->catfile($dir, 'prog'), undef, oct(6755));
	is((stat $target)[2] & oct(7777), oct(755), 'installed without the set-id bits');
	chmod oct(4755), $target;
	my $scan = App::FileSync::Scan::scan(root => $dir);
	is($scan->{manifest}{prog}{mode}, oct(755), 'and a set-id file is described without them');
};

subtest 'Server: a put must announce its size and hash' => sub {
	# Old: s and h were optional on a put, so an upload had no size limit and
	# nothing checked what arrived.
	my $env = TestSync->new_env();
	$env->write_file(client => 'a.txt', 'a');
	my $hash = Digest::SHA::sha256_hex('a');
	for my $missing (qw(s h)) {
		my $conn = session($env);	# its plan pushes a.txt
		my %put = (cmd => 'put', p => 'a.txt', s => 1, h => $hash, m => 1, mode => 420);
		delete $put{$missing};
		is(call($conn, \%put)->{error}, "Bad '$missing' for 'a.txt'", "a put without $missing is refused");



( run in 2.073 seconds using v1.01-cache-2.11-cpan-eb134014566 )