FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_regex.cc
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2019 Siemens AG
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
10 #include <cppunit/TestFixture.h>
11 #include <cppunit/extensions/HelperMacros.h>
12 #include <boost/regex.hpp>
13 
14 #include "ojoregex.hpp"
15 
16 using namespace std;
17 
22 class regexTest : public CPPUNIT_NS :: TestFixture {
23  CPPUNIT_TEST_SUITE (regexTest);
24  CPPUNIT_TEST (regTest);
25  CPPUNIT_TEST (badNameTest);
26  CPPUNIT_TEST (regTestSpecialEnd);
27 
28  CPPUNIT_TEST_SUITE_END ();
29 
30 protected:
41  void regTest (void) {
42 
43  const std::string gplLicense = "GPL-2.0";
44  const std::string lgplLicense = "LGPL-2.1+";
45  // REUSE-IgnoreStart
46  std::string content = "SPDX-License-Identifier: " + gplLicense + " AND "
47  + lgplLicense;
48  // REUSE-IgnoreStart
49  boost::regex listRegex(SPDX_LICENSE_LIST, boost::regex_constants::icase);
50  boost::regex nameRegex(SPDX_LICENSE_NAMES, boost::regex_constants::icase);
51 
52  std::string::const_iterator begin = content.begin();
53  std::string::const_iterator end = content.end();
54  boost::match_results<std::string::const_iterator> what;
55 
56  string licenseList;
57  boost::regex_search(begin, end, what, listRegex);
58  licenseList = what[1].str();
59 
60  // Check if the correct license list is found
61  CPPUNIT_ASSERT_EQUAL(gplLicense + " AND " + lgplLicense, licenseList);
62 
63  // Find the actual licenses in the list
64  begin = licenseList.begin();
65  end = licenseList.end();
66  list<string> licensesFound;
67 
68  while (begin != end)
69  {
70  boost::smatch res;
71  if (boost::regex_search(begin, end, res, nameRegex))
72  {
73  licensesFound.push_back(res[1].str());
74  begin = res[0].second;
75  }
76  else
77  {
78  break;
79  }
80  }
81 
82  size_t expectedNos = 2;
83  size_t actualNos = licensesFound.size();
84  // Check if 2 licenses are found
85  CPPUNIT_ASSERT_EQUAL(expectedNos, actualNos);
86  // Check if the result contains the expected string
87  CPPUNIT_ASSERT(
88  std::find(licensesFound.begin(), licensesFound.end(), gplLicense)
89  != licensesFound.end());
90  CPPUNIT_ASSERT(
91  std::find(licensesFound.begin(), licensesFound.end(), lgplLicense)
92  != licensesFound.end());
93  };
94 
105  void badNameTest (void) {
106 
107  const std::string gplLicense = "GPL-2.0";
108  const std::string badLicense = "AB";
109  // REUSE-IgnoreStart
110  std::string content = "SPDX-License-Identifier: " + gplLicense + " AND "
111  + badLicense;
112  // REUSE-IgnoreStart
113  boost::regex listRegex(SPDX_LICENSE_LIST, boost::regex_constants::icase);
114  boost::regex nameRegex(SPDX_LICENSE_NAMES, boost::regex_constants::icase);
115 
116  std::string::const_iterator begin = content.begin();
117  std::string::const_iterator end = content.end();
118  boost::match_results<std::string::const_iterator> what;
119 
120  string licenseList;
121  boost::regex_search(begin, end, what, listRegex);
122  licenseList = what[1].str();
123 
124  // Check if only correct license is found
125  CPPUNIT_ASSERT_EQUAL(gplLicense, licenseList);
126 
127  // Find the actual license in the list
128  begin = licenseList.begin();
129  end = licenseList.end();
130  list<string> licensesFound;
131 
132  while (begin != end)
133  {
134  boost::smatch res;
135  if (boost::regex_search(begin, end, res, nameRegex))
136  {
137  licensesFound.push_back(res[1].str());
138  begin = res[0].second;
139  }
140  else
141  {
142  break;
143  }
144  }
145 
146  size_t expectedNos = 1;
147  size_t actualNos = licensesFound.size();
148  // Check if only 1 license is found
149  CPPUNIT_ASSERT_EQUAL(expectedNos, actualNos);
150  // Check if the result contains the expected string
151  CPPUNIT_ASSERT(
152  std::find(licensesFound.begin(), licensesFound.end(), gplLicense)
153  != licensesFound.end());
154  // Check if the result does not contain the bad string
155  CPPUNIT_ASSERT(
156  std::find(licensesFound.begin(), licensesFound.end(), badLicense)
157  == licensesFound.end());
158  };
159 
170  void regTestSpecialEnd (void) {
171 
172  const std::string gplLicense = "GPL-2.0-only";
173  const std::string lgplLicense = "LGPL-2.1-or-later";
174  const std::string mitLicense = "MIT";
175  const std::string mplLicense = "MPL-1.1+";
176  // REUSE-IgnoreStart
177  std::string content = "SPDX-License-Identifier: (" + gplLicense + " AND "
178  + lgplLicense + ") OR " + mplLicense + " AND " + mitLicense + ".";
179  // REUSE-IgnoreStart
180  boost::regex listRegex(SPDX_LICENSE_LIST, boost::regex_constants::icase);
181  boost::regex nameRegex(SPDX_LICENSE_NAMES, boost::regex_constants::icase);
182 
183  std::string::const_iterator begin = content.begin();
184  std::string::const_iterator end = content.end();
185  boost::match_results<std::string::const_iterator> what;
186 
187  string licenseList;
188  boost::regex_search(begin, end, what, listRegex);
189  licenseList = what[1].str();
190 
191  // Check if the correct license list is found
192  CPPUNIT_ASSERT_EQUAL("(" + gplLicense + " AND " + lgplLicense + ") OR " +
193  mplLicense + " AND " + mitLicense + ".", licenseList);
194 
195  // Find the actual licenses in the list
196  begin = licenseList.begin();
197  end = licenseList.end();
198  list<string> licensesFound;
199 
200  while (begin != end)
201  {
202  boost::smatch res;
203  if (boost::regex_search(begin, end, res, nameRegex))
204  {
205  licensesFound.push_back(res[1].str());
206  begin = res[0].second;
207  }
208  else
209  {
210  break;
211  }
212  }
213 
214  size_t expectedNos = 4;
215  size_t actualNos = licensesFound.size();
216  // Check if 4 licenses are found
217  CPPUNIT_ASSERT_EQUAL(expectedNos, actualNos);
218  // Check if the result contains the expected string
219  CPPUNIT_ASSERT(
220  std::find(licensesFound.begin(), licensesFound.end(), gplLicense)
221  != licensesFound.end());
222  CPPUNIT_ASSERT(
223  std::find(licensesFound.begin(), licensesFound.end(), lgplLicense)
224  != licensesFound.end());
225  CPPUNIT_ASSERT(
226  std::find(licensesFound.begin(), licensesFound.end(), mitLicense)
227  != licensesFound.end());
228  CPPUNIT_ASSERT(
229  std::find(licensesFound.begin(), licensesFound.end(), mplLicense)
230  != licensesFound.end());
231  };
232 
233 };
234 
235 CPPUNIT_TEST_SUITE_REGISTRATION( regexTest );
Test fixture to test regex accuracy.
Definition: test_regex.cc:22
void regTestSpecialEnd(void)
Test regex on a special test string.
Definition: test_regex.cc:170
void regTest(void)
Test regex on a test string.
Definition: test_regex.cc:41
void badNameTest(void)
Test regex on a string with bad identifier.
Definition: test_regex.cc:105
#define SPDX_LICENSE_NAMES
Regex to filter license names from list of license list.
Definition: ojoregex.hpp:34
#define SPDX_LICENSE_LIST
Regex to filter the list of licenses.
Definition: ojoregex.hpp:24