FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_scanners.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 *********************************************************************/
17 
18 #include <cppunit/TestFixture.h>
19 #include <cppunit/extensions/HelperMacros.h>
20 
21 #include <vector>
22 #include <cstring>
23 #include <ostream>
24 #include <algorithm>
25 
26 #include "OjoAgent.hpp"
27 
28 using namespace std;
29 
30 ostream& operator<<(ostream& out, const vector<ojomatch>& l)
31 {
32  for (auto m : l)
33  out << '[' << m.start << ':' << m.end << "]: '" << m.content << "'";
34  return out;
35 }
36 
40 const std::string testContent = "!/usr/bin/env python3\n"
41  "# -*- coding: utf-8 -*-\n"
42  "\n"
43  "\"\"\"\n"
44  "\n"
45  "SPDX-License-Identifier: GPL-2.0\n"
46  "\n"
47  "This program is free software; you can redistribute it and/or\n"
48  "modify it under the terms of the GNU General Public License\n"
49  "version 2 as published by the Free Software Foundation.\n"
50  "This program is distributed in the hope that it will be useful,\n"
51  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
52  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
53  "GNU General Public License for more details.\n"
54  "\n"
55  "You should have received a copy of the GNU General Public License along\n"
56  "with this program; if not, write to the Free Software Foundation, Inc.,\n"
57  "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
58  "\"\"\"";
59 
60 const std::string testContentWithoutIdentifier = "!/usr/bin/env python3\n"
61  "# -*- coding: utf-8 -*-\n"
62  "\n"
63  "\"\"\"\n"
64  "This program is free software; you can redistribute it and/or\n"
65  "modify it under the terms of the GNU General Public License\n"
66  "version 2 as published by the Free Software Foundation.\n"
67  "This program is distributed in the hope that it will be useful,\n"
68  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
69  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
70  "GNU General Public License for more details.\n"
71  "\n"
72  "You should have received a copy of the GNU General Public License along\n"
73  "with this program; if not, write to the Free Software Foundation, Inc.,\n"
74  "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
75  "\"\"\"";
76 
77 const std::string multipleSpdxLicense = "!/usr/bin/env python3\n"
78  "# -*- coding: utf-8 -*-\n"
79  "\n"
80  "\"\"\"\n"
81  "\n"
82  "SPDX-License-Identifier: GPL-2.0 AND LGPL-2.1+ WITH Classpath-exception-2.0\n"
83  "\n"
84  "This program is free software; you can redistribute it and/or\n"
85  "modify it under the terms of the GNU General Public License\n"
86  "version 2 as published by the Free Software Foundation.\n"
87  "This program is distributed in the hope that it will be useful,\n"
88  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
89  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
90  "GNU General Public License for more details.\n"
91  "\n"
92  "You should have received a copy of the GNU General Public License along\n"
93  "with this program; if not, write to the Free Software Foundation, Inc.,\n"
94  "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
95  "\"\"\"";
96 
97 class scannerTestSuite : public CPPUNIT_NS :: TestFixture {
98  CPPUNIT_TEST_SUITE (scannerTestSuite);
99  CPPUNIT_TEST (spdxContentTest);
100  CPPUNIT_TEST (nonSpdxContentTest);
101  CPPUNIT_TEST (multiSpdxContentTest);
102 
103  CPPUNIT_TEST_SUITE_END ();
104 
105 private:
111  void scannerTest (const string& content, vector<string> expectedStrings)
112  {
113  // Temporary store the content in a file
114  char* tempFilePath = strdup("/tmp/ojo-XXXXXX");
115  mkstemp(tempFilePath);
116 
117  fstream tempFile(tempFilePath);
118  tempFile << content;
119  tempFile.close();
120 
121  OjoAgent ojo;
122  vector<ojomatch> matches = ojo.processFile(tempFilePath);
123 
124  // Remove the temporary file
125  unlink(tempFilePath);
126 
127  CPPUNIT_ASSERT_EQUAL(expectedStrings.size(), matches.size());
128 
129  for (auto expected : expectedStrings)
130  {
131  CPPUNIT_ASSERT(std::find(matches.begin(), matches.end(), expected) != matches.end());
132  }
133  }
134 
135 protected:
144  {
145  scannerTest(testContent, {"GPL-2.0"});
146  }
147 
156  {
157  scannerTest(testContentWithoutIdentifier, {});
158  }
159 
168  {
169  scannerTest(multipleSpdxLicense, {"GPL-2.0", "LGPL-2.1+",
170  "Classpath-exception-2.0", "Dual-license"});
171  }
172 };
173 
174 CPPUNIT_TEST_SUITE_REGISTRATION( scannerTestSuite );
void nonSpdxContentTest()
Test ojo on content without 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 spdxContentTest()
Test ojo on content with SPDX license.