FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_scanners.cc
1 /*
2  SPDX-FileCopyrightText: © 2019 Siemens AG
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include <cppunit/TestFixture.h>
8 #include <cppunit/extensions/HelperMacros.h>
9 
10 #include <vector>
11 #include <cstring>
12 #include <ostream>
13 #include <algorithm>
14 
15 #include "OjoAgent.hpp"
16 
17 using namespace std;
18 
19 ostream& operator<<(ostream& out, const vector<ojomatch>& l)
20 {
21  for (auto m : l)
22  out << '[' << m.start << ':' << m.end << "]: '" << m.content << "'";
23  return out;
24 }
25 
29 // REUSE-IgnoreStart
30 const std::string testContent = "!/usr/bin/env python3\n"
31  "# -*- coding: utf-8 -*-\n"
32  "\n"
33  "\"\"\"\n"
34  "\n"
35  "SPDX-License-Identifier: GPL-2.0\n"
36  "\n"
37  "This program is free software; you can redistribute it and/or\n"
38  "modify it under the terms of the GNU General Public License\n"
39  "version 2 as published by the Free Software Foundation.\n"
40  "This program is distributed in the hope that it will be useful,\n"
41  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
42  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
43  "GNU General Public License for more details.\n"
44  "\n"
45  "You should have received a copy of the GNU General Public License along\n"
46  "with this program; if not, write to the Free Software Foundation, Inc.,\n"
47  "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
48  "\"\"\"";
49 // REUSE-IgnoreEnd
50 
51 const std::string testContentWithoutIdentifier = "!/usr/bin/env python3\n"
52  "# -*- coding: utf-8 -*-\n"
53  "\n"
54  "\"\"\"\n"
55  "This program is free software; you can redistribute it and/or\n"
56  "modify it under the terms of the GNU General Public License\n"
57  "version 2 as published by the Free Software Foundation.\n"
58  "This program is distributed in the hope that it will be useful,\n"
59  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
60  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
61  "GNU General Public License for more details.\n"
62  "\n"
63  "You should have received a copy of the GNU General Public License along\n"
64  "with this program; if not, write to the Free Software Foundation, Inc.,\n"
65  "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
66  "\"\"\"";
67 
68 // REUSE-IgnoreStart
69 const std::string multipleSpdxLicense = "!/usr/bin/env python3\n"
70  "# -*- coding: utf-8 -*-\n"
71  "\n"
72  "\"\"\"\n"
73  "\n"
74  "SPDX-License-Identifier: GPL-2.0 AND LGPL-2.1+ WITH Classpath-exception-2.0\n"
75  "\n"
76  "This program is free software; you can redistribute it and/or\n"
77  "modify it under the terms of the GNU General Public License\n"
78  "version 2 as published by the Free Software Foundation.\n"
79  "This program is distributed in the hope that it will be useful,\n"
80  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
81  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
82  "GNU General Public License for more details.\n"
83  "\n"
84  "You should have received a copy of the GNU General Public License along\n"
85  "with this program; if not, write to the Free Software Foundation, Inc.,\n"
86  "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
87  "\"\"\"";
88 // REUSE-IgnoreEnd
89 
90 class scannerTestSuite : public CPPUNIT_NS :: TestFixture {
91  CPPUNIT_TEST_SUITE (scannerTestSuite);
92  CPPUNIT_TEST (spdxContentTest);
93  CPPUNIT_TEST (nonSpdxContentTest);
94  CPPUNIT_TEST (multiSpdxContentTest);
95 
96  CPPUNIT_TEST_SUITE_END ();
97 
98 private:
104  void scannerTest (const string& content, vector<string> expectedStrings)
105  {
106  // Temporary store the content in a file
107  char* tempFilePath = strdup("/tmp/ojo-XXXXXX");
108  mkstemp(tempFilePath);
109 
110  fstream tempFile(tempFilePath);
111  tempFile << content;
112  tempFile.close();
113 
114  OjoAgent ojo;
115  vector<ojomatch> matches = ojo.processFile(tempFilePath);
116 
117  // Remove the temporary file
118  unlink(tempFilePath);
119 
120  CPPUNIT_ASSERT_EQUAL(expectedStrings.size(), matches.size());
121 
122  for (auto expected : expectedStrings)
123  {
124  CPPUNIT_ASSERT(std::find(matches.begin(), matches.end(), expected) != matches.end());
125  }
126  }
127 
128 protected:
137  {
138  scannerTest(testContent, {"GPL-2.0"});
139  }
140 
149  {
150  scannerTest(testContentWithoutIdentifier, {});
151  }
152 
161  {
162  scannerTest(multipleSpdxLicense, {"GPL-2.0", "LGPL-2.1+",
163  "Classpath-exception-2.0", "Dual-license"});
164  }
165 };
166 
167 CPPUNIT_TEST_SUITE_REGISTRATION( scannerTestSuite );
void spdxContentTest()
Test ojo on content with SPDX license.
void scannerTest(const string &content, vector< string > expectedStrings)
Runs a scan on content and check matches against expectedStrings.
void multiSpdxContentTest()
Test ojo on content with multiple SPDX license.
void nonSpdxContentTest()
Test ojo on content without SPDX license.
std::ostream & operator<<(std::ostream &os, const std::vector< int > &x)
<< operator overload to appends a vector to an ostream object
Definition: testUtils.hpp:27