1#!/usr/bin/env python3 2# Copyright 2021 The Pigweed Authors 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); you may not 5# use this file except in compliance with the License. You may obtain a copy of 6# the License at 7# 8# https://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13# License for the specific language governing permissions and limitations under 14# the License. 15"""Finds files for a given product.""" 16 17import unittest 18 19from pw_stm32cube_build import inject_init 20 21 22class AddPreMainInitTest(unittest.TestCase): 23 """add_pre_main_init tests.""" 24 25 def test_spaces(self): 26 startup = '\n'.join( 27 [ 28 '/* Call the clock system intitialization function.*/', 29 ' bl SystemInit ', 30 '/* Call static constructors */', 31 ' bl __libc_init_array', 32 '/* Call the application\'s entry point.*/', 33 ' bl main', 34 ' bx lr ', 35 '.size Reset_Handler, .-Reset_Handler', 36 ] 37 ) 38 39 new_startup = inject_init.add_pre_main_init(startup) 40 41 self.assertEqual( 42 new_startup, 43 '\n'.join( 44 [ 45 '/* Call the clock system intitialization function.*/', 46 ' bl SystemInit ', 47 '/* Call static constructors */', 48 ' bl __libc_init_array', 49 '/* Call the application\'s entry point.*/', 50 'bl pw_stm32cube_Init', 51 ' bl main', 52 ' bx lr ', 53 '.size Reset_Handler, .-Reset_Handler', 54 ] 55 ), 56 ) 57 58 def test_tabs(self): 59 startup = '\n'.join( 60 [ 61 'LoopFillZerobss:', 62 ' ldr r3, = _ebss', 63 ' cmp r2, r3', 64 ' bcc FillZerobss', 65 '/* Call static constructors */', 66 ' bl __libc_init_array', 67 '/* Call the application\'s entry point.*/', 68 ' bl main', 69 '', 70 'LoopForever:', 71 ' b LoopForever', 72 ] 73 ) 74 75 new_startup = inject_init.add_pre_main_init(startup) 76 77 self.assertEqual( 78 new_startup, 79 '\n'.join( 80 [ 81 'LoopFillZerobss:', 82 ' ldr r3, = _ebss', 83 ' cmp r2, r3', 84 ' bcc FillZerobss', 85 '/* Call static constructors */', 86 ' bl __libc_init_array', 87 '/* Call the application\'s entry point.*/', 88 'bl pw_stm32cube_Init', 89 ' bl main', 90 '', 91 'LoopForever:', 92 ' b LoopForever', 93 ] 94 ), 95 ) 96 97 def test_main_not_found(self): 98 startup = '\n'.join( 99 [ 100 '/* Call the clock system intitialization function.*/', 101 ' bl SystemInit ', 102 '/* Call static constructors */', 103 ' bl __libc_init_array', 104 '/* Call the application\'s entry point.*/', 105 ' bl application_entry', 106 ' bx lr ', 107 '.size Reset_Handler, .-Reset_Handler', 108 ] 109 ) 110 111 with self.assertRaises(ValueError): 112 inject_init.add_pre_main_init(startup) 113 114 115if __name__ == '__main__': 116 unittest.main() 117