aboutsummaryrefslogtreecommitdiffstats
path: root/tools/ipc-modem/tests/ipc-modem.py
blob: 25331ae16cfe83fe42f9a4301c994d536775cc1e (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
#
#  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

# sysexits.h
class SysExit(object):
    #define EX_USAGE        64      /* command line usage error */
    EX_USAGE = sh.ErrorReturnCode_64

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

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

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

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

        if 'VALGRIND' in os.environ:
            self.timeout = 60
            ipc_modem = sh.Command(os.environ['VALGRIND'])
            self.ipc_modem = ipc_modem.bake('-v',
                                            '--log-file=valgrind.%p.log',
                                            '--leak-check=full',
                                            command_path,
                                            '--dry-run')
        else:
            self.timeout = 3
            ipc_modem = sh.Command(command_path)
            self.ipc_modem = ipc_modem.bake('--dry-run')

    def test_help(self):
        try:
            self.ipc_modem()
        except SysExit.EX_USAGE:
            pass
        else:
            raise Exception()

    def test_boot(self, timeout=None):
        if timeout==None:
            timeout = self.timeout

        self.ipc_modem('boot',  _timeout=timeout)

    def test_power_on(self, timeout=None):
        if timeout==None:
            timeout = self.timeout

        self.ipc_modem('power-on',  _timeout=timeout)

    def test_power_off(self, timeout=None):
        if timeout==None:
            timeout = self.timeout

        self.ipc_modem('power-off', _timeout=timeout)

    def test_start(self, timeout=None):
        if timeout==None:
            timeout = self.timeout

        try:
            self.ipc_modem('start',  _timeout=timeout)
        except sh.TimeoutException:
            pass
        else:
            raise Exception()

    def test_call_with_number(self, timeout=None):
        if timeout==None:
            timeout = self.timeout

        expected_output = "[I] Got call number!"
        output = ""
        try:
            output = get_output(self.ipc_modem('power-on',
                                               '--call=0102030405',
                                               _timeout=timeout))
        except:
            raise Exception()

        if output != expected_output:
            raise Exception()

    def test_call_without_number(self, timeout=None):
        if timeout==None:
            timeout = self.timeout

        expected_output = "[E] Missing call number"
        output = get_output(self.ipc_modem('power-on',
                                           '--call=',
                                           _timeout=timeout,
                                           _ok_code=SysExit.EX_USAGE.exit_code))
        if output != expected_output:
            raise Exception()

    def test_commands(self):
        self.test_boot()
        self.test_power_on()
        self.test_power_off()
        self.test_start()
        self.test_call_with_number()
        self.test_call_without_number()

def main():
    ipc_modem = IpcModem()
    ipc_modem.test_help()
    ipc_modem.test_commands()

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