wpkg test coverage results

Coverage test results of the Windows Packager by Made to Order Software Corporation.

LCOV - code coverage report
Current view: top level - tests/unittests - unittest_version.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 113 125 90.4 %
Date: 2013-06-17 Functions: 6 7 85.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*    unittest_version.cpp
       2             :  *    Copyright (C) 2013  Made to Order Software Corporation
       3             :  *
       4             :  *    This program is free software; you can redistribute it and/or modify
       5             :  *    it under the terms of the GNU General Public License as published by
       6             :  *    the Free Software Foundation; either version 2 of the License, or
       7             :  *    (at your option) any later version.
       8             :  *
       9             :  *    This program is distributed in the hope that it will be useful,
      10             :  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  *    GNU General Public License for more details.
      13             :  *
      14             :  *    You should have received a copy of the GNU General Public License along
      15             :  *    with this program; if not, write to the Free Software Foundation, Inc.,
      16             :  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      17             :  *
      18             :  *    Authors
      19             :  *    Alexis Wilke   alexis@m2osw.com
      20             :  */
      21             : 
      22             : #include "unittest_version.h"
      23             : #include "libdebpackages/debian_version.h"
      24             : #include <stdexcept>
      25             : #include <cstring>
      26             : 
      27           1 : CPPUNIT_TEST_SUITE_REGISTRATION( VersionUnitTests );
      28             : 
      29           2 : void VersionUnitTests::setUp()
      30             : {
      31           2 : }
      32             : 
      33             : namespace
      34             : {
      35             : 
      36           0 : std::string print_version(const std::string& version)
      37             : {
      38           0 :     std::stringstream result;
      39           0 :     for(const char *s(version.c_str()); *s != '\0'; ++s) {
      40           0 :         if(static_cast<unsigned char>(*s) < ' ') result << "^" << static_cast<char>(*s + '@');
      41           0 :         else if(static_cast<unsigned char>(*s) >= 0x80) result << "\\x" << std::hex << static_cast<int>(static_cast<unsigned char>(*s));
      42           0 :         else if(*s == 0x7F) result << "<DEL>";
      43           0 :         else if(*s == '^') result << "^^";
      44           0 :         else if(*s == '@') result << "@@";
      45           0 :         else result << *s;
      46             :     }
      47           0 :     return result.str();
      48             : }
      49             : 
      50       40201 : void check_version(char const * const version, char const * const error_msg)
      51             : {
      52             :     // validate_debian_version()
      53             :     {
      54             :         char error_string[256];
      55       40201 :         strcpy(error_string, "no errors");
      56       40201 :         int valid(validate_debian_version(version, error_string, sizeof(error_string) / sizeof(error_string[0])));
      57             : //printf("from {%s} result = %d [%s]\n", print_version(version).c_str(), valid, error_string);
      58       40201 :         if(error_msg == 0)
      59             :         {
      60             :             // in this case it must be valid
      61       40001 :             CPPUNIT_ASSERT(valid == 1);
      62       40001 :             CPPUNIT_ASSERT(strcmp(error_string, "no errors") == 0); // err buffer unchanged
      63             :         }
      64             :         else
      65             :         {
      66         200 :             CPPUNIT_ASSERT(valid == 0);
      67         200 :             CPPUNIT_ASSERT(strcmp(error_msg, error_string) == 0);
      68             :         }
      69             :     }
      70       40201 : }
      71             : 
      72             : //DEBIAN_PACKAGE_EXPORT int validate_debian_version(const char *string, char *error_string, size_t error_size);
      73             : //DEBIAN_PACKAGE_EXPORT debian_version_handle_t string_to_debian_version(const char *string, char *error_string, size_t error_size);
      74             : //DEBIAN_PACKAGE_EXPORT int debian_version_to_string(const debian_version_handle_t debian_version, char *string, size_t string_size);
      75             : //DEBIAN_PACKAGE_EXPORT int debian_versions_compare(const debian_version_handle_t left, const debian_version_handle_t right);
      76             : //DEBIAN_PACKAGE_EXPORT void delete_debian_version(debian_version_handle_t debian_version);
      77             : }
      78             : 
      79             : 
      80           1 : void VersionUnitTests::valid_versions()
      81             : {
      82           1 :     check_version("1.0", NULL);
      83             : 
      84             :     // many valid versions generated randomly to increase the likelyhood
      85             :     // of things I would otherwise not think of
      86       10001 :     for(int i(0); i < 10000; ++i)
      87             :     {
      88             :         // WARNING: in the sizeof() before we do a -1 to ignore the '\0' at the end of the string
      89       10000 :         const char valid_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+.~:-";
      90             : 
      91             :         // simple version (no epoch/revision)
      92             :         {
      93       10000 :             std::stringstream ss;
      94       10000 :             ss << rand() % 10;
      95       10000 :             std::string v(ss.str());
      96       10000 :             int size(rand() % 20);
      97      104670 :             for(int j(0); j < size; ++j)
      98             :             {
      99             :                 // we do not include the : and - from the valid chars
     100       94670 :                 char c(valid_chars[rand() % (sizeof(valid_chars) / sizeof(valid_chars[0]) - 3)]);
     101       94670 :                 v += c;
     102             :             }
     103       10000 :             check_version(v.c_str(), NULL);
     104             :         }
     105             : 
     106             :         // epoch + version
     107             :         {
     108       10000 :             std::stringstream ss;
     109       10000 :             ss << rand() % 0x7FFFFFFF << ":" << rand() % 10;
     110       10000 :             std::string v(ss.str());
     111       10000 :             int size(rand() % 20);
     112      104483 :             for(int j(0); j < size; ++j)
     113             :             {
     114             :                 // we do not include the - from the valid chars
     115       94483 :                 char c(valid_chars[rand() % (sizeof(valid_chars) / sizeof(valid_chars[0]) - 2)]);
     116       94483 :                 v += c;
     117             :             }
     118       10000 :             check_version(v.c_str(), NULL);
     119             :         }
     120             : 
     121             :         // version + revision
     122             :         {
     123       10000 :             std::stringstream ss;
     124       10000 :             ss << rand() % 10;
     125       10000 :             std::string v(ss.str());
     126       10000 :             int vsize(rand() % 20);
     127      104084 :             for(int j(0); j < vsize; ++j)
     128             :             {
     129       94084 :                 char c(valid_chars[rand() % (sizeof(valid_chars) / sizeof(valid_chars[0]) - 2)]);
     130       94084 :                 if(c == ':')
     131             :                 {
     132        1425 :                     c = '-';
     133             :                 }
     134       94084 :                 v += c;
     135             :             }
     136       10000 :             v += '-';
     137       10000 :             int rsize(rand() % 20 + 1);
     138      115761 :             for(int j(0); j < rsize; ++j)
     139             :             {
     140      105761 :                 char c(valid_chars[rand() % (sizeof(valid_chars) / sizeof(valid_chars[0]) - 3)]);
     141      105761 :                 v += c;
     142             :             }
     143       10000 :             check_version(v.c_str(), NULL);
     144             :         }
     145             : 
     146             :         // epoch + version + revision
     147             :         {
     148       10000 :             std::stringstream ss;
     149       10000 :             ss << rand() % 0x7FFFFFFF << ":" << rand() % 10;
     150       10000 :             std::string v(ss.str());
     151       10000 :             int vsize(rand() % 20 + 1);
     152      114899 :             for(int j(0); j < vsize; ++j)
     153             :             {
     154      104899 :                 char c(valid_chars[rand() % (sizeof(valid_chars) / sizeof(valid_chars[0]) - 1)]);
     155      104899 :                 v += c;
     156             :             }
     157       10000 :             v += '-';
     158       10000 :             int rsize(rand() % 20 + 1);
     159      115262 :             for(int j(0); j < rsize; ++j)
     160             :             {
     161      105262 :                 char c(valid_chars[rand() % (sizeof(valid_chars) / sizeof(valid_chars[0]) - 3)]);
     162      105262 :                 v += c;
     163             :             }
     164       10000 :             check_version(v.c_str(), NULL);
     165             :         }
     166             :     }
     167           1 : }
     168             : 
     169             : 
     170           1 : void VersionUnitTests::invalid_versions()
     171             : {
     172             :     // empty
     173           1 :     check_version("", "invalid version, digit expected as first character");
     174             : 
     175             :     // epoch
     176           1 :     check_version(":", "empty epoch");
     177           1 :     check_version("a:", "non-decimal epoch");
     178           1 :     check_version("-10:", "non-decimal epoch");
     179           1 :     check_version("99999999999999999:", "invalid decimal epoch");
     180           1 :     check_version("3:", "invalid version, digit expected as first character");
     181             : 
     182             :     // revision
     183           1 :     check_version("-", "empty revision");
     184           1 :     check_version("--", "empty revision");
     185           1 :     check_version("+-", "empty revision");
     186           1 :     check_version("#-", "empty revision");
     187           1 :     check_version("-a", "invalid version, digit expected as first character");
     188           1 :     check_version("-0", "invalid version, digit expected as first character");
     189           1 :     check_version("-+", "invalid version, digit expected as first character");
     190           1 :     check_version("-3$7", "invalid character in revision");
     191           1 :     check_version("-3.7", "invalid version, digit expected as first character");
     192             : 
     193             :     // version
     194           1 :     check_version("3.7#", "invalid character in version");
     195           1 :     check_version("3$7", "invalid character in version");
     196             : 
     197         256 :     for(int i(1); i < 256; ++i)
     198             :     {
     199         255 :         const char valid_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz:-+.~";
     200         255 :         char c(static_cast<char>(i));
     201         255 :         if(strchr(valid_chars, c) != NULL)
     202             :         {
     203             :             // skip all valid characters
     204          67 :             continue;
     205             :         }
     206         188 :         std::string v;
     207        2444 :         for(int j(0); j < 12; ++j)
     208             :         {
     209        2256 :             if(v.empty() || v[v.length() - 1] == '-' || v[v.length() - 1] == ':')
     210             :             {
     211         227 :                 std::stringstream ss;
     212         227 :                 ss << rand() % 10;
     213         227 :                 v += ss.str();
     214             :             }
     215        2256 :             if(j == 6)
     216             :             {
     217             :                 // add the spurious character now
     218         188 :                 v += c;
     219             :             }
     220        2256 :             char vc(valid_chars[rand() % (sizeof(valid_chars) / sizeof(valid_chars[0]) - 1)]);
     221        2256 :             if(vc == ':')
     222             :             {
     223          33 :                 if(strchr(v.c_str(), ':') == NULL)
     224             :                 {
     225             :                     // on first ':' ensure epoch is a number
     226          32 :                     std::string::size_type p = v.find_first_not_of("0123456789");
     227          32 :                     if(p != std::string::npos)
     228             :                     {
     229             :                         // not a number, create such
     230          28 :                         std::stringstream ss;
     231          28 :                         if(v[0] < '0' || v[0] > '9')
     232             :                         {
     233             :                             // first character must be a digit
     234           0 :                             ss << rand() % 10;
     235           0 :                             v = ss.str() + v;
     236             :                         }
     237          28 :                         ss << rand();
     238          28 :                         v = ss.str() + ":" + v;
     239          28 :                         continue;
     240             :                     }
     241             :                 }
     242             :             }
     243        2228 :             v += vc;
     244             :         }
     245             :         // check whether the bad character was inserted after the last dash
     246             :         {
     247         188 :             std::string::size_type p(v.find_last_of("-"));
     248         188 :             if(p == v.length() - 1)
     249             :             {
     250           5 :                 std::stringstream ss;
     251           5 :                 ss << rand() % 10;
     252           5 :                 v += ss.str();
     253             :             }
     254         183 :             else if(v.find_first_of(c, p) == std::string::npos)
     255             :             {
     256         165 :                 check_version(v.c_str(), "invalid character in version");
     257             :             }
     258             :             else
     259             :             {
     260          18 :                 check_version(v.c_str(), "invalid character in revision");
     261             :             }
     262             :         }
     263         188 :     }
     264           4 : }
     265             : 
     266             : 
     267             : 
     268             : 
     269             : 
     270             : 
     271             : 
     272             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.9

The wpkg tool is an open source tool created by Made to Order Software Corporation.