1#!/usr/bin/env perl
2#
3# Copyright (c) 2005, 2006 Marcel Moolenaar
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27
28use strict;
29use warnings;
30
31use File::Basename;
32
33my $disk = "/tmp/disk-$$";
34my $mntpt_prefix = "/tmp/mount-$$";
35
36my %steps = (
37    "000" => "gctl class=PART",
38    "001" => "gctl class=PART verb=bogus",
39    "010" => "gctl class=PART verb=create",
40    "011" => "gctl class=PART verb=create provider=bogus",
41    "020" => "mdcfg create pristine",
42    "021" => "gctl class=PART verb=create provider=%dev% entries=-1",
43    "022" => "gctl class=PART verb=create provider=%dev% entries=128",
44    "023" => "gctl class=PART verb=create provider=%dev%",
45    "024" => "gctl class=PART verb=modify geom=%dev%",
46    "025" => "conf",
47    "030" => "gctl class=PART verb=add",
48    "031" => "gctl class=PART verb=add geom=bogus",
49    "032" => "gctl class=PART verb=add geom=%dev%",
50    "033" => "gctl class=PART verb=add geom=%dev% type=bogus",
51    "034" => "gctl class=PART verb=add geom=%dev% type=ed0101b0-2a71-11da-ba81-003048416ace",
52    "035" => "gctl class=PART verb=add geom=%dev% type=ed0101b0-2a71-11da-ba81-003048416ace start=1",
53    "036" => "gctl class=PART verb=add geom=%dev% type=ed0101b0-2a71-11da-ba81-003048416ace start=34",
54    "037" => "gctl class=PART verb=add geom=%dev% type=ed0101b0-2a71-11da-ba81-003048416ace start=34 end=12345678",
55    "038" => "gctl class=PART verb=add geom=%dev% type=ed0101b0-2a71-11da-ba81-003048416ace start=162 end=417 entry=129",
56    "039" => "gctl class=PART verb=add geom=%dev% type=ed0101b0-2a71-11da-ba81-003048416ace start=162 end=417 entry:8=5",
57    "040" => "gctl class=PART verb=add geom=%dev% type=83d34ed5-c4ff-11da-b65b-000347c5d7f3 start=34 end=161 entry=5",
58    "041" => "gctl class=PART verb=add geom=%dev% type=83d34ed5-c4ff-11da-b65b-000347c5d7f3 start=34 end=546",
59    "042" => "gctl class=PART verb=add geom=%dev% type=83d34ed5-c4ff-11da-b65b-000347c5d7f3 start=162 end=417",
60    "043" => "gctl class=PART verb=add geom=%dev% type=83d34ed5-c4ff-11da-b65b-000347c5d7f3 start=100 end=300",
61    "044" => "gctl class=PART verb=add geom=%dev% type=83d34ed5-c4ff-11da-b65b-000347c5d7f3 start=300 end=500",
62    "045" => "gctl class=PART verb=add geom=%dev% type=83d34ed5-c4ff-11da-b65b-000347c5d7f3 start=34 end=161 entry:8",
63    "046" => "gctl class=PART verb=add geom=%dev% type=d2bd4509-c4ff-11da-b4cc-00306e39b62f start=418 end=546 entry:8",
64    "047" => "conf",
65    "050" => "gctl class=PART verb=remove geom=%dev% entry=5",
66    "051" => "gctl class=PART verb=remove geom=%dev% entry=2",
67    "052" => "gctl class=PART verb=remove geom=%dev% entry=1",
68    "053" => "gctl class=PART verb=remove geom=%dev% entry=1",
69    "054" => "conf",
70    "060" => "gctl class=PART verb=add geom=%dev% type=516e7cb6-6ecf-11d6-8ff8-00022d09712b start=34 end=546 entry:8=1",
71    "061" => "mount %dev%p1",
72    "062" => "gctl class=PART verb=delete geom=%dev% entry=1",
73    "063" => "umount %dev%p1",
74    "064" => "gctl class=PART verb=delete geom=%dev% entry=1",
75    "065" => "conf",
76    "100" => "mdcfg destroy",
77    "110" => "mdcfg create corrupted",
78    "111" => "gctl class=PART verb=add geom=%dev%",
79    "120" => "mdcfg destroy",
80);
81
82my %result = (
83    "000" => "FAIL Verb missing",
84    "001" => "FAIL 22 verb 'bogus'",
85    "010" => "FAIL 87 provider",
86    "011" => "FAIL 22 provider 'bogus'",
87    "020" => "",
88    "021" => "FAIL 22 entries -1",
89    "022" => "PASS",
90    "023" => "FAIL 17 geom '%dev%'",
91    "024" => "FAIL 87 entry",
92    "025" => "b1856477950e5786898c8f01361196cf",
93    "030" => "FAIL 87 geom",
94    "031" => "FAIL 22 geom 'bogus'",
95    "032" => "FAIL 87 type",
96    "033" => "FAIL 22 type 'bogus'",
97    "034" => "FAIL 87 start",
98    "035" => "FAIL 22 start 1",
99    "036" => "FAIL 87 end",
100    "037" => "FAIL 22 end 12345678",
101    "038" => "FAIL 22 entry 129",
102    "039" => "PASS entry=5",
103    "040" => "FAIL 17 entry 5",
104    "041" => "FAIL 28 start/end 34/546",
105    "042" => "FAIL 28 start/end 162/417",
106    "043" => "FAIL 28 start/end 100/300",
107    "044" => "FAIL 28 start/end 300/500",
108    "045" => "PASS entry=1",
109    "046" => "PASS entry=2",
110    "047" => "50783a39eecfc62a29db24381e12b9d8",
111    "050" => "PASS",
112    "051" => "PASS",
113    "052" => "PASS",
114    "053" => "FAIL 2 entry 1",
115    "054" => "b1856477950e5786898c8f01361196cf",
116    "060" => "PASS",
117    "061" => "PASS",
118    "062" => "FAIL 16",
119    "063" => "PASS",
120    "064" => "PASS",
121    "065" => "b1856477950e5786898c8f01361196cf",
122    "100" => "",
123    "110" => "",
124    "111" => "FAIL 6 geom '%dev%'",
125    "120" => "",
126);
127
128my $verbose = "";
129if (exists $ENV{'TEST_VERBOSE'}) {
130    $verbose = "-v";
131}
132
133# Compile the driver...
134my $st = system("make obj && make all");
135if ($st != 0) {
136    print "1..0 # SKIP error compiling test.c\n";
137    exit 0;
138}
139chomp(my $cmd = `make '-V\${.OBJDIR}/\${PROG}'`);
140
141my $out = basename($cmd) . ".out";
142
143# Make sure we have permission to use gctl...
144if (`$cmd` =~ "^FAIL Permission denied") {
145    print "1..0 # SKIP insufficient permissions\n";
146    exit 0;
147}
148
149my $debugflags_oid = 'kern.geom.debugflags';
150chomp(my $old_geom_debugflags = `sysctl -n $debugflags_oid`);
151if ($? != 0) {
152    print "1..0 # SKIP could not query $debugflags_oid\n";
153    exit 0;
154}
155if (system("sysctl $debugflags_oid=0") != 0) {
156    print "1..0 # SKIP could not set $debugflags_oid=0\n";
157    exit 0;
158}
159
160my $count = keys (%steps);
161print "1..$count\n";
162
163my $nr = 1;
164my $dev = "n/a";
165foreach my $key (sort keys %steps) {
166    my ($action, $args) = split(/ /, $steps{$key}, 2);
167    my $res = $result{$key};
168    $args = "" if (not defined $args);
169    $args =~ s/%dev%/$dev/g;
170    $res =~ s/%dev%/$dev/g;
171
172    if ($action =~ "^gctl") {
173	my $errmsg = "";
174	system("$cmd $verbose $args | tee $out 2>&1");
175	chomp($st = `tail -1 $out`);
176	if ($st ne $res) {
177	    $errmsg = "\"$st\" (actual) != \"$res\" (expected)\n";
178	}
179	printf("%sok $nr \# gctl($key)%s\n",
180	    ($errmsg eq "" ? "" : "not "),
181	    ($errmsg eq "" ? "" : " - $errmsg"));
182	unlink $out;
183    } elsif ($action =~ "^mdcfg") {
184	my $errmsg = "";
185	if ($args =~ "^create") {
186	    # NOTE: `count=1024` affects $key => {"025" "054", "065"}.
187	    if (system("dd if=/dev/zero of=$disk count=1024 2>&1") == 0) {
188		chomp($dev = `mdconfig -a -t vnode -f $disk`);
189		if ($? == 0) {
190		    if (system("gpart create -s GPT $dev") != 0) {
191			$errmsg = "gpart create failed";
192		    }
193		} else {
194		    $errmsg = "mdconfig -a failed";
195		}
196	    } else {
197		$errmsg = "dd failed";
198	    }
199	} elsif ($args =~ "^destroy") {
200	    $dev =~ s/md/-u /g;
201	    if (system("mdconfig -d $dev") != 0) {
202		$errmsg = "mdconfig -d failed";
203	    }
204	    unlink $disk;
205	    $dev = "n/a";
206	}
207	printf("%sok $nr # mdcfg($key)%s\n",
208	    ($errmsg eq "" ? "" : "not "),
209	    ($errmsg eq "" ? "" : " - $errmsg"));
210    } elsif ($action =~ "^conf") {
211	system("sysctl -b kern.geom.conftxt | grep -a $dev | sed -e s:$disk:DISK:g -e s:$dev:DEV:g | sort | md5 -p | tee $out 2>&1");
212	$st = `tail -1 $out`;
213	if ($st =~ "^$res") {
214	    print "ok $nr \# conf($key)\n";
215	} else {
216	    print "not ok $nr \# conf($key) - $st\n";
217	}
218	unlink $out;
219    } elsif ($action =~ "^mount") {
220	my $errmsg = "";
221	mkdir("$mntpt_prefix-$args");
222	if (system("newfs /dev/$args") == 0) {
223	    if (system("mount /dev/$args $mntpt_prefix-$args") != 0) {
224		$errmsg = "mount failed";
225	    }
226	} else {
227	    $errmsg = "newfs failed";
228	}
229	printf("%sok $nr # mount($key)%s\n",
230	    ($errmsg eq "" ? "" : "not "),
231	    ($errmsg eq "" ? "" : " - $errmsg"));
232    } elsif ($action =~ "^umount") {
233	system("umount $mntpt_prefix-$args");
234	system("rmdir $mntpt_prefix-$args");
235	print "ok $nr \# umount($key)\n";
236    }
237    $nr += 1;
238}
239END {
240    system("sysctl $debugflags_oid=$old_geom_debugflags");
241    unlink($cmd);
242}
243