xref: /freebsd/tools/regression/geom_gpt/gctl_test.t (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
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# $FreeBSD$
28
29use strict;
30use warnings;
31
32use File::Basename;
33
34my $disk = "/tmp/disk-$$";
35my $mntpt_prefix = "/tmp/mount-$$";
36
37my %steps = (
38    "000" => "gctl class=PART",
39    "001" => "gctl class=PART verb=bogus",
40    "010" => "gctl class=PART verb=create",
41    "011" => "gctl class=PART verb=create provider=bogus",
42    "020" => "mdcfg create pristine",
43    "021" => "gctl class=PART verb=create provider=%dev% entries=-1",
44    "022" => "gctl class=PART verb=create provider=%dev% entries=128",
45    "023" => "gctl class=PART verb=create provider=%dev%",
46    "024" => "gctl class=PART verb=modify geom=%dev%",
47    "025" => "conf",
48    "030" => "gctl class=PART verb=add",
49    "031" => "gctl class=PART verb=add geom=bogus",
50    "032" => "gctl class=PART verb=add geom=%dev%",
51    "033" => "gctl class=PART verb=add geom=%dev% type=bogus",
52    "034" => "gctl class=PART verb=add geom=%dev% type=ed0101b0-2a71-11da-ba81-003048416ace",
53    "035" => "gctl class=PART verb=add geom=%dev% type=ed0101b0-2a71-11da-ba81-003048416ace start=1",
54    "036" => "gctl class=PART verb=add geom=%dev% type=ed0101b0-2a71-11da-ba81-003048416ace start=34",
55    "037" => "gctl class=PART verb=add geom=%dev% type=ed0101b0-2a71-11da-ba81-003048416ace start=34 end=12345678",
56    "038" => "gctl class=PART verb=add geom=%dev% type=ed0101b0-2a71-11da-ba81-003048416ace start=162 end=417 entry=129",
57    "039" => "gctl class=PART verb=add geom=%dev% type=ed0101b0-2a71-11da-ba81-003048416ace start=162 end=417 entry:8=5",
58    "040" => "gctl class=PART verb=add geom=%dev% type=83d34ed5-c4ff-11da-b65b-000347c5d7f3 start=34 end=161 entry=5",
59    "041" => "gctl class=PART verb=add geom=%dev% type=83d34ed5-c4ff-11da-b65b-000347c5d7f3 start=34 end=546",
60    "042" => "gctl class=PART verb=add geom=%dev% type=83d34ed5-c4ff-11da-b65b-000347c5d7f3 start=162 end=417",
61    "043" => "gctl class=PART verb=add geom=%dev% type=83d34ed5-c4ff-11da-b65b-000347c5d7f3 start=100 end=300",
62    "044" => "gctl class=PART verb=add geom=%dev% type=83d34ed5-c4ff-11da-b65b-000347c5d7f3 start=300 end=500",
63    "045" => "gctl class=PART verb=add geom=%dev% type=83d34ed5-c4ff-11da-b65b-000347c5d7f3 start=34 end=161 entry:8",
64    "046" => "gctl class=PART verb=add geom=%dev% type=d2bd4509-c4ff-11da-b4cc-00306e39b62f start=418 end=546 entry:8",
65    "047" => "conf",
66    "050" => "gctl class=PART verb=remove geom=%dev% entry=5",
67    "051" => "gctl class=PART verb=remove geom=%dev% entry=2",
68    "052" => "gctl class=PART verb=remove geom=%dev% entry=1",
69    "053" => "gctl class=PART verb=remove geom=%dev% entry=1",
70    "054" => "conf",
71    "060" => "gctl class=PART verb=add geom=%dev% type=516e7cb6-6ecf-11d6-8ff8-00022d09712b start=34 end=546 entry:8=1",
72    "061" => "mount %dev%p1",
73    "062" => "gctl class=PART verb=delete geom=%dev% entry=1",
74    "063" => "umount %dev%p1",
75    "064" => "gctl class=PART verb=delete geom=%dev% entry=1",
76    "065" => "conf",
77    "100" => "mdcfg destroy",
78    "110" => "mdcfg create corrupted",
79    "111" => "gctl class=PART verb=add geom=%dev%",
80    "120" => "mdcfg destroy",
81);
82
83my %result = (
84    "000" => "FAIL Verb missing",
85    "001" => "FAIL 22 verb 'bogus'",
86    "010" => "FAIL 87 provider",
87    "011" => "FAIL 22 provider 'bogus'",
88    "020" => "",
89    "021" => "FAIL 22 entries -1",
90    "022" => "PASS",
91    "023" => "FAIL 17 geom '%dev%'",
92    "024" => "FAIL 87 entry",
93    "025" => "b1856477950e5786898c8f01361196cf",
94    "030" => "FAIL 87 geom",
95    "031" => "FAIL 22 geom 'bogus'",
96    "032" => "FAIL 87 type",
97    "033" => "FAIL 22 type 'bogus'",
98    "034" => "FAIL 87 start",
99    "035" => "FAIL 22 start 1",
100    "036" => "FAIL 87 end",
101    "037" => "FAIL 22 end 12345678",
102    "038" => "FAIL 22 entry 129",
103    "039" => "PASS entry=5",
104    "040" => "FAIL 17 entry 5",
105    "041" => "FAIL 28 start/end 34/546",
106    "042" => "FAIL 28 start/end 162/417",
107    "043" => "FAIL 28 start/end 100/300",
108    "044" => "FAIL 28 start/end 300/500",
109    "045" => "PASS entry=1",
110    "046" => "PASS entry=2",
111    "047" => "50783a39eecfc62a29db24381e12b9d8",
112    "050" => "PASS",
113    "051" => "PASS",
114    "052" => "PASS",
115    "053" => "FAIL 2 entry 1",
116    "054" => "b1856477950e5786898c8f01361196cf",
117    "060" => "PASS",
118    "061" => "PASS",
119    "062" => "FAIL 16",
120    "063" => "PASS",
121    "064" => "PASS",
122    "065" => "b1856477950e5786898c8f01361196cf",
123    "100" => "",
124    "110" => "",
125    "111" => "FAIL 6 geom '%dev%'",
126    "120" => "",
127);
128
129my $verbose = "";
130if (exists $ENV{'TEST_VERBOSE'}) {
131    $verbose = "-v";
132}
133
134# Compile the driver...
135my $st = system("make obj && make all");
136if ($st != 0) {
137    print "1..0 # SKIP error compiling test.c\n";
138    exit 0;
139}
140chomp(my $cmd = `make '-V\${.OBJDIR}/\${PROG}'`);
141
142my $out = basename($cmd) . ".out";
143
144# Make sure we have permission to use gctl...
145if (`$cmd` =~ "^FAIL Permission denied") {
146    print "1..0 # SKIP insufficient permissions\n";
147    exit 0;
148}
149
150my $debugflags_oid = 'kern.geom.debugflags';
151chomp(my $old_geom_debugflags = `sysctl -n $debugflags_oid`);
152if ($? != 0) {
153    print "1..0 # SKIP could not query $debugflags_oid\n";
154    exit 0;
155}
156if (system("sysctl $debugflags_oid=0") != 0) {
157    print "1..0 # SKIP could not set $debugflags_oid=0\n";
158    exit 0;
159}
160
161my $count = keys (%steps);
162print "1..$count\n";
163
164my $nr = 1;
165my $dev = "n/a";
166foreach my $key (sort keys %steps) {
167    my ($action, $args) = split(/ /, $steps{$key}, 2);
168    my $res = $result{$key};
169    $args = "" if (not defined $args);
170    $args =~ s/%dev%/$dev/g;
171    $res =~ s/%dev%/$dev/g;
172
173    if ($action =~ "^gctl") {
174	my $errmsg = "";
175	system("$cmd $verbose $args | tee $out 2>&1");
176	chomp($st = `tail -1 $out`);
177	if ($st ne $res) {
178	    $errmsg = "\"$st\" (actual) != \"$res\" (expected)\n";
179	}
180	printf("%sok $nr \# gctl($key)%s\n",
181	    ($errmsg eq "" ? "" : "not "),
182	    ($errmsg eq "" ? "" : " - $errmsg"));
183	unlink $out;
184    } elsif ($action =~ "^mdcfg") {
185	my $errmsg = "";
186	if ($args =~ "^create") {
187	    # NOTE: `count=1024` affects $key => {"025" "054", "065"}.
188	    if (system("dd if=/dev/zero of=$disk count=1024 2>&1") == 0) {
189		chomp($dev = `mdconfig -a -t vnode -f $disk`);
190		if ($? == 0) {
191		    if (system("gpart create -s GPT $dev") != 0) {
192			$errmsg = "gpart create failed";
193		    }
194		} else {
195		    $errmsg = "mdconfig -a failed";
196		}
197	    } else {
198		$errmsg = "dd failed";
199	    }
200	} elsif ($args =~ "^destroy") {
201	    $dev =~ s/md/-u /g;
202	    if (system("mdconfig -d $dev") != 0) {
203		$errmsg = "mdconfig -d failed";
204	    }
205	    unlink $disk;
206	    $dev = "n/a";
207	}
208	printf("%sok $nr # mdcfg($key)%s\n",
209	    ($errmsg eq "" ? "" : "not "),
210	    ($errmsg eq "" ? "" : " - $errmsg"));
211    } elsif ($action =~ "^conf") {
212	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");
213	$st = `tail -1 $out`;
214	if ($st =~ "^$res") {
215	    print "ok $nr \# conf($key)\n";
216	} else {
217	    print "not ok $nr \# conf($key) - $st\n";
218	}
219	unlink $out;
220    } elsif ($action =~ "^mount") {
221	my $errmsg = "";
222	mkdir("$mntpt_prefix-$args");
223	if (system("newfs /dev/$args") == 0) {
224	    if (system("mount /dev/$args $mntpt_prefix-$args") != 0) {
225		$errmsg = "mount failed";
226	    }
227	} else {
228	    $errmsg = "newfs failed";
229	}
230	printf("%sok $nr # mount($key)%s\n",
231	    ($errmsg eq "" ? "" : "not "),
232	    ($errmsg eq "" ? "" : " - $errmsg"));
233    } elsif ($action =~ "^umount") {
234	system("umount $mntpt_prefix-$args");
235	system("rmdir $mntpt_prefix-$args");
236	print "ok $nr \# umount($key)\n";
237    }
238    $nr += 1;
239}
240END {
241    system("sysctl $debugflags_oid=$old_geom_debugflags");
242    unlink($cmd);
243}
244