Skip to content

Add: test script for validating Qualcomm HWRNG functionality #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause-Clear

# Qualcomm Hardware Random Number Generator (QRNG) Script
# Overview

The qcom_hwrng test script validates Qualcomm Hardware Random Number Generator (HWRNG) basic functionality. This test ensures that the HWRNG kernel driver is correctly integrated and functional.

## Features

- Driver Validation: Confirms the presence and correct configuration of the qcom_hwrng kernel driver.
- Dependency Check: Verifies the availability of required tools like rngtest before execution.
- Automated Result Logging: Outputs test results to a .res file for automated result collection.
- Remote Execution Ready: Supports remote deployment and execution via scp and ssh.

## Prerequisites

Ensure the following components are present in the target:

- `rngtest` (Binary Available in /usr/bin) - this test app can be compiled from https://github.com/cernekee/rng-tools/

## Directory Structure
```
Runner/
├── suites/
│ ├── Kernel/
│ │ ├── FunctionalArea/
│ │ │ ├── baseport/
│ │ │ │ ├── qcom_hwrng/
│ │ │ │ │ ├── run.sh
```
## Usage

1. Copy repo to Target Device: Use scp to transfer the scripts from the host to the target device. The scripts should be copied to the /var directory on the target device.

2. Verify Transfer: Ensure that the repo have been successfully copied to the /var directory on the target device.

3. Run Scripts: Navigate to the /var directory on the target device and execute the scripts as needed.

---
Quick Example
```
git clone <this-repo>
cd <this-repo>
scp -r common Runner user@target_device_ip:/var
ssh user@target_device_ip
cd /var/Runner && ./run-test.sh qcom_hwrng

Sample output:
sh-5.2# ./run-test.sh qcom_hwrng
[Executing test case: /var/Runner/suites/Kernel/FunctionalArea/baseport/qcom_hwrng] 2025-05-16 06:08:41 -
[INFO] 2025-05-16 06:08:41 - -----------------------------------------------------------------------------------------
[INFO] 2025-05-16 06:08:41 - -------------------Starting qcom_hwrng Testcase----------------------------
[INFO] 2025-05-16 06:08:41 - qcom_hwrng successfully set as the current RNG source.
[INFO] 2025-05-16 06:08:41 - Checking if dependency binary is available
[PASS] 2025-05-16 06:08:41 - Test related dependencies are present.
cat: write error: Broken pipe
[PASS] 2025-05-16 06:08:41 - qcom_hwrng : Test Passed
[INFO] 2025-05-16 06:08:41 - -------------------Completed qcom_hwrng Testcase----------------------------
```
4. Results will be available in the `/var/Runner/suites/Kernel/FunctionalArea/baseport/qcom_hwrng/` directory.

## Notes

- The script sets qcom_hwrng as the primary hwrng.
- It validates Qualcomm Hardware Random Number Generator (HWRNG) basic functionality.
- If any critical tool is missing, the script exits with an error message.
74 changes: 74 additions & 0 deletions Runner/suites/Kernel/FunctionalArea/baseport/qcom_hwrng/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/bin/sh

# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause-Clear

# Robustly find and source init_env
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
INIT_ENV=""
SEARCH="$SCRIPT_DIR"
while [ "$SEARCH" != "/" ]; do
if [ -f "$SEARCH/init_env" ]; then
INIT_ENV="$SEARCH/init_env"
break
fi
SEARCH=$(dirname "$SEARCH")
done

if [ -z "$INIT_ENV" ]; then
echo "[ERROR] Could not find init_env (starting at $SCRIPT_DIR)" >&2
exit 1
fi

# Only source if not already loaded (idempotent)
if [ -z "$__INIT_ENV_LOADED" ]; then
# shellcheck disable=SC1090
. "$INIT_ENV"
fi
# Always source functestlib.sh, using $TOOLS exported by init_env
# shellcheck disable=SC1090,SC1091
. "$TOOLS/functestlib.sh"

TESTNAME="qcom_hwrng"
test_path=$(find_test_case_by_name "$TESTNAME")
cd "$test_path" || exit 1

log_info "-----------------------------------------------------------------------------------------"
log_info "-------------------Starting $TESTNAME Testcase----------------------------"

# Set the hardware RNG source to Qualcomm's RNG
if [ -e /sys/class/misc/hw_random/rng_current ]; then
echo qcom_hwrng > /sys/class/misc/hw_random/rng_current
else
echo "Path /sys/class/misc/hw_random/rng_current does not exist."
log_fail "$TESTNAME : Test Failed"
exit 1
fi

# Verify that qcom_hwrng was successfully set
current_rng=$(cat /sys/class/misc/hw_random/rng_current)
if [ "$current_rng" != "qcom_hwrng" ]; then
log_info "Error: Failed to set qcom_hwrng as the current RNG source."
log_fail "$TESTNAME : Test Failed"
exit 1
else
log_info "qcom_hwrng successfully set as the current RNG source."
fi

log_info "Checking if dependency binary is available"
check_dependencies rngtest

cat /dev/random | rngtest -c 1000 > /tmp/qcom_hwrng_output.txt 2>&1
Copy link
Contributor

@smuppand smuppand Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of using cat. Its better to use dd. Also its better to use current folder to save the log file for post processing.


grep 'FIPS 140-2 failures' /tmp/qcom_hwrng_output.txt | awk '{print $NF}' > /tmp/rngtest_failures.txt

value=$(cat /tmp/rngtest_failures.txt)

if [ "$value" -lt 10 ]; then
log_pass "$TESTNAME : Test Passed"
echo "$TESTNAME PASS" > $test_path/$TESTNAME.res
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For effectiveness, better add exit 0

else
log_fail "$TESTNAME : Test Failed"
echo "$TESTNAME FAIL" > $test_path/$TESTNAME.res
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For effectiveness, better add exit 1

fi
log_info "-------------------Completed $TESTNAME Testcase----------------------------"
Loading