Debian Codegraph @

test/user_script_test.py

raw symbols SCIP
1 # pyOCD debugger
2 # Copyright (c) 2020 Arm Limited
3 # SPDX-License-Identifier: Apache-2.0
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import argparse
18 import os
19 import sys
20 import traceback
21 import logging
22
23 from pyocd.core.helpers import ConnectHelper
24 from pyocd.probe.pydapaccess import DAPAccess
25 from pyocd.core.memory_map import MemoryType
26 from pyocd.commands.execution_context import CommandExecutionContext
27
28 from test_util import (
29 Test,
30 TestResult,
31 get_session_options,
32 get_target_test_params,
33 get_test_binary_path,
34 TEST_DIR,
35 )
36
37 TEST_USER_SCRIPT = os.path.join(TEST_DIR, "test_user_script.py")
38
39 class UserScriptTestResult(TestResult):
40 def __init__(self):
41 super(UserScriptTestResult, self).__init__(None, None, None)
42 self.name = "user_script"
43
44 class UserScriptTest(Test):
45 def __init__(self):
46 super(UserScriptTest, self).__init__("User Script Test", user_script_test)
47
48 def run(self, board):
49 try:
50 result = self.test_function(board.unique_id)
51 except Exception as e:
52 result = UserScriptTestResult()
53 result.passed = False
54 print("Exception %s when testing board %s" % (e, board.unique_id))
55 traceback.print_exc(file=sys.stdout)
56 result.board = board
57 result.test = self
58 return result
59
60 def user_script_test(board_id):
61 with ConnectHelper.session_with_chosen_probe(
62 unique_id=board_id, user_script=TEST_USER_SCRIPT, **get_session_options()) as session:
63 board = session.board
64 target = session.target
65
66 test_params = get_target_test_params(session)
67 session.probe.set_clock(test_params['test_clock'])
68
69 memory_map = target.get_memory_map()
70 boot_region = memory_map.get_boot_memory()
71 ram_region = memory_map.get_default_region_of_type(MemoryType.RAM)
72 binary_file = get_test_binary_path(board.test_binary)
73
74 test_pass_count = 0
75 test_count = 0
76 result = UserScriptTestResult()
77
78 # TEST basic functionality
79 print("\n------ Testing delegates ------")
80
81 # TODO verify user script delegates were called
82 target.reset_and_halt()
83 target.resume()
84 target.halt()
85 target.step()
86
87 test_count += 1
88 test_pass_count += 1
89 print("TEST PASSED")
90
91 # TEST user defined commands
92 print("\n------ Testing user defined commands ------")
93 context = CommandExecutionContext()
94 context.attach_session(session)
95
96 def test_command(cmd):
97 try:
98 print("\nTEST: %s" % cmd)
99 context.process_command_line(cmd)
100 except Exception:
101 print("TEST FAILED")
102 traceback.print_exc(file=sys.stdout)
103 return False
104 else:
105 print("TEST PASSED")
106 return True
107
108 # Verify command with float, int, str args.
109 if test_command("testcmd 3.14 0xbeef foobar"):
110 test_pass_count += 1
111 test_count += 1
112
113 # Verify varargs: all should be strings in the cmd's args
114 if test_command("anothertestcmd a b 1 2 fee fie foe"):
115 test_pass_count += 1
116 test_count += 1
117
118 print("\nTest Summary:")
119 print("Pass count %i of %i tests" % (test_pass_count, test_count))
120 if test_pass_count == test_count:
121 print("USER SCRIPT TEST PASSED")
122 else:
123 print("USER SCRIPT TEST FAILED")
124
125 target.reset()
126
127 result.passed = test_count == test_pass_count
128 return result
129
130 if __name__ == "__main__":
131 parser = argparse.ArgumentParser(description='pyOCD user script test')
132 parser.add_argument('-d', '--debug', action="store_true", help='Enable debug logging')
133 parser.add_argument("-da", "--daparg", dest="daparg", nargs='+', help="Send setting to DAPAccess layer.")
134 args = parser.parse_args()
135 level = logging.DEBUG if args.debug else logging.INFO
136 logging.basicConfig(level=level)
137 DAPAccess.set_args(args.daparg)
138 # Set to debug to print some of the decisions made while flashing
139 session = ConnectHelper.session_with_chosen_probe(**get_session_options())
140 test = UserScriptTest()
141 result = [test.run(session.board)]