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_control.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 121 130 93.1 %
Date: 2013-06-17 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*    unittest_control.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_control.h"
      23             : #include "unittest_main.h"
      24             : #include <stdexcept>
      25             : #include <cstring>
      26             : 
      27           1 : CPPUNIT_TEST_SUITE_REGISTRATION( ControlUnitTests );
      28             : 
      29           2 : void ControlUnitTests::setUp()
      30             : {
      31           2 : }
      32             : 
      33             : 
      34             : 
      35           1 : void ControlUnitTests::files_field_to_list()
      36             : {
      37           1 :     const wpkg_control::file_list_t files;
      38           1 :     CPPUNIT_ASSERT(to_string(files).empty());
      39           1 : }
      40             : 
      41             : 
      42             : 
      43           4 : void ControlUnitTests::check_field(const std::string& field_name, const std::string& default_format, wpkg_control::file_item::format_t sources_format)
      44             : {
      45             :     {
      46           4 :         std::tr1::shared_ptr<wpkg_control::control_file> ctrl(new wpkg_control::control_file);
      47             : 
      48             :         // test with a simple list
      49             :         ctrl->set_field(field_name, "list\n"
      50             :             "/etc/t1.conf\n"
      51             :             "/usr/bin/t1\n"
      52             :             "/usr/share/doc/t1/copyright\n"
      53             :             "\"/usr/share/doc/t 1/copyright\"\n"
      54           4 :         );
      55             : 
      56           4 :         if(field_name != "Conf-Files")
      57             :         {
      58             :             try
      59             :             {
      60           3 :                 const wpkg_control::file_list_t invalid_files(ctrl->get_files("Conf-Files"));
      61           3 :                 CPPUNIT_ASSERT(!"get_files() with the wrong name succeeded");
      62             :             }
      63           3 :             catch(const wpkg_control::wpkg_control_exception_undefined&)
      64             :             {
      65             :                 // success
      66             :             }
      67             :         }
      68             : 
      69           4 :         const wpkg_control::file_list_t files(ctrl->get_files(field_name));
      70           4 :         CPPUNIT_ASSERT(files.size() == 4);
      71             : 
      72             :         // file 0
      73           4 :         CPPUNIT_ASSERT(files[0].get_format() == wpkg_control::file_item::format_list);
      74           4 :         CPPUNIT_ASSERT(files[0].best_format(wpkg_control::file_item::format_unknown) == wpkg_control::file_item::format_unknown);
      75           4 :         CPPUNIT_ASSERT(files[0].best_format(wpkg_control::file_item::format_list) == wpkg_control::file_item::format_list);
      76           4 :         CPPUNIT_ASSERT(files[0].get_filename() == "/etc/t1.conf");
      77           4 :         CPPUNIT_ASSERT(files[0].get_mode() == 0);
      78           4 :         CPPUNIT_ASSERT(files[0].get_size() == 0);
      79           4 :         CPPUNIT_ASSERT(files[0].get_checksum() == "");
      80             : 
      81             :         // file 1
      82           4 :         CPPUNIT_ASSERT(files[1].get_format() == wpkg_control::file_item::format_list);
      83           4 :         CPPUNIT_ASSERT(files[1].best_format(wpkg_control::file_item::format_list) == wpkg_control::file_item::format_list);
      84           4 :         CPPUNIT_ASSERT(files[1].get_filename() == "/usr/bin/t1");
      85           4 :         CPPUNIT_ASSERT(files[1].get_mode() == 0);
      86           4 :         CPPUNIT_ASSERT(files[1].get_size() == 0);
      87           4 :         CPPUNIT_ASSERT(files[1].get_checksum() == "");
      88             : 
      89             :         // file 2
      90           4 :         CPPUNIT_ASSERT(files[2].get_format() == wpkg_control::file_item::format_list);
      91           4 :         CPPUNIT_ASSERT(files[2].best_format(wpkg_control::file_item::format_list) == wpkg_control::file_item::format_list);
      92           4 :         CPPUNIT_ASSERT(files[2].get_filename() == "/usr/share/doc/t1/copyright");
      93           4 :         CPPUNIT_ASSERT(files[2].get_mode() == 0);
      94           4 :         CPPUNIT_ASSERT(files[2].get_size() == 0);
      95           4 :         CPPUNIT_ASSERT(files[2].get_checksum() == "");
      96             : 
      97             :         // file 3
      98           4 :         CPPUNIT_ASSERT(files[3].get_format() == wpkg_control::file_item::format_list);
      99           4 :         CPPUNIT_ASSERT(files[3].best_format(wpkg_control::file_item::format_list) == wpkg_control::file_item::format_list);
     100           4 :         CPPUNIT_ASSERT(files[3].get_filename() == "/usr/share/doc/t 1/copyright");
     101           4 :         CPPUNIT_ASSERT(files[3].get_mode() == 0);
     102           4 :         CPPUNIT_ASSERT(files[3].get_size() == 0);
     103           4 :         CPPUNIT_ASSERT(files[3].get_checksum() == "");
     104             :     }
     105             : 
     106             :     {
     107           4 :         std::tr1::shared_ptr<wpkg_control::control_file> ctrl(new wpkg_control::control_file);
     108             : 
     109             :         // test with the conffiles format
     110           4 :         ctrl->set_field(field_name, (default_format == "conffiles" ? "" : "conffiles") + std::string("\n"
     111             :             "/etc/t2.conf 0123456789abcdef0123456789abcdef\n"
     112             :             "/usr/bin/t2 cdef0123456789abcdef0123456789ab\n"
     113             :             "/usr/share/doc/t2/copyright 021346578a9bcedf021346578a9bcedf\n"
     114             :             "\"/usr/share/doc/t 2/copyright\" 021346578a9bcedf021346578a9bcedf\n")
     115           8 :         );
     116             : 
     117           4 :         if(field_name != "Files")
     118             :         {
     119             :             try
     120             :             {
     121           3 :                 const wpkg_control::file_list_t invalid_files(ctrl->get_files("Files"));
     122           3 :                 CPPUNIT_ASSERT(!"get_files() with the wrong name succeeded");
     123             :             }
     124           3 :             catch(const wpkg_control::wpkg_control_exception_undefined&)
     125             :             {
     126             :                 // success
     127             :             }
     128             :         }
     129             : 
     130           4 :         const wpkg_control::file_list_t files(ctrl->get_files(field_name));
     131           4 :         CPPUNIT_ASSERT(files.size() == 4);
     132             : 
     133             :         // file 0
     134           4 :         CPPUNIT_ASSERT(files[0].get_format() == wpkg_control::file_item::format_conffiles);
     135           4 :         CPPUNIT_ASSERT(files[0].best_format(wpkg_control::file_item::format_unknown) == wpkg_control::file_item::format_unknown);
     136           4 :         CPPUNIT_ASSERT(files[0].best_format(wpkg_control::file_item::format_list) == wpkg_control::file_item::format_conffiles);
     137           4 :         CPPUNIT_ASSERT(files[0].get_filename() == "/etc/t2.conf");
     138           4 :         CPPUNIT_ASSERT(files[0].get_mode() == 0);
     139           4 :         CPPUNIT_ASSERT(files[0].get_size() == 0);
     140           4 :         CPPUNIT_ASSERT(files[0].get_checksum() == "0123456789abcdef0123456789abcdef");
     141             : 
     142             :         // file 1
     143           4 :         CPPUNIT_ASSERT(files[1].get_format() == wpkg_control::file_item::format_conffiles);
     144           4 :         CPPUNIT_ASSERT(files[1].best_format(wpkg_control::file_item::format_list) == wpkg_control::file_item::format_conffiles);
     145           4 :         CPPUNIT_ASSERT(files[1].get_filename() == "/usr/bin/t2");
     146           4 :         CPPUNIT_ASSERT(files[1].get_mode() == 0);
     147           4 :         CPPUNIT_ASSERT(files[1].get_size() == 0);
     148           4 :         CPPUNIT_ASSERT(files[1].get_checksum() == "cdef0123456789abcdef0123456789ab");
     149             : 
     150             :         // file 2
     151           4 :         CPPUNIT_ASSERT(files[2].get_format() == wpkg_control::file_item::format_conffiles);
     152           4 :         CPPUNIT_ASSERT(files[2].best_format(wpkg_control::file_item::format_list) == wpkg_control::file_item::format_conffiles);
     153           4 :         CPPUNIT_ASSERT(files[2].get_filename() == "/usr/share/doc/t2/copyright");
     154           4 :         CPPUNIT_ASSERT(files[2].get_mode() == 0);
     155           4 :         CPPUNIT_ASSERT(files[2].get_size() == 0);
     156           4 :         CPPUNIT_ASSERT(files[2].get_checksum() == "021346578a9bcedf021346578a9bcedf");
     157             : 
     158             :         // file 3
     159           4 :         CPPUNIT_ASSERT(files[3].get_format() == wpkg_control::file_item::format_conffiles);
     160           4 :         CPPUNIT_ASSERT(files[3].best_format(wpkg_control::file_item::format_list) == wpkg_control::file_item::format_conffiles);
     161           4 :         CPPUNIT_ASSERT(files[3].get_filename() == "/usr/share/doc/t 2/copyright");
     162           4 :         CPPUNIT_ASSERT(files[3].get_mode() == 0);
     163           4 :         CPPUNIT_ASSERT(files[3].get_size() == 0);
     164           4 :         CPPUNIT_ASSERT(files[3].get_checksum() == "021346578a9bcedf021346578a9bcedf");
     165             :     }
     166             : 
     167             :     {
     168           4 :         std::tr1::shared_ptr<wpkg_control::control_file> ctrl(new wpkg_control::control_file);
     169             : 
     170             :         // test with the conffiles format
     171           4 :         std::string fmt;
     172           4 :         if(default_format != "sources" && default_format != "md5sum")
     173             :         {
     174           1 :             switch(rand() & 3)
     175             :             {
     176             :             case 0:
     177           0 :                 fmt = "sources";
     178           0 :                 sources_format = wpkg_control::file_item::format_md5sum;
     179           0 :                 break;
     180             : 
     181             :             case 1:
     182           0 :                 fmt = "md5sum";
     183           0 :                 sources_format = wpkg_control::file_item::format_md5sum;
     184           0 :                 break;
     185             : 
     186             :             case 2:
     187           0 :                 fmt = "sha1";
     188           0 :                 sources_format = wpkg_control::file_item::format_sha1;
     189           0 :                 break;
     190             : 
     191             :             case 3:
     192           1 :                 fmt = "sha256";
     193           1 :                 sources_format = wpkg_control::file_item::format_sha256;
     194           1 :                 break;
     195             : 
     196             :             }
     197             :         }
     198             :         ctrl->set_field(field_name, fmt + "\n"
     199             :             "0123456789abcdef0123456789abcdef 1234 /etc/t3.conf\n"
     200             :             "\"cdef0123456789abcdef0123456789ab\" 3455 /usr/bin/t3\n"
     201             :             "021346578a9bcedf021346578a9bcedf \"1122\" /usr/share/doc/t3/copyright\n"
     202             :             "021346578a900000021346578a9bcedf 333 \"/usr/share/doc/t 3/index.html\"\n"
     203           4 :         );
     204             : 
     205           4 :         if(field_name != "Checksums-Sha1")
     206             :         {
     207             :             try
     208             :             {
     209           3 :                 const wpkg_control::file_list_t invalid_files(ctrl->get_files("Checksums-Sha1"));
     210           3 :                 CPPUNIT_ASSERT(!"get_files() with the wrong name succeeded");
     211             :             }
     212           3 :             catch(const wpkg_control::wpkg_control_exception_undefined&)
     213             :             {
     214             :                 // success
     215             :             }
     216             :         }
     217             : 
     218           4 :         const wpkg_control::file_list_t files(ctrl->get_files(field_name));
     219           4 :         CPPUNIT_ASSERT(files.size() == 4);
     220             : 
     221             :         // file 0
     222           4 :         CPPUNIT_ASSERT(files[0].get_format() == sources_format);
     223           4 :         CPPUNIT_ASSERT(files[0].best_format(wpkg_control::file_item::format_unknown) == wpkg_control::file_item::format_unknown);
     224           4 :         CPPUNIT_ASSERT(files[0].best_format(wpkg_control::file_item::format_list) == sources_format);
     225           4 :         CPPUNIT_ASSERT(files[0].get_filename() == "/etc/t3.conf");
     226           4 :         CPPUNIT_ASSERT(files[0].get_mode() == 0);
     227           4 :         CPPUNIT_ASSERT(files[0].get_size() == 1234);
     228           4 :         CPPUNIT_ASSERT(files[0].get_checksum() == "0123456789abcdef0123456789abcdef");
     229             : 
     230             :         // file 1
     231           4 :         CPPUNIT_ASSERT(files[1].get_format() == sources_format);
     232           4 :         CPPUNIT_ASSERT(files[1].best_format(wpkg_control::file_item::format_list) == sources_format);
     233           4 :         CPPUNIT_ASSERT(files[1].get_filename() == "/usr/bin/t3");
     234           4 :         CPPUNIT_ASSERT(files[1].get_mode() == 0);
     235           4 :         CPPUNIT_ASSERT(files[1].get_size() == 3455);
     236           4 :         CPPUNIT_ASSERT(files[1].get_checksum() == "cdef0123456789abcdef0123456789ab");
     237             : 
     238             :         // file 2
     239           4 :         CPPUNIT_ASSERT(files[2].get_format() == sources_format);
     240           4 :         CPPUNIT_ASSERT(files[2].best_format(wpkg_control::file_item::format_list) == sources_format);
     241           4 :         CPPUNIT_ASSERT(files[2].get_filename() == "/usr/share/doc/t3/copyright");
     242           4 :         CPPUNIT_ASSERT(files[2].get_mode() == 0);
     243           4 :         CPPUNIT_ASSERT(files[2].get_size() == 1122);
     244           4 :         CPPUNIT_ASSERT(files[2].get_checksum() == "021346578a9bcedf021346578a9bcedf");
     245             : 
     246             :         // file 3
     247           4 :         CPPUNIT_ASSERT(files[3].get_format() == sources_format);
     248           4 :         CPPUNIT_ASSERT(files[3].best_format(wpkg_control::file_item::format_list) == sources_format);
     249           4 :         CPPUNIT_ASSERT(files[3].get_filename() == "/usr/share/doc/t 3/index.html");
     250           4 :         CPPUNIT_ASSERT(files[3].get_mode() == 0);
     251           4 :         CPPUNIT_ASSERT(files[3].get_size() == 333);
     252           4 :         CPPUNIT_ASSERT(files[3].get_checksum() == "021346578a900000021346578a9bcedf");
     253             :     }
     254             : 
     255           4 : }
     256             : 
     257           1 : void ControlUnitTests::all_files_field()
     258             : {
     259           1 :     check_field("Checksums-Sha1", "sources", wpkg_control::file_item::format_sha1);
     260           1 :     check_field("Checksums-Sha256", "sources", wpkg_control::file_item::format_sha256);
     261           1 :     check_field("Conf-Files", "conffiles", wpkg_control::file_item::format_md5sum);
     262           1 :     check_field("Files", "sources", wpkg_control::file_item::format_md5sum);
     263           4 : }
     264             : 
     265             : 
     266             : 
     267             : 
     268             : // 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.