• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

IpMapTest.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003     A brief file description
00004 
00005     @section license License
00006 
00007     Licensed to the Apache Software Foundation (ASF) under one
00008     or more contributor license agreements.  See the NOTICE file
00009     distributed with this work for additional information
00010     regarding copyright ownership.  The ASF licenses this file
00011     to you under the Apache License, Version 2.0 (the
00012     "License"); you may not use this file except in compliance
00013     with the License.  You may obtain a copy of the License at
00014 
00015     http://www.apache.org/licenses/LICENSE-2.0
00016 
00017     Unless required by applicable law or agreed to in writing, software
00018     distributed under the License is distributed on an "AS IS" BASIS,
00019     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020     See the License for the specific language governing permissions and
00021     limitations under the License.
00022 */
00023 
00024 #include <ts/IpMap.h>
00025 #include <ts/TestBox.h>
00026 
00027 void
00028 IpMapTestPrint(IpMap& map) {
00029   printf("IpMap Dump\n");
00030   for ( IpMap::iterator spot(map.begin()), limit(map.end())
00031       ; spot != limit
00032       ; ++spot
00033   ) {
00034     ip_text_buffer ipb1, ipb2;
00035 
00036     printf("%s - %s : %p\n", ats_ip_ntop(spot->min(), ipb1, sizeof ipb1), ats_ip_ntop(spot->max(), ipb2, sizeof(ipb2)), spot->data());
00037   }
00038   printf("\n");
00039 }
00040 
00041 REGRESSION_TEST(IpMap_Basic)(RegressionTest* t, int /* atype ATS_UNUSED */, int*  pstatus) {
00042   TestBox tb(t, pstatus);
00043 
00044   IpMap map;
00045   void* const markA = reinterpret_cast<void*>(1);
00046   void* const markB = reinterpret_cast<void*>(2);
00047   void* const markC = reinterpret_cast<void*>(3);
00048   void* mark; // for retrieval
00049 
00050   in_addr_t ip5 = htonl(5), ip9 = htonl(9);
00051   in_addr_t ip10 = htonl(10), ip15 = htonl(15), ip20 = htonl(20);
00052   in_addr_t ip50 = htonl(50), ip60 = htonl(60);
00053   in_addr_t ip100 = htonl(100), ip120 = htonl(120), ip140 = htonl(140);
00054   in_addr_t ip150 = htonl(150), ip160 = htonl(160);
00055   in_addr_t ip200 = htonl(200);
00056   in_addr_t ip0 = 0;
00057   in_addr_t ipmax = ~static_cast<in_addr_t>(0);
00058 
00059   *pstatus = REGRESSION_TEST_PASSED;
00060 
00061   map.mark(ip10,ip20,markA);
00062   map.mark(ip5, ip9, markA);
00063   tb.check(map.getCount() == 1, "Coalesce failed");
00064   tb.check(map.contains(ip9), "Range max not found.");
00065   tb.check(map.contains(ip10, &mark), "Span min not found.");
00066   tb.check(mark == markA, "Mark not preserved.");
00067 
00068   map.fill(ip15, ip100, markB);
00069   tb.check(map.getCount() == 2, "Fill failed.");
00070   tb.check(map.contains(ip50, &mark), "Fill interior missing.");
00071   tb.check(mark == markB, "Fill mark not preserved.");
00072   tb.check(!map.contains(ip200), "Span min not found.");
00073   tb.check(map.contains(ip15, &mark), "Old span interior not found.");
00074   tb.check(mark == markA, "Fill overwrote mark.");
00075 
00076   map.clear();
00077   tb.check(map.getCount() == 0, "Clear failed.");
00078 
00079   map.mark(ip20, ip50, markA);
00080   map.mark(ip100, ip150, markB);
00081   map.fill(ip10, ip200, markC);
00082   tb.check(map.getCount() == 5, "Test 3 failed [expected 5, got %d].", map.getCount());
00083   tb.check(map.contains(ip15, &mark), "Test 3 - left span missing.");
00084   tb.check(map.contains(ip60, &mark), "Test 3 - middle span missing.");
00085   tb.check(mark == markC, "Test 3 - fill mark wrong.");
00086   tb.check(map.contains(ip160), "Test 3 - right span missing.");
00087   tb.check(map.contains(ip120, &mark), "Test 3 - right mark span missing.");
00088   tb.check(mark == markB, "Test 3 - wrong data on right mark span.");
00089   map.unmark(ip140, ip160);
00090   tb.check(map.getCount() == 5, "Test 3 unmark failed [expected 5, got %d].", map.getCount());
00091   tb.check(!map.contains(ip140), "Test 3 - unmark left edge still there.");
00092   tb.check(!map.contains(ip150), "Test 3 - unmark middle still there.");
00093   tb.check(!map.contains(ip160), "Test 3 - unmark right edge still there.");
00094 
00095   map.clear();
00096   map.mark(ip20,ip20, markA);
00097   tb.check(map.contains(ip20), "Map failed on singleton insert");
00098   map.mark(ip10, ip200, markB);
00099   mark=0;
00100   map.contains(ip20, &mark);
00101   tb.check(mark == markB, "Map held singleton against range.");
00102   map.mark(ip100, ip120, markA);
00103   map.mark(ip150, ip160, markB);
00104   map.mark(ip0, ipmax, markC);
00105   tb.check(map.getCount() == 1, "IpMap: Full range fill left extra ranges.");
00106 }
00107 
00108 REGRESSION_TEST(IpMap_Unmark)(RegressionTest* t, int /* atype ATS_UNUSED */, int* pstatus) {
00109   TestBox tb(t, pstatus);
00110   IpMap map;
00111 //  ip_text_buffer ipb1, ipb2;
00112   void* const markA = reinterpret_cast<void*>(1);
00113 
00114   IpEndpoint a_0, a_0_0_0_16, a_0_0_0_17, a_max;
00115   IpEndpoint a_10_28_56_0,a_10_28_56_4, a_10_28_56_255;
00116   IpEndpoint a_10_28_55_255, a_10_28_57_0;
00117   IpEndpoint a_63_128_1_12;
00118   IpEndpoint a_loopback, a_loopback2;
00119   IpEndpoint a6_0, a6_max, a6_fe80_9d90, a6_fe80_9d9d, a6_fe80_9d95;
00120 
00121   ats_ip_pton("0.0.0.0", &a_0);
00122   ats_ip_pton("0.0.0.16", &a_0_0_0_16);
00123   ats_ip_pton("0.0.0.17", &a_0_0_0_17);
00124   ats_ip_pton("255.255.255.255", &a_max);
00125   ats_ip_pton("10.28.55.255", &a_10_28_55_255);
00126   ats_ip_pton("10.28.56.0", &a_10_28_56_0);
00127   ats_ip_pton("10.28.56.4", &a_10_28_56_4);
00128   ats_ip_pton("10.28.56.255", &a_10_28_56_255);
00129   ats_ip_pton("10.28.57.0", &a_10_28_57_0);
00130   ats_ip_pton("63.128.1.12", &a_63_128_1_12);
00131   ats_ip_pton("::", &a6_0);
00132   ats_ip_pton("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", &a6_max);
00133   ats_ip_pton("fe80::221:9bff:fe10:9d90", &a6_fe80_9d90);
00134   ats_ip_pton("fe80::221:9bff:fe10:9d9d", &a6_fe80_9d9d);
00135   ats_ip_pton("fe80::221:9bff:fe10:9d95", &a6_fe80_9d95);
00136   ats_ip_pton("127.0.0.1", &a_loopback);
00137   ats_ip_pton("127.0.0.255", &a_loopback2);
00138   *pstatus = REGRESSION_TEST_PASSED;
00139 
00140   map.mark(&a_0, &a_max, markA);
00141   tb.check(map.getCount() == 1, "IpMap Unmark: Full range not single.");
00142   map.unmark(&a_10_28_56_0, &a_10_28_56_255);
00143   tb.check(map.getCount() == 2, "IpMap Unmark: Range unmark failed.");
00144   // Generic range check.
00145   tb.check(!map.contains(&a_10_28_56_0), "IpMap Unmark: Range unmark min address not removed.");
00146   tb.check(!map.contains(&a_10_28_56_255), "IpMap Unmark: Range unmark max address not removed.");
00147   tb.check(map.contains(&a_10_28_55_255), "IpMap Unmark: Range unmark min-1 address removed.");
00148   tb.check(map.contains(&a_10_28_57_0), "IpMap Unmark: Range unmark max+1 address removed.");
00149   // Test min bounded range.
00150   map.unmark(&a_0, &a_0_0_0_16);
00151   tb.check(!map.contains(&a_0), "IpMap Unmark: Range unmark zero address not removed.");
00152   tb.check(!map.contains(&a_0_0_0_16), "IpMap Unmark: Range unmark zero bounded range max not removed.");
00153   tb.check(map.contains(&a_0_0_0_17), "IpMap Unmark: Range unmark zero bounded range max+1 removed.");
00154 }
00155 
00156 REGRESSION_TEST(IpMap_Fill)(RegressionTest* t, int /* atype ATS_UNUSED */, int* pstatus) {
00157   TestBox tb(t, pstatus);
00158   IpMap map;
00159   ip_text_buffer ipb1, ipb2;
00160   void* const allow = reinterpret_cast<void*>(0);
00161   void* const deny = reinterpret_cast<void*>(~0); 
00162   void* const markA = reinterpret_cast<void*>(1);
00163   void* const markB = reinterpret_cast<void*>(2);
00164   void* const markC = reinterpret_cast<void*>(3);
00165   void* mark; // for retrieval
00166 
00167   IpEndpoint a0,a_10_28_56_0,a_10_28_56_255,a3,a4;
00168   IpEndpoint a_9_255_255_255, a_10_0_0_0, a_10_0_0_19, a_10_0_0_255, a_10_0_1_0;
00169   IpEndpoint a_10_28_56_4, a_max, a_loopback, a_loopback2;
00170   IpEndpoint a_10_28_55_255, a_10_28_57_0;
00171   IpEndpoint a_63_128_1_12;
00172   IpEndpoint a_0000_0000, a_0000_0001, a_ffff_ffff;
00173   IpEndpoint a_fe80_9d8f, a_fe80_9d90, a_fe80_9d95, a_fe80_9d9d, a_fe80_9d9e;
00174 
00175   *pstatus = REGRESSION_TEST_PASSED;
00176 
00177   ats_ip_pton("0.0.0.0", &a0);
00178   ats_ip_pton("255.255.255.255", &a_max);
00179 
00180   ats_ip_pton("9.255.255.255", &a_9_255_255_255);
00181   ats_ip_pton("10.0.0.0", &a_10_0_0_0);
00182   ats_ip_pton("10.0.0.19", &a_10_0_0_19);
00183   ats_ip_pton("10.0.0.255", &a_10_0_0_255);
00184   ats_ip_pton("10.0.1.0", &a_10_0_1_0);
00185 
00186   ats_ip_pton("10.28.55.255", &a_10_28_55_255);
00187   ats_ip_pton("10.28.56.0", &a_10_28_56_0);
00188   ats_ip_pton("10.28.56.4", &a_10_28_56_4);
00189   ats_ip_pton("10.28.56.255", &a_10_28_56_255);
00190   ats_ip_pton("10.28.57.0", &a_10_28_57_0);
00191 
00192   ats_ip_pton("192.168.1.0", &a3);
00193   ats_ip_pton("192.168.1.255", &a4);
00194 
00195   ats_ip_pton("::", &a_0000_0000);
00196   ats_ip_pton("::1", &a_0000_0001);
00197   ats_ip_pton("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", &a_ffff_ffff);
00198   ats_ip_pton("fe80::221:9bff:fe10:9d8f", &a_fe80_9d8f);
00199   ats_ip_pton("fe80::221:9bff:fe10:9d90", &a_fe80_9d90);
00200   ats_ip_pton("fe80::221:9bff:fe10:9d95", &a_fe80_9d95);
00201   ats_ip_pton("fe80::221:9bff:fe10:9d9d", &a_fe80_9d9d);
00202   ats_ip_pton("fe80::221:9bff:fe10:9d9e", &a_fe80_9d9e);
00203 
00204   ats_ip_pton("127.0.0.0", &a_loopback);
00205   ats_ip_pton("127.0.0.255", &a_loopback2);
00206   ats_ip_pton("63.128.1.12", &a_63_128_1_12);
00207 
00208   map.fill(&a_10_28_56_0,&a_10_28_56_255,deny);
00209   map.fill(&a0,&a_max,allow);
00210 
00211   tb.check(map.contains(&a_10_28_56_4,&mark), "IpMap Fill: Target not found.");
00212   tb.check(mark == deny, "IpMap Fill: Expected deny, got allow at %s.", ats_ip_ntop(&a_10_28_56_4, ipb1, sizeof(ipb1)));
00213 
00214   map.clear();
00215   map.fill(&a_loopback, &a_loopback, allow);
00216   tb.check(map.contains(&a_loopback), "IpMap fill: singleton not marked.");
00217   map.fill(&a0, &a_max, deny);
00218 
00219   mark=0;
00220   tb.check(map.contains(&a_loopback, &mark), "IpMap fill: singleton marking lost.");
00221   tb.check(mark == allow, "IpMap fill: overwrote existing singleton mark.");
00222   if (tb.check(map.begin() != map.end(), "IpMap fill: map is empty.")) {
00223     if (tb.check(++(map.begin()) != map.end(), "IpMap fill: only one range.")) {
00224       tb.check(-1 == ats_ip_addr_cmp(map.begin()->max(), (++map.begin())->min()), "IpMap fill: ranges not disjoint [%s < %s].", ats_ip_ntop(map.begin()->max(), ipb1, sizeof(ipb1)), ats_ip_ntop((++map.begin())->min(), ipb2, sizeof(ipb2)));
00225     }
00226   }
00227 
00228   map.clear();
00229   map.fill(&a_loopback, &a_loopback2, markA);
00230   map.fill(&a_10_28_56_0, &a_10_28_56_255, markB);
00231   tb.check(!map.contains(&a_63_128_1_12, &mark), "IpMap fill[2]: over extended range.");
00232   map.fill(&a0, &a_max, markC);
00233   tb.check(map.getCount() == 5, "IpMap[2]: Fill failed.");
00234   if (tb.check(map.contains(&a_63_128_1_12, &mark), "IpMap fill[2]: Collapsed range.")) {
00235     tb.check(mark == markC, "IpMap fill[2]: invalid mark for range gap.");
00236   }
00237 
00238   map.clear();
00239   map.fill(&a_10_0_0_0, &a_10_0_0_255, allow);
00240   map.fill(&a_loopback, &a_loopback2, allow);
00241   tb.check(!map.contains(&a_63_128_1_12, &mark), "IpMap fill[3]: invalid mark between ranges.");
00242   tb.check(map.contains(&a_10_0_0_19, &mark) && mark == allow, "IpMap fill[3]: invalid mark in lower range.");
00243   map.fill(&a0, &a_max, deny);
00244   if (!tb.check(map.getCount() == 5, "IpMap[3]: Wrong number of ranges."))
00245     IpMapTestPrint(map);
00246   if (tb.check(map.contains(&a_63_128_1_12, &mark), "IpMap fill[3]: Missing mark between ranges")) {
00247     tb.check(mark == deny, "IpMap fill[3]: gap range invalidly marked");
00248   }
00249 
00250   map.fill(&a_fe80_9d90, &a_fe80_9d9d, markA);
00251   map.fill(&a_0000_0001, &a_0000_0001, markA);
00252   map.fill(&a_0000_0000, &a_ffff_ffff, markB);
00253 
00254   tb.check(map.contains(&a_0000_0000, &mark) && mark == markB,
00255            "IpMap Fill[v6]: Zero address has bad mark.");
00256   tb.check(map.contains(&a_ffff_ffff, &mark) && mark == markB,
00257            "IpMap Fill[v6]: Max address has bad mark.");
00258   tb.check(map.contains(&a_fe80_9d90, &mark) && mark == markA,
00259            "IpMap Fill[v6]: 9d90 address has bad mark.");
00260   tb.check(map.contains(&a_fe80_9d8f, &mark) && mark == markB,
00261            "IpMap Fill[v6]: 9d8f address has bad mark.");
00262   tb.check(map.contains(&a_fe80_9d9d, &mark) && mark == markA,
00263            "IpMap Fill[v6]: 9d9d address has bad mark.");
00264   tb.check(map.contains(&a_fe80_9d9e, &mark) && mark == markB,
00265            "IpMap Fill[v6]: 9d9b address has bad mark.");
00266   tb.check(map.contains(&a_0000_0001, &mark) && mark == markA,
00267            "IpMap Fill[v6]: ::1 has bad mark.");
00268   
00269   tb.check(map.getCount() == 10, "IpMap Fill[pre-refill]: Bad range count.");
00270   // These should be ignored by the map as it is completely covered for IPv6.
00271   map.fill(&a_fe80_9d90, &a_fe80_9d9d, markA);
00272   map.fill(&a_0000_0001, &a_0000_0001, markC);
00273   map.fill(&a_0000_0000, &a_ffff_ffff, markB);
00274   tb.check(map.getCount() == 10, "IpMap Fill[post-refill]: Bad range count.");
00275 
00276   map.clear();
00277   map.fill(&a_fe80_9d90, &a_fe80_9d9d, markA);
00278   map.fill(&a_0000_0001, &a_0000_0001, markC);
00279   map.fill(&a_0000_0000, &a_ffff_ffff, markB);
00280   tb.check(map.contains(&a_0000_0000, &mark) && mark == markB,
00281            "IpMap Fill[v6-2]: Zero address has bad mark.");
00282   tb.check(map.contains(&a_ffff_ffff, &mark) && mark == markB,
00283            "IpMap Fill[v6-2]: Max address has bad mark.");
00284   tb.check(map.contains(&a_fe80_9d90, &mark) && mark == markA,
00285            "IpMap Fill[v6-2]: 9d90 address has bad mark.");
00286   tb.check(map.contains(&a_fe80_9d8f, &mark) && mark == markB,
00287            "IpMap Fill[v6-2]: 9d8f address has bad mark.");
00288   tb.check(map.contains(&a_fe80_9d9d, &mark) && mark == markA,
00289            "IpMap Fill[v6-2]: 9d9d address has bad mark.");
00290   tb.check(map.contains(&a_fe80_9d9e, &mark) && mark == markB,
00291            "IpMap Fill[v6-2]: 9d9b address has bad mark.");
00292   tb.check(map.contains(&a_0000_0001, &mark) && mark == markC,
00293            "IpMap Fill[v6-2]: ::1 has bad mark.");
00294  
00295 }

Generated by  doxygen 1.7.1