rdata_test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012, 2013  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /* $Id$ */
00018 
00019 /*! \file */
00020 
00021 #include <config.h>
00022 
00023 #include <atf-c.h>
00024 
00025 #include <unistd.h>
00026 
00027 #include <isc/types.h>
00028 
00029 #include <dns/rdata.h>
00030 
00031 #include "dnstest.h"
00032 
00033 
00034 /*
00035  * Individual unit tests
00036  */
00037 
00038 /* Successful load test */
00039 ATF_TC(hip);
00040 ATF_TC_HEAD(hip, tc) {
00041         atf_tc_set_md_var(tc, "descr", "that a oversized HIP record will "
00042                                        "be rejected");
00043 }
00044 ATF_TC_BODY(hip, tc) {
00045         unsigned char hipwire[DNS_RDATA_MAXLENGTH] = {
00046                                     0x01, 0x00, 0x00, 0x01, 0x00, 0x00,
00047                                     0x04, 0x41, 0x42, 0x43, 0x44, 0x00 };
00048         unsigned char buf[1024*1024];
00049         isc_buffer_t source, target;
00050         dns_rdata_t rdata;
00051         dns_decompress_t dctx;
00052         isc_result_t result;
00053         size_t i;
00054 
00055         UNUSED(tc);
00056 
00057         /*
00058          * Fill the rest of input buffer with compression pointers.
00059          */
00060         for (i = 12; i < sizeof(hipwire) - 2; i += 2) {
00061                 hipwire[i] = 0xc0;
00062                 hipwire[i+1] = 0x06;
00063         }
00064 
00065         isc_buffer_init(&source, hipwire, sizeof(hipwire));
00066         isc_buffer_add(&source, sizeof(hipwire));
00067         isc_buffer_setactive(&source, i);
00068         isc_buffer_init(&target, buf, sizeof(buf));
00069         dns_rdata_init(&rdata);
00070         dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_ANY);
00071         result = dns_rdata_fromwire(&rdata, dns_rdataclass_in,
00072                                     dns_rdatatype_hip, &source, &dctx,
00073                                     0, &target);
00074         dns_decompress_invalidate(&dctx);
00075         ATF_REQUIRE_EQ(result, DNS_R_FORMERR);
00076 }
00077 
00078 ATF_TC(edns_client_subnet);
00079 ATF_TC_HEAD(edns_client_subnet, tc) {
00080         atf_tc_set_md_var(tc, "descr",
00081                           "check EDNS client subnet option parsing");
00082 }
00083 ATF_TC_BODY(edns_client_subnet, tc) {
00084         struct {
00085                 unsigned char data[64];
00086                 size_t len;
00087                 isc_boolean_t ok;
00088         } test_data[] = {
00089                 {
00090                         /* option code with no content */
00091                         { 0x00, 0x08, 0x0, 0x00 }, 4, ISC_FALSE
00092                 },
00093                 {
00094                         /* Option code family 0, source 0, scope 0 */
00095                         {
00096                           0x00, 0x08, 0x00, 0x04,
00097                           0x00, 0x00, 0x00, 0x00
00098                         },
00099                         8, ISC_TRUE
00100                 },
00101                 {
00102                         /* Option code family 1 (ipv4), source 0, scope 0 */
00103                         {
00104                           0x00, 0x08, 0x00, 0x04,
00105                           0x00, 0x01, 0x00, 0x00
00106                         },
00107                         8, ISC_TRUE
00108                 },
00109                 {
00110                         /* Option code family 2 (ipv6) , source 0, scope 0 */
00111                         {
00112                           0x00, 0x08, 0x00, 0x04,
00113                           0x00, 0x02, 0x00, 0x00
00114                         },
00115                         8, ISC_TRUE
00116                 },
00117                 {
00118                         /* extra octet */
00119                         {
00120                           0x00, 0x08, 0x00, 0x05,
00121                           0x00, 0x00, 0x00, 0x00,
00122                           0x00
00123                         },
00124                         9, ISC_FALSE
00125                 },
00126                 {
00127                         /* source too long for IPv4 */
00128                         {
00129                           0x00, 0x08, 0x00,    8,
00130                           0x00, 0x01,   33, 0x00,
00131                           0x00, 0x00, 0x00, 0x00
00132                         },
00133                         12, ISC_FALSE
00134                 },
00135                 {
00136                         /* source too long for IPv6 */
00137                         {
00138                           0x00, 0x08, 0x00,   20,
00139                           0x00, 0x02,  129, 0x00,
00140                           0x00, 0x00, 0x00, 0x00,
00141                           0x00, 0x00, 0x00, 0x00,
00142                           0x00, 0x00, 0x00, 0x00,
00143                           0x00, 0x00, 0x00, 0x00,
00144                         },
00145                         24, ISC_FALSE
00146                 },
00147                 {
00148                         /* scope too long for IPv4 */
00149                         {
00150                           0x00, 0x08, 0x00,    8,
00151                           0x00, 0x01, 0x00,   33,
00152                           0x00, 0x00, 0x00, 0x00
00153                         },
00154                         12, ISC_FALSE
00155                 },
00156                 {
00157                         /* scope too long for IPv6 */
00158                         {
00159                           0x00, 0x08, 0x00,   20,
00160                           0x00, 0x02, 0x00,  129,
00161                           0x00, 0x00, 0x00, 0x00,
00162                           0x00, 0x00, 0x00, 0x00,
00163                           0x00, 0x00, 0x00, 0x00,
00164                           0x00, 0x00, 0x00, 0x00,
00165                         },
00166                         24, ISC_FALSE
00167                 },
00168                 {
00169                         /* length too short for source generic */
00170                         {
00171                           0x00, 0x08, 0x00,    5,
00172                           0x00, 0x00,   17, 0x00,
00173                           0x00, 0x00,
00174                         },
00175                         19, ISC_FALSE
00176                 },
00177                 {
00178                         /* length too short for source ipv4 */
00179                         {
00180                           0x00, 0x08, 0x00,    7,
00181                           0x00, 0x01,   32, 0x00,
00182                           0x00, 0x00, 0x00, 0x00
00183                         },
00184                         11, ISC_FALSE
00185                 },
00186                 {
00187                         /* length too short for source ipv6 */
00188                         {
00189                           0x00, 0x08, 0x00,   19,
00190                           0x00, 0x02,  128, 0x00,
00191                           0x00, 0x00, 0x00, 0x00,
00192                           0x00, 0x00, 0x00, 0x00,
00193                           0x00, 0x00, 0x00, 0x00,
00194                           0x00, 0x00, 0x00, 0x00,
00195                         },
00196                         23, ISC_FALSE
00197                 },
00198                 {
00199                         /* sentinal */
00200                         { 0x00 }, 0, ISC_FALSE
00201                 }
00202         };
00203         unsigned char buf[1024*1024];
00204         isc_buffer_t source, target;
00205         dns_rdata_t rdata;
00206         dns_decompress_t dctx;
00207         isc_result_t result;
00208         size_t i;
00209 
00210         UNUSED(tc);
00211 
00212         for (i = 0; test_data[i].len != 0; i++) {
00213                 isc_buffer_init(&source, test_data[i].data, test_data[i].len);
00214                 isc_buffer_add(&source, test_data[i].len);
00215                 isc_buffer_setactive(&source, test_data[i].len);
00216                 isc_buffer_init(&target, buf, sizeof(buf));
00217                 dns_rdata_init(&rdata);
00218                 dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_ANY);
00219                 result = dns_rdata_fromwire(&rdata, dns_rdataclass_in,
00220                                             dns_rdatatype_opt, &source,
00221                                             &dctx, 0, &target);
00222                 dns_decompress_invalidate(&dctx);
00223                 if (test_data[i].ok)
00224                         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00225                 else
00226                         ATF_REQUIRE(result != ISC_R_SUCCESS);
00227         }
00228 }
00229 
00230 ATF_TC(wks);
00231 ATF_TC_HEAD(wks, tc) {
00232         atf_tc_set_md_var(tc, "descr", "wks to/from struct");
00233 }
00234 ATF_TC_BODY(wks, tc) {
00235         struct {
00236                 unsigned char data[64];
00237                 size_t len;
00238                 isc_boolean_t ok;
00239         } test_data[] = {
00240                 {
00241                         /* too short */
00242                         { 0x00, 0x08, 0x0, 0x00 }, 4, ISC_FALSE
00243                 },
00244                 {
00245                         /* minimal TCP */
00246                         { 0x00, 0x08, 0x0, 0x00, 6 }, 5, ISC_TRUE
00247                 },
00248                 {
00249                         /* minimal UDP */
00250                         { 0x00, 0x08, 0x0, 0x00, 17 }, 5, ISC_TRUE
00251                 },
00252                 {
00253                         /* minimal other */
00254                         { 0x00, 0x08, 0x0, 0x00, 1 }, 5, ISC_TRUE
00255                 },
00256                 {
00257                         /* sentinal */
00258                         { 0x00 }, 0, ISC_FALSE
00259                 }
00260         };
00261         unsigned char buf1[1024];
00262         unsigned char buf2[1024];
00263         isc_buffer_t source, target1, target2;
00264         dns_rdata_t rdata;
00265         dns_decompress_t dctx;
00266         isc_result_t result;
00267         size_t i;
00268         dns_rdata_in_wks_t wks;
00269 
00270         UNUSED(tc);
00271 
00272         for (i = 0; test_data[i].len != 0; i++) {
00273                 isc_buffer_init(&source, test_data[i].data, test_data[i].len);
00274                 isc_buffer_add(&source, test_data[i].len);
00275                 isc_buffer_setactive(&source, test_data[i].len);
00276                 isc_buffer_init(&target1, buf1, sizeof(buf1));
00277                 dns_rdata_init(&rdata);
00278                 dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_ANY);
00279                 result = dns_rdata_fromwire(&rdata, dns_rdataclass_in,
00280                                             dns_rdatatype_wks, &source,
00281                                             &dctx, 0, &target1);
00282                 dns_decompress_invalidate(&dctx);
00283                 if (test_data[i].ok)
00284                         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00285                 else
00286                         ATF_REQUIRE(result != ISC_R_SUCCESS);
00287                 if (result != ISC_R_SUCCESS)
00288                         continue;
00289                 result = dns_rdata_tostruct(&rdata, &wks, NULL);
00290                 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00291                 isc_buffer_init(&target2, buf2, sizeof(buf2));
00292                 dns_rdata_reset(&rdata);
00293                 result = dns_rdata_fromstruct(&rdata, dns_rdataclass_in,
00294                                               dns_rdatatype_wks, &wks,
00295                                               &target2);
00296                 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00297                 ATF_REQUIRE_EQ(isc_buffer_usedlength(&target2),
00298                                                      test_data[i].len);
00299                 ATF_REQUIRE_EQ(memcmp(buf2, test_data[i].data,
00300                                       test_data[i].len), 0);
00301         }
00302 }
00303 
00304 ATF_TC(isdn);
00305 ATF_TC_HEAD(isdn, tc) {
00306         atf_tc_set_md_var(tc, "descr", "isdn to/from struct");
00307 }
00308 ATF_TC_BODY(isdn, tc) {
00309         struct {
00310                 unsigned char data[64];
00311                 size_t len;
00312                 isc_boolean_t ok;
00313         } test_data[] = {
00314                 {
00315                         /* "" */
00316                         { 0x00 }, 1, ISC_TRUE
00317                 },
00318                 {
00319                         /* "\001" */
00320                         { 0x1, 0x01 }, 2, ISC_TRUE
00321                 },
00322                 {
00323                         /* "\001" "" */
00324                         { 0x1, 0x01, 0x00 }, 3, ISC_TRUE
00325                 },
00326                 {
00327                         /* "\000" "\001" */
00328                         { 0x1, 0x01, 0x01, 0x01 }, 4, ISC_TRUE
00329                 },
00330                 {
00331                         /* sentinal */
00332                         { 0x00 }, 0, ISC_FALSE
00333                 }
00334         };
00335         unsigned char buf1[1024];
00336         unsigned char buf2[1024];
00337         isc_buffer_t source, target1, target2;
00338         dns_rdata_t rdata;
00339         dns_decompress_t dctx;
00340         isc_result_t result;
00341         size_t i;
00342         dns_rdata_isdn_t isdn;
00343 
00344         UNUSED(tc);
00345 
00346         for (i = 0; test_data[i].len != 0; i++) {
00347                 isc_buffer_init(&source, test_data[i].data, test_data[i].len);
00348                 isc_buffer_add(&source, test_data[i].len);
00349                 isc_buffer_setactive(&source, test_data[i].len);
00350                 isc_buffer_init(&target1, buf1, sizeof(buf1));
00351                 dns_rdata_init(&rdata);
00352                 dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_ANY);
00353                 result = dns_rdata_fromwire(&rdata, dns_rdataclass_in,
00354                                             dns_rdatatype_isdn, &source,
00355                                             &dctx, 0, &target1);
00356                 dns_decompress_invalidate(&dctx);
00357                 if (test_data[i].ok)
00358                         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00359                 else
00360                         ATF_REQUIRE(result != ISC_R_SUCCESS);
00361                 if (result != ISC_R_SUCCESS)
00362                         continue;
00363                 result = dns_rdata_tostruct(&rdata, &isdn, NULL);
00364                 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00365                 isc_buffer_init(&target2, buf2, sizeof(buf2));
00366                 dns_rdata_reset(&rdata);
00367                 result = dns_rdata_fromstruct(&rdata, dns_rdataclass_in,
00368                                               dns_rdatatype_isdn, &isdn,
00369                                               &target2);
00370                 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00371                 ATF_REQUIRE_EQ(isc_buffer_usedlength(&target2),
00372                                                      test_data[i].len);
00373                 ATF_REQUIRE_EQ(memcmp(buf2, test_data[i].data,
00374                                       test_data[i].len), 0);
00375         }
00376 }
00377 
00378 /*
00379  * Main
00380  */
00381 ATF_TP_ADD_TCS(tp) {
00382         ATF_TP_ADD_TC(tp, hip);
00383         ATF_TP_ADD_TC(tp, edns_client_subnet);
00384         ATF_TP_ADD_TC(tp, wks);
00385         ATF_TP_ADD_TC(tp, isdn);
00386 
00387         return (atf_no_error());
00388 }
00389 

Generated on Tue Apr 28 17:41:02 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha