Skip to content

Commit 8f29437

Browse files
committed
Add: Qualcomm User Data Encryption test script & Document
- Checks for fscryptctl binary presence - Creates a random sw encryption key - Applies and verifies encryption policy - Confirms functionality with a test file - Added yaml config Signed-off-by: Bharani Bhuvanagiri <bbharani@qti.qualcomm.com>
1 parent 34562ed commit 8f29437

File tree

3 files changed

+336
-0
lines changed

3 files changed

+336
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
2+
SPDX-License-Identifier: BSD-3-Clause-Clear
3+
4+
# Qualcomm UserDataEncryption Functionality Test Script
5+
## Overview
6+
7+
The `UserDataEncryption` test script verifies basic filesystem encryption functionality. It generates a 64-byte key, adds it to the system, applies an encryption policy to a mount directory, and confirms the setup by creating and reading a test file. This ensures that key management and encryption policies work as expected.
8+
9+
## Features
10+
11+
- **Dependency Check**: Verifies the presence of the `fscryptctl` binary.
12+
- **Key Management**: Generates a 64-byte key and adds it to the filesystem.
13+
- **Encryption Policy**: Applies and verifies encryption policy on a mount directory.
14+
- **Functional Validation**: Creates and reads a test file to confirm encryption functionality.
15+
- **Automated Result Logging**: Outputs test results to a `.res` file for automated result collection.
16+
17+
## Prerequisites
18+
19+
Ensure the following components are present on the target device:
20+
21+
- `fscryptctl` binary available in `/data/`
22+
- Sufficient permissions to create and mount directories
23+
24+
## Directory Structure
25+
```
26+
Runner/
27+
├── suites/
28+
│ ├── Kernel/
29+
│ │ │ ├── baseport/
30+
│ │ │ │ ├── UserDataEncryption/
31+
│ │ │ │ │ ├── run.sh
32+
```
33+
## Usage
34+
35+
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 ```/<user-defined-location>``` directory on the target device.
36+
37+
2. Verify Transfer: Ensure that the repo have been successfully copied to the ```/<user-defined-location>``` directory on the target device.
38+
39+
3. Run Scripts: Navigate to the ```/<user-defined-location>``` directory on the target device and execute the scripts as needed.
40+
41+
---
42+
Quick Example
43+
```
44+
git clone <this-repo>
45+
cd <this-repo>
46+
scp -r common Runner user@target_device_ip:/<user-defined-location>
47+
ssh user@target_device_ip
48+
cd /<user-defined-location>/Runner && ./run-test.sh UserDataEncryption
49+
50+
Sample output:
51+
sh-5.2# ./run-test.sh UserDataEncryption
52+
[Executing test case: UserDataEncryption] 2025-11-18 15:03:05 -
53+
[INFO] Running as root. Continuing...
54+
[INFO] 2025-11-18 15:03:05 - -----------------------------------------------------------------------------------------
55+
[INFO] 2025-11-18 15:03:05 - -------------------Starting UserDataEncryption Testcase----------------------------
56+
[INFO] 2025-11-18 15:03:05 - === Test Initialization ===
57+
[INFO] 2025-11-18 15:03:05 - Checking if dependency binary is available
58+
[INFO] 2025-11-18 15:03:05 - Temporary key file created: /tmp/tmp.yV9Fgr7z7T
59+
[INFO] 2025-11-18 15:03:05 - Generating 64-byte encryption key
60+
[INFO] 2025-11-18 15:03:05 - Creating unique mount folder under /mnt
61+
[INFO] 2025-11-18 15:03:05 - Created unique mount directory: /mnt/testing.pb1KIn
62+
[INFO] 2025-11-18 15:03:05 - Derived filesystem mount point: /var
63+
[INFO] 2025-11-18 15:03:05 - Adding encryption key to the filesystem
64+
[INFO] 2025-11-18 15:03:05 - No relevant, non-benign errors for modules [fscrypt] in recent dmesg.
65+
[INFO] 2025-11-18 15:03:05 - Key ID: 80ae0d678e52fb126ea6eda7d32861eb
66+
[INFO] 2025-11-18 15:03:05 - Checking key status
67+
[INFO] 2025-11-18 15:03:05 - Key Status: Present (user_count=1, added_by_self)
68+
[INFO] 2025-11-18 15:03:05 - Setting encryption policy on /mnt/testing.pb1KIn
69+
[INFO] 2025-11-18 15:03:06 - No relevant, non-benign errors for modules [fscrypt] in recent dmesg.
70+
[INFO] 2025-11-18 15:03:06 - Verifying encryption policy
71+
[INFO] 2025-11-18 15:03:06 - No relevant, non-benign errors for modules [fscrypt] in recent dmesg.
72+
[INFO] 2025-11-18 15:03:06 - Policy verification successful: Master key identifier matches key_id
73+
[INFO] 2025-11-18 15:03:06 - Creating test file in encrypted directory
74+
[INFO] 2025-11-18 15:03:06 - Reading test file
75+
[PASS] 2025-11-18 15:03:06 - UserDataEncryption : Test Passed
76+
[INFO] 2025-11-18 15:03:06 - -------------------Completed UserDataEncryption Testcase----------------------------
77+
[INFO] 2025-11-18 15:03:06 - Cleaning up mount directory: /mnt/testing.pb1KIn
78+
[INFO] 2025-11-18 15:03:06 - Deleted test file: /mnt/testing.pb1KIn/file.txt
79+
[INFO] 2025-11-18 15:03:06 - Removed mount directory: /mnt/testing.pb1KIn
80+
[PASS] 2025-11-18 15:03:06 - UserDataEncryption passed
81+
82+
[INFO] 2025-11-18 15:03:06 - ========== Test Summary ==========
83+
PASSED:
84+
UserDataEncryption
85+
86+
FAILED:
87+
None
88+
89+
SKIPPED:
90+
None
91+
[INFO] 2025-11-18 15:03:06 - ==================================
92+
```
93+
4. Results will be available in the `/<user-defined-location>/Runner/suites/Kernel/baseport/UserDataEncryption/` directory.
94+
95+
## Notes
96+
97+
- The script uses /data/UserDataEncryption for all operations.
98+
- Temporary files such as the encryption key are cleaned up after the test.
99+
- If any test fails, the script logs the error and exits with a failure code.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
metadata:
2+
format: Lava-Test Test Definition 1.0
3+
name: UserDataEncryption
4+
description: "Validates fscrypt-based user data encryption on supported filesystems"
5+
maintainer:
6+
- bbharani@qti.qualcomm.com
7+
os:
8+
- openembedded
9+
scope:
10+
- functional
11+
devices:
12+
- rb3gen2
13+
- ridesx
14+
- lemans evk
15+
- monaco evk
16+
17+
run:
18+
steps:
19+
- cd Runner
20+
- $PWD/suites/Kernel/Baseport/UserDataEncryption/run.sh || true
21+
- $PWD/utils/send-to-lava.sh $PWD/suites/Kernel/Baseport/UserDataEncryption/UserDataEncryption.res || true
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#!/bin/sh
2+
3+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
4+
# SPDX-License-Identifier: BSD-3-Clause-Clear
5+
6+
# Ensure script runs as root
7+
if [ "$(id -u)" -ne 0 ]; then
8+
echo "[ERROR] This script must be run as root." >&2
9+
exit 0
10+
else
11+
echo "[INFO] Running as root. Continuing..."
12+
fi
13+
14+
# Robustly find and source init_env
15+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
16+
INIT_ENV=""
17+
SEARCH="$SCRIPT_DIR"
18+
while [ "$SEARCH" != "/" ]; do
19+
if [ -f "$SEARCH/init_env" ]; then
20+
INIT_ENV="$SEARCH/init_env"
21+
break
22+
fi
23+
SEARCH=$(dirname "$SEARCH")
24+
done
25+
26+
if [ -z "$INIT_ENV" ]; then
27+
echo "[ERROR] Could not find init_env (starting at $SCRIPT_DIR)" >&2
28+
exit 0
29+
fi
30+
31+
if [ -z "$__INIT_ENV_LOADED" ]; then
32+
# shellcheck disable=SC1090
33+
. "$INIT_ENV"
34+
fi
35+
36+
# shellcheck disable=SC1090,SC1091
37+
. "$TOOLS/functestlib.sh"
38+
39+
FSCRYPTCTL="${FSCRYPTCTL:-fscryptctl}"
40+
TESTNAME="UserDataEncryption"
41+
test_path=$(find_test_case_by_name "$TESTNAME")
42+
43+
if [ -z "$test_path" ]; then
44+
log_fail "Path not found for $TESTNAME test. Falling back to SCRIPT_DIR: $SCRIPT_DIR"
45+
test_path="$SCRIPT_DIR"
46+
47+
echo "$TESTNAME FAIL" > "$test_path/$TESTNAME.res"
48+
log_fail "$TESTNAME : Test case directory not found"
49+
exit 0
50+
fi
51+
52+
cd "$test_path" || {
53+
log_fail "Failed to change directory to $test_path"
54+
log_fail "Path not found for $TESTNAME test. Falling back to SCRIPT_DIR: $SCRIPT_DIR"
55+
test_path="$SCRIPT_DIR"
56+
echo "$TESTNAME FAIL" > "$test_path/$TESTNAME.res"
57+
exit 0
58+
}
59+
60+
res_file="./$TESTNAME.res"
61+
62+
# Globals that cleanup will use
63+
MOUNT_DIR=""
64+
FS_PATH=""
65+
key_id=""
66+
KEY_FILE=""
67+
68+
cleanup() {
69+
if [ -n "$MOUNT_DIR" ] && [ "$MOUNT_DIR" != "/" ]; then
70+
log_info "Cleaning up mount directory: $MOUNT_DIR"
71+
72+
if rm -f "$MOUNT_DIR/file.txt" 2>/dev/null; then
73+
log_info "Deleted test file: $MOUNT_DIR/file.txt"
74+
else
75+
log_warn "Failed to delete test file: $MOUNT_DIR/file.txt"
76+
fi
77+
78+
if rmdir "$MOUNT_DIR" 2>/dev/null; then
79+
log_info "Removed mount directory: $MOUNT_DIR"
80+
else
81+
log_warn "Failed to remove mount directory: $MOUNT_DIR"
82+
fi
83+
fi
84+
85+
if [ -n "$key_id" ]; then
86+
"$FSCRYPTCTL" remove_key "$key_id" "$FS_PATH" >/dev/null 2>&1 || true
87+
fi
88+
[ -n "$KEY_FILE" ] && rm -f "$KEY_FILE" 2>/dev/null || true
89+
}
90+
91+
# Run cleanup on normal exit, Ctrl-C, or SIGTERM
92+
trap cleanup EXIT INT TERM
93+
94+
log_info "-----------------------------------------------------------------------------------------"
95+
log_info "-------------------Starting $TESTNAME Testcase----------------------------"
96+
log_info "=== Test Initialization ==="
97+
98+
log_info "Checking if dependency binary is available"
99+
check_dependencies "$FSCRYPTCTL"
100+
101+
if ! command -v "$FSCRYPTCTL" >/dev/null 2>&1; then
102+
log_warn "$FSCRYPTCTL binary was not found. Skipping $TESTNAME."
103+
echo "$TESTNAME SKIP" > "$res_file"
104+
exit 0
105+
fi
106+
107+
# Create a secure temporary file for the key
108+
if KEY_FILE="$(mktemp)"; then
109+
log_info "Temporary key file created: $KEY_FILE"
110+
chmod 600 "$KEY_FILE"
111+
else
112+
log_fail "$TESTNAME : Failed to create temporary key file"
113+
echo "[ERROR] Failed to create temporary key file" >&2
114+
exit 0
115+
fi
116+
117+
# Step 1: Generate a 64-byte key
118+
log_info "Generating 64-byte encryption key"
119+
if ! head -c 64 /dev/urandom > "$KEY_FILE"; then
120+
log_fail "$TESTNAME : Failed to generate encryption key"
121+
echo "$TESTNAME FAIL" > "$res_file"
122+
exit 0
123+
fi
124+
125+
# Step 2: Create mount folder (this will create an unique folder under mnt)
126+
log_info "Creating unique mount folder under /mnt"
127+
MOUNT_DIR=$(mktemp -d /mnt/testing.XXXXXX)
128+
if [ ! -d "$MOUNT_DIR" ]; then
129+
log_fail "$TESTNAME : Failed to create mount directory"
130+
echo "$TESTNAME FAIL" > "$res_file"
131+
exit 0
132+
fi
133+
log_info "Created unique mount directory: $MOUNT_DIR"
134+
135+
136+
FS_PATH=$(df --output=target "$MOUNT_DIR" | tail -n 1)
137+
if [ -z "$FS_PATH" ]; then
138+
log_fail "$TESTNAME : Failed to determine filesystem mount point for $MOUNT_DIR"
139+
echo "$TESTNAME FAIL" > "$res_file"
140+
exit 0
141+
fi
142+
log_info "Derived filesystem mount point: $FS_PATH"
143+
144+
# Step 3: Add the key to the filesystem
145+
log_info "Adding encryption key to the filesystem"
146+
key_id=$("$FSCRYPTCTL" add_key "$FS_PATH" < "$KEY_FILE" 2>/dev/null)
147+
scan_dmesg_errors "$SCRIPT_DIR" "fscrypt" ""
148+
149+
if [ -z "$key_id" ]; then
150+
log_fail "$TESTNAME : Failed to add encryption key"
151+
echo "$TESTNAME FAIL" > "$res_file"
152+
exit 0
153+
fi
154+
155+
log_info "Key ID: $key_id"
156+
157+
# Step 4: Check key status
158+
log_info "Checking key status"
159+
status=$("$FSCRYPTCTL" key_status "$key_id" "$FS_PATH" 2>/dev/null)
160+
if [ -z "$status" ]; then
161+
log_fail "$TESTNAME : Failed to get key status"
162+
echo "$TESTNAME FAIL" > "$res_file"
163+
exit 0
164+
fi
165+
log_info "Key Status: $status"
166+
167+
if ! echo "$status" | grep -q "^Present"; then
168+
log_fail "$TESTNAME : Key is not usable (status: $status)"
169+
echo "$TESTNAME FAIL" > "$res_file"
170+
exit 0
171+
fi
172+
173+
174+
# Step 5: Set encryption policy
175+
log_info "Setting encryption policy on $MOUNT_DIR"
176+
177+
if ! "$FSCRYPTCTL" set_policy "$key_id" "$MOUNT_DIR"; then
178+
log_fail "$TESTNAME : Failed to set encryption policy"
179+
echo "$TESTNAME FAIL" > "$res_file"
180+
exit 0
181+
fi
182+
scan_dmesg_errors "$SCRIPT_DIR" "fscrypt" ""
183+
184+
185+
# Step 6: Verify policy
186+
log_info "Verifying encryption policy"
187+
policy_output=$("$FSCRYPTCTL" get_policy "$MOUNT_DIR" 2>/dev/null)
188+
scan_dmesg_errors "$SCRIPT_DIR" "fscrypt" ""
189+
190+
191+
policy_key=$(echo "$policy_output" | awk -F': ' '/Master key identifier/ {print $2}' | tr -d '[:space:]')
192+
193+
if [ "$policy_key" = "$key_id" ]; then
194+
log_info "Policy verification successful: Master key identifier matches key_id"
195+
else
196+
log_fail "$TESTNAME : Policy verification failed (expected $key_id, got $policy_key)"
197+
echo "$TESTNAME FAIL" > "$res_file"
198+
exit 0
199+
fi
200+
201+
# Step 7: Create and read a test file
202+
log_info "Creating test file in encrypted directory"
203+
echo "file" > "$MOUNT_DIR/file.txt"
204+
205+
log_info "Reading test file"
206+
file_content=$(cat "$MOUNT_DIR/file.txt")
207+
if [ "$file_content" = "file" ]; then
208+
log_pass "$TESTNAME : Test Passed"
209+
echo "$TESTNAME PASS" > "$res_file"
210+
else
211+
log_fail "$TESTNAME : Test Failed"
212+
echo "$TESTNAME FAIL" > "$res_file"
213+
exit 0
214+
fi
215+
216+
log_info "-------------------Completed $TESTNAME Testcase----------------------------"

0 commit comments

Comments
 (0)