aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_receive.c
blob: 1452ed3e207b6aec89262aee6f289d64d73400d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <isotp/isotp.h>
#include <check.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

extern IsoTpShims SHIMS;
extern IsoTpReceiveHandle RECEIVE_HANDLE;

extern uint16_t last_can_frame_sent_arb_id;
extern uint8_t last_can_payload_sent;
extern uint8_t last_can_payload_size;
extern bool can_frame_was_sent;

extern bool message_was_received;
extern uint16_t last_message_received_arb_id;
extern uint8_t last_message_received_payload[];
extern uint8_t last_message_received_payload_size;

extern uint16_t last_message_sent_arb_id;
extern bool last_message_sent_status;
extern uint8_t last_message_sent_payload[];
extern uint8_t last_message_sent_payload_size;

extern void setup();

START_TEST (test_receive_empty_can_message)
{
    const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0};
    fail_if(RECEIVE_HANDLE.completed);
    IsoTpMessage message = isotp_continue_receive(&SHIMS, &RECEIVE_HANDLE, 0x100, data, 0);
    fail_if(message.completed);
    fail_if(message_was_received);
}
END_TEST

START_TEST (test_receive_wrong_id)
{
    const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0};
    fail_if(RECEIVE_HANDLE.completed);
    IsoTpMessage message = isotp_continue_receive(&SHIMS, &RECEIVE_HANDLE, 0x100, data, 1);
    fail_if(message.completed);
    fail_if(message_was_received);
}
END_TEST

START_TEST (test_receive_bad_pci)
{
    // 4 is a reserved number for the PCI field - only 0-3 are allowed
    const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0x40};
    IsoTpMessage message = isotp_continue_receive(&SHIMS, &RECEIVE_HANDLE, 0x2a, data, 1);
    fail_if(message.completed);
    fail_if(message_was_received);
}
END_TEST

START_TEST (test_receive_single_frame_empty_payload)
{
    const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0x00, 0x12, 0x34};
    fail_if(RECEIVE_HANDLE.completed);
    IsoTpMessage message = isotp_continue_receive(&SHIMS, &RECEIVE_HANDLE, 0x2a, data, 3);
    fail_unless(RECEIVE_HANDLE.completed);
    fail_unless(message.completed);
    fail_unless(message_was_received);
    ck_assert_int_eq(last_message_received_arb_id, 0x2a);
    ck_assert_int_eq(last_message_received_payload_size, 0);
}
END_TEST

START_TEST (test_receive_single_frame)
{
    const uint8_t data[CAN_MESSAGE_BYTE_SIZE] = {0x02, 0x12, 0x34};
    IsoTpMessage message = isotp_continue_receive(&SHIMS, &RECEIVE_HANDLE, 0x2a, data, 3);
    fail_unless(message.completed);
    fail_unless(message_was_received);
    ck_assert_int_eq(last_message_received_arb_id, 0x2a);
    ck_assert_int_eq(last_message_received_payload_size, 2);
    ck_assert_int_eq(last_message_received_payload[0], 0x12);
    ck_assert_int_eq(last_message_received_payload[1], 0x34);
}
END_TEST

Suite* testSuite(void) {
    Suite* s = suite_create("iso15765");
    TCase *tc_core = tcase_create("receive");
    tcase_add_checked_fixture(tc_core, setup, NULL);
    tcase_add_test(tc_core, test_receive_wrong_id);
    tcase_add_test(tc_core, test_receive_bad_pci);
    tcase_add_test(tc_core, test_receive_single_frame);
    tcase_add_test(tc_core, test_receive_single_frame_empty_payload);
    tcase_add_test(tc_core, test_receive_empty_can_message);
    suite_add_tcase(s, tc_core);

    return s;
}

int main(void) {
    int numberFailed;
    Suite* s = testSuite();
    SRunner *sr = srunner_create(s);
    // Don't fork so we can actually use gdb
    srunner_set_fork_status(sr, CK_NOFORK);
    srunner_run_all(sr, CK_NORMAL);
    numberFailed = srunner_ntests_failed(sr);
    srunner_free(sr);
    return (numberFailed == 0) ? 0 : 1;
}