1#!/usr/bin/env python3 2# Copyright 2024 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"""Tests for snapshot processing.""" 16 17import base64 18import unittest 19from pw_snapshot.processor import process_snapshot 20 21_RISCV_EXPECTED_SNAPSHOT = """ 22 ____ _ __ _____ _ _____ ____ _____ __ ______ ______ 23 / __ \\ | / / / ___// | / / | / __ \\/ ___// / / / __ \\/_ __/ 24 / /_/ / | /| / / \\__ \\/ |/ / /| | / /_/ /\\__ \\/ /_/ / / / / / / 25 / ____/| |/ |/ / ___/ / /| / ___ |/ ____/___/ / __ / /_/ / / / 26 /_/ |__/|__/____/____/_/ |_/_/ |_/_/ /____/_/ /_/\\____/ /_/ 27 /_____/ 28 29 30Snapshot capture reason: 31 Example Reason 32 33Reason token: 0x6d617845 34Project name: example_project 35Device: hyper-fast-gshoe 36CPU Arch: RV32I 37Device FW version: gShoe-debug-1.2.1-6f23412b+ 38Snapshot UUID: 00000001 39 40All registers: 41mepc 0x20000001 42mcause 0x20000002 43mstatus 0x20000003 44""" 45 46_ARM_EXPECTED_SNAPSHOT = """ 47 ____ _ __ _____ _ _____ ____ _____ __ ______ ______ 48 / __ \\ | / / / ___// | / / | / __ \\/ ___// / / / __ \\/_ __/ 49 / /_/ / | /| / / \\__ \\/ |/ / /| | / /_/ /\\__ \\/ /_/ / / / / / / 50 / ____/| |/ |/ / ___/ / /| / ___ |/ ____/___/ / __ / /_/ / / / 51 /_/ |__/|__/____/____/_/ |_/_/ |_/_/ /____/_/ /_/\\____/ /_/ 52 /_____/ 53 54 55Snapshot capture reason: 56 Example Reason 57 58Reason token: 0x6d617845 59Project name: example_project 60Device: hyper-fast-gshoe 61CPU Arch: ARMV7M 62Device FW version: gShoe-debug-1.2.1-6f23412b+ 63Snapshot UUID: 00000001 64 65Exception caused by a unknown exception. 66 67No active Crash Fault Status Register (CFSR) fields. 68 69All registers: 70pc 0x20000001 71lr 0x20000002 72psr 0x20000003 73""" 74 75 76class ProcessorTest(unittest.TestCase): 77 """Tests that the metadata processor produces expected results.""" 78 79 def test_riscv_process_snapshot(self): 80 """Test processing snapshot of a RISCV CPU""" 81 82 snapshot = base64.b64decode( 83 "ggFYCg5FeGFtcGxlIFJlYXNvbhoPZXhhbXBs" 84 "ZV9wcm9qZWN0IhtnU2hvZS1kZWJ1Zy0xLjIu" 85 "MS02ZjIzNDEyYisyEGh5cGVyLWZhc3QtZ3No" 86 "b2U6BAAAAAFABaIBEgiBgICAAhCCgICAAhiD" 87 "gICAAg==" 88 ) 89 90 output = process_snapshot(snapshot) 91 self.assertEqual(output, _RISCV_EXPECTED_SNAPSHOT) 92 93 def test_cortexm_process_snapshot(self): 94 """Test processing snapshot of a ARM CPU""" 95 96 snapshot = base64.b64decode( 97 "ggFYCg5FeGFtcGxlIFJlYXNvbhoPZXhhbXBsZV9wc" 98 "m9qZWN0IhtnU2hvZS1kZWJ1Zy0xLjIuMS02ZjIzND" 99 "EyYisyEGh5cGVyLWZhc3QtZ3Nob2U6BAAAAAFAAqI" 100 "BEgiBgICAAhCCgICAAhiDgICAAg==" 101 ) 102 103 output = process_snapshot(snapshot) 104 self.assertEqual(output, _ARM_EXPECTED_SNAPSHOT) 105 106 107if __name__ == '__main__': 108 unittest.main() 109