|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Arm Limited and affiliates. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "nsconfig.h" |
| 19 | +#include "string.h" |
| 20 | +#include "ns_types.h" |
| 21 | +#include "ns_trace.h" |
| 22 | +#include "common_functions.h" |
| 23 | +#include "nsdynmemLIB.h" |
| 24 | +#include "Service_Libs/mac_neighbor_table/mac_neighbor_table.h" |
| 25 | +#include "nsdynmemLIB_stub.h" |
| 26 | +#include "common_functions_stub.h" |
| 27 | +#include "test_mac_neighbor_table.h" |
| 28 | + |
| 29 | +/** |
| 30 | + * Remove entry notify |
| 31 | + * |
| 32 | + * \param entry_ptr Pointer to removed entry |
| 33 | + * \param user_data pointer for user to detect interface |
| 34 | + */ |
| 35 | +static void test_neighbor_entry_remove_notify(mac_neighbor_table_entry_t *entry_ptr, void *user_data) |
| 36 | +{ |
| 37 | + |
| 38 | +} |
| 39 | + |
| 40 | +static bool test_bool = true; |
| 41 | +/** |
| 42 | + * NUD entry notify |
| 43 | + * |
| 44 | + * \param entry_ptr Pointer to neighbor entry |
| 45 | + * \param user_data pointer for user to detect interface |
| 46 | + * |
| 47 | + * \return true NUD message generated |
| 48 | + * \return false When NUD is not generated |
| 49 | + */ |
| 50 | +static bool test_neighbor_entry_nud_notify(mac_neighbor_table_entry_t *entry_ptr, void *user_data) |
| 51 | +{ |
| 52 | + return test_bool; |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +bool test_mac_neighbor_table_create() |
| 57 | +{ |
| 58 | + nsdynmemlib_stub.returnCounter = 0; |
| 59 | + mac_neighbor_table_t *entry = mac_neighbor_table_create(10, test_neighbor_entry_remove_notify, test_neighbor_entry_nud_notify, NULL, 200); |
| 60 | + |
| 61 | + if (entry) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + nsdynmemlib_stub.returnCounter = 1; |
| 65 | + entry = mac_neighbor_table_create(10, test_neighbor_entry_remove_notify, test_neighbor_entry_nud_notify, NULL, 200); |
| 66 | + |
| 67 | + if (!entry) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + mac_neighbor_table_delete(entry); |
| 72 | + return true; |
| 73 | +} |
| 74 | + |
| 75 | +bool test_mac_neighbor_table_use() |
| 76 | +{ |
| 77 | + nsdynmemlib_stub.returnCounter = 1; |
| 78 | + mac_neighbor_table_t *entry = mac_neighbor_table_create(10, test_neighbor_entry_remove_notify, test_neighbor_entry_nud_notify, NULL, 200); |
| 79 | + |
| 80 | + if (!entry) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + uint8_t mac1[8]; |
| 84 | + uint8_t mac2[8]; |
| 85 | + uint8_t mac3[8]; |
| 86 | + uint8_t mac4[8]; |
| 87 | + memset(mac1, 0, 8); |
| 88 | + memset(mac2, 1, 8); |
| 89 | + memset(mac3, 2, 8); |
| 90 | + memset(mac4, 5, 8); |
| 91 | + |
| 92 | + mac_neighbor_table_entry_t * mac_entry1 = mac_neighbor_table_entry_allocate(entry, mac1); |
| 93 | + if (!mac_entry1 || mac_entry1->index != 0) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + mac_neighbor_table_entry_t * mac_entry2 = mac_neighbor_table_entry_allocate(entry, mac2); |
| 98 | + if (!mac_entry2 || mac_entry2->index != 1) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + mac_neighbor_table_entry_t *mac_entry3 = mac_neighbor_table_entry_allocate(entry, mac3); |
| 103 | + |
| 104 | + if (!mac_entry3 || mac_entry3->index != 2) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + for (unsigned char i=0; i< 7; i++) { |
| 109 | + mac_neighbor_table_entry_t *test = mac_neighbor_table_entry_allocate(entry, mac4); |
| 110 | + if (!test || test->index != 3+i) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + mac4[0] += 1; |
| 114 | + } |
| 115 | + |
| 116 | + mac_neighbor_table_entry_t *test2 = mac_neighbor_table_entry_allocate(entry, mac4); |
| 117 | + if (test2) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + mac_neighbor_table_neighbor_timeout_update(entry, 10); |
| 123 | + |
| 124 | + if (mac_entry3->lifetime != 230 || mac_entry2->lifetime != 230 || mac_entry1->lifetime != 230) { |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + mac_neighbor_table_neighbor_connected(entry, mac_entry3); |
| 129 | + |
| 130 | + if (!mac_entry3->connected_device) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + mac_neighbor_table_trusted_neighbor(entry, mac_entry3, true); |
| 135 | + |
| 136 | + if (!mac_entry3->trusted_device) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + |
| 140 | + mac_neighbor_table_neighbor_timeout_update(entry, 31); |
| 141 | + |
| 142 | + if (mac_entry3->lifetime != 199 || mac_entry2->lifetime != 199 || mac_entry1->lifetime != 199) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + mac_neighbor_table_neighbor_timeout_update(entry, 170); |
| 147 | + |
| 148 | + if (mac_entry3->lifetime != 29 || mac_entry2->lifetime != 29 || mac_entry1->lifetime != 29) { |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + if (!mac_entry3->nud_active || !mac_entry2->nud_active || !mac_entry1->nud_active) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + mac_neighbor_table_neighbor_timeout_update(entry, 1); |
| 157 | + |
| 158 | + mac_neighbor_table_neighbor_refresh(entry, mac_entry2, 79); |
| 159 | + mac_neighbor_table_neighbor_refresh(entry, mac_entry1, 600); |
| 160 | + |
| 161 | + mac_neighbor_table_neighbor_timeout_update(entry, 40); |
| 162 | + |
| 163 | + if (!mac_entry2->nud_active) { |
| 164 | + return false; |
| 165 | + } |
| 166 | + |
| 167 | + if (mac_neighbor_table_attribute_discover(entry, mac_entry3->index)) { |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + if (!mac_neighbor_table_attribute_discover(entry, mac_entry2->index)) { |
| 172 | + return false; |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + if (!mac_neighbor_table_address_discover(entry, mac_entry1, 3) ) { |
| 178 | + return false; |
| 179 | + } |
| 180 | + |
| 181 | + if (mac_neighbor_table_address_discover(entry, mac_entry1, 2) ) { |
| 182 | + return false; |
| 183 | + } |
| 184 | + mac_entry2->mac16 = 1; |
| 185 | + common_functions_stub.uint16_value = 1; |
| 186 | + |
| 187 | + if (!mac_neighbor_table_address_discover(entry, mac_entry1, 2) ) { |
| 188 | + return false; |
| 189 | + } |
| 190 | + |
| 191 | + if (mac_neighbor_table_address_discover(entry, mac_entry1, 4) ) { |
| 192 | + return false; |
| 193 | + } |
| 194 | + |
| 195 | + mac_neighbor_table_neighbor_remove(entry, mac_entry2); |
| 196 | + |
| 197 | + mac_neighbor_table_neighbor_remove(entry, mac_entry3); |
| 198 | + |
| 199 | + mac_neighbor_table_neighbor_list_clean(entry); |
| 200 | + mac_neighbor_table_delete(entry); |
| 201 | + return true; |
| 202 | +} |
| 203 | + |
| 204 | + |
0 commit comments