LCOV - code coverage report
Current view: top level - tests - tld_test_object.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 63 126 50.0 %
Date: 2018-08-28 01:54:14 Functions: 3 4 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* TLD library -- test the C++ TLD interface
       2             :  * Copyright (c) 2011-2018  Made to Order Software Corp.  All Rights Reserved
       3             :  *
       4             :  * Permission is hereby granted, free of charge, to any person obtaining a
       5             :  * copy of this software and associated documentation files (the
       6             :  * "Software"), to deal in the Software without restriction, including
       7             :  * without limitation the rights to use, copy, modify, merge, publish,
       8             :  * distribute, sublicense, and/or sell copies of the Software, and to
       9             :  * permit persons to whom the Software is furnished to do so, subject to
      10             :  * the following conditions:
      11             :  *
      12             :  * The above copyright notice and this permission notice shall be included
      13             :  * in all copies or substantial portions of the Software.
      14             :  *
      15             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      16             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      17             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      18             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      19             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      20             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      21             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      22             :  */
      23             : 
      24             : /** \file
      25             :  * \brief Test the tld() function through the C++ object.
      26             :  *
      27             :  * This file implements various test to verify that the
      28             :  * tld() function functions as expected in C++.
      29             :  */
      30             : 
      31             : #include "libtld/tld.h"
      32             : #include <string.h>
      33             : #include <stdlib.h>
      34             : #include <stdio.h>
      35             : //#include <limits.h>
      36             : 
      37             : int err_count = 0;
      38             : int verbose = 0;
      39             : 
      40             : 
      41           0 : void error(const std::string& msg)
      42             : {
      43           0 :     fprintf(stderr, "%s\n", msg.c_str());
      44           0 :     ++err_count;
      45           0 : }
      46             : 
      47             : 
      48             : #define EXPECTED_THROW(f) \
      49             :     try \
      50             :     { \
      51             :         static_cast<void>(bad.f()); \
      52             :         error("error: bad." #f "() of \"\" did not throw an error."); \
      53             :     } \
      54             :     catch(const invalid_domain&) \
      55             :     { \
      56             :     }
      57             : 
      58             : 
      59             : 
      60           4 : void test_valid_uri(const char *uri, const char *tld, const char *domain, const char *sub_domains, tld_category category, const char *country)
      61             : {
      62           4 :     if(verbose)
      63             :     {
      64           0 :         printf("testing uri \"%s\"\n", uri);
      65             :     }
      66             : 
      67             :     // create object with a URI
      68           8 :     tld_object o(uri);
      69             : 
      70           4 :     if(!o.is_valid())
      71             :     {
      72             :         char r[32], s[32];
      73           0 :         snprintf(r, sizeof(r), "%d", o.result());
      74           0 :         snprintf(s, sizeof(s), "%d", o.status());
      75           0 :         error("error: o.is_valid() of \"" + std::string(uri) + "\" result is not true (result: " + r + ", status: " + s + ").");
      76           0 :         return;
      77             :     }
      78             : 
      79           4 :     if(o.result() != TLD_RESULT_SUCCESS)
      80             :     {
      81           0 :         error("error: o.result() of \"" + std::string(uri) + "\" result was not success.");
      82             :     }
      83             : 
      84           4 :     if(o.status() != TLD_STATUS_VALID)
      85             :     {
      86           0 :         error("error: o.status() of \"" + std::string(uri) + "\" result was not valid.");
      87             :     }
      88             : 
      89           4 :     if(!o.is_valid())
      90             :     {
      91           0 :         error("error: o.is_valid() of \"" + std::string(uri) + "\" result was not valid.");
      92             :     }
      93             : 
      94           4 :     if(o.domain() != uri)
      95             :     {
      96           0 :         error("error: o.domain() of \"" + std::string(uri) + "\" result was not valid.");
      97             :     }
      98             : 
      99           4 :     if(o.sub_domains() != sub_domains)
     100             :     {
     101           0 :         error("error: o.sub_domains() of \"" + std::string(uri) + "\" result was not valid.");
     102             :     }
     103             : 
     104           4 :     if(o.full_domain() != domain + std::string(tld))
     105             :     {
     106           0 :         error("error: o.full_domain() of \"" + std::string(uri) + "\" result was not valid.");
     107             :     }
     108             : 
     109           4 :     if(o.domain_only() != domain)
     110             :     {
     111           0 :         error("error: o.domain_only() of \"" + std::string(uri) + "\" result was not valid.");
     112             :     }
     113             : 
     114           4 :     if(o.tld_only() != tld)
     115             :     {
     116           0 :         error("error: o.tld_only() of \"" + std::string(uri) + "\" result was not valid.");
     117             :     }
     118             : 
     119           4 :     if(o.category() != category)
     120             :     {
     121           0 :         error("error: o.category() of \"" + std::string(uri) + "\" result was not valid.");
     122             :     }
     123             : 
     124           4 :     if(o.country() != (country == NULL ? "" : country))
     125             :     {
     126           0 :         error("error: o.country() of \"" + std::string(uri) + "\" result was not valid.");
     127             :     }
     128             : }
     129             : 
     130             : 
     131             : 
     132           1 : void test_invalid()
     133             : {
     134             :     {
     135             :         // when empty that's equivalent to NULL or ""
     136           1 :         if(verbose)
     137             :         {
     138           0 :             printf("testing NULL pointer\n");
     139             :         }
     140             : 
     141           2 :         tld_object bad;
     142             : 
     143           1 :         if(bad.is_valid())
     144             :         {
     145             :             char r[32], s[32];
     146           0 :             snprintf(r, sizeof(r), "%d", bad.result());
     147           0 :             snprintf(s, sizeof(s), "%d", bad.status());
     148           0 :             error(std::string("error:test_invalid(1): bad.is_valid() of \"\" result is true?! (result: ") + r + ", status: " + s + ").");
     149           0 :             return;
     150             :         }
     151             : 
     152           1 :         if(bad.status() != TLD_STATUS_UNDEFINED)
     153             :         {
     154           0 :             error("error: bad.status() of \"\" is not UNDEFINED.");
     155           0 :             return;
     156             :         }
     157             : 
     158           1 :         if(bad.result() != TLD_RESULT_NULL)
     159             :         {
     160           0 :             error("error: bad.status() of \"\" is not NULL.");
     161           0 :             return;
     162             :         }
     163             : 
     164           1 :         if(bad.domain() != "")
     165             :         {
     166           0 :             error("error:test_invalid(1): bad.domain() did not return \"\" as expected.");
     167             :         }
     168             : 
     169           1 :         EXPECTED_THROW(sub_domains);
     170           1 :         EXPECTED_THROW(full_domain);
     171           1 :         EXPECTED_THROW(domain_only);
     172           1 :         EXPECTED_THROW(tld_only);
     173             : 
     174           1 :         if(bad.category() != TLD_CATEGORY_UNDEFINED)
     175             :         {
     176           0 :             error("error: bad.category() did not return UNDEFINED as expected.");
     177             :         }
     178             : 
     179           1 :         if(bad.country() != "")
     180             :         {
     181           0 :             error("error: bad.country() did not return \"\" as expected, got \"" + bad.country() + "\" instead.");
     182             :         }
     183             :     }
     184             : 
     185             :     {
     186             :         // unknown TLD
     187           1 :         if(verbose)
     188             :         {
     189           0 :             printf("testing \"www.example.unknown\"\n");
     190             :         }
     191             : 
     192           2 :         std::string uri("www.example.unknown");
     193           2 :         tld_object bad(uri);
     194             : 
     195           1 :         if(bad.is_valid())
     196             :         {
     197             :             char r[32], s[32];
     198           0 :             snprintf(r, sizeof(r), "%d", bad.result());
     199           0 :             snprintf(s, sizeof(s), "%d", bad.status());
     200           0 :             error(std::string("error:test_invalid(2): bad.is_valid() of \"\" result is true?! (result: ") + r + ", status: " + s + ").");
     201           0 :             return;
     202             :         }
     203             : 
     204           1 :         if(bad.status() != TLD_STATUS_UNDEFINED)
     205             :         {
     206           0 :             error("error: bad.status() of \"\" is not UNDEFINED.");
     207           0 :             return;
     208             :         }
     209             : 
     210           1 :         if(bad.result() != TLD_RESULT_NOT_FOUND)
     211             :         {
     212             :             char r[32];
     213           0 :             snprintf(r, sizeof(r), "%d", bad.result());
     214           0 :             error(std::string("error: bad.result() of \"\" is ") + r + " instead of NOT_FOUND (5).");
     215           0 :             return;
     216             :         }
     217             : 
     218             :         // TBD: if we clear the f_domain then this would be ""
     219           1 :         if(bad.domain() != "www.example.unknown")
     220             :         {
     221           0 :             error("error:test_invalid(2): bad.domain() did not return \"\" as expected.");
     222             :         }
     223             : 
     224           1 :         EXPECTED_THROW(sub_domains);
     225           1 :         EXPECTED_THROW(full_domain);
     226           1 :         EXPECTED_THROW(domain_only);
     227           1 :         EXPECTED_THROW(tld_only);
     228             : 
     229           1 :         if(bad.category() != TLD_CATEGORY_UNDEFINED)
     230             :         {
     231           0 :             error("error: bad.category() did not return UNDEFINED as expected.");
     232             :         }
     233             : 
     234           1 :         if(bad.country() != "")
     235             :         {
     236           0 :             error("error: bad.country() did not return \"\" as expected, got \"" + bad.country() + "\" instead.");
     237             :         }
     238             :     }
     239             : 
     240             :     {
     241           1 :         if(verbose)
     242             :         {
     243           0 :             printf("testing \"el.eritrea.er\"\n");
     244             :         }
     245             : 
     246             :         // invalid TLD
     247           2 :         std::string uri("el.eritrea.er");
     248           2 :         tld_object bad(uri);
     249             : 
     250           1 :         if(bad.is_valid())
     251             :         {
     252             :             char r[32], s[32];
     253           0 :             snprintf(r, sizeof(r), "%d", bad.result());
     254           0 :             snprintf(s, sizeof(s), "%d", bad.status());
     255           0 :             error(std::string("error:test_invalid(3): bad.is_valid() of \"\" result is true?! (result: ") + r + ", status: " + s + ").");
     256           0 :             return;
     257             :         }
     258             : 
     259           1 :         if(bad.status() != TLD_STATUS_UNUSED)
     260             :         {
     261           0 :             error("error: bad.status() of \"\" is not UNUSED.");
     262           0 :             return;
     263             :         }
     264             : 
     265           1 :         if(bad.result() != TLD_RESULT_INVALID)
     266             :         {
     267             :             char r[32];
     268           0 :             snprintf(r, sizeof(r), "%d", bad.result());
     269           0 :             error(std::string("error: bad.result() of \"\" is ") + r + " instead of INVALID (1).");
     270           0 :             return;
     271             :         }
     272             : 
     273             :         // TBD: if we clear the f_domain then this would be ""
     274           1 :         if(bad.domain() != "el.eritrea.er")
     275             :         {
     276           0 :             error("error:test_invalid(3): bad.domain() did not return \"\" as expected.");
     277             :         }
     278             : 
     279           1 :         EXPECTED_THROW(sub_domains);
     280           1 :         EXPECTED_THROW(full_domain);
     281           1 :         EXPECTED_THROW(domain_only);
     282           1 :         EXPECTED_THROW(tld_only);
     283             : 
     284           1 :         if(bad.category() != TLD_CATEGORY_COUNTRY)
     285             :         {
     286           0 :             error("error: bad.category() did not return COUNTRY as expected.");
     287             :         }
     288             : 
     289           1 :         if(bad.country() != "Eritrea")
     290             :         {
     291           0 :             error("error: bad.country() did not return \"Eritrea\" as expected, got \"" + bad.country() + "\" instead.");
     292             :         }
     293             :     }
     294             : }
     295             : 
     296             : 
     297             : 
     298           1 : int main(int argc, char *argv[])
     299             : {
     300           1 :     printf("testing tld object version %s\n", tld_version());
     301             : 
     302           1 :     if(argc > 1)
     303             :     {
     304           0 :         if(strcmp(argv[1], "-v") == 0)
     305             :         {
     306           0 :             verbose = 1;
     307             :         }
     308             :     }
     309             : 
     310             :     /* Call all the tests, one by one.
     311             :      *
     312             :      * Failures are "recorded" in the err_count global variable
     313             :      * and the process stops with an error message and exit(1)
     314             :      * if err_count is not zero.
     315             :      *
     316             :      * Exceptions that should not occur are expected to also
     317             :      * be caught and reported as errors.
     318             :      */
     319             :     try
     320             :     {
     321           1 :         test_valid_uri("test-with-a-dash.mat.br", ".mat.br", "test-with-a-dash", "", TLD_CATEGORY_COUNTRY, "Brazil"); // no sub-domains
     322           1 :         test_valid_uri("www.m2osw.com", ".com", "m2osw", "www", TLD_CATEGORY_INTERNATIONAL, NULL); // one sub-domains (standard .com)
     323           1 :         test_valid_uri("test.valid.uri.domain.com.ac", ".com.ac", "domain", "test.valid.uri", TLD_CATEGORY_COUNTRY, "Ascension Island"); // many sub-domains
     324           1 :         test_valid_uri("sub-domain.www.ck", ".ck", "www", "sub-domain", TLD_CATEGORY_COUNTRY, "Cook Islands"); // exception test
     325             : 
     326           1 :         test_invalid();
     327             :     }
     328           0 :     catch(const invalid_domain&)
     329             :     {
     330           0 :         error("error: caught an exception when everything is expected to be valid.");
     331             :     }
     332             : 
     333           1 :     if(err_count)
     334             :     {
     335           0 :         fprintf(stderr, "%d error%s occured.\n",
     336           0 :                     err_count, err_count != 1 ? "s" : "");
     337             :     }
     338           1 :     exit(err_count ? 1 : 0);
     339             : }
     340             : 
     341             : /* vim: ts=4 sw=4 et
     342             :  */

Generated by: LCOV version 1.12