Home | History | Annotate | Line # | Download | only in gpt
t_gpt.sh revision 1.5
      1 # $NetBSD: t_gpt.sh,v 1.5 2015/12/04 16:59:39 christos Exp $
      2 #
      3 # Copyright (c) 2015 The NetBSD Foundation, Inc.
      4 # All rights reserved.
      5 #
      6 # This code is derived from software contributed to The NetBSD Foundation
      7 # by Christos Zoulas
      8 #
      9 # Redistribution and use in source and binary forms, with or without
     10 # modification, are permitted provided that the following conditions
     11 # are met:
     12 # 1. Redistributions of source code must retain the above copyright
     13 #    notice, this list of conditions and the following disclaimer.
     14 # 2. Redistributions in binary form must reproduce the above copyright
     15 #    notice, this list of conditions and the following disclaimer in the
     16 #    documentation and/or other materials provided with the distribution.
     17 #
     18 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28 # POSSIBILITY OF SUCH DAMAGE.
     29 #
     30 
     31 size=10240
     32 newsize=20480
     33 shdr=34
     34 disk=gpt.disk
     35 uuid="........-....-....-....-............"
     36 zero="00000000-0000-0000-0000-000000000000"
     37 src=$(atf_get_srcdir)
     38 
     39 prepare() {
     40 	rm -f $disk
     41 	dd if=/dev/zero of=$disk seek=$size count=1
     42 }
     43 
     44 prepare_2part() {
     45 	prepare
     46 	atf_check -s exit:0 -o empty -e empty gpt create $disk
     47 	atf_check -s exit:0 -o match:"$(partaddmsg 1 34 1024)" -e empty \
     48 	    gpt add -t efi -s 1024 $disk
     49 	atf_check -s exit:0 -o match:"$(partaddmsg 2 1058 9150)" -e empty \
     50 	    gpt add $disk
     51 }
     52 
     53 # Calling this from tests does not work. BUG!
     54 check_2part() {
     55 	atf_check -s exit:0 -o file:"$src/gpt.2part.show.normal" \
     56 	    -e empty gpt show $disk
     57 	atf_check -s exit:0 -o file:"$src/gpt.2part.show.uuid" \
     58 	    -e empty gpt show -u $disk
     59 }
     60 
     61 partaddmsg() {
     62 	echo "^$disk: Partition $1 added: $uuid $2 $3\$"
     63 }
     64 
     65 partresmsg() {
     66 	echo "^$disk: Partition $1 resized: $2 $3\$"
     67 }
     68 
     69 recovermsg() {
     70 	echo "^$disk: Recovered $1 GPT [a-z]* from $2\$"
     71 }
     72 
     73 atf_test_case create_empty
     74 create_empty_head() {
     75 	atf_set "descr" "Create empty disk"
     76 }
     77 
     78 create_empty_body() {
     79 	prepare
     80 	atf_check -s exit:0 -o empty -e empty gpt create $disk
     81 	atf_check -s exit:0 -o file:"$src/gpt.empty.show.normal" \
     82 	    -e empty gpt show $disk
     83 }
     84 
     85 atf_test_case create_2part
     86 create_2part_head() {
     87 	atf_set "descr" "Create 2 partition disk"
     88 }
     89 
     90 create_2part_body() {
     91 	prepare_2part
     92 	check_2part
     93 }
     94 
     95 atf_test_case backup_2part
     96 backup_2part_head() {
     97 	atf_set "descr" "Backup 2 partition disk"
     98 }
     99 
    100 backup_2part_body() {
    101 	prepare_2part
    102 	atf_check -s exit:0 -o save:test.backup -e empty gpt backup $disk
    103 	atf_check -s exit:0 -o file:"$src/gpt.backup" -e empty \
    104 	    sed -e "s/$uuid/$zero/g" "test.backup"
    105 }
    106 
    107 atf_test_case restore_2part
    108 restore_2part_head() {
    109 	atf_set "descr" "Restore 2 partition disk"
    110 }
    111 
    112 restore_2part_body() {
    113 	prepare_2part
    114 	atf_check -s exit:0 -o save:test.backup -e empty gpt backup $disk
    115 	prepare
    116 	atf_check -s exit:0 -o empty -e empty gpt restore -i test.backup $disk
    117 	check_2part
    118 }
    119 
    120 atf_test_case recover_backup
    121 recover_backup_head() {
    122 	atf_set "descr" "Recover the backup GPT header and table"
    123 }
    124 
    125 recover_backup_body() {
    126 	prepare_2part
    127 	dd conv=notrunc if=/dev/zero of=$disk seek=$((size - shdr)) count=$shdr
    128 	atf_check -s exit:0 -o match:"$(recovermsg secondary primary)" \
    129 	    -e empty gpt recover $disk
    130 	check_2part
    131 }
    132 
    133 atf_test_case recover_primary
    134 recover_primary_head() {
    135 	atf_set "descr" "Recover the primary GPT header and table"
    136 }
    137 
    138 recover_primary_body() {
    139 	prepare_2part
    140 	dd conv=notrunc if=/dev/zero of=$disk seek=1 count=$shdr
    141 	atf_check -s exit:0 -o match:"$(recovermsg primary secondary)" \
    142 	    -e empty gpt recover $disk
    143 	check_2part
    144 }
    145 
    146 atf_test_case resize_2part
    147 resize_2part_head() {
    148 	atf_set "descr" "Resize a 2 partition disk and partition"
    149 }
    150 
    151 resize_2part_body() {
    152 	prepare_2part
    153 	dd conv=notrunc if=/dev/zero of=$disk seek=$newsize count=1
    154 	atf_check -s exit:0 -o empty -e empty gpt resizedisk $disk
    155 	atf_check -s exit:0 -o file:"$src/gpt.resizedisk.show.normal" \
    156 	    gpt show $disk
    157 	atf_check -s exit:0 -o match:"$(partresmsg 2 1058 19390)" \
    158 	    -e empty gpt resize -i 2 $disk
    159 	atf_check -s exit:0 -o file:"$src/gpt.resizepart.show.normal" \
    160 	    gpt show $disk
    161 }
    162 
    163 atf_init_test_cases() {
    164 	atf_add_test_case create_empty
    165 	atf_add_test_case create_2part
    166 	atf_add_test_case backup_2part
    167 	atf_add_test_case restore_2part
    168 	atf_add_test_case recover_backup
    169 	atf_add_test_case recover_primary
    170 	atf_add_test_case resize_2part
    171 }
    172