aboutsummaryrefslogtreecommitdiffstats
path: root/tools/tests/nv_data-md5.py
blob: 374ded285738821dcd02a67f592e451f3b5925f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
#
#  This file is part of libsamsung-ipc.
#
#  Copyright (C) 2020 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
#
#  libsamsung-ipc is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 2 of the License, or
#  (at your option) any later version.
#
#  libsamsung-ipc is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with libsamsung-ipc.  If not, see <http://www.gnu.org/licenses/>.

import os
import re
import sys
import sh

def usage(progname):
    print('{} [test]'.format(progname))
    sys.exit(1)

def get_output(data):
    return str(data).replace(os.linesep, '')

class NvDataMD5(object):
    def __init__(self):
        srcdir = os.environ.get('srcdir', None)

        command_path = ''
        if srcdir:
            command_path = '.' + os.sep + 'nv_data-md5'
        # Enable to run tests without automake
        else:
            command_path = os.path.dirname(sys.argv[0]) \
                + os.sep \
                + '..'   \
                + os.sep \
                + 'nv_data-md5'

        if 'VALGRIND' in os.environ:
            self.nv_data_md5 = sh.Command(os.environ['VALGRIND'])
            self.nv_data_md5 = self.nv_data_md5.bake(
                '-v',
                '--log-file=valgrind.%p.log',
                '--leak-check=full',
                command_path)
        else:
            self.nv_data_md5 = sh.Command(command_path)
    def test_help(self):
        try:
            self.nv_data_md5()
        except sh.ErrorReturnCode_1:
            pass
        else:
            raise Exception()

    def test_commands(self):
        expected_md5 = '5293814414abb3831e3fc1a1b35e69bc'
        NV_DATA_SIZE = 0x200000
        nv_data_bin = get_output(sh.mktemp())

        # Create nv_data.bin
        sh.ddrescue('/dev/zero', nv_data_bin, '-s', str(NV_DATA_SIZE))

        output = get_output(self.nv_data_md5(nv_data_bin))

        print(output)

        if output != expected_md5:
            raise Exception()

        os.unlink(nv_data_bin)

def main():
    nv_data_md5 = NvDataMD5()
    nv_data_md5.test_help()
    nv_data_md5.test_commands()

if __name__ == '__main__':
    rc = main()
    sys.exit(rc)