Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stoip6test.cpp Source File

stoip6test.cpp

00001 /*
00002  * Copyright (c) 2015 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "CppUTest/TestHarness.h"
00017 #include "ip6string.h"
00018 #include "ipv6_test_values.h"
00019 #include <stdlib.h>
00020 #include <string.h>
00021 
00022 TEST_GROUP(stoip6)
00023 {
00024     void setup() {
00025     }
00026 
00027     void teardown() {
00028     }
00029 };
00030 
00031 TEST(stoip6, ZeroAddress)
00032 {
00033     for (int i = 0; ipv6_test_values[i].addr; i++) {
00034         uint8_t ip[16];
00035         char *addr = ipv6_test_values[i].addr;
00036         stoip6(addr, strlen(addr), ip);
00037         CHECK(0 == memcmp(ip, ipv6_test_values[i].bin, 16));
00038     }
00039 }
00040 
00041 TEST(stoip6, TooShort)
00042 {
00043     char *addr = "FFFF:FFFF:";
00044     uint8_t ip[16];
00045     uint8_t correct[16] = {0};
00046     // This should stop parsing when too short address given.
00047     // Despite partial parsing, the entire buffer should be filled with zeroes
00048     CHECK(false == stoip6(addr, strlen(addr), ip));
00049     CHECK(0 == memcmp(ip, correct, 16));
00050 }
00051 
00052 TEST(stoip6, TooLongString)
00053 {
00054     // String len must be less than 40, otherwise not valid
00055     char *addr = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:";
00056     uint8_t ip[16] = {0};
00057     uint8_t correct[16] = {0};
00058     // This should not fill anything, too long string.
00059     // This is basically only validation we do
00060     CHECK(false == stoip6(addr, strlen(addr), ip));
00061     CHECK(0 == memcmp(ip, correct, 16));
00062 }
00063 
00064 TEST(stoip6, TooManyFields)
00065 {
00066     // String len must be less than 40
00067     char *addr = "FF:FF:FF:FF:FF:FF:FFFF:FFFF:FFFF:FFFF:";
00068     uint8_t ip[16] = {0};
00069     uint8_t correct[16] = {0};
00070 
00071     CHECK(false == stoip6(addr, strlen(addr), ip));
00072     CHECK(0 == memcmp(ip, correct, 16));
00073 }
00074 
00075 TEST(stoip6, Prefixlen)
00076 {
00077     CHECK(0 == sipv6_prefixlength("::"));
00078     CHECK(64 == sipv6_prefixlength("::/64"));
00079 }
00080 
00081 // This test revealed a off-by-one error in stoip6() when the code was ran under valgrind.
00082 // The IP address is copied from the test_2duts_ping -test, where the valgrind message
00083 // was originally spotted.
00084 TEST(stoip6, RegressionTestForOffByOne)
00085 {
00086     const char *sourceAddr = "fd00:db8::643f:f54a:ec29:cdbb";
00087 
00088     // Use heap based test string to make valgrind spot the problem.
00089     char *sourceTemp = (char *)strdup(sourceAddr);
00090     size_t sourceTempLen = strlen(sourceTemp);
00091 
00092     uint8_t ip[16];
00093     const uint8_t correct[16] = { 0xfd, 0x00, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
00094                                   0x64, 0x3f, 0xf5, 0x4a, 0xec, 0x29, 0xcd, 0xbb
00095                                 };
00096 
00097     CHECK(true == stoip6(sourceTemp, sourceTempLen, ip));
00098     CHECK(0 == memcmp(ip, correct, 16));
00099 
00100     free(sourceTemp);
00101 }
00102 
00103 // Test various illegal formats to ensure proper rejection
00104 TEST(stoip6, InvalidAddresses)
00105 {
00106     uint8_t ip[16];
00107     uint8_t correct[16] = {0};
00108 
00109     const char *invalidArray[] = {
00110         "FFFF:FFFF::FFFF::FFFF", // Two ::
00111         "F:F:F:FqF:F:F:F:F",     // Non-hex character
00112         "F:F:F:FFFFF:F:F:F:F"    // >4 hex characters in a segment
00113     };
00114 
00115     for (uint8_t i = 0; i < 3; ++i) {
00116         CHECK(false == stoip6(invalidArray[i], strlen(invalidArray[i]), ip));
00117         CHECK(0 == memcmp(ip, correct, 16));
00118     }
00119 }
00120 
00121 /***********************************************************/
00122 /* Second test group for the old tests that were once lost */
00123 
00124 const char string_addr[][40] = {
00125     "2001:db8::1:0:0:1",                    // 1
00126     "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1",  // 2
00127     "2001:db8::1",                          // 3
00128     "2001:db8::2:1",                        // 4
00129     "2001:db8:aaaa:bbbb:cccc:dddd:0:1",     // 5
00130     "2001:db8::aaaa:0:0:1",                 // 6
00131     "2001:0:0:1::1",                        // 7
00132     "2001:0:0:1::",                         // 8
00133     "2001:db8::",                           // 9
00134     "::aaaa:0:0:1",                         // 10
00135     "::1",                                  // 11
00136     "::",                                   // 12
00137 };
00138 
00139 
00140 const uint8_t hex_addr[][16] = {
00141     { 0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
00142     { 0x20, 0x01, 0xd, 0xb8, 0xaa, 0xaa, 0xbb, 0xbb, 0xcc, 0xcc, 0xdd, 0xdd, 0xee, 0xee, 0x00, 0x01 },
00143     { 0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
00144     { 0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1 },
00145     { 0x20, 0x01, 0xd, 0xb8, 0xaa, 0xaa, 0xbb, 0xbb, 0xcc, 0xcc, 0xdd, 0xdd, 0, 0, 0x00, 0x01 },
00146     { 0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0xaa, 0xaa, 0, 0, 0, 0, 0, 1 },
00147     { 0x20, 0x01, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
00148     { 0x20, 0x01, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
00149     { 0x20, 0x01, 0xd, 0xb8 },
00150     { 0, 0, 0, 0, 0, 0, 0, 0, 0xaa, 0xaa, 0, 0, 0, 0, 0, 1 },
00151     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
00152     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
00153 };
00154 
00155 char buf[40];
00156 int i = 0;
00157 
00158 TEST_GROUP(stoip6_2)
00159 {
00160     void setUp(void) {
00161         memset(buf, 0, 40);
00162     }
00163 
00164     void tearDown(void) {
00165         i++;
00166     }
00167 };
00168 
00169 /* Unity test code starts */
00170 
00171 
00172 TEST(stoip6_2, test_2_1)
00173 {
00174     i = 0;
00175     CHECK(true == stoip6(string_addr[i], strlen(string_addr[i]), buf));
00176     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00177 }
00178 
00179 TEST(stoip6_2, test_2_2)
00180 {
00181     CHECK(true == stoip6(string_addr[i], strlen(string_addr[i]), buf));
00182     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00183 }
00184 TEST(stoip6_2, test_2_3)
00185 {
00186     CHECK(true == stoip6(string_addr[i], strlen(string_addr[i]), buf));
00187     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00188 }
00189 TEST(stoip6_2, test_2_4)
00190 {
00191     CHECK(true == stoip6(string_addr[i], strlen(string_addr[i]), buf));
00192     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00193 }
00194 TEST(stoip6_2, test_2_5)
00195 {
00196     CHECK(true == stoip6(string_addr[i], strlen(string_addr[i]), buf));
00197     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00198 }
00199 TEST(stoip6_2, test_2_6)
00200 {
00201     CHECK(true == stoip6(string_addr[i], strlen(string_addr[i]), buf));
00202     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00203 }
00204 TEST(stoip6_2, test_2_7)
00205 {
00206     CHECK(true == stoip6(string_addr[i], strlen(string_addr[i]), buf));
00207     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00208 }
00209 TEST(stoip6_2, test_2_8)
00210 {
00211     CHECK(true == stoip6(string_addr[i], strlen(string_addr[i]), buf));
00212     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00213 }
00214 TEST(stoip6_2, test_2_9)
00215 {
00216     CHECK(true == stoip6(string_addr[i], strlen(string_addr[i]), buf));
00217     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00218 }
00219 TEST(stoip6_2, test_2_10)
00220 {
00221     CHECK(true == stoip6(string_addr[i], strlen(string_addr[i]), buf));
00222     CHECK(0 == memcmp(hex_addr[i], buf, 16));
00223 }
00224 TEST(stoip6_2, test_2_11)
00225 {
00226     CHECK(true == stoip6(string_addr[i], strlen(string_addr[i]), buf));
00227     CHECK(0 == memcmp(hex_addr[i], buf, 16));
00228 }
00229 TEST(stoip6_2, test_2_12)
00230 {
00231     CHECK(true == stoip6(string_addr[i], strlen(string_addr[i]), buf));
00232     CHECK(0 == memcmp(hex_addr[i], buf, 16));
00233 }
00234 
00235 /***********************************************************/
00236 /* Third test group for stoip6_prefix */
00237 
00238 const char string_prefix_addr[][40] = {
00239     "2001:db8::1:0:0:1/64",     // 1
00240     "2001::/60",                // 2
00241     "::1/48",                   // 3
00242     "::/00",                    // 4
00243     "2002::02/99",              // 5
00244     "2003::03/",                // 6
00245     "2004::04",                 // 7
00246     "2005::05/2000",            // 8
00247     "2005::05/-1",              // 9
00248 };
00249 
00250 
00251 const uint8_t hex_prefix_addr[][16] = {
00252     { 0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 },  // 1
00253     { 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },       // 2
00254     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },             // 3
00255     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },             // 4
00256     { 0x20, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 },       // 5
00257     { 0x20, 0x03, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },       // 6
00258     { 0x20, 0x04, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4 },       // 7
00259     { 0x20, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5 },       // 8
00260     { 0x20, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5 },       // 9
00261 };
00262 
00263 const int_fast16_t prefix_len_tbl[] = {64, 60, 48, 0, 99, 0, -1, -1, -1};
00264 const int prefix_status_tbl[] = {0, 0, 0, 0, 0, 0, 0, -1, -1};
00265 
00266 TEST_GROUP(stoip6_3)
00267 {
00268     void setup() {
00269     }
00270 
00271     void teardown() {
00272     }
00273 };
00274 
00275 TEST(stoip6_3, stoip6_prefix_test)
00276 {
00277     for (int i = 0; i < 9; i++) {
00278         uint8_t ip[16];
00279         int_fast16_t prefix_len;
00280         int result;
00281         const char *addr = &string_prefix_addr[i][0];
00282 
00283         result = stoip6_prefix(addr, ip, &prefix_len);
00284         CHECK(result == prefix_status_tbl[i]);
00285         if (result == 0) {
00286             CHECK(0 == memcmp(ip, &hex_prefix_addr[i][0], 16));
00287             CHECK(prefix_len == prefix_len_tbl[i])
00288         }
00289     }
00290 }
00291