# Test SDK Integration with Self Test

To help with integration, the SDK has an optional Self Test component. The self test is a quick and easy way to test that various components of your integration with the Memfault SDK are functioning as expected.

The Self Test component contains the following tests:

* Device Info
* Component Boot
* Coredump Region
* Data Export
* Reboot Reason
* Time
* Coredump Storage Capacity
* Coredump Storage Operation

## Component Dependencies[​](#component-dependencies "Direct link to Component Dependencies")

We recommend running the test via a CLI command. This can be integrated through your device's existing shell/console or by using the [Demo CLI](/docs/mcu/demo-cli.md) component of the SDK. If this is not available, you can call the self test directly with `memfault_self_test_run()` passing the desired test flags. See [`include/memfault/self_test.h`](https://github.com/memfault/memfault-firmware-sdk/blob/5a16c1d5c8b21e04e5c0c6b42db257d00ec35a59/components/include/memfault/core/self_test.h#L39) for more details.

## Optional Platform Dependencies[​](#optional-platform-dependencies "Direct link to Optional Platform Dependencies")

To successfully run all of the tests within the Self Test, there are a few functions that need to be implemented for your platform. If these functions are not implemented, these tests will fail.

### Time Test Requirements[​](#time-test-requirements "Direct link to Time Test Requirements")

* `memfault_self_test_platform_delay_ms()`: A function to delay the CPU by the specified number of milliseconds.

### Coredump Storage Test Requirements[​](#coredump-storage-test-requirements "Direct link to Coredump Storage Test Requirements")

* `memfault_self_test_platform_disable_irqs()`: A function to disable any interrupts from peripherals or an RTOS.
* `memfault_self_test_platform_enable_irqs()`: A function to enable interrupts from peripherals or an RTOS.

For more info on these functions, check out [`include/memfault/self_test.h`](https://github.com/memfault/memfault-firmware-sdk/blob/5a16c1d5c8b21e04e5c0c6b42db257d00ec35a59/components/include/memfault/core/self_test.h#L52)

## Enabling The Self Test Component[​](#enabling-the-self-test-component "Direct link to Enabling The Self Test Component")

First we'll need to enable the component so it is built with our application.

* Zephyr
* ESP-IDF
* Demo CLI

1. Set `CONFIG_MEMFAULT_SHELL_SELF_TEST=y` in your `prj.conf` file.
2. Rebuild your application with `west build`

1) Set `CONFIG_MEMFAULT_CLI_SELF_TEST=y` in your `sdkconfig.default` file.
2) Clean your generated `sdkconfig`, and rebuild with `idf.py build`

1. Add `#define MEMFAULT_DEMO_CLI_SELF_TEST 1` to your `memfault_platform_config.h` file.
2. Rebuild your application.

## Running The Self Test[​](#running-the-self-test "Direct link to Running The Self Test")

The Self Test has 3 modes of operation controlled by the arguments passed to the command. These modes are:

* Default Mode: Running the command with no arguments
* Reboot Reason Mode: Running the command with `reboot` and `reboot_verify`
* Coredump Storage Mode: Running the command with `coredump_storage`

## Default Mode[​](#default-mode "Direct link to Default Mode")

The default mode is used when running the Self Test without any additional arguments. This mode will run the following tests:

* Device Info
* Component Boot
* Data Export
* Time
* Coredump Region
* Coredump Storage Capacity

- Zephyr
- ESP-IDF
- Demo CLI

1. Run the command `mflt test self`.

1) Run the command `memfault_self_test`.

1. Run the command `self_test`.

Check the output from each of the tests for the results. For more info on each test, see their respective sections below.

### Device Info Test[​](#device-info-test "Direct link to Device Info Test")

This test validates the info returned by your implementation of `memfault_platform_get_device_info()`. The test performs these checks:

* Build ID validation
* NULL field checks
* Field value validation

If there are any problems, the test will show a failure and where it occurred.

#### Example Success Output[​](#device-info-success "Direct link to Example Success Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Device Info Test

MFLT:[INFO] =============================

MFLT:[INFO] All fields valid

MFLT:[INFO] =============================
```

#### Example Failure Output[​](#device-info-failure "Direct link to Example Failure Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Device Info Test

MFLT:[INFO] =============================

MFLT:[ERRO] Invalid char |, found in ||freertos-example

MFLT:[ERRO] One or more fields is invalid. Check for correct length and contents

MFLT:[ERRO] Device Serial invalid

MFLT:[INFO] =============================
```

### Component Boot Test[​](#component-boot-test "Direct link to Component Boot Test")

This test checks for that each component of the SDK has it's respective `_boot()` function called before the test runs. The components checked by this test are:

* Event Storage
* Logging
* Reboot Reason Tracking
* Trace Event

This test always checks for these components, so check the output for the components that are utilized by your integration.

note

It is safe to ignore any lines resulting from unused components

#### Example Success Output[​](#component-boot-success "Direct link to Example Success Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Component Boot Test

MFLT:[INFO] =============================

MFLT:[INFO] Component       | Booted?|

MFLT:[INFO] -----------------------------

MFLT:[INFO] All components booted

MFLT:[INFO] =============================
```

#### Example Failure Output[​](#component-boot-failure "Direct link to Example Failure Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Component Boot Test

MFLT:[INFO] =============================

MFLT:[INFO] Component       | Booted?|

MFLT:[INFO] -----------------------------

MFLT:[ERRO] Trace Event     |      no|

MFLT:[INFO] =============================
```

### Coredump Regions Test[​](#coredump-regions-test "Direct link to Coredump Regions Test")

This test prints out information on the coredump regions that will be collected when a fault or assert occurs. The test will print out the region's starting address, length, type. The regions will be grouped into Platform Regions, Arch Regions, and SDK Regions. Platform Regions are collected by your implementation of `memfault_platform_coredump_get_regions()`. Arch Regions are collected by the SDK for your device's architecture (e.g. ARM, Xtensa, etc). SDK Regions are collected for SDK features like logging or heap tracing.

note

Depending on your implementation, some regions may or may not be present. Use the output to check for expected regions and their size

#### Example Output[​](#coredump-regions-output "Direct link to Example Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Coredump Regions Test

MFLT:[INFO] =============================

MFLT:[INFO] Coredump group: Platform Regions

MFLT:[INFO] -----------------------------

MFLT:[INFO]    Address|    Length|  Type|

MFLT:[INFO] -----------------------------

MFLT:[INFO] 0x20006adc|       512|     0|

MFLT:[INFO] 0x20006ab8|       640|     0|

MFLT:[INFO] 0x200001a0|       828|     0|

MFLT:[INFO] 0x20006b8c|       100|     0|

MFLT:[INFO] 0x2000789c|       100|     0|

MFLT:[INFO] 0x2000635c|       100|     0|

MFLT:[INFO] 0x200004dc|       100|     0|

MFLT:[INFO] 0x2000053c|       100|     0|

MFLT:[INFO] 0x20006ff0|       256|     0|

MFLT:[INFO] 0x20007d38|       256|     0|

MFLT:[INFO] 0x20006b30|       256|     0|

MFLT:[INFO] 0x20002490|       256|     0|

MFLT:[INFO] 0x200062f0|       256|     0|

MFLT:[INFO] 0x200093ac|        64|     0|

MFLT:[INFO] 0x200093ec|        64|     0|

MFLT:[INFO] -----------------------------

MFLT:[INFO] Coredump group: Arch Regions

MFLT:[INFO] -----------------------------

MFLT:[INFO]    Address|    Length|  Type|

MFLT:[INFO] -----------------------------

MFLT:[INFO] 0xe000ed24|        28|     1|

MFLT:[INFO] 0xe000ed18|        12|     1|

MFLT:[INFO] 0xe000e004|        16|     1|

MFLT:[INFO] 0xe000ed04|         8|     1|

MFLT:[INFO] 0xe000edfc|         4|     1|

MFLT:[INFO] 0xe000e100|         4|     1|

MFLT:[INFO] 0xe000e200|         4|     1|

MFLT:[INFO] 0xe000e300|         4|     1|

MFLT:[INFO] 0xe000e400|        32|     1|

MFLT:[INFO] -----------------------------

MFLT:[INFO] Coredump group: SDK Regions

MFLT:[INFO] -----------------------------

MFLT:[INFO]    Address|    Length|  Type|

MFLT:[INFO] -----------------------------

MFLT:[INFO] 0x20009184|        40|     0|

MFLT:[INFO] 0x200081ac|       512|     0|

MFLT:[INFO] 0x2000010c|        16|     0|

MFLT:[INFO] 0x20008f84|       512|     0|

MFLT:[INFO] -----------------------------

MFLT:[INFO] =============================
```

### Data Export Test[​](#data-export-test "Direct link to Data Export Test")

This test helps verify that any chunks sent to the console will not be broken by newline characters. Chunks containing newline characters will fail to process.

#### Example Success Output[​](#data-export-success "Direct link to Example Success Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Data Export Line Test

MFLT:[INFO] =============================

MFLT:[INFO] Printing 112 characters, confirm line ends with '1' and is not split

MFLT:[INFO] +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++1

MFLT:[INFO] =============================
```

#### Example Failure Output[​](#data-export-failure "Direct link to Example Failure Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Data Export Line Test

MFLT:[INFO] =============================

MFLT:[INFO] Printing 112 characters, confirm line ends with '1' and is not split

MFLT:[INFO] ++++++++++++++++++++++++++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++1

MFLT:[INFO] =============================
```

### Time Test[​](#time-test "Direct link to Time Test")

This test verifies the implementation of two platform functions, `memfault_platform_time_get_current()` and `memfault_platform_get_time_since_boot_ms()`.

#### Example Success Output[​](#time-test-success "Direct link to Example Success Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Time Test

MFLT:[INFO] =============================

MFLT:[INFO] Time since boot test succeeded

MFLT:[INFO] Verify received timestamp for accuracy. Timestamp received 1709186585

MFLT:[INFO] =============================
```

#### Example Failure Output[​](#time-test-failure "Direct link to Example Failure Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Time Test

MFLT:[INFO] =============================

MFLT:[ERRO] Time since boot not monotonically increasing: start[1930] vs end[1930]

MFLT:[ERRO] Current timestamp could not be recovered

MFLT:[INFO] =============================
```

### Coredump Storage Capacity[​](#coredump-storage-capacity-test "Direct link to Coredump Storage Capacity")

This test checks to see if the storage allocated for a coredump is large enough to handle a worst-case coredump size based on the regions to be collected.

#### Example Success Output[​](#coredump-storage-capacity-success "Direct link to Example Success Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Coredump Storage Capacity Test

MFLT:[INFO] =============================

MFLT:[INFO] =============================
```

#### Example Failure Output[​](#coredump-storage-capacity-failure "Direct link to Example Failure Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Coredump Storage Capacity Test

MFLT:[INFO] =============================

MFLT:[ERRO] Coredump storage is 512B but need 5688B

MFLT:[INFO] =============================
```

## Reboot Reason Mode[​](#reboot-reason-mode "Direct link to Reboot Reason Mode")

The Reboot Reason mode tests the reboot reason tracking implementation on the device. This test first reboots the device with a known reboot reason. After the device reboots, a second test is run to verify the reason. To run this mode:

* Zephyr
* ESP-IDF
* Demo CLI

1. Run the command `mflt test self reboot`.
2. Wait for the device to reboot.
3. Run the command `mflt test self reboot_verify`.

1) Run the command `memfault_self_test reboot`.
2) Wait for the device to reboot.
3) Run the command `memfault_self_test reboot_verify`.

1. Run the command `self_test reboot`.
2. Wait for the device to reboot.
3. Run the command `self_test reboot_verify`.

### Example Test Start Output[​](#reboot-reason-start "Direct link to Example Test Start Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Reboot Reason Test

MFLT:[INFO] =============================

MFLT:[INFO] This test will now reboot the device to test reboot reason tracking

MFLT:[INFO] After the device reboots, please run with reboot_verify argument

MFLT:[INFO] =============================
```

### Example Success Output[​](#reboot-reason-success "Direct link to Example Success Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Reboot Reason Test

MFLT:[INFO] =============================

MFLT:[INFO] Reboot reason test successful

MFLT:[INFO] =============================
```

### Example Failure Output[​](#reboot-reason-failure "Direct link to Example Failure Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Reboot Reason Test

MFLT:[INFO] =============================

MFLT:[ERRO] Reboot reason test failed:

MFLT:[ERRO] get_reboot_reason result: 0, prior_stored_reason: 0x00008001

MFLT:[INFO] =============================
```

## Coredump Storage Mode \[Experimental][​](#coredump-storage-mode "Direct link to Coredump Storage Mode \[Experimental]")

The Coredump Storage mode will run the experimental Coredump Storage test. The test exercises the `memfault_platform_coredump_storage` functions to validate the coredump storage operations. The test will perform erases, writes, and reads across the entire coredump storage space. If there is a coredump present, the test will not proceed.

info

This test is experimental due to the requirements that interrupts be disabled before running and enabled after completion. This may require additional modifications such as disabling watchdogs.

* Zephyr
* ESP-IDF
* Demo CLI

1. Set `CONFIG_MEMFAULT_SHELL_SELF_TEST_COREDUMP_STORAGE=y` in your `prj.conf` file and rebuild.
2. Run the command `mflt test self coredump_storage`.

1) Set `CONFIG_MEMFAULT_CLI_SELF_TEST_COREDUMP_STORAGE=y` in your `sdkconfig.default` file and rebuild.
2) Run the command `memfault_self_test coredump_storage`.

1. Add the line `#define MEMFAULT_DEMO_CLI_SELF_TEST_COREDUMP_STORAGE 1` to your `memfault_platform_config.h` file and rebuild.
2. Run the command `self_test coredump_storage`.

### Example Success Output[​](#coredump-storage-success "Direct link to Example Success Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Coredump Storage Test

MFLT:[INFO] =============================

MFLT:[INFO] Coredump Storage Verification Passed

MFLT:[INFO] =============================
```

### Example Failure Output[​](#coredump-storage-failure "Direct link to Example Failure Output")

```
MFLT:[INFO] =============================

MFLT:[INFO] Coredump Storage Test

MFLT:[INFO] =============================

MFLT:[ERRO] Coredump Storage Verification Failed

MFLT:[ERRO] Read pattern mismatch during memfault_platform_coredump_storage_write() test

MFLT:[ERRO] Storage offset: 0x00000000, write size: 12

MFLT:[ERRO] Expected: a0a1a2a3a4a5a6a7a8a9aaab

MFLT:[ERRO] Actual  : 000000000000000000000000

MFLT:[INFO] =============================
```
