xref: /aosp_15_r20/system/extras/verity/fec/tests/fec.py (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker# Copyright (C) 2015 The Android Open Source Project
2*288bf522SAndroid Build Coastguard Worker#
3*288bf522SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*288bf522SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*288bf522SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*288bf522SAndroid Build Coastguard Worker#
7*288bf522SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
8*288bf522SAndroid Build Coastguard Worker#
9*288bf522SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*288bf522SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*288bf522SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*288bf522SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*288bf522SAndroid Build Coastguard Worker# limitations under the License.
14*288bf522SAndroid Build Coastguard Worker
15*288bf522SAndroid Build Coastguard Workerimport math
16*288bf522SAndroid Build Coastguard Workerimport os
17*288bf522SAndroid Build Coastguard Workerimport random
18*288bf522SAndroid Build Coastguard Workerimport shutil
19*288bf522SAndroid Build Coastguard Workerimport subprocess
20*288bf522SAndroid Build Coastguard Workerimport sys
21*288bf522SAndroid Build Coastguard Workerimport tempfile
22*288bf522SAndroid Build Coastguard Worker
23*288bf522SAndroid Build Coastguard Workerblocksize = 4096
24*288bf522SAndroid Build Coastguard Workerroots = 2
25*288bf522SAndroid Build Coastguard Worker
26*288bf522SAndroid Build Coastguard Workerdef corrupt(image, offset, length):
27*288bf522SAndroid Build Coastguard Worker    print "corrupting %d bytes at offset %d" % (length, offset)
28*288bf522SAndroid Build Coastguard Worker    f = os.open(image, os.O_WRONLY)
29*288bf522SAndroid Build Coastguard Worker    os.lseek(f, offset, os.SEEK_SET)
30*288bf522SAndroid Build Coastguard Worker    os.write(f, os.urandom(length))
31*288bf522SAndroid Build Coastguard Worker    os.close(f)
32*288bf522SAndroid Build Coastguard Worker
33*288bf522SAndroid Build Coastguard Workerdef corruptmax(image, roots):
34*288bf522SAndroid Build Coastguard Worker    size = os.stat(image).st_size
35*288bf522SAndroid Build Coastguard Worker
36*288bf522SAndroid Build Coastguard Worker    blocks = int(math.ceil(float(size) / blocksize))
37*288bf522SAndroid Build Coastguard Worker    rounds = int(math.ceil(float(blocks) / (255 - roots)))
38*288bf522SAndroid Build Coastguard Worker
39*288bf522SAndroid Build Coastguard Worker    max_errors = int(math.floor(rounds * roots / 2)) * blocksize
40*288bf522SAndroid Build Coastguard Worker    offset = random.randrange(0, size - max_errors)
41*288bf522SAndroid Build Coastguard Worker
42*288bf522SAndroid Build Coastguard Worker    corrupt(image, offset, max_errors)
43*288bf522SAndroid Build Coastguard Worker
44*288bf522SAndroid Build Coastguard Workerdef encode(image, fec, roots):
45*288bf522SAndroid Build Coastguard Worker    if subprocess.call([ "fec", "--roots= " + str(roots), image, fec ]) != 0:
46*288bf522SAndroid Build Coastguard Worker        raise Exception("encoding failed")
47*288bf522SAndroid Build Coastguard Worker
48*288bf522SAndroid Build Coastguard Workerdef decode(image, fec, output):
49*288bf522SAndroid Build Coastguard Worker    return subprocess.call([ "fec", "--decode", image, fec, output ])
50*288bf522SAndroid Build Coastguard Worker
51*288bf522SAndroid Build Coastguard Workerdef compare(a, b):
52*288bf522SAndroid Build Coastguard Worker    return subprocess.call([ "cmp", "-s", a, b ])
53*288bf522SAndroid Build Coastguard Worker
54*288bf522SAndroid Build Coastguard Workerdef simg2img(image, output):
55*288bf522SAndroid Build Coastguard Worker    print "creating a non-sparse copy of '%s' to '%s'" % (image, output)
56*288bf522SAndroid Build Coastguard Worker    if subprocess.call([ "simg2img", image, output]) != 0:
57*288bf522SAndroid Build Coastguard Worker        raise Exception("simg2img failed")
58*288bf522SAndroid Build Coastguard Worker
59*288bf522SAndroid Build Coastguard Workerdef main(argv):
60*288bf522SAndroid Build Coastguard Worker    image = argv[0]
61*288bf522SAndroid Build Coastguard Worker
62*288bf522SAndroid Build Coastguard Worker    temp_img = tempfile.NamedTemporaryFile()
63*288bf522SAndroid Build Coastguard Worker    temp_cor = tempfile.NamedTemporaryFile()
64*288bf522SAndroid Build Coastguard Worker    temp_fec = tempfile.NamedTemporaryFile()
65*288bf522SAndroid Build Coastguard Worker    temp_out = tempfile.NamedTemporaryFile()
66*288bf522SAndroid Build Coastguard Worker
67*288bf522SAndroid Build Coastguard Worker    simg2img(image, temp_img.name)
68*288bf522SAndroid Build Coastguard Worker    simg2img(image, temp_cor.name)
69*288bf522SAndroid Build Coastguard Worker
70*288bf522SAndroid Build Coastguard Worker    encode(image, temp_fec.name, roots)
71*288bf522SAndroid Build Coastguard Worker    corruptmax(temp_cor.name, roots)
72*288bf522SAndroid Build Coastguard Worker
73*288bf522SAndroid Build Coastguard Worker    if decode(temp_cor.name, temp_fec.name, temp_out.name) != 0:
74*288bf522SAndroid Build Coastguard Worker        raise Exception("FAILED: failed to correct maximum expected errors")
75*288bf522SAndroid Build Coastguard Worker
76*288bf522SAndroid Build Coastguard Worker    if compare(temp_img.name, temp_out.name) != 0:
77*288bf522SAndroid Build Coastguard Worker        raise Exception("FAILED: corrected file not identical")
78*288bf522SAndroid Build Coastguard Worker    else:
79*288bf522SAndroid Build Coastguard Worker        print "corrected content matches original"
80*288bf522SAndroid Build Coastguard Worker
81*288bf522SAndroid Build Coastguard Worker    corrupt(temp_cor.name, 0, blocksize)
82*288bf522SAndroid Build Coastguard Worker
83*288bf522SAndroid Build Coastguard Worker    if decode(temp_cor.name, temp_fec.name, temp_out.name) == 0:
84*288bf522SAndroid Build Coastguard Worker        raise Exception("FAILED: corrected more than maximum number of errors?")
85*288bf522SAndroid Build Coastguard Worker
86*288bf522SAndroid Build Coastguard Worker    print "PASSED"
87*288bf522SAndroid Build Coastguard Worker
88*288bf522SAndroid Build Coastguard Workerif __name__ == '__main__':
89*288bf522SAndroid Build Coastguard Worker    main(sys.argv[1:])
90