test_entity.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. #
  4. # Copyright 2018-2020 Espressif Systems (Shanghai) CO LTD
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. import sys
  19. import unittest
  20. try:
  21. from entity import Entity, EntityDB
  22. except ImportError:
  23. sys.path.append('../')
  24. from entity import Entity, EntityDB
  25. class EntityTest(unittest.TestCase):
  26. def test_create_none(self):
  27. entity = Entity(Entity.ALL)
  28. self.assertEqual(Entity.Specificity.NONE, entity.specificity)
  29. entity = Entity(None)
  30. self.assertEqual(Entity.Specificity.NONE, entity.specificity)
  31. entity = Entity()
  32. self.assertEqual(Entity.Specificity.NONE, entity.specificity)
  33. def test_create_archive(self):
  34. entity = Entity('libfreertos.a')
  35. self.assertEqual(Entity.Specificity.ARCHIVE, entity.specificity)
  36. entity = Entity('libfreertos.a', Entity.ALL, Entity.ALL)
  37. self.assertEqual(Entity.Specificity.ARCHIVE, entity.specificity)
  38. entity = Entity('libfreertos.a', None, None)
  39. self.assertEqual(Entity.Specificity.ARCHIVE, entity.specificity)
  40. entity = Entity('libfreertos.a', Entity.ALL, None)
  41. self.assertEqual(Entity.Specificity.ARCHIVE, entity.specificity)
  42. entity = Entity('libfreertos.a', None, Entity.ALL)
  43. self.assertEqual(Entity.Specificity.ARCHIVE, entity.specificity)
  44. def test_create_obj(self):
  45. entity = Entity('libfreertos.a', 'croutine')
  46. self.assertEqual(Entity.Specificity.OBJ, entity.specificity)
  47. entity = Entity('libfreertos.a', 'croutine', Entity.ALL)
  48. self.assertEqual(Entity.Specificity.OBJ, entity.specificity)
  49. entity = Entity('libfreertos.a', 'croutine', None)
  50. self.assertEqual(Entity.Specificity.OBJ, entity.specificity)
  51. def test_create_symbol(self):
  52. entity = Entity('libfreertos.a', 'croutine', 'prvCheckPendingReadyList')
  53. self.assertEqual(Entity.Specificity.SYMBOL, entity.specificity)
  54. def test_create_invalid(self):
  55. with self.assertRaises(ValueError):
  56. Entity(None, 'croutine')
  57. with self.assertRaises(ValueError):
  58. Entity(Entity.ALL, 'croutine')
  59. with self.assertRaises(ValueError):
  60. Entity(None, None, 'prvCheckPendingReadyList')
  61. with self.assertRaises(ValueError):
  62. Entity(Entity.ALL, Entity.ALL, 'prvCheckPendingReadyList')
  63. with self.assertRaises(ValueError):
  64. Entity(None, Entity.ALL, 'prvCheckPendingReadyList')
  65. with self.assertRaises(ValueError):
  66. Entity(Entity.ALL, None, 'prvCheckPendingReadyList')
  67. with self.assertRaises(ValueError):
  68. Entity('libfreertos.a', None, 'prvCheckPendingReadyList')
  69. with self.assertRaises(ValueError):
  70. Entity('libfreertos.a', Entity.ALL, 'prvCheckPendingReadyList')
  71. def test_compare_different_specificity(self):
  72. # Different specificity: NONE < ARCHIVE < OBJ < SYMBOL
  73. entity_a = Entity()
  74. entity_b = Entity('libfreertos.a')
  75. self.assertLess(entity_a, entity_b)
  76. entity_a = Entity('libfreertos.a')
  77. entity_b = Entity('libfreertos.a', 'croutine')
  78. self.assertLess(entity_a, entity_b)
  79. entity_a = Entity('libfreertos.a', 'croutine')
  80. entity_b = Entity('libfreertos.a', 'croutine', 'prvCheckPendingReadyList')
  81. self.assertLess(entity_a, entity_b)
  82. entity_a = Entity(Entity.ALL)
  83. entity_b = Entity('libfreertos.a')
  84. self.assertLess(entity_a, entity_b)
  85. entity_a = Entity('libfreertos.a', Entity.ALL)
  86. entity_b = Entity('libfreertos.a', 'croutine')
  87. self.assertLess(entity_a, entity_b)
  88. entity_a = Entity('libfreertos.a', 'croutine', Entity.ALL)
  89. entity_b = Entity('libfreertos.a', 'croutine', 'prvCheckPendingReadyList')
  90. self.assertLess(entity_a, entity_b)
  91. def test_compare_equal(self):
  92. # Compare equal specificities and members
  93. entity_a = Entity()
  94. entity_b = Entity()
  95. self.assertEqual(entity_a, entity_b)
  96. entity_a = Entity('libfreertos.a')
  97. entity_b = Entity('libfreertos.a')
  98. self.assertEqual(entity_a, entity_b)
  99. entity_a = Entity('libfreertos.a', 'croutine')
  100. entity_b = Entity('libfreertos.a', 'croutine')
  101. self.assertEqual(entity_a, entity_b)
  102. entity_a = Entity('libfreertos.a', 'croutine', 'prvCheckPendingReadyList')
  103. entity_b = Entity('libfreertos.a', 'croutine', 'prvCheckPendingReadyList')
  104. self.assertEqual(entity_a, entity_b)
  105. def test_compare_none_vs_all(self):
  106. # Two entities might have the same specifity whether
  107. # Entity.ALL is used or not specified; the latter is
  108. # considered less than the former.
  109. entity_a = Entity()
  110. entity_b = Entity(Entity.ALL)
  111. self.assertLess(entity_a, entity_b)
  112. entity_a = Entity('libfreertos.a')
  113. entity_b = Entity('libfreertos.a', Entity.ALL, Entity.ALL)
  114. self.assertLess(entity_a, entity_b)
  115. entity_a = Entity('libfreertos.a', 'croutine')
  116. entity_b = Entity('libfreertos.a', 'croutine', Entity.ALL)
  117. self.assertLess(entity_a, entity_b)
  118. def test_compare_same_specificity(self):
  119. # Test that entities will be compared alphabetically
  120. # when the specificities are the same.
  121. entity_a = Entity('libfreertos_a.a')
  122. entity_b = Entity('libfreertos_b.a')
  123. self.assertLess(entity_a, entity_b)
  124. entity_a = Entity('libfreertos_b.a', 'croutine_a')
  125. entity_b = Entity('libfreertos_a.a', 'croutine_b')
  126. self.assertLess(entity_b, entity_a)
  127. entity_a = Entity('libfreertos.a', 'croutine', 'prvCheckPendingReadyList_a')
  128. entity_b = Entity('libfreertos.a', 'croutine', 'prvCheckPendingReadyList_b')
  129. self.assertLess(entity_a, entity_b)
  130. entity_a = Entity('libfreertos.a', 'croutine_b', 'prvCheckPendingReadyList_a')
  131. entity_b = Entity('libfreertos.a', 'croutine_a', 'prvCheckPendingReadyList_b')
  132. self.assertLess(entity_b, entity_a)
  133. entity_a = Entity('libfreertos_a.a', 'croutine_b', 'prvCheckPendingReadyList_a')
  134. entity_b = Entity('libfreertos_b.a', 'croutine_a', 'prvCheckPendingReadyList_b')
  135. self.assertLess(entity_a, entity_b)
  136. def test_compare_all_non_character(self):
  137. # Test that Entity.ALL is not treated as an
  138. # ordinary character in comparisons.
  139. entity_a = Entity(Entity.ALL)
  140. entity_b = Entity(chr(ord(Entity.ALL[0]) - 1))
  141. self.assertLess(entity_a, entity_b)
  142. entity_a = Entity('libfreertos.a', Entity.ALL)
  143. entity_b = Entity('libfreertos.a', chr(ord(Entity.ALL[0]) - 1))
  144. self.assertLess(entity_a, entity_b)
  145. entity_a = Entity('libfreertos.a', 'croutine', '*')
  146. entity_b = Entity('libfreertos.a', 'croutine', chr(ord(Entity.ALL[0]) - 1))
  147. self.assertLess(entity_a, entity_b)
  148. class EntityDBTest(unittest.TestCase):
  149. def setUp(self):
  150. self.entities = EntityDB()
  151. with open('data/test_entity/libfreertos.a.txt') as objdump:
  152. self.entities.add_sections_info(objdump)
  153. def test_get_archives(self):
  154. archives = self.entities.get_archives()
  155. self.assertEqual(set(archives), set(['libfreertos.a']))
  156. def test_get_objs(self):
  157. objs = self.entities.get_objects('libfreertos.a')
  158. self.assertEqual(set(objs), set(['croutine.S.obj', 'croutine.c.obj', 'croutine.cpp.obj', 'timers.o']))
  159. def test_get_sections(self):
  160. # Needs disambugation between possible matches: croutine.S, croutine.c, croutine.cpp
  161. with self.assertRaises(ValueError):
  162. self.entities.get_sections('libfreertos.a', 'croutine')
  163. # Test disambugation works
  164. sections = self.entities.get_sections('libfreertos.a', 'croutine.c')
  165. expected = set(['.literal.prvCheckPendingReadyList', '.literal.prvCheckDelayedList'])
  166. self.assertEqual(set(sections), expected)
  167. sections = self.entities.get_sections('libfreertos.a', 'croutine.S')
  168. expected = set(['.debug_frame', '.debug_info', '.debug_abbrev'])
  169. self.assertEqual(set(sections), expected)
  170. # Test .o extension works
  171. sections = self.entities.get_sections('libfreertos.a', 'timers')
  172. expected = set(['.literal.prvGetNextExpireTime', '.literal.prvInsertTimerInActiveList'])
  173. self.assertEqual(set(sections), expected)
  174. def test_parsing(self):
  175. # Tests parsing objdump with the following:
  176. #
  177. # - non-ascii characters
  178. # - different architecture string
  179. # - different column entries for each sections
  180. # - unexpected 'comments'
  181. with open('data/test_entity/parse_test.txt') as objdump:
  182. self.entities.add_sections_info(objdump)
  183. sections = self.entities.get_sections('ěščřžýáíé.a', 'croutine')
  184. self.assertEqual(set(sections), set(['.text', '.data', '.bss']))
  185. sections = self.entities.get_sections('ěščřžýáíé.a', 'FreeRTOS-ěščřžýáíé')
  186. self.assertEqual(set(sections), set(['.literal.ěščřžýáíé']))
  187. if __name__ == '__main__':
  188. unittest.main()