test_newlogging.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #! /usr/bin/env python
  2. # Copyright 2025 Google LLC
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://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,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # -*- coding: utf8 -*-
  16. """
  17. Tests for pebble.loghashing.newlogging
  18. """
  19. from pebble.loghashing.newlogging import dehash_line, dehash_line_unformatted
  20. from pebble.loghashing.dehashing import dehash_line as legacy_dehash_line
  21. import os
  22. test_log_dict = {'43': {'file': '../src/fw/activity/activity.c',
  23. 'line': '804',
  24. 'level': '200',
  25. 'color': 'YELLOW',
  26. 'msg': 'activity tracking started'},
  27. '114': {'file': '../src/fw/driver/ispp.c',
  28. 'line': '1872',
  29. 'level': '0',
  30. 'color': 'RED',
  31. 'msg': 'Start Authentication Process %d (%x) %s'},
  32. '214': {'file': 'pointer_print.c',
  33. 'line': '1872',
  34. 'level': '0',
  35. 'color': 'RED',
  36. 'msg': 'My address is %p %p'},
  37. '64856': {'color': 'GREY',
  38. 'file': '../src/fw/services/common/clock.c',
  39. 'level': '200',
  40. 'line': '768',
  41. 'msg': 'Changed timezone to id %u, gmtoff is %ld'},
  42. '100000': {'color': 'GREY',
  43. 'file': '../src/fw/services/common/string.c',
  44. 'level': '200',
  45. 'line': '111',
  46. 'msg': 'string 1 %s, string 2 %s'},
  47. '11082': {'color': 'GREY',
  48. 'file': '../src/fw/resource/resource_storage.c',
  49. 'level': '50',
  50. 'line': '120',
  51. 'msg': '0x%lx != 0x%lx'},
  52. '1073741824': {'color': 'GREY',
  53. 'file': 'hc_protocol.c',
  54. 'level': '0',
  55. 'line': '69',
  56. 'msg': 'Init BLE SPI Protocol'},
  57. 'new_logging_version': 'NL0101'
  58. }
  59. def test_dehash_line():
  60. """
  61. Test for dehash_line()
  62. """
  63. # Console Line - No arguments
  64. line = "? A 21:35:14.375 :0> NL:{:x}".format(43)
  65. assert ("D A 21:35:14.375 activity.c:804> activity tracking started" ==
  66. dehash_line(line, test_log_dict))
  67. # Console Line - Arguments
  68. line = "? A 21:35:14.375 :0> NL:{:x} a a `Success`".format(114)
  69. assert ("* A 21:35:14.375 ispp.c:1872> Start Authentication Process 10 (a) Success" ==
  70. dehash_line(line, test_log_dict))
  71. # Support Line - No arguments
  72. line = "2015-09-05 02:16:16:000GMT :0> NL:{:x}".format(43)
  73. assert ("2015-09-05 02:16:16:000GMT activity.c:804> activity tracking started" ==
  74. dehash_line(line, test_log_dict))
  75. # Support Line - Arguments
  76. line = "2015-09-05 02:16:19:000GMT :0> NL:{:x} 10 10 `Success`".format(114)
  77. assert ("2015-09-05 02:16:19:000GMT ispp.c:1872> Start Authentication Process 16 (10) Success" ==
  78. dehash_line(line, test_log_dict))
  79. # App Log
  80. line = "D A 21:35:14.375 file.c:0> This is an app debug line"
  81. assert (line == dehash_line(line, test_log_dict))
  82. # Pointer format conversion
  83. line = "2015-09-05 02:16:19:000GMT :0> NL:{:x} 164 1FfF".format(214)
  84. assert ("2015-09-05 02:16:19:000GMT pointer_print.c:1872> My address is 164 1fff" ==
  85. dehash_line(line, test_log_dict))
  86. # Two's compliment negative value
  87. line = "2015-09-05 02:16:19:000GMT :0> NL:{:x} 10 ffff8170".format(64856)
  88. assert ("2015-09-05 02:16:19:000GMT clock.c:768> Changed timezone to id 16, gmtoff is -32400" ==
  89. dehash_line(line, test_log_dict))
  90. # Two's compliment negative value
  91. line = "2015-09-05 02:16:19:000GMT :0> NL:{:x} 9AEBC155 43073997".format(11082)
  92. assert ("2015-09-05 02:16:19:000GMT resource_storage.c:120> 0x9aebc155 != 0x43073997" ==
  93. dehash_line(line, test_log_dict))
  94. # Empty string parameter - 1
  95. line = "? A 21:35:14.375 :0> NL:{:x} `` `string`".format(100000)
  96. assert ("D A 21:35:14.375 string.c:111> string 1 , string 2 string" ==
  97. dehash_line(line, test_log_dict))
  98. # Empty string parameter - 2 - trailing space
  99. line = "? A 21:35:14.375 :0> NL:{:x} `string` `` ".format(100000)
  100. assert ("D A 21:35:14.375 string.c:111> string 1 string, string 2 " ==
  101. dehash_line(line, test_log_dict))
  102. # Empty string parameter - 2 - no trailing space
  103. line = "? A 21:35:14.375 :0> NL:{:x} `string` ``".format(100000)
  104. assert ("D A 21:35:14.375 string.c:111> string 1 string, string 2 " ==
  105. dehash_line(line, test_log_dict))
  106. # Missing closing `
  107. line = "? A 21:35:14.375 :0> NL:{:x} `string` `string".format(100000)
  108. assert ("D A 21:35:14.375 string.c:111> string 1 string, string 2 string" ==
  109. dehash_line(line, test_log_dict))
  110. def test_dehash_invalid_parameters():
  111. """
  112. Tests for invalid number of parameters
  113. """
  114. # Not enough parameters
  115. line = "2015-09-05 02:16:19:000GMT :0> NL:{:x} 164".format(214)
  116. assert ("2015-09-05 02:16:19:000GMT pointer_print.c:1872> :0> NL:d6 164 " \
  117. "----> ERROR: not enough arguments for format string" ==
  118. dehash_line(line, test_log_dict))
  119. # Too many parameters
  120. line = "2015-09-05 02:16:19:000GMT :0> NL:{:x} 164 1FfF 17".format(214)
  121. assert ("2015-09-05 02:16:19:000GMT pointer_print.c:1872> :0> NL:d6 164 1FfF 17 " \
  122. "----> ERROR: not all arguments converted during string formatting" ==
  123. dehash_line(line, test_log_dict))
  124. # Unterminated string (last `)
  125. line = "2015-09-05 02:16:19:000GMT :0> NL:{:x} 10 10 `Success".format(114)
  126. assert ("2015-09-05 02:16:19:000GMT ispp.c:1872> Start Authentication Process 16 (10) Success" ==
  127. dehash_line(line, test_log_dict))
  128. # Unterminated string (first `)
  129. line = "2015-09-05 02:16:19:000GMT :0> NL:{:x} 10 10 Success`".format(114)
  130. assert ("2015-09-05 02:16:19:000GMT ispp.c:1872> Start Authentication Process 16 (10) Success" ==
  131. dehash_line(line, test_log_dict))
  132. # Unterminated string (No `s)
  133. line = "2015-09-05 02:16:19:000GMT :0> NL:{:x} 10 10 Success".format(114)
  134. assert ("2015-09-05 02:16:19:000GMT ispp.c:1872> Start Authentication Process 16 (10) Success" ==
  135. dehash_line(line, test_log_dict))
  136. # Invalid hex character
  137. line = "2015-09-05 02:16:19:000GMT :0> NL:{:x} 10 1q0 Success".format(114)
  138. assert ("2015-09-05 02:16:19:000GMT ispp.c:1872> :0> NL:72 10 1q0 Success " \
  139. "----> ERROR: %x format: a number is required, not str" ==
  140. dehash_line(line, test_log_dict))
  141. # Unicode
  142. line = "? A 21:35:14.375 :0> NL:{:x} `unicode` `Pebble β`".format(100000)
  143. assert ("D A 21:35:14.375 string.c:111> string 1 unicode, string 2 Pebble β" ==
  144. dehash_line(line, test_log_dict))
  145. def test_legacy_dehash_line():
  146. """
  147. Test legacy dehash_line()
  148. """
  149. # Console Line - No arguments
  150. line = "? A 21:35:14.375 :0> NL:{:x}".format(43)
  151. assert ("D A 21:35:14.375 activity.c:804> activity tracking started" ==
  152. legacy_dehash_line(line, test_log_dict))
  153. def test_unformatted():
  154. """
  155. Test dehash_line_unformatted()
  156. """
  157. line = "? A 21:35:14.375 :0> NL:{:x} a a `Success`".format(114)
  158. line_dict = dehash_line_unformatted(line, test_log_dict)
  159. assert (line_dict['level'] == "0")
  160. assert (line_dict['task'] == "A")
  161. assert (line_dict['time'] == "21:35:14.375")
  162. assert (os.path.basename(line_dict['file']) == "ispp.c")
  163. assert (line_dict['line'] == "1872")
  164. assert (line_dict['formatted_msg'] == "Start Authentication Process 10 (a) Success")
  165. def test_core_number():
  166. """
  167. Test core number decoding
  168. """
  169. # Core number 0
  170. line = "? A 21:35:14.375 :0> NL:{:x} a a `Success`".format(114)
  171. line_dict = dehash_line_unformatted(line, test_log_dict)
  172. assert (line_dict['core_number'] == "0")
  173. # Core number 1
  174. line = "? A 21:35:14.375 :0> NL:{:x}".format(1073741824)
  175. line_dict = dehash_line_unformatted(line, test_log_dict)
  176. assert (line_dict['core_number'] == "1")
  177. def test_ble_decode():
  178. """
  179. Test BLE decode.
  180. timedate.now() is used, so ignore the date/time
  181. """
  182. line = ":0> NL:{:x}".format(1073741824)
  183. line_dict = dehash_line_unformatted(line, test_log_dict)
  184. assert (line_dict['level'] == "0")
  185. assert (line_dict['task'] == "-")
  186. assert (os.path.basename(line_dict['file']) == "hc_protocol.c")
  187. assert (line_dict['line'] == "69")
  188. assert (line_dict['formatted_msg'] == "Init BLE SPI Protocol")