Skip to content

Commit 2a72d43

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 Signed-off-by: Bharani Bhuvanagiri <bbharani@qti.qualcomm.com>
1 parent 7c59ffe commit 2a72d43

File tree

2 files changed

+229
-0
lines changed

2 files changed

+229
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
│ │ ├── FunctionalArea/
30+
│ │ │ ├── baseport/
31+
│ │ │ │ ├── UserDataEncryption/
32+
│ │ │ │ │ ├── run.sh
33+
```
34+
## Usage
35+
36+
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.
37+
38+
2. Verify Transfer: Ensure that the repo have been successfully copied to the ```/<user-defined-location>``` directory on the target device.
39+
40+
3. Run Scripts: Navigate to the ```/<user-defined-location>``` directory on the target device and execute the scripts as needed.
41+
42+
---
43+
Quick Example
44+
```
45+
git clone <this-repo>
46+
cd <this-repo>
47+
scp -r common Runner user@target_device_ip:/<user-defined-location>
48+
ssh user@target_device_ip
49+
cd /<user-defined-location>/Runner && ./run-test.sh UserDataEncryption
50+
51+
Sample output:
52+
sh-5.2# ./run-test.sh UserDataEncryption
53+
[Executing test case: UserDataEncryption] 1970-01-11 18:09:02 -
54+
[INFO] 1970-01-11 18:09:02 - -----------------------------------------------------------------------------------------
55+
[INFO] 1970-01-11 18:09:02 - -------------------Starting UserDataEncryption Testcase----------------------------
56+
[INFO] 1970-01-11 18:09:02 - === Test Initialization ===
57+
[INFO] 1970-01-11 18:09:02 - Checking if dependency binary is available
58+
[INFO] 1970-01-11 18:09:02 - Generating 64-byte encryption key
59+
[INFO] 1970-01-11 18:09:02 - Creating mount folder at /mnt/testing
60+
[INFO] 1970-01-11 18:09:02 - /mnt/testing already exists. Deleting it first.
61+
[INFO] 1970-01-11 18:09:02 - Adding encryption key to the filesystem
62+
[INFO] 1970-01-11 18:09:02 - Key ID: a17eee9a6d74585b6703b54285e95894
63+
[INFO] 1970-01-11 18:09:02 - Checking key status
64+
[INFO] 1970-01-11 18:09:02 - Key Status: Present (user_count=1, added_by_self)
65+
[INFO] 1970-01-11 18:09:02 - Setting encryption policy on /mnt/testing
66+
[INFO] 1970-01-11 18:09:02 - Verifying encryption policy
67+
[INFO] 1970-01-11 18:09:02 - Policy verification successful
68+
[INFO] 1970-01-11 18:09:02 - Creating test file in encrypted directory
69+
[INFO] 1970-01-11 18:09:02 - Reading test file
70+
[PASS] 1970-01-11 18:09:02 - UserDataEncryption : Test Passed
71+
[INFO] 1970-01-11 18:09:02 - -------------------Completed UserDataEncryption Testcase----------------------------
72+
[PASS] 1970-01-11 18:09:02 - UserDataEncryption passed
73+
74+
[INFO] 1970-01-11 18:09:02 - ========== Test Summary ==========
75+
PASSED:
76+
UserDataEncryption
77+
78+
FAILED:
79+
None
80+
81+
SKIPPED:
82+
None
83+
[INFO] 1970-01-11 18:09:02 - ==================================
84+
```
85+
4. Results will be available in the `/<user-defined-location>/Runner/suites/Kernel/FunctionalArea/baseport/UserDataEncryption/` directory.
86+
87+
## Notes
88+
89+
- The script uses /data/UserDataEncryption for all operations.
90+
- Temporary files such as the encryption key are cleaned up after the test.
91+
- If any test fails, the script logs the error and exits with a failure code.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#!/bin/sh
2+
3+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
4+
# SPDX-License-Identifier: BSD-3-Clause-Clear
5+
6+
# Robustly find and source init_env
7+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
8+
INIT_ENV=""
9+
SEARCH="$SCRIPT_DIR"
10+
while [ "$SEARCH" != "/" ]; do
11+
if [ -f "$SEARCH/init_env" ]; then
12+
INIT_ENV="$SEARCH/init_env"
13+
break
14+
fi
15+
SEARCH=$(dirname "$SEARCH")
16+
done
17+
18+
if [ -z "$INIT_ENV" ]; then
19+
echo "[ERROR] Could not find init_env (starting at $SCRIPT_DIR)" >&2
20+
exit 1
21+
fi
22+
23+
if [ -z "$__INIT_ENV_LOADED" ]; then
24+
# shellcheck disable=SC1090
25+
. "$INIT_ENV"
26+
fi
27+
28+
# shellcheck disable=SC1090,SC1091
29+
. "$TOOLS/functestlib.sh"
30+
31+
TESTNAME="UserDataEncryption"
32+
test_path=$(find_test_case_by_name "$TESTNAME")
33+
cd "$test_path" || exit 1
34+
res_file="./$TESTNAME.res"
35+
36+
log_info "-----------------------------------------------------------------------------------------"
37+
log_info "-------------------Starting $TESTNAME Testcase----------------------------"
38+
log_info "=== Test Initialization ==="
39+
40+
log_info "Checking if dependency binary is available"
41+
check_dependencies fscryptctl
42+
43+
KEY_FILE="/data/std_key"
44+
MOUNT_DIR="/mnt/testing"
45+
46+
# Step 1: Generate a 64-byte key
47+
log_info "Generating 64-byte encryption key"
48+
if ! head -c 64 /dev/urandom > "$KEY_FILE"; then
49+
log_fail "$TESTNAME : Failed to generate encryption key"
50+
echo "$TESTNAME FAIL" > "$res_file"
51+
exit 1
52+
fi
53+
54+
# Step 2: Create mount folder
55+
log_info "Creating mount folder at $MOUNT_DIR"
56+
if [ -d "$MOUNT_DIR" ]; then
57+
log_info "$MOUNT_DIR already exists. Deleting it first."
58+
if ! rm -rf "$MOUNT_DIR"; then
59+
log_fail "$TESTNAME : Failed to delete existing mount directory"
60+
echo "$TESTNAME FAIL" > "$res_file"
61+
rm -f "$KEY_FILE"
62+
exit 1
63+
fi
64+
fi
65+
66+
if ! mkdir -p "$MOUNT_DIR"; then
67+
log_fail "$TESTNAME : Failed to create mount directory"
68+
echo "$TESTNAME FAIL" > "$res_file"
69+
rm -f "$KEY_FILE"
70+
exit 1
71+
fi
72+
73+
# Step 3: Add the key to the filesystem
74+
log_info "Adding encryption key to the filesystem"
75+
key_id=$(/data/fscryptctl add_key /mnt < "$KEY_FILE" 2>/dev/null)
76+
if [ -z "$key_id" ]; then
77+
log_fail "$TESTNAME : Failed to add encryption key"
78+
echo "$TESTNAME FAIL" > "$res_file"
79+
rm -f "$KEY_FILE"
80+
exit 1
81+
fi
82+
83+
log_info "Key ID: $key_id"
84+
85+
# Step 4: Check key status
86+
log_info "Checking key status"
87+
status=$(/data/fscryptctl key_status "$key_id" / 2>/dev/null)
88+
if [ -z "$status" ]; then
89+
log_fail "$TESTNAME : Failed to get key status"
90+
echo "$TESTNAME FAIL" > "$res_file"
91+
rm -f "$KEY_FILE"
92+
exit 1
93+
fi
94+
log_info "Key Status: $status"
95+
96+
# Step 5: Set encryption policy
97+
log_info "Setting encryption policy on $MOUNT_DIR"
98+
if ! /data/fscryptctl set_policy "$key_id" "$MOUNT_DIR"; then
99+
log_fail "$TESTNAME : Failed to set encryption policy"
100+
echo "$TESTNAME FAIL" > "$res_file"
101+
rm -f "$KEY_FILE"
102+
exit 1
103+
fi
104+
105+
# Step 6: Verify policy
106+
log_info "Verifying encryption policy"
107+
policy_output=$(/data/fscryptctl get_policy "$MOUNT_DIR" 2>/dev/null)
108+
if echo "$policy_output" | grep -q "$key_id"; then
109+
log_info "Policy verification successful"
110+
else
111+
log_fail "$TESTNAME : Policy verification failed"
112+
echo "$TESTNAME FAIL" > "$res_file"
113+
rm -f "$KEY_FILE"
114+
exit 1
115+
fi
116+
117+
# Step 7: Create and read a test file
118+
log_info "Creating test file in encrypted directory"
119+
echo "file" > "$MOUNT_DIR/file.txt"
120+
121+
log_info "Reading test file"
122+
file_content=$(cat "$MOUNT_DIR/file.txt")
123+
if [ "$file_content" = "file" ]; then
124+
log_pass "$TESTNAME : Test Passed"
125+
echo "$TESTNAME PASS" > "$res_file"
126+
else
127+
log_fail "$TESTNAME : Test Failed"
128+
echo "$TESTNAME FAIL" > "$res_file"
129+
rm -f "$KEY_FILE"
130+
exit 1
131+
fi
132+
133+
# Cleanup
134+
rm -f "$KEY_FILE"
135+
rm -f "$MOUNT_DIR/file.txt"
136+
rmdir "$MOUNT_DIR"
137+
138+
log_info "-------------------Completed $TESTNAME Testcase----------------------------"

0 commit comments

Comments
 (0)