"Fossies" - the Fresh Open Source Software Archive

Member "cryptsetup-2.4.3/tests/generators/generate-luks2-metadata-size-1m-secondary.img.sh" (24 Jul 2021, 2624 Bytes) of package /linux/misc/cryptsetup-2.4.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/bin/bash
    2 
    3 . lib.sh
    4 
    5 #
    6 # *** Description ***
    7 #
    8 # generate secondary header with one of allowed json area
    9 # size values. Test whether auto-recovery code is able
   10 # to validate secondary header with non-default json area
   11 # size.
   12 #
   13 # primary header is corrupted on purpose.
   14 #
   15 
   16 # $1 full target dir
   17 # $2 full source luks2 image
   18 
   19 function prepare()
   20 {
   21     cp $SRC_IMG $TGT_IMG
   22     test -d $TMPDIR || mkdir $TMPDIR
   23     read_luks2_json0 $TGT_IMG $TMPDIR/json0
   24     read_luks2_bin_hdr0 $TGT_IMG $TMPDIR/hdr0
   25     read_luks2_bin_hdr1 $TGT_IMG $TMPDIR/hdr1
   26 }
   27 
   28 function generate()
   29 {
   30     # 1 MiB metadata
   31     TEST_MDA_SIZE=$LUKS2_HDR_SIZE_1M
   32 
   33     TEST_MDA_SIZE_BYTES=$((TEST_MDA_SIZE*512))
   34     TEST_JSN_SIZE=$((TEST_MDA_SIZE-LUKS2_BIN_HDR_SIZE))
   35     KEYSLOTS_OFFSET=$((TEST_MDA_SIZE*1024))
   36     JSON_DIFF=$(((TEST_MDA_SIZE-LUKS2_HDR_SIZE)*1024))
   37     JSON_SIZE=$((TEST_JSN_SIZE*512))
   38     DATA_OFFSET=16777216
   39 
   40     json_str=$(jq -c --arg jdiff $JSON_DIFF --arg jsize $JSON_SIZE --arg off $DATA_OFFSET \
   41            '.keyslots[].area.offset |= ( . | tonumber + ($jdiff | tonumber) | tostring) |
   42             .config.json_size = $jsize |
   43             .segments."0".offset = $off' $TMPDIR/json0)
   44     test -n "$json_str" || exit 2
   45     test ${#json_str} -lt $((LUKS2_JSON_SIZE*512)) || exit 2
   46 
   47     write_luks2_json "$json_str" $TMPDIR/json0 $TEST_JSN_SIZE
   48 
   49     write_bin_hdr_size $TMPDIR/hdr0 $TEST_MDA_SIZE_BYTES
   50     write_bin_hdr_size $TMPDIR/hdr1 $TEST_MDA_SIZE_BYTES
   51 
   52     write_bin_hdr_offset $TMPDIR/hdr1 $TEST_MDA_SIZE_BYTES
   53 
   54     merge_bin_hdr_with_json $TMPDIR/hdr0 $TMPDIR/json0 $TMPDIR/area0 $TEST_JSN_SIZE
   55     merge_bin_hdr_with_json $TMPDIR/hdr1 $TMPDIR/json0 $TMPDIR/area1 $TEST_JSN_SIZE
   56 
   57     erase_checksum $TMPDIR/area0
   58     chks0=$(calc_sha256_checksum_file $TMPDIR/area0)
   59     write_checksum $chks0 $TMPDIR/area0
   60 
   61     erase_checksum $TMPDIR/area1
   62     chks0=$(calc_sha256_checksum_file $TMPDIR/area1)
   63     write_checksum $chks0 $TMPDIR/area1
   64 
   65     kill_bin_hdr $TMPDIR/area0
   66 
   67     write_luks2_hdr0 $TMPDIR/area0 $TGT_IMG $TEST_MDA_SIZE
   68     write_luks2_hdr1 $TMPDIR/area1 $TGT_IMG $TEST_MDA_SIZE
   69 }
   70 
   71 function check()
   72 {
   73     read_luks2_bin_hdr0 $TGT_IMG $TMPDIR/hdr_res0 $TEST_MDA_SIZE
   74     local str_res0=$(head -c 6 $TMPDIR/hdr_res0)
   75     test "$str_res0" = "VACUUM" || exit 2
   76     read_luks2_json1 $TGT_IMG $TMPDIR/json_res1 $TEST_JSN_SIZE
   77     jq -c --arg koff $KEYSLOTS_OFFSET --arg jsize $JSON_SIZE \
   78         'if ([.keyslots[].area.offset] | map(tonumber) | min | tostring != $koff) or
   79             (.config.json_size != $jsize)
   80         then error("Unexpected value in result json") else empty end' $TMPDIR/json_res1 || exit 5
   81 }
   82 
   83 function cleanup()
   84 {
   85     rm -f $TMPDIR/*
   86     rm -fd $TMPDIR
   87 }
   88 
   89 test $# -eq 2 || exit 1
   90 
   91 TGT_IMG=$1/$(test_img_name $0)
   92 SRC_IMG=$2
   93 
   94 prepare
   95 generate
   96 check
   97 cleanup