1212839Sbrian# $FreeBSD: stable/10/sbin/growfs/tests/legacy_test.pl 324690 2017-10-17 15:49:36Z ngie $
2212839Sbrian
3212839Sbrianuse strict;
4212839Sbrianuse warnings;
5324690Sngieuse POSIX;
6212839Sbrianuse Test::More tests => 19;
7212839Sbrianuse Fcntl qw(:DEFAULT :seek);
8212839Sbrian
9212839Sbrianuse constant BLK => 512;
10212839Sbrianuse constant BLKS_PER_MB => 2048;
11212839Sbrian
12212839Sbrianmy $unit;
13212839SbrianEND { system "mdconfig -du$unit" if defined $unit };
14212839Sbrian
15324690Sngiesub fsck_md {
16324690Sngie    my ($is_clean, $md);
17324690Sngie
18324690Sngie    $md = shift;
19324690Sngie
20324690Sngie    chomp(my @fsck_output = `fsck_ffs -Ffy ${md}a`);
21324690Sngie    $is_clean = WIFEXITED($?) &&
22324690Sngie    	(WEXITSTATUS($?) == 0 || WEXITSTATUS($?) == 7);
23324690Sngie    ok($is_clean, "checking ${md}a's filesystem");
24324690Sngie    if ($is_clean) {
25324690Sngie	diag "filesystem reported clean";
26324690Sngie    } else {
27324690Sngie	diag "filesystem not reported clean: " . join("\n", @fsck_output);
28324690Sngie    }
29324690Sngie}
30324690Sngie
31212839Sbriansub setsize {
32212839Sbrian    my ($partszMB, $unitszMB) = @_;
33212839Sbrian
34212839Sbrian    open my $fd, "|-", "disklabel -R md$unit /dev/stdin" or die;
35212839Sbrian    print $fd "a: ", ($partszMB * BLKS_PER_MB), " 0 4.2BSD 1024 8192\n";
36212839Sbrian    print $fd "c: ", ($unitszMB * BLKS_PER_MB), " 0 unused 0 0\n";
37212839Sbrian    close $fd;
38212839Sbrian}
39212839Sbrian
40212839Sbriansub fill {
41212839Sbrian    my ($start, $size, $content) = @_;
42212839Sbrian
43212839Sbrian    my $content512 = $content x (int(512 / length $content) + 1);
44212839Sbrian    substr($content512, 512) = "";
45212839Sbrian    sysopen my $fd, "/dev/md$unit", O_RDWR or die "/dev/md$unit: $!";
46212839Sbrian    seek($fd, $start * BLK, SEEK_SET);
47212839Sbrian    while ($size) {
48212839Sbrian	syswrite($fd, $content512) == 512 or die "write: $!";
49212839Sbrian	$size--;
50212839Sbrian    }
51212839Sbrian}
52212839Sbrian
53212839SbrianSKIP: {
54212839Sbrian    skip "Cannot test without UID 0", 19 if $<;
55212839Sbrian
56212839Sbrian    chomp(my $md = `mdconfig -s40m`);
57212839Sbrian    like($md, qr/^md\d+$/, "Created $md with size 40m") or die;
58212839Sbrian    $unit = substr $md, 2;
59212839Sbrian
60212839Sbrian    for my $type (1..2) {
61212839Sbrian
62212839Sbrian	initialise: {
63212839Sbrian	    ok(setsize(10, 40), "Sized ${md}a to 10m");
64212839Sbrian	    system "newfs -O $type -U ${md}a >/dev/null";
65212839Sbrian	    is($?, 0, "Initialised the filesystem on ${md}a as UFS$type");
66324690Sngie
67324690Sngie	    fsck_md($md);
68212839Sbrian	}
69212839Sbrian
70212839Sbrian	extend20_zeroed: {
71212839Sbrian	    ok(setsize(20, 40), "Sized ${md}a to 20m");
72212839Sbrian	    diag "Filling the extent with zeros";
73212839Sbrian	    fill(10 * BLKS_PER_MB, 10 * BLKS_PER_MB, chr(0));
74212839Sbrian	    my $out = `growfs -y ${md}a`;
75212839Sbrian	    is($?, 0, "Extended the filesystem on ${md}a") or print $out;
76212839Sbrian
77212839Sbrian	    my ($unallocated) = $out =~ m{\d+ sectors cannot be allocated};
78212839Sbrian	    fill(30 * BLKS_PER_MB - $unallocated, $unallocated, chr(0))
79212839Sbrian		if $unallocated;
80212839Sbrian
81324690Sngie	    fsck_md($md);
82212839Sbrian	}
83212839Sbrian
84212839Sbrian	extend30_garbaged: {
85212839Sbrian	    ok(setsize(30, 40), "Sized ${md}a to 30m");
86212839Sbrian	    diag "Filling the extent with garbage";
87212839Sbrian	    fill(20 * BLKS_PER_MB, 10 * BLKS_PER_MB, chr(0xaa) . chr(0x55));
88212839Sbrian	    my $out = `growfs -y ${md}a`;
89212839Sbrian	    is($?, 0, "Extended the filesystem on ${md}a") or print $out;
90212839Sbrian
91212839Sbrian	    my ($unallocated) = $out =~ m{\d+ sectors cannot be allocated};
92212839Sbrian	    fill(30 * BLKS_PER_MB - $unallocated, $unallocated, chr(0))
93212839Sbrian		if $unallocated;
94212839Sbrian
95324690Sngie	    fsck_md($md);
96212839Sbrian	}
97212839Sbrian    }
98212839Sbrian
99212839Sbrian    system "mdconfig -du$unit";
100212839Sbrian    undef $unit;
101212839Sbrian}
102