Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / tests / generators / generate-luks2-missing-trailing-null-byte-json0.img.sh
1 #!/bin/bash
2
3 . lib.sh
4
5 #
6 # *** Description ***
7 #
8 # generate primary header with well-formed json but missing
9 # trailing null byte.
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 PATTERN="\"config\":{"
18 KEY="\"config_key\":\""
19
20 function generate()
21 {
22         read -r json_str < $TMPDIR/json0
23         json_len=${#json_str}
24         pindex=$(strindex $json_str $PATTERN)
25         test $pindex -gt 0 || exit 2
26
27         offset=${#PATTERN}
28         offset=$((offset+pindex))
29         key_len=${#KEY}
30         remain=$((LUKS2_JSON_SIZE*512-key_len-json_len-1)) # -1: closing '"'
31         if [ ${json_str:offset:1} = "}" ]; then
32                 format_str="%s%s%s"
33         else
34                 format_str="%s%s,%s"
35                 remain=$((remain-1)) # also count with separating ','
36         fi
37         test $remain -gt 0 || exit 2
38
39         fill=$(repeat_str "X" $remain)
40         fill=$(repeat_str "X" $remain)"\""
41
42         printf $format_str $KEY $fill ${json_str:$offset} | _dd of=$TMPDIR/json0 bs=1 seek=$offset conv=notrunc
43
44         lib_mangle_json_hdr0_kill_hdr1
45 }
46
47 function check()
48 {
49         lib_hdr1_killed || exit 2
50         lib_hdr0_checksum || exit 2
51
52         read_luks2_json0 $TGT_IMG $TMPDIR/json_res0
53         read -r json_str_res0 < $TMPDIR/json_res0
54         test ${#json_str_res0} -eq $((LUKS2_JSON_SIZE*512)) || exit 2
55 }
56
57 lib_prepare $@
58 generate
59 check
60 lib_cleanup