1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <../includes/common.h>
18 #include <../includes/memutils.h>
19 #include <nfc_int.h>
20 #include <rw_int.h>
21 
22 #define RW_MFC_STATE_READ_NDEF 0x03
23 #define RW_MFC_SUBSTATE_READ_BLOCK 0x03
24 #define RW_MFC_DATA_LEN 0x10
25 #define P_MFC_NDEF_LENGTH 1024
26 
27 extern tRW_CB rw_cb;
28 tNFC_CONN *p_data = nullptr;
29 tRW_MFC_CB *p_mfc = nullptr;
30 
31 char enable_selective_overload = ENABLE_NONE;
32 
33 bool isTestInProgress = false;
34 struct sigaction new_action, old_action;
sigsegv_handler(int signum,siginfo_t * info,void * context)35 void sigsegv_handler(int signum, siginfo_t *info, void *context) {
36     if (isTestInProgress && info->si_signo == SIGSEGV) {
37         (*old_action.sa_sigaction)(signum, info, context);
38         return;
39     }
40     exit(EXIT_FAILURE);
41 }
42 
GKI_freebuf(void *)43 void GKI_freebuf(void *) {}
44 
GKI_start_timer(uint8_t,int32_t,bool)45 void GKI_start_timer(uint8_t, int32_t, bool) {}
46 
GKI_stop_timer(uint8_t)47 void GKI_stop_timer(uint8_t) {}
48 
cback(tRW_EVENT,tRW_DATA *)49 void cback(tRW_EVENT, tRW_DATA *) {}
50 
poc_cback(tRW_EVENT event,tRW_DATA * p_rw_data)51 void poc_cback(tRW_EVENT event, tRW_DATA *p_rw_data) {
52     (void)event;
53     (void)p_rw_data;
54 }
55 
exit_handler(void)56 void exit_handler(void) {
57     if (p_data) {
58         if (p_data->data.p_data) {
59             free(p_data->data.p_data);
60             p_data->data.p_data = nullptr;
61         }
62         free(p_data);
63         p_data = nullptr;
64     }
65 
66     if (p_mfc) {
67         if (p_mfc->p_ndef_buffer) {
68             free(p_mfc->p_ndef_buffer);
69             p_mfc->p_ndef_buffer = nullptr;
70         }
71         free(p_mfc);
72         p_mfc = nullptr;
73     }
74 }
75 
main()76 int main() {
77     atexit(exit_handler);
78     sigemptyset(&new_action.sa_mask);
79     new_action.sa_flags = SA_SIGINFO;
80     new_action.sa_sigaction = sigsegv_handler;
81     sigaction(SIGSEGV, &new_action, &old_action);
82 
83     tNFC_ACTIVATE_DEVT p_activate_params = {};
84     p_activate_params.protocol = NFC_PROTOCOL_ISO_DEP;
85     p_activate_params.rf_tech_param.mode = NFC_DISCOVERY_TYPE_POLL_A;
86     RW_SetActivatedTagType(&p_activate_params, &poc_cback);
87     FAIL_CHECK(rw_cb.p_cback == &poc_cback);
88 
89     p_mfc = &rw_cb.tcb.mfc;
90 
91     GKI_init();
92     rw_init();
93 
94     uint8_t selres = 1;
95     uint8_t uid[MFC_UID_LEN] = {1};
96 
97     enable_selective_overload = ENABLE_MALLOC_CHECK;
98     FAIL_CHECK(rw_mfc_select(selres, uid) == NFC_STATUS_OK);
99 
100     p_mfc->state = RW_MFC_STATE_READ_NDEF;
101     p_mfc->substate = RW_MFC_SUBSTATE_READ_BLOCK;
102 
103     tNFC_CONN_CB *p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
104 
105     p_data = (tNFC_CONN *)malloc(sizeof(tNFC_CONN));
106     FAIL_CHECK(p_data);
107 
108     // NOLINTNEXTLINE(clang-analyzer-unix.MallocSizeof)
109     p_data->data.p_data = (NFC_HDR *)malloc(sizeof(uint8_t) * 16);
110     FAIL_CHECK(p_data->data.p_data);
111 
112     p_data->data.status = NFC_STATUS_OK;
113     tNFC_CONN_EVT event = NFC_DATA_CEVT;
114 
115     NFC_HDR *mfc_data = (NFC_HDR *)p_data->data.p_data;
116     mfc_data->len = RW_MFC_DATA_LEN;
117     mfc_data->offset = 0;
118     p_mfc->ndef_length = P_MFC_NDEF_LENGTH;
119     p_mfc->p_ndef_buffer = (uint8_t *)malloc(sizeof(uint8_t) * 16);
120     enable_selective_overload = ENABLE_FREE_CHECK | ENABLE_REALLOC_CHECK;
121     FAIL_CHECK(p_mfc->p_ndef_buffer);
122 
123     rw_cb.p_cback = cback;
124 
125     isTestInProgress = true;
126     p_cb->p_cback(0, event, p_data);
127     isTestInProgress = false;
128 
129     return EXIT_SUCCESS;
130 }
131