1# Gabeldorsche Certification Tests
2
3[TOC]
4
5## What is GD cert test
6
7A core problem behind Bluetooth interoperability testing is testers' inability to
8automate test operations on certain peripherals. For example, although an user
9can automate API calls on Android with tools like
10[SL4A](https://android.googlesource.com/platform/external/sl4a/) or ADB shell
11commands, they often struggle with automating corresponding operations on
12carkits, headsets, and smart watches. Even if they find a way to automatically
13control one model of such peripheral device using device maker's tools or
14self-developed after-market tools such as relays and robotic arms, such kind of
15tool usually does not scale to other models of peripherals due to their
16differences in physical user interfaces.
17
18GD certification test framework comes into rescue. In
19the framework, a common API is defined using a [gRPC](https://grpc.io/) protobuf
20for profiles such as HFP, AVRCP and function groups like pairing, scanning, and
21advertising. Everyone who wants to test their Bluetooth device implements an
22**gRPC Facade Server** using this protobuf definition. This server executable is
23responsible in gluing various gRPC based APIs with their device specific
24automation hook through a **Device Specific Driver**. The server will then
25expose an IP address and a port number to the test runner. A **Test Runner**
26process will load and execute the actual test case and use the auto-generated
27gRPC facade client APIs to interact with the device being tested. The following
28diagram shows how the system could be configured:
29
30![cert_test_architecture_drawing](./cert_test_architecture_drawing.png)
31
32## Terminology
33
34**gRPC Facade**
35:   A set of automation APIs of a specific profile (HFP, AVRCP, etc) or function
36    group (pairing, scanning, advertising, etc) defined in gRPC protobuf format.
37
38**Test Runner**
39:   A process that loads and runs the actual test cases and use auto-generated
40    gRPC facade client library to interact with test devices. Currently, the
41    preferred test runner is the [Android ACTS](https://android.googlesource.com/platform/tools/test/connectivity/+/refs/heads/master/acts/)
42    framework and test cases are written in Python for fast iteration and easy debugging
43
44**gRPC Facade Server**
45:   A concrete implementation of the gRPC automation APIs, which will convert
46    RPC requests into invocations of device specific automation hook using a
47    **Device Specific Driver**. This server can be written in any language and
48    on any platform. The server need to expose an IP address and 3 ports
49
50**Tester Signal Port**
51:   A port to indicate that the server is ready for operation to the **Test
52    Runner**
53
54**gRPC Root Server Port**
55:   A port that allows **Test Runner** to start and stop various functional
56    facade services
57
58**gRPC Facade Server Port**
59:   A port that allows **Test Runner** to interact with actual profile
60    implementations on the test device
61
62**Device Specific Driver**
63:   A library or some mechanism that allows the **gRPC Facade Server** to
64    control the test device. This library is opaque to the **Test Runner** that
65    should have no knowledge of how this component works * **Root-Canal**: A
66    Bluetooth PHY emulator that takes either LMP or HCI packets. The goal is for
67    this emulator to simulate a Physical RF environment without using any real
68    Bluetooth adatper.
69
70## How to run GD cert test in Android tree
71
72Assume user has an Android checkout and finished `source build/envsetup.sh` and
73`lunch` to a preferred target
74
75### Run GD cert tests on host machine
76
77```shell
78$ANDROID_BUILD_TOP/packages/modules/Bluetooth/system/gd/cert/run
79```
80
81### Run GD cert tests on devices for the first time
82
83Connect at least two Android devices and follow on-screen instructions after
84running the following command
85
86```shell
87$ANDROID_BUILD_TOP/packages/modules/Bluetooth/system/gd/cert/set_up_and_run_device_cert.sh
88```
89
90### Run GD cert tests on devices for the second time and after
91
92Keeping the same set of devices connected
93
94```shell
95$ANDROID_BUILD_TOP/packages/modules/Bluetooth/system/gd/cert/run
96```
97
98### `packages/modules/Bluetooth/system/gd/cert/run` command reference
99
100*   `--clean`: Remove any test setup files and do a clean test run
101*   `--repeat=N`: Repeat running the same set of tests N times without redoing
102    test setup
103*   `--test_file=<file_name>`: Running only tests listed in `<file_name>`
104*   `--test_filter=<test_filter>`: Test case filter in the format of
105    "TestClass:test_case_name", for multiple test cases, quote them using " and
106    separate each filter by space such as "TestClass1:test_case_1
107    TestClass2:test_case_2"
108
109### Run GD cert tests on devices over SSH
110
111The following assumptions assume the following configuration
112
113*   Local setup: Linux or MAC laptop with two Android phones connected
114*   Remote setup: Linux or MAC workstation
115
1161.  Check if your ADB version is up to date on both host machine and remote
117    workstation. Both versions should be 29.0.3 or above. If not, uninstall and
118    reinstall your adb.
119
1201.  Enable SSH port forwarding for both ADB port and various ports used by our
121    tests, run the following command on your Mac, assuming the following
122    configuration {value=2}
123
124    *   ADB Port: 5037
125    *   Cert Device:
126        *   gRPC Root Server Port: 8896
127        *   gRPC Facade Port: 8898
128        *   Signal Port: 8894
129    *   Device Under Test:
130        *   gRPC Root Server Port: 8897
131        *   gRPC Facade Port: 8899
132        *   Signal Port: 8895
133
134    Among these ports, ADB Port needs to be forwarded from local machine to
135    listen on remote workstation so that its adb client can connect to local
136    machine's adb server (`ssh -R`). Signal Port needs to be forwarded from
137    remote workstation to listen on local machine so that local phone can
138    connect to Test Runner listening on this port during Facade Server bring up
139    (`ssh -L`). Both gRPC Root Server Port and gRPC Facade Port need to be
140    forwarded from local machine to listen on remote workstation so that Test
141    Runner can connect to these ports on the Facade Server (`ssh -R`).
142
143    Hence, the resulting `ssh` command is:
144
145    ```shell
146    ssh -R 5037:127.0.0.1:5037 -R 6401:127.0.0.1:6401 -R 6402:127.0.0.1:6402 \
147    -R 6403:127.0.0.1:6403 -L 8894:127.0.0.1:8894 -L 8895:127.0.0.1:8895 \
148    -R 8896:127.0.0.1:8896 -R 8897:127.0.0.1:8897 -R 8898:127.0.0.1:8898 \
149    -R 8899:127.0.0.1:8899 <host_name>
150    ```
151
1521.  Connect all your devices, open a different terminal on your Mac and run
153
154    ```shell
155    adb kill-server && adb devices
156    ```
157
158    You should see devices on your local machine shows up
159
1601.  SSH into your remote workstation and run
161
162    ```shell
163    adb kill-server && adb devices
164    ```
165
166    You should see same set of devices connected to your local machine
167
1681.  Continue with device setup
169
170    ```shell
171    $ANDROID_BUILD_TOP/packages/modules/Bluetooth/system/gd/cert/set_up_and_run_device_cert.sh
172    ```
173
1741.  In subsequent runs
175
176    ```shell
177    $ANDROID_BUILD_TOP/packages/modules/Bluetooth/system/gd/cert/run
178    ```
179
180## How to debug GD cert tests
181
182Logs are produced and saved whenever GD cert tests runs. Depending on how the
183tests were run, the log root is different
184
185When running tests on local Android checkout, logs or most recent run are stored
186at
187
188*   /tmp/logs/HostOnlyCert/latest
189
190Navigate test logs
191
192In test root, the following logs are available:
193
194*   In every directory layer:
195    *   test_run_debug.txt: DEBUG level output from Python logging API
196        scoped at their respective layer based on the directory
197*   In test root directory:
198    *   test_summary.json: A summary test results, including stack traces
199        for any failures and metadata
200    *   test_configs.json: The ACTs config used to run this test
201    *   GdDevice_cert_stack_backing_process_coverage_summary.txt: code
202        coverage summary from llvm-cov
203*   In test class directory:
204    *   rootcanal_logs.txt: Root-Canal stdout and stderrr, host test only
205    *   Cert stack logs:
206        *   GdDevice_cert_stack_backing_logs.txt: facade server process log
207        *   cert_stack_btsnoop_hci.log: HCI packet log on cert device
208        *   cert_stack_system_log: Android phone logcat output, device based
209            test only
210    *   Device under test logs:
211        *   GdDevice_stack_under_test_backing_logs.txt: facade server
212            process log
213        *   stack_under_test_btsnoop_hci.log: HCI packet log on cert device
214        *   stack_under_test_system_log: Android phone logcat output, device
215            based test only
216    *   Individual test directories: logs for individual test cases
217
218## PTS test case coverage
219
220A Python decorator is used to indicate a test's association with a Bluetooth SIG
221Profile Tuning Suite (PTS) Qualification test. For example
222
223```python
224@metadata(
225    pts_test_id="L2CAP/EXF/BV-01-C",
226    pts_test_name="Extended Features Information Response for "
227    "Enhanced Retransmission Mode")
228```
229
230These information can be found at the Test Case Reference List (TCRL) document
231in
232[Qualification Test Requirements](https://www.bluetooth.com/specifications/qualification-test-requirements/)
233page at the Bluetooth SIG website.
234
235## Code coverage
236
237If the facade server binary is compiled using [`clang`](https://clang.llvm.org/)
238and with
239[`-fprofile-instr-generate -fcoverage-mapping`](https://clang.llvm.org/docs/SourceBasedCodeCoverage.html)
240flags. A `.profraw` file will be generated in ech test class log directory after
241the test run. The Test Runner will then try to index these profiling data using
242[`llvm-profdata`](https://llvm.org/docs/CommandGuide/llvm-profdata.html),
243iteratively merge them from each test class, and generate a line-by-line test
244coverage report using
245[`llvm-cov`](https://llvm.org/docs/CommandGuide/llvm-cov.html).
246