Home | History | Annotate | Line # | Download | only in gpt
t_gpt.sh revision 1.4
      1 # $NetBSD: t_gpt.sh,v 1.4 2015/12/04 01:42:47 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 shdr=34
     33 disk=gpt.disk
     34 uuid="........-....-....-....-............"
     35 zero="00000000-0000-0000-0000-000000000000"
     36 src=$(atf_get_srcdir)
     37 
     38 prepare() {
     39 	rm -f $disk
     40 	dd if=/dev/zero of=$disk seek=$size count=1
     41 }
     42 
     43 prepare_2part() {
     44 	prepare
     45 	atf_check -s exit:0 -o empty -e empty gpt create $disk
     46 	atf_check -s exit:0 -o match:"$(partmsg 1 34 1024)" -e empty \
     47 	    gpt add -t efi -s 1024 $disk
     48 	atf_check -s exit:0 -o match:"$(partmsg 2 1058 9150)" -e empty \
     49 	    gpt add $disk
     50 }
     51 
     52 # Calling this from tests does not work. BUG!
     53 check_2part() {
     54 	atf_check -s exit:0 -o file:"$src/gpt.2part.show.normal" \
     55 	    -e empty gpt show $disk
     56 	atf_check -s exit:0 -o file:"$src/gpt.2part.show.uuid" \
     57 	    -e empty gpt show -u $disk
     58 }
     59 
     60 partmsg() {
     61 	echo "^$disk: Partition $1 added: $uuid $2 $3\$"
     62 }
     63 
     64 recovermsg() {
     65 	echo "^$disk: Recovered $1 GPT [a-z]* from $2\$"
     66 }
     67 
     68 atf_test_case create_empty
     69 create_empty_head() {
     70 	atf_set "descr" "Create empty disk"
     71 }
     72 
     73 create_empty_body() {
     74 	prepare
     75 	atf_check -s exit:0 -o empty -e empty gpt create $disk
     76 	atf_check -s exit:0 -o file:"$src/gpt.empty.show.normal" \
     77 	    -e empty gpt show $disk
     78 }
     79 
     80 atf_test_case create_2part
     81 create_2part_head() {
     82 	atf_set "descr" "Create 2 partition disk"
     83 }
     84 
     85 create_2part_body() {
     86 	prepare_2part
     87 	check_2part
     88 }
     89 
     90 atf_test_case backup_2part
     91 backup_2part_head() {
     92 	atf_set "descr" "Backup 2 partition disk"
     93 }
     94 
     95 backup_2part_body() {
     96 	prepare_2part
     97 	atf_check -s exit:0 -o save:test.backup -e empty gpt backup $disk
     98 	atf_check -s exit:0 -o file:"$src/gpt.backup" -e empty \
     99 	    sed -e "s/$uuid/$zero/g" "test.backup"
    100 }
    101 
    102 atf_test_case restore_2part
    103 restore_2part_head() {
    104 	atf_set "descr" "Restore 2 partition disk"
    105 }
    106 
    107 restore_2part_body() {
    108 	prepare_2part
    109 	atf_check -s exit:0 -o save:test.backup -e empty gpt backup $disk
    110 	prepare
    111 	atf_check -s exit:0 -o empty -e empty gpt restore -i test.backup $disk
    112 	check_2part
    113 }
    114 
    115 atf_test_case recover_backup
    116 recover_backup_head() {
    117 	atf_set "descr" "Recover the backup GPT header and table"
    118 }
    119 
    120 recover_backup_body() {
    121 	prepare_2part
    122 	dd conv=notrunc if=/dev/zero of=$disk seek=$((size - shdr)) count=$shdr
    123 	atf_check -s exit:0 -o match:"$(recovermsg secondary primary)" \
    124 	    -e empty gpt recover $disk
    125 	check_2part
    126 }
    127 
    128 atf_test_case recover_primary
    129 recover_primary_head() {
    130 	atf_set "descr" "Recover the primary GPT header and table"
    131 }
    132 
    133 recover_primary_body() {
    134 	prepare_2part
    135 	dd conv=notrunc if=/dev/zero of=$disk seek=1 count=$shdr
    136 	atf_check -s exit:0 -o match:"$(recovermsg primary secondary)" \
    137 	    -e empty gpt recover $disk
    138 	check_2part
    139 }
    140 
    141 atf_init_test_cases() {
    142 	atf_add_test_case create_empty
    143 	atf_add_test_case create_2part
    144 	atf_add_test_case backup_2part
    145 	atf_add_test_case restore_2part
    146 	atf_add_test_case recover_backup
    147 	atf_add_test_case recover_primary
    148 }
    149