1#!/usr/bin/env python3
2#
3#  Copyright (c) 2020, The OpenThread Authors.
4#  All rights reserved.
5#
6#  Redistribution and use in source and binary forms, with or without
7#  modification, are permitted provided that the following conditions are met:
8#  1. Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10#  2. Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#  3. Neither the name of the copyright holder nor the
14#     names of its contributors may be used to endorse or promote products
15#     derived from this software without specific prior written permission.
16#
17#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27#  POSSIBILITY OF SUCH DAMAGE.
28#
29
30import importlib.util
31import inspect
32import json
33import logging
34import os
35import sys
36
37THREAD_CERT_DIR = './tests/scripts/thread-cert'
38sys.path.append(THREAD_CERT_DIR)
39
40import thread_cert
41from pktverify.packet_verifier import PacketVerifier
42
43logging.basicConfig(level=logging.INFO,
44                    format='File "%(pathname)s", line %(lineno)d, in %(funcName)s\n'
45                    '%(asctime)s - %(levelname)s - %(message)s')
46
47
48def main():
49    json_file = sys.argv[1]
50    with open(json_file, 'rt') as fp:
51        test_info = json.load(fp)
52
53    script = test_info['script']
54
55    script = os.path.relpath(script, THREAD_CERT_DIR)
56
57    module_name = os.path.splitext(script)[0].replace('/', '.')
58    logging.info("Loading %s as module %s ...", script, module_name)
59
60    spec = importlib.util.spec_from_file_location(module_name, os.path.join(THREAD_CERT_DIR, script))
61    mod = importlib.util.module_from_spec(spec)
62    spec.loader.exec_module(mod)
63
64    test_class = None
65
66    for name, member in inspect.getmembers(mod):
67        if isinstance(member, type) and issubclass(member, thread_cert.TestCase):
68            assert test_class is None, (test_class, member)
69            test_class = member
70
71    assert test_class is not None, "can not find a test class in %s" % script
72
73    test_instance = test_class()
74
75    pv = PacketVerifier(json_file)
76    pv.add_common_vars()
77    test_instance.verify(pv)
78
79    print("Packet verification passed: %s" % json_file, file=sys.stderr)
80
81
82if __name__ == '__main__':
83    main()
84