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] = {0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
00046     // This should sto parsing when too short address given, the buffer hoewever is filled to that long
00047     // So basically there is no error handling. We just check that first FFFF:FFFF gets filled and not trash after that.
00048     // Rest should be filled with zeroes
00049     stoip6(addr, strlen(addr), ip);
00050     CHECK(0 == memcmp(ip, correct, 16));
00051 }
00052 
00053 TEST(stoip6, TooLongString)
00054 {
00055     // String len must be less than 40, otherwise not valid
00056     char *addr = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:";
00057     uint8_t ip[16] = {0};
00058     uint8_t correct[16] = {0};
00059     // This should not fill anything, too long string.
00060     // This is basically only validation we do
00061     stoip6(addr, strlen(addr), ip);
00062     CHECK(0 == memcmp(ip, correct, 16));
00063 }
00064 
00065 TEST(stoip6, TooManyFields)
00066 {
00067     // String len must be less than 40
00068     char *addr = "FF:FF:FF:FF:FF:FF:FFFF:FFFF:FFFF:FFFF:";
00069     uint8_t ip[17] = {0};
00070     uint8_t correct[17] = { 0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0};
00071     // Again.. there is not really any error handling (no return value)
00072     // Just make sure that it does not overflow
00073     stoip6(addr, strlen(addr), ip);
00074     CHECK(0 == memcmp(ip, correct, 17)); // Note, we are checking 17, to make sure one byte after address in not touched.
00075 }
00076 
00077 TEST(stoip6, Prefixlen)
00078 {
00079     CHECK(0 == sipv6_prefixlength("::"));
00080     CHECK(64 == sipv6_prefixlength("::/64"));
00081 }
00082 
00083 // This test revealed a off-by-one error in stoip6() when the code was ran under valgrind.
00084 // The IP address is copied from the test_2duts_ping -test, where the valgrind message
00085 // was originally spotted.
00086 TEST(stoip6, RegressionTestForOffByOne)
00087 {
00088     const char *sourceAddr = "fd00:db8::643f:f54a:ec29:cdbb";
00089 
00090     // Use heap based test string to make valgrind spot the problem.
00091     char *sourceTemp = (char *)strdup(sourceAddr);
00092     size_t sourceTempLen = strlen(sourceTemp);
00093 
00094     uint8_t ip[16];
00095     const uint8_t correct[16] = { 0xfd, 0x00, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
00096                                   0x64, 0x3f, 0xf5, 0x4a, 0xec, 0x29, 0xcd, 0xbb
00097                                 };
00098 
00099     stoip6(sourceTemp, sourceTempLen, ip);
00100 
00101     CHECK(0 == memcmp(ip, correct, 16));
00102 
00103     free(sourceTemp);
00104 }
00105 
00106 /***********************************************************/
00107 /* Second test group for the old tests that were once lost */
00108 
00109 const char string_addr[][40] =
00110 {
00111     "2001:db8::1:0:0:1",                    // 1
00112     "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1",  // 2
00113     "2001:db8::1",                          // 3
00114     "2001:db8::2:1",                        // 4
00115     "2001:db8:aaaa:bbbb:cccc:dddd:0:1",     // 5
00116     "2001:db8::aaaa:0:0:1",                 // 6
00117     "2001:0:0:1::1",                        // 7
00118     "2001:0:0:1::",                         // 8
00119     "2001:db8::",                           // 9
00120     "::aaaa:0:0:1",                         // 10
00121     "::1",                                  // 11
00122     "::",                                   // 12
00123 };
00124 
00125 
00126 const uint8_t hex_addr[][16] =
00127 {
00128     { 0x20, 0x01, 0xd, 0xb8, 0,0,0,0,0,1,0,0,0,0,0,1 },
00129     { 0x20, 0x01, 0xd, 0xb8, 0xaa, 0xaa, 0xbb, 0xbb, 0xcc, 0xcc, 0xdd, 0xdd, 0xee, 0xee, 0x00, 0x01 },
00130     { 0x20, 0x01, 0xd, 0xb8, 0,0,0,0,0,0,0,0,0,0,0,1 },
00131     { 0x20, 0x01, 0xd,0xb8, 0,0,0,0,0,0,0,0, 0,2,0,1 },
00132     { 0x20, 0x01, 0xd, 0xb8, 0xaa, 0xaa, 0xbb, 0xbb, 0xcc, 0xcc, 0xdd, 0xdd, 0,0, 0x00, 0x01 },
00133     { 0x20, 0x01, 0xd, 0xb8, 0,0,0,0,0xaa,0xaa,0,0,0,0,0,1 },
00134     { 0x20, 0x01, 0,0 ,0,0 ,0,1,0,0,0,0,0,0,0,1 },
00135     { 0x20, 0x01, 0,0 ,0,0 ,0,1,0,0,0,0,0,0,0,0 },
00136     { 0x20, 0x01, 0xd, 0xb8 },
00137     { 0,0,0,0,0,0,0,0,0xaa,0xaa,0,0,0,0,0,1 },
00138     { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
00139     { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
00140 };
00141 
00142 char buf[40];
00143 int i = 0;
00144 
00145 TEST_GROUP(stoip6_2)
00146 {
00147     void setUp(void)
00148     {
00149         memset(buf, 0, 40);
00150     }
00151 
00152     void tearDown(void)
00153     {
00154         i++;
00155     }
00156 };
00157 
00158 /* Unity test code starts */
00159 
00160 
00161 TEST(stoip6_2, test_2_1)
00162 {
00163     i = 0;
00164     stoip6(string_addr[i], strlen(string_addr[i]), buf);
00165     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00166 }
00167 
00168 TEST(stoip6_2, test_2_2)
00169 {
00170     stoip6(string_addr[i], strlen(string_addr[i]), buf);
00171     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00172 }
00173 TEST(stoip6_2, test_2_3)
00174 {
00175     stoip6(string_addr[i], strlen(string_addr[i]), buf);
00176     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00177 }
00178 TEST(stoip6_2, test_2_4)
00179 {
00180     stoip6(string_addr[i], strlen(string_addr[i]), buf);
00181     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00182 }
00183 TEST(stoip6_2, test_2_5)
00184 {
00185     stoip6(string_addr[i], strlen(string_addr[i]), buf);
00186     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00187 }
00188 TEST(stoip6_2, test_2_6)
00189 {
00190     stoip6(string_addr[i], strlen(string_addr[i]), buf);
00191     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00192 }
00193 TEST(stoip6_2, test_2_7)
00194 {
00195     stoip6(string_addr[i], strlen(string_addr[i]), buf);
00196     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00197 }
00198 TEST(stoip6_2, test_2_8)
00199 {
00200     stoip6(string_addr[i], strlen(string_addr[i]), buf);
00201     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00202 }
00203 TEST(stoip6_2, test_2_9)
00204 {
00205     stoip6(string_addr[i], strlen(string_addr[i]), buf);
00206     CHECK(0 == memcmp(hex_addr[i], buf, strlen(buf)));
00207 }
00208 TEST(stoip6_2, test_2_10)
00209 {
00210     stoip6(string_addr[i], strlen(string_addr[i]), buf);
00211     CHECK(0 == memcmp(hex_addr[i], buf, 16));
00212 }
00213 TEST(stoip6_2, test_2_11)
00214 {
00215     stoip6(string_addr[i], strlen(string_addr[i]), buf);
00216     CHECK(0 == memcmp(hex_addr[i], buf, 16));
00217 }
00218 TEST(stoip6_2, test_2_12)
00219 {
00220     stoip6(string_addr[i], strlen(string_addr[i]), buf);
00221     CHECK(0 == memcmp(hex_addr[i], buf, 16));
00222 }
00223