OSDN Git Service

source test-lib.sh (not init.sh) from each test
[android-x86/external-parted.git] / tests / t4100-dvh-partition-limits.sh
1 #!/bin/sh
2
3 # Copyright (C) 2008 Free Software Foundation, Inc.
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 test_description='enforce limits on partition start sector and length'
19
20 # Need root privileges to use mount.
21 privileges_required_=1
22
23 : ${srcdir=.}
24 . $srcdir/test-lib.sh
25
26 ####################################################
27 # Create and mount a file system capable of dealing with >=2TB files.
28 # We must be able to create a file with an apparent length of 2TB or larger.
29 # It needn't be a large file system.
30 fs=fs_file
31 mp=`pwd`/mount-point
32 n=4096
33
34 test_expect_success \
35     'create an XFS file system' \
36     '
37     dd if=/dev/zero of=$fs bs=1MB count=2 seek=20 &&
38     mkfs.xfs -q $fs &&
39     mkdir "$mp"
40
41     '
42
43 # Unmount upon interrupt, failure, etc., as well as upon normal completion.
44 cleanup_() { cd "$test_dir_" && umount "$mp" > /dev/null 2>&1; }
45
46 test_expect_success \
47     'mount it' \
48     '
49     mount -o loop $fs "$mp" &&
50     cd "$mp"
51
52     '
53 dev=loop-file
54
55 do_mkpart()
56 {
57   start_sector=$1
58   end_sector=$2
59   # echo '********' $(echo $end_sector - $start_sector + 1 |bc)
60   dd if=/dev/zero of=$dev bs=1b count=2k seek=$end_sector 2> /dev/null &&
61   parted -s $dev mklabel $table_type &&
62   parted -s $dev mkpart p xfs ${start_sector}s ${end_sector}s
63 }
64
65 # Specify the starting sector number and length in sectors,
66 # rather than start and end.
67 do_mkpart_start_and_len()
68 {
69   start_sector=$1
70   len=$2
71   end_sector=$(echo $start_sector + $len - 1|bc)
72   do_mkpart $start_sector $end_sector
73 }
74
75 for table_type in dvh; do
76
77 test_expect_success \
78     "$table_type: a partition length of 2^32-1 works." \
79     '
80     end=$(echo $n+2^32-2|bc) &&
81     do_mkpart $n $end
82     '
83
84 test_expect_success \
85     'print the result' \
86     'parted -s $dev unit s p > out 2>&1 &&
87      sed -n "/^  *1  *$n/s/  */ /gp" out|sed "s/  *\$//" > k && mv k out &&
88      echo " 1 ${n}s ${end}s 4294967295s primary" > exp &&
89      diff -u out exp
90     '
91
92 test_expect_failure \
93     "$table_type: a partition length of exactly 2^32 sectors provokes failure." \
94     'do_mkpart $n $(echo $n+2^32-1|bc) > err 2>&1'
95
96 bad_part_length()
97 { echo "Error: partition length of $1 sectors exceeds the"\
98   "$table_type-partition-table-imposed maximum of 4294967295"; }
99 test_expect_success \
100     'check for new diagnostic' \
101     'bad_part_length 4294967296 > exp && diff -u err exp'
102
103 # FIXME: investigate this.
104 # Unexpectedly to me, both of these failed with this same diagnostic:
105 #
106 #   Error: partition length of 4294967296 sectors exceeds the \
107 #   DOS-partition-table-imposed maximum of 2^32-1" > exp &&
108 #
109 # I expected the one below to fail with a length of _4294967297_.
110 # Debugging, I see that _check_partition *does* detect this,
111 # but the diagnostic doesn't get displayed because of the wonders
112 # of parted's exception mechanism.
113
114 test_expect_failure \
115     "$table_type: a partition length of 2^32+1 sectors provokes failure." \
116     'do_mkpart $n $(echo $n+2^32|bc) > err 2>&1'
117
118 test_expect_success \
119     'check for new diagnostic' \
120     'bad_part_length 4294967297 > exp && diff -u err exp'
121
122 # =========================================================
123 # Now consider partition starting sector numbers.
124 bad_start_sector()
125 { echo "Error: starting sector number, $1 exceeds the"\
126   "$table_type-partition-table-imposed maximum of 4294967295"; }
127
128 test_expect_success \
129     "$table_type: a partition start sector number of 2^32-1 works." \
130     'do_mkpart_start_and_len $(echo 2^32-1|bc) 1000'
131
132 # FIXME: this partition number 9 (not requested!) looks totally bogus
133 # FIXME: For now, we just expect what the code produces.
134 # FIXME: In the long run, figure out if it's sensible.
135 cat > exp <<EOF
136 Model:  (file)
137 Disk: 4294970342s
138 Sector size (logical/physical): 512B/512B
139 Partition Table: $table_type
140
141 Number  Start        End          Size   Type      File system  Name  Flags
142  9      0s           4095s        4096s  extended
143  1      4294967295s  4294968294s  1000s  primary
144
145 EOF
146
147 test_expect_success \
148     'print the result' \
149     'parted -s $dev unit s p > out 2>&1 &&
150      sed "s/Disk .*:/Disk:/;s/ *$//" out > k && mv k out &&
151      diff -u out exp
152     '
153
154 test_expect_failure \
155     "$table_type: a partition start sector number of 2^32 must fail." \
156     'do_mkpart_start_and_len $(echo 2^32|bc) 1000 > err 2>&1'
157 test_expect_success \
158     'check for new diagnostic' \
159     'bad_start_sector 4294967296 > exp && diff -u err exp'
160
161 test_expect_failure \
162     "$table_type: a partition start sector number of 2^32+1 must fail, too." \
163     'do_mkpart_start_and_len $(echo 2^32+1|bc) 1000 > err 2>&1'
164 test_expect_success \
165     'check for new diagnostic' \
166     'bad_start_sector 4294967297 > exp && diff -u err exp'
167
168 done
169
170 test_done