Imported Upstream version 2.3.7
[platform/upstream/cryptsetup.git] / tests / generators / generate-luks2-correct-full-json0.img.sh
1 #!/bin/bash
2
3 . lib.sh
4
5 #
6 # *** Description ***
7 #
8 # generate header with correct json of maximal size in primary slot.
9 # Secondary header is broken on purpose.
10 #
11
12 # $1 full target dir
13 # $2 full source luks2 image
14
15 PATTERN="\"config\":{"
16 KEY="\"config_key\":\""
17
18 function prepare()
19 {
20         cp $SRC_IMG $TGT_IMG
21         test -d $TMPDIR || mkdir $TMPDIR
22         read_luks2_json0 $TGT_IMG $TMPDIR/json0
23         read_luks2_bin_hdr0 $TGT_IMG $TMPDIR/hdr0
24         read_luks2_bin_hdr1 $TGT_IMG $TMPDIR/hdr1
25 }
26
27 function generate()
28 {
29         read -r json_str < $TMPDIR/json0
30         json_len=${#json_str}
31         pindex=$(strindex $json_str $PATTERN)
32         test $pindex -gt 0 || exit 2
33
34         offset=${#PATTERN}
35         offset=$((offset+pindex))
36         key_len=${#KEY}
37         remain=$((LUKS2_JSON_SIZE*512-json_len-key_len-2)) # -2: closing '"' and terminating '\0'
38         if [ ${json_str:offset:1} = "}" ]; then
39                 format_str="%s%s%s"
40         else
41                 format_str="%s%s,%s"
42                 remain=$((remain-1)) # also count with separating ','
43         fi
44         test $remain -gt 0 || exit 2
45
46         fill=$(repeat_str "X" $remain)"\""
47
48         printf $format_str $KEY $fill ${json_str:$offset} | _dd of=$TMPDIR/json0 bs=1 seek=$offset conv=notrunc
49
50         merge_bin_hdr_with_json $TMPDIR/hdr0 $TMPDIR/json0 $TMPDIR/area0
51         erase_checksum $TMPDIR/area0
52         chks0=$(calc_sha256_checksum_file $TMPDIR/area0)
53         write_checksum $chks0 $TMPDIR/area0
54         write_luks2_hdr0 $TMPDIR/area0 $TGT_IMG
55         kill_bin_hdr $TMPDIR/hdr1
56         write_luks2_hdr1 $TMPDIR/hdr1 $TGT_IMG
57 }
58
59 function check()
60 {
61         read_luks2_bin_hdr1 $TGT_IMG $TMPDIR/hdr_res1
62         local str_res1=$(head -c 6 $TMPDIR/hdr_res1)
63         test "$str_res1" = "VACUUM" || exit 2
64
65         read_luks2_json0 $TGT_IMG $TMPDIR/json_res0
66         chks_res0=$(read_sha256_checksum $TGT_IMG)
67         test "$chks0" = "$chks_res0" || exit 2
68         #json_str_res0=$(< $TMPDIR/json_res0)
69         read -r json_str_res0 < $TMPDIR/json_res0
70         test ${#json_str_res0} -eq $((LUKS2_JSON_SIZE*512-1)) || exit 2
71 }
72
73 function cleanup()
74 {
75         rm -f $TMPDIR/*
76         rm -fd $TMPDIR
77 }
78
79 test $# -eq 2 || exit 1
80
81 TGT_IMG=$1/$(test_img_name $0)
82 SRC_IMG=$2
83
84 prepare
85 generate
86 check
87 cleanup