Skip to content

Commit 82f67a7

Browse files
committed
Conclude unit testing section
1 parent 9ebb0e9 commit 82f67a7

File tree

5 files changed

+160
-44
lines changed

5 files changed

+160
-44
lines changed

ProjectAnalysisReport.md

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,78 @@ Dakle, ima smisla udubiti se u kod i razmisljati na drugi nacin, odnosno ne samo
191191

192192
Kao sto smo vec spomenuli, mozemo se fokusirati na nekoliko funkcija za koje smo vec napisali testove. Za ovo cemo napraviti novi folder channel_sounding_behavior u koji cemo smestiti spomenuta prosirenja. Pogledajmo prvo `bt_le_cs_security_enable` funkciju i njeno ocekivano ponasanje. Funkcija pravi bafer HCI komande, popunjava ga sa pokazivacem na konekciju i salje komandu sinhrono. Vec smo pokrili tzv. happy path, odnosno slucaj u kome je sve kao sto ocekujemo. Uz to, testirali smo i slucaj u kome `bt_hci_cmd_create` funkcija vrati nepravilan bafer (NULL). Slucajevi koje bismo jos mogli pokriti jesu kada se kao pokazivac na konekciju prosledi NULL vrednost kao i slucaj kada `bt_hci_cmd_send_sync` funkcija pukne. Ova dva slucaja su pokrivena testovima `test_sec_enable_hci_cmd_fail_null_conn` i `test_sec_enable_cmd_send_fail`. Na slican nacun su dodati i sledeci testovi:
193193

194-
TODO(avra): pobrojati testove i dodati kratak opis (mozda tabela)
195-
* `test_read_remote_supported_capabilities_complete_null_buf` - Provera ponasanja funkcije sa prosledjenim nevalidnim baferom (Ocekivano ponasanje - vraca gresku)
196-
* `test_read_remote_supported_capabilities_complete_invalid_buf_len` - Provera ponasanja funkcije sa pogresnom velicinom ulaznog bafera (Ocekivano ponasanje - funkcija se zavrsava ranim izlaskom)
197-
* `test_read_remote_supported_capabilities_complete_evt_status_fail` - Provera ponasanja funkcije kada `bt_conn_lookup_handle` vrati dogadjaj sa statusom greske (Ocekivano ponasanje - funkcija se zavrsava ranim izlaskom)
198-
* `test_read_remote_supported_capabilities_complete_conn_lookup_handle_fail` - Provera ponasanja funkcije kada `bt_conn_lookup_handle` vrati NULL pokazivac (Ocekivano ponasanje - funkcija se zavrsava ranim izlaskom)
199-
* `test_read_remote_fae_table_hci_cmd_fail` - Provera ponasanja funkcije kada
200-
* `test_read_remote_fae_table_send_sync_failed` -
201-
194+
### Testovi za `bt_le_cs_security_enable`
195+
196+
1. **`test_sec_enable_hci_cmd_fail`**
197+
* Provera ponašanja kada `bt_hci_cmd_create` ne uspe (vrati NULL)
198+
* Očekivano: Vraća `-ENOBUFS`
199+
***NAPOMENA**: Test je prolazio i pre izmena
200+
201+
2. **`test_sec_enable_hci_cmd_fail_null_conn`**
202+
* Provera ponašanja sa NULL parametrom za konekciju
203+
* Očekivano: Vraća `-ENOBUFS`
204+
***NAPOMENA**: Test je prolazio i pre izmena
205+
206+
3. **`test_sec_enable_cmd_send_fail`**
207+
* Provera ponašanja kada `bt_hci_cmd_send_sync` ne uspe
208+
* Očekivano: Vraća grešku sa sinhronizacije
209+
***NAPOMENA**: Test je prolazio i pre izmena
210+
211+
### Testovi za `bt_hci_le_cs_read_remote_supported_capabilities_complete`
212+
213+
4. **`test_read_remote_supported_capabilities_complete_null_buf`**
214+
* Provera ponašanja sa NULL baferom
215+
* Očekivano: Prekid bez operacija
216+
***NAPOMENA**: Test je prolazio i pre izmena
217+
218+
5. **`test_read_remote_supported_capabilities_complete_invalid_buf_len`**
219+
* Provera ponašanja sa nevalidnom dužinom bafera
220+
* Očekivano: Prekid izvršavanja
221+
* :warning: **NAPOMENA**: Test nije prolazio pre izmena (nedostajala je provera dužine)
222+
223+
6. **`test_read_remote_supported_capabilities_complete_evt_status_fail`**
224+
* Provera ponašanja kada status događaja pokazuje grešku
225+
* Očekivano: Prekid izvršavanja
226+
***NAPOMENA**: Test je prolazio i pre izmena
227+
228+
7. **`test_read_remote_supported_capabilities_complete_conn_lookup_handle_fail`**
229+
* Provera ponašanja kada pretraga konekcije ne uspe
230+
* Očekivano: Prekid izvršavanja
231+
* :warning: **NAPOMENA**: Test nije prolazio pre izmena (nedostajala je provera za NULL)
232+
233+
### Testovi za `bt_le_cs_read_remote_fae_table`
234+
235+
8. **`test_read_remote_fae_table_hci_cmd_fail`**
236+
* Provera ponašanja kada kreiranje HCI komande ne uspe
237+
* Očekivano: Vraća `-ENOBUFS`
238+
***NAPOMENA**: Test je prolazio i pre izmena
239+
240+
9. **`test_read_remote_fae_table_null_conn`**
241+
* Provera ponašanja sa NULL parametrom za konekciju
242+
* Očekivano: Vraća `-EFAULT`
243+
***NAPOMENA**: Test je prolazio i pre izmena
244+
245+
10. **`test_read_remote_fae_table_send_sync_failed`**
246+
* Provera ponašanja kada sinhronizacija komande ne uspe
247+
* Očekivano: Vraća grešku sa sinhronizacije
248+
***NAPOMENA**: Test je prolazio i pre izmena
249+
250+
### Testovi za `bt_hci_le_cs_read_remote_fae_table_complete`
251+
252+
11. **`test_bt_hci_le_cs_read_remote_fae_table_complete_invalid_buf_size_fail`**
253+
* Provera ponašanja sa nevalidnom veličinom bafera
254+
* Očekivano: Prekid izvršavanja
255+
* :warning: **NAPOMENA**: Test nije prolazio pre izmena (nedostajala provera dužine)
256+
257+
12. **`test_bt_hci_le_cs_read_remote_fae_table_complete_evt_status_fail`**
258+
* Provera ponašanja kada status događaja pokazuje grešku
259+
* Očekivano: Prekid izvršavanja
260+
***NAPOMENA**: Test je prolazio i pre izmena
261+
262+
13. **`test_bt_hci_le_cs_read_remote_fae_table_complete_conn_lookup_fail`**
263+
* Provera ponašanja kada pretraga konekcije ne uspe
264+
* Očekivano: Prekid izvršavanja
265+
* :warning: **NAPOMENA**: Test nije prolazio pre izmena (nedostajala provera za NULL)
202266

203267
_Napomena_: kod funkcija koje se zavrsavaju ranije ocekujemo da neke podfunkcije nikada nece biti pozvane. Ovaj uslov eksplicitno ukljucujemo u ocekivanja testa pomocu `zasser_` funkcija.
204268

@@ -326,9 +390,9 @@ PROJECT EXECUTION FAILED
326390
327391
Ovog puta nismo dobili veliki broj palih testova, ali dobijamo indikaciju da mozda treba obratiti paznju na baratanje neispravnim ulaznim vrednostima u funkcije.
328392
329-
Vidimo da nije svuda rigorozno proveravano da li se prosledjuju ispravne vrednosti, tako da cemo tu proveru dodati u izvorni kod i ponovo pokrenuti testove. Izmene koje su dodate ticu se uglavnom provere ulaznih argumenata funkcija (TODO dodati ako je jos nesto nadjeno) i mogu se pogledati u <patch_name.patch>. Nakon primene izmena, ponovo je pokrenuta skripta i vidimo da sada svi testovi prolaze. Mozemo nastaviti sa generisanjem izvestaja pokrivenosti za ispravljene testove. Krajnji rezultati su prikazani na slici ispod:
393+
Vidimo da nije svuda rigorozno proveravano da li se prosledjuju ispravne vrednosti, tako da cemo tu proveru dodati u izvorni kod i ponovo pokrenuti testove. Izmene koje su dodate ticu se uglavnom provere ulaznih argumenata funkcija i mogu se pogledati u `cs_error_handling.patch`. Nakon primene izmena, ponovo je pokrenuta skripta i vidimo da sada svi testovi prolaze. Mozemo nastaviti sa generisanjem izvestaja pokrivenosti za ispravljene testove. Krajnji rezultati pokrivenosti su prikazani na slici ispod:
330394
331-
TODO: slika
395+
![Final unit test coverage](images/final_ut_coverage.png)
332396
333397
#### Integration tests
334398

images/final_ut_coverage.png

410 KB
Loading

patches/cs_error_handling.patch

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
diff --git a/subsys/bluetooth/host/cs.c b/subsys/bluetooth/host/cs.c
2+
index da2480739d1..8a87be8bfef 100644
3+
--- a/subsys/bluetooth/host/cs.c
4+
+++ b/subsys/bluetooth/host/cs.c
5+
@@ -297,6 +297,11 @@ void bt_hci_le_cs_read_remote_supported_capabilities_complete(struct net_buf *bu
6+
struct bt_conn_le_cs_capabilities remote_cs_capabilities;
7+
struct bt_hci_evt_le_cs_read_remote_supported_capabilities_complete *evt;
8+
9+
+ if (!buf) {
10+
+ LOG_ERR("Input buffer is NULL");
11+
+ return;
12+
+ }
13+
+
14+
if (buf->len < sizeof(*evt)) {
15+
LOG_ERR("Unexpected end of buffer");
16+
return;
17+
@@ -446,6 +451,11 @@ int bt_le_cs_read_remote_fae_table(struct bt_conn *conn)
18+
}
19+
20+
cp = net_buf_add(buf, sizeof(*cp));
21+
+
22+
+ if (!cp) {
23+
+ return -EFAULT;
24+
+ }
25+
+
26+
cp->handle = sys_cpu_to_le16(conn->handle);
27+
28+
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_READ_REMOTE_FAE_TABLE, buf, NULL);
29+
@@ -463,6 +473,12 @@ void bt_hci_le_cs_read_remote_fae_table_complete(struct net_buf *buf)
30+
}
31+
32+
evt = net_buf_pull_mem(buf, sizeof(*evt));
33+
+
34+
+ if(!evt) {
35+
+ LOG_ERR("Could not pull memory for evt");
36+
+ return;
37+
+ }
38+
+
39+
if (evt->status) {
40+
LOG_WRN("Read Remote FAE Table failed with status 0x%02X", evt->status);
41+
return;
42+
@@ -900,6 +916,10 @@ int bt_le_cs_security_enable(struct bt_conn *conn)
43+
struct bt_hci_cp_le_security_enable *cp;
44+
struct net_buf *buf;
45+
46+
+ if (!conn) {
47+
+ return -ENOBUFS;
48+
+ }
49+
+
50+
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CS_SECURITY_ENABLE, sizeof(*cp));
51+
if (!buf) {
52+
return -ENOBUFS;

testing/unit/channel_sounding_behavior/src/error_handling.c

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ ZTEST(channel_sounding_tests_error_handling, test_sec_enable_hci_cmd_fail_null_c
6565
zassert_equal(net_buf_simple_add_fake.call_count, 0);
6666
}
6767

68-
ZTEST(channel_sounding_tests, test_sec_enable_cmd_send_fail)
68+
ZTEST(channel_sounding_tests_error_handling, test_sec_enable_cmd_send_fail)
6969
{
7070
// [Given]
7171
// Assign the mock structure to test connection
@@ -107,17 +107,17 @@ ZTEST(channel_sounding_tests, test_sec_enable_cmd_send_fail)
107107
zassert_equal(bt_hci_cmd_send_sync_fake.call_count, 1);
108108
}
109109

110-
// ZTEST(channel_sounding_tests_error_handling, test_read_remote_supported_capabilities_complete_null_buf)
111-
// {
112-
// // Function under test
113-
// bt_hci_le_cs_read_remote_supported_capabilities_complete(NULL);
110+
ZTEST(channel_sounding_tests_error_handling, test_read_remote_supported_capabilities_complete_null_buf)
111+
{
112+
// Function under test
113+
bt_hci_le_cs_read_remote_supported_capabilities_complete(NULL);
114114

115-
// // Assertions
116-
// zassert_equal(net_buf_simple_pull_mem_fake.call_count, 0, "net_buf_simple_pull_mem unexpectedly called!");
117-
// zassert_equal(bt_conn_lookup_handle_fake.call_count, 0, "bt_conn_lookup_handle unexpectedly called!");
118-
// zassert_equal(notify_remote_cs_capabilities_fake.call_count, 0, "notify_remote_cs_capabilities unexpectedly called!");
119-
// zassert_equal(bt_conn_unref_fake.call_count, 0, "bt_conn_unref unexpectedly called!");
120-
// }
115+
// Assertions
116+
zassert_equal(net_buf_simple_pull_mem_fake.call_count, 0, "net_buf_simple_pull_mem unexpectedly called!");
117+
zassert_equal(bt_conn_lookup_handle_fake.call_count, 0, "bt_conn_lookup_handle unexpectedly called!");
118+
zassert_equal(notify_remote_cs_capabilities_fake.call_count, 0, "notify_remote_cs_capabilities unexpectedly called!");
119+
zassert_equal(bt_conn_unref_fake.call_count, 0, "bt_conn_unref unexpectedly called!");
120+
}
121121

122122
ZTEST(channel_sounding_tests_error_handling, test_read_remote_supported_capabilities_complete_invalid_buf_len){
123123
// Setup
@@ -279,32 +279,32 @@ ZTEST(channel_sounding_tests_error_handling, test_read_remote_fae_table_hci_cmd_
279279
zassert_equal_ptr(net_buf_simple_add_fake.call_count, 0, "net_buf_simple_add called more times than expected!");
280280
}
281281

282-
// ZTEST(channel_sounding_tests_error_handling, test_read_remote_fae_table_null_conn)
283-
// {
284-
// // Setup
285-
// struct bt_conn *test_conn = &test_conn_mock;
286-
// // struct net_buf buf;
287-
288-
// static uint8_t test_data[256];
289-
// static struct net_buf test_buf = {.b = {
290-
// .data = test_data,
291-
// .len = 0,
292-
// .size = sizeof(test_data),
293-
// }};
282+
ZTEST(channel_sounding_tests_error_handling, test_read_remote_fae_table_null_conn)
283+
{
284+
// Setup
285+
struct bt_conn *test_conn = &test_conn_mock;
286+
// struct net_buf buf;
287+
288+
static uint8_t test_data[256];
289+
static struct net_buf test_buf = {.b = {
290+
.data = test_data,
291+
.len = 0,
292+
.size = sizeof(test_data),
293+
}};
294294

295-
// bt_hci_cmd_create_fake.return_val = &test_buf;
295+
bt_hci_cmd_create_fake.return_val = &test_buf;
296296

297-
// // Replace generic mock implementation with stubbed function
298-
// net_buf_simple_add_fake.return_val= NULL;
297+
// Replace generic mock implementation with stubbed function
298+
net_buf_simple_add_fake.return_val= NULL;
299299

300-
// // Test
301-
// int ret = bt_le_cs_read_remote_fae_table(test_conn);
300+
// Function under test
301+
int ret = bt_le_cs_read_remote_fae_table(test_conn);
302302

303-
// // Assertions
304-
// zassert_equal(ret, 0, "Function failed");
305-
// zassert_equal(bt_hci_cmd_create_fake.call_count, 1, "bt_hci_cmd_create not called");
306-
// zassert_equal(bt_hci_cmd_send_sync_fake.call_count, 0, "bt_hci_cmd_send_sync called more times than expected!");
307-
// }
303+
// Assertions
304+
zassert_equal(ret, -EFAULT, "Function failed");
305+
zassert_equal(bt_hci_cmd_create_fake.call_count, 1, "bt_hci_cmd_create not called");
306+
zassert_equal(bt_hci_cmd_send_sync_fake.call_count, 0, "bt_hci_cmd_send_sync called more times than expected!");
307+
}
308308

309309
ZTEST(channel_sounding_tests_error_handling, test_read_remote_fae_table_send_sync_failed)
310310
{

testing/unit/channel_sounding_behavior/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void *net_buf_simple_pull_mem_mock(struct net_buf_simple *buf, size_t len)
206206
return data; // Return the original data pointer
207207
}
208208

209-
ZTEST(channel_sounding_tests, test_successful_read)
209+
ZTEST(channel_sounding_tests, test_bt_hci_le_cs_read_remote_fae_table_complete_success)
210210
{
211211
struct bt_conn *test_conn = &test_conn_mock;
212212

0 commit comments

Comments
 (0)