Skip to content

Commit 05b6d5f

Browse files
nayaknedSebastianSchildt
authored andcommitted
Fixed is_valid() for (N)TSCF formats. Added unit tests.
Signed-off-by: Naresh Nayak <[email protected]>
1 parent b722be8 commit 05b6d5f

File tree

6 files changed

+233
-6
lines changed

6 files changed

+233
-6
lines changed

src/avtp/acf/Ntscf.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ uint8_t Avtp_Ntscf_IsValid(Avtp_Ntscf_t* pdu, size_t bufferSize)
159159
}
160160

161161
// Avtp_Ntscf_GetNtscfDataLength returns quadlets. Convert the length field to octets
162-
if (Avtp_Ntscf_GetNtscfDataLength(pdu) * 4> bufferSize) {
162+
if (Avtp_Ntscf_GetNtscfDataLength(pdu) > bufferSize) {
163163
return FALSE;
164164
}
165165

src/avtp/acf/Tscf.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
#ifdef LINUX_KERNEL1722
3131
#include <linux/errno.h>
3232
#include <linux/string.h>
33-
#else
34-
#include <errno.h>
33+
#else
34+
#include <errno.h>
3535
#include <string.h>
3636
#endif
3737

@@ -218,7 +218,7 @@ uint8_t Avtp_Tscf_IsValid(Avtp_Tscf_t* pdu, size_t bufferSize)
218218
}
219219

220220
// Avtp_Tscf_GetStreamDataLength returns quadlets. Convert the length field to octets
221-
if (Avtp_Tscf_GetStreamDataLength(pdu) * 4 > bufferSize) {
221+
if (Avtp_Tscf_GetStreamDataLength(pdu) > bufferSize) {
222222
return FALSE;
223223
}
224224

unit/CMakeLists.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,21 @@ target_link_libraries(test-rvf open1722 cmocka)
7070
target_include_directories(test-rvf PUBLIC ../include)
7171
add_test(NAME test-rvf COMMAND test-rvf)
7272

73+
add_executable(test-tscf test-tscf.c)
74+
target_link_libraries(test-tscf open1722 cmocka)
75+
target_include_directories(test-tscf PUBLIC ../include)
76+
add_test(NAME test-tscf COMMAND test-tscf)
77+
78+
add_executable(test-ntscf test-ntscf.c)
79+
target_link_libraries(test-ntscf open1722 cmocka)
80+
target_include_directories(test-ntscf PUBLIC ../include)
81+
add_test(NAME test-ntscf COMMAND test-ntscf)
82+
7383
add_executable(test-vss test-vss.c)
7484
target_link_libraries(test-vss open1722 open1722custom cmocka)
7585
target_include_directories(test-vss PUBLIC ../include)
7686
add_test(NAME test-vss COMMAND test-vss)
7787

7888
add_dependencies(unittests test-can test-aaf
7989
test-avtp test-crf test-cvf
80-
test-rvf test-vss)
90+
test-rvf test-vss test-tscf test-ntscf)

unit/test-can.c

+26-1
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,37 @@ static void can_set_payload(void **state) {
116116
}
117117
}
118118

119+
static void can_is_valid(void **state) {
120+
121+
uint8_t pdu[MAX_PDU_SIZE], result;
122+
123+
// Valid IEEE 1722 CAN Frame
124+
Avtp_Can_Init((Avtp_Can_t*)pdu);
125+
assert_int_equal(Avtp_Can_IsValid((Avtp_Can_t*)pdu, MAX_PDU_SIZE), 1);
126+
127+
// Not a IEEE 1722 CAN Frame
128+
memset(pdu, 0, MAX_PDU_SIZE);
129+
assert_int_equal(Avtp_Can_IsValid((Avtp_Can_t*)pdu, MAX_PDU_SIZE), 0);
130+
131+
// Valid IEEE 1722 CAN Frame (Length 24, Buffer 25)
132+
Avtp_Can_Init((Avtp_Can_t*)pdu);
133+
Avtp_Can_SetAcfMsgLength((Avtp_Can_t*)pdu, 6);
134+
assert_int_equal(Avtp_Can_IsValid((Avtp_Can_t*)pdu, 25), 1);
135+
136+
// Invalid IEEE 1722 CAN Frame (Length 24 but buffer only 9!)
137+
Avtp_Can_Init((Avtp_Can_t*)pdu);
138+
Avtp_Can_SetAcfMsgLength((Avtp_Can_t*)pdu, 6);
139+
assert_int_equal(Avtp_Can_IsValid((Avtp_Can_t*)pdu, 9), 0);
140+
141+
}
142+
119143
int main(void)
120144
{
121145
const struct CMUnitTest tests[] = {
122146
cmocka_unit_test(can_init),
123147
cmocka_unit_test(can_brief_init),
124-
cmocka_unit_test(can_set_payload)
148+
cmocka_unit_test(can_set_payload),
149+
cmocka_unit_test(can_is_valid)
125150
};
126151

127152
return cmocka_run_group_tests(tests, NULL, NULL);

unit/test-ntscf.c

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2025, COVESA
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* * Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of COVESA nor the names of its contributors may be
13+
* used to endorse or promote products derived from this software without
14+
* specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*
27+
* SPDX-License-Identifier: BSD-3-Clause
28+
*/
29+
30+
#include <stdarg.h>
31+
#include <stddef.h>
32+
#include <setjmp.h>
33+
#include <cmocka.h>
34+
#include <arpa/inet.h>
35+
#include <errno.h>
36+
#include <string.h>
37+
#include <math.h>
38+
#include <stdio.h>
39+
40+
#include "avtp/acf/Ntscf.h"
41+
#include "avtp/CommonHeader.h"
42+
43+
#define MAX_PDU_SIZE 1500
44+
45+
static void ntscf_init(void **state) {
46+
47+
uint8_t pdu[MAX_PDU_SIZE];
48+
uint8_t init_pdu[AVTP_NTSCF_HEADER_LEN];
49+
50+
// Check the lengths
51+
assert_int_equal(sizeof(Avtp_Ntscf_t), AVTP_NTSCF_HEADER_LEN);
52+
53+
// Check init function while passing in a null pointer
54+
Avtp_Ntscf_Init(NULL);
55+
56+
// Check if the function is initializing properly
57+
Avtp_Ntscf_Init((Avtp_Ntscf_t*)pdu);
58+
memset(init_pdu, 0, AVTP_NTSCF_HEADER_LEN);
59+
init_pdu[0] = AVTP_SUBTYPE_NTSCF; // Setting AVTP Subtype as NTSCF
60+
init_pdu[1] = 0x80; // Setting Stream as valid
61+
assert_memory_equal(init_pdu, pdu, AVTP_NTSCF_HEADER_LEN);
62+
}
63+
64+
static void ntscf_is_valid(void **state) {
65+
66+
uint8_t pdu[MAX_PDU_SIZE], result;
67+
68+
// Valid IEEE 1722 NTSCF Frame
69+
Avtp_Ntscf_Init((Avtp_Ntscf_t*)pdu);
70+
assert_int_equal(Avtp_Ntscf_IsValid((Avtp_Ntscf_t*)pdu, MAX_PDU_SIZE), 1);
71+
72+
// Not a IEEE 1722 NTSCF Frame
73+
memset(pdu, 0, MAX_PDU_SIZE);
74+
assert_int_equal(Avtp_Ntscf_IsValid((Avtp_Ntscf_t*)pdu, MAX_PDU_SIZE), 0);
75+
76+
// Valid IEEE 1722 NTSCF Frame (Length 28, Buffer 30)
77+
Avtp_Ntscf_Init((Avtp_Ntscf_t*)pdu);
78+
Avtp_Ntscf_SetNtscfDataLength((Avtp_Ntscf_t*)pdu, 28);
79+
assert_int_equal(Avtp_Ntscf_IsValid((Avtp_Ntscf_t*)pdu, 30), 1);
80+
81+
// Invalid IEEE 1722 NTSCF Frame (Length 24 but buffer only 9!)
82+
Avtp_Ntscf_Init((Avtp_Ntscf_t*)pdu);
83+
Avtp_Ntscf_SetNtscfDataLength((Avtp_Ntscf_t*)pdu, 24);
84+
assert_int_equal(Avtp_Ntscf_IsValid((Avtp_Ntscf_t*)pdu, 9), 0);
85+
86+
}
87+
88+
int main(void)
89+
{
90+
const struct CMUnitTest tests[] = {
91+
cmocka_unit_test(ntscf_init),
92+
cmocka_unit_test(ntscf_is_valid)
93+
};
94+
95+
return cmocka_run_group_tests(tests, NULL, NULL);
96+
}

unit/test-tscf.c

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2025, COVESA
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* * Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of COVESA nor the names of its contributors may be
13+
* used to endorse or promote products derived from this software without
14+
* specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*
27+
* SPDX-License-Identifier: BSD-3-Clause
28+
*/
29+
30+
#include <stdarg.h>
31+
#include <stddef.h>
32+
#include <setjmp.h>
33+
#include <cmocka.h>
34+
#include <arpa/inet.h>
35+
#include <errno.h>
36+
#include <string.h>
37+
#include <math.h>
38+
#include <stdio.h>
39+
40+
#include "avtp/acf/Tscf.h"
41+
#include "avtp/CommonHeader.h"
42+
43+
#define MAX_PDU_SIZE 1500
44+
45+
static void tscf_init(void **state) {
46+
47+
uint8_t pdu[MAX_PDU_SIZE];
48+
uint8_t init_pdu[AVTP_TSCF_HEADER_LEN];
49+
50+
// Check the lengths
51+
assert_int_equal(sizeof(Avtp_Tscf_t), AVTP_TSCF_HEADER_LEN);
52+
53+
// Check init function while passing in a null pointer
54+
Avtp_Tscf_Init(NULL);
55+
56+
// Check if the function is initializing properly
57+
Avtp_Tscf_Init((Avtp_Tscf_t*)pdu);
58+
memset(init_pdu, 0, AVTP_TSCF_HEADER_LEN);
59+
init_pdu[0] = AVTP_SUBTYPE_TSCF; // Setting AVTP Subtype as TSCF
60+
init_pdu[1] = 0x80; // Setting Stream as valid
61+
assert_memory_equal(init_pdu, pdu, AVTP_TSCF_HEADER_LEN);
62+
}
63+
64+
static void tscf_is_valid(void **state) {
65+
66+
uint8_t pdu[MAX_PDU_SIZE], result;
67+
68+
// Valid IEEE 1722 TSCF Frame
69+
Avtp_Tscf_Init((Avtp_Tscf_t*)pdu);
70+
assert_int_equal(Avtp_Tscf_IsValid((Avtp_Tscf_t*)pdu, MAX_PDU_SIZE), 1);
71+
72+
// Not a IEEE 1722 TSCF Frame
73+
memset(pdu, 0, MAX_PDU_SIZE);
74+
assert_int_equal(Avtp_Tscf_IsValid((Avtp_Tscf_t*)pdu, MAX_PDU_SIZE), 0);
75+
76+
// Valid IEEE 1722 TSCF Frame (Length 28, Buffer 30)
77+
Avtp_Tscf_Init((Avtp_Tscf_t*)pdu);
78+
Avtp_Tscf_SetStreamDataLength((Avtp_Tscf_t*)pdu, 28);
79+
assert_int_equal(Avtp_Tscf_IsValid((Avtp_Tscf_t*)pdu, 30), 1);
80+
81+
// Invalid IEEE 1722 TSCF Frame (Length 24 but buffer only 9!)
82+
Avtp_Tscf_Init((Avtp_Tscf_t*)pdu);
83+
Avtp_Tscf_SetStreamDataLength((Avtp_Tscf_t*)pdu, 24);
84+
assert_int_equal(Avtp_Tscf_IsValid((Avtp_Tscf_t*)pdu, 9), 0);
85+
86+
}
87+
88+
int main(void)
89+
{
90+
const struct CMUnitTest tests[] = {
91+
cmocka_unit_test(tscf_init),
92+
cmocka_unit_test(tscf_is_valid),
93+
};
94+
95+
return cmocka_run_group_tests(tests, NULL, NULL);
96+
}

0 commit comments

Comments
 (0)