FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_regex.cc
1 /*********************************************************************
2 Copyright (C) 2019, Siemens AG
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 *********************************************************************/
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
23 #include <boost/regex.hpp>
24 
25 #include "ojoregex.hpp"
26 
27 using namespace std;
28 
33 class regexTest : public CPPUNIT_NS :: TestFixture {
34  CPPUNIT_TEST_SUITE (regexTest);
35  CPPUNIT_TEST (regTest);
36  CPPUNIT_TEST (badNameTest);
37 
38  CPPUNIT_TEST_SUITE_END ();
39 
40 protected:
51  void regTest (void) {
52 
53  const std::string gplLicense = "GPL-2.0";
54  const std::string lgplLicense = "LGPL-2.1+";
55  std::string content = "SPDX-License-Identifier: " + gplLicense + " AND "
56  + lgplLicense;
57  boost::regex listRegex(SPDX_LICENSE_LIST, boost::regex_constants::icase);
58  boost::regex nameRegex(SPDX_LICENSE_NAMES, boost::regex_constants::icase);
59 
60  std::string::const_iterator begin = content.begin();
61  std::string::const_iterator end = content.end();
62  boost::match_results<std::string::const_iterator> what;
63 
64  string licenseList;
65  boost::regex_search(begin, end, what, listRegex);
66  licenseList = what[1].str();
67 
68  // Check if the correct license list is found
69  CPPUNIT_ASSERT_EQUAL(gplLicense + " AND " + lgplLicense, licenseList);
70 
71  // Find the actual licenses in the list
72  begin = licenseList.begin();
73  end = licenseList.end();
74  list<string> licensesFound;
75 
76  while (begin != end)
77  {
78  boost::smatch res;
79  if (boost::regex_search(begin, end, res, nameRegex))
80  {
81  licensesFound.push_back(res[1].str());
82  begin = res[0].second;
83  }
84  else
85  {
86  break;
87  }
88  }
89 
90  size_t expectedNos = 2;
91  size_t actualNos = licensesFound.size();
92  // Check if 2 licenses are found
93  CPPUNIT_ASSERT_EQUAL(expectedNos, actualNos);
94  // Check if the result contains the expected string
95  CPPUNIT_ASSERT(
96  std::find(licensesFound.begin(), licensesFound.end(), gplLicense)
97  != licensesFound.end());
98  CPPUNIT_ASSERT(
99  std::find(licensesFound.begin(), licensesFound.end(), lgplLicense)
100  != licensesFound.end());
101  };
102 
113  void badNameTest (void) {
114 
115  const std::string gplLicense = "GPL-2.0";
116  const std::string badLicense = "AB";
117  std::string content = "SPDX-License-Identifier: " + gplLicense + " AND "
118  + badLicense;
119  boost::regex listRegex(SPDX_LICENSE_LIST, boost::regex_constants::icase);
120  boost::regex nameRegex(SPDX_LICENSE_NAMES, boost::regex_constants::icase);
121 
122  std::string::const_iterator begin = content.begin();
123  std::string::const_iterator end = content.end();
124  boost::match_results<std::string::const_iterator> what;
125 
126  string licenseList;
127  boost::regex_search(begin, end, what, listRegex);
128  licenseList = what[1].str();
129 
130  // Check if only correct license is found
131  CPPUNIT_ASSERT_EQUAL(gplLicense, licenseList);
132 
133  // Find the actual license in the list
134  begin = licenseList.begin();
135  end = licenseList.end();
136  list<string> licensesFound;
137 
138  while (begin != end)
139  {
140  boost::smatch res;
141  if (boost::regex_search(begin, end, res, nameRegex))
142  {
143  licensesFound.push_back(res[1].str());
144  begin = res[0].second;
145  }
146  else
147  {
148  break;
149  }
150  }
151 
152  size_t expectedNos = 1;
153  size_t actualNos = licensesFound.size();
154  // Check if only 1 license is found
155  CPPUNIT_ASSERT_EQUAL(expectedNos, actualNos);
156  // Check if the result contains the expected string
157  CPPUNIT_ASSERT(
158  std::find(licensesFound.begin(), licensesFound.end(), gplLicense)
159  != licensesFound.end());
160  // Check if the result does not contain the bad string
161  CPPUNIT_ASSERT(
162  std::find(licensesFound.begin(), licensesFound.end(), badLicense)
163  == licensesFound.end());
164  };
165 
166 };
167 
168 CPPUNIT_TEST_SUITE_REGISTRATION( regexTest );
void regTest(void)
Test regex on a test string.
Definition: test_regex.cc:51
#define SPDX_LICENSE_NAMES
Regex to filter license names from list of license list.
Definition: ojoregex.hpp:44
void badNameTest(void)
Test regex on a string with bad identifier.
Definition: test_regex.cc:113
#define SPDX_LICENSE_LIST
Regex to filter the list of licenses.
Definition: ojoregex.hpp:35
Test fixture to test regex accuracy.
Definition: test_regex.cc:33