"Fossies" - the Fresh Open Source Software Archive

Member "cryptsetup-2.4.3/tests/generators/generate-luks2-invalid-json-size-c2.img.sh" (24 Jul 2021, 2091 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 primary header with config json size mismatching
    9 # value in binary header
   10 #
   11 # secondary header is corrupted on purpose as well
   12 #
   13 
   14 # $1 full target dir
   15 # $2 full source luks2 image
   16 
   17 function prepare()
   18 {
   19     cp $SRC_IMG $TGT_IMG
   20     test -d $TMPDIR || mkdir $TMPDIR
   21     read_luks2_json0 $TGT_IMG $TMPDIR/json0
   22     read_luks2_bin_hdr0 $TGT_IMG $TMPDIR/hdr0
   23     read_luks2_bin_hdr1 $TGT_IMG $TMPDIR/hdr1
   24 }
   25 
   26 function generate()
   27 {
   28     JS=$(((LUKS2_HDR_SIZE-LUKS2_BIN_HDR_SIZE)*512))
   29     TEST_MDA_SIZE=$LUKS2_HDR_SIZE_32K
   30     TEST_MDA_SIZE_BYTES=$((TEST_MDA_SIZE*512))
   31     TEST_JSN_SIZE=$((TEST_MDA_SIZE-LUKS2_BIN_HDR_SIZE))
   32 
   33     json_str=$(jq -c '.' $TMPDIR/json0)
   34 
   35     write_luks2_json "$json_str" $TMPDIR/json0 $TEST_JSN_SIZE
   36 
   37     write_bin_hdr_size $TMPDIR/hdr0 $TEST_MDA_SIZE_BYTES
   38     write_bin_hdr_size $TMPDIR/hdr1 $TEST_MDA_SIZE_BYTES
   39     write_bin_hdr_offset $TMPDIR/hdr1 $TEST_MDA_SIZE_BYTES
   40 
   41     merge_bin_hdr_with_json $TMPDIR/hdr0 $TMPDIR/json0 $TMPDIR/area0 $TEST_JSN_SIZE
   42     merge_bin_hdr_with_json $TMPDIR/hdr1 $TMPDIR/json0 $TMPDIR/area1 $TEST_JSN_SIZE
   43 
   44     erase_checksum $TMPDIR/area0
   45     chks0=$(calc_sha256_checksum_file $TMPDIR/area0)
   46     write_checksum $chks0 $TMPDIR/area0
   47 
   48     erase_checksum $TMPDIR/area1
   49     chks0=$(calc_sha256_checksum_file $TMPDIR/area1)
   50     write_checksum $chks0 $TMPDIR/area1
   51 
   52     write_luks2_hdr0 $TMPDIR/area0 $TGT_IMG $TEST_MDA_SIZE
   53     write_luks2_hdr1 $TMPDIR/area1 $TGT_IMG $TEST_MDA_SIZE
   54 }
   55 
   56 function check()
   57 {
   58     read_luks2_bin_hdr0 $TGT_IMG $TMPDIR/hdr_res0
   59     local str_res1=$(head -c 4 $TMPDIR/hdr_res0)
   60     test "$str_res1" = "LUKS" || exit 2
   61 
   62     read_luks2_bin_hdr1 $TGT_IMG $TMPDIR/hdr_res1 $TEST_MDA_SIZE
   63     local str_res1=$(head -c 4 $TMPDIR/hdr_res1)
   64     test "$str_res1" = "SKUL" || exit 2
   65 
   66     read_luks2_json0 $TGT_IMG $TMPDIR/json_res0
   67     jq -c --arg js $JS 'if .config.json_size != ( $js | tostring )
   68            then error("Unexpected value in result json") else empty end' $TMPDIR/json_res0 || exit 5
   69 }
   70 
   71 function cleanup()
   72 {
   73     rm -f $TMPDIR/*
   74     rm -fd $TMPDIR
   75 }
   76 
   77 test $# -eq 2 || exit 1
   78 
   79 TGT_IMG=$1/$(test_img_name $0)
   80 SRC_IMG=$2
   81 
   82 prepare
   83 generate
   84 check
   85 cleanup