FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_ninkawrapper.cc
1 /*
2  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include <boost/format.hpp>
8 #include <boost/assign/list_of.hpp>
9 #include <cppunit/TestFixture.h>
10 #include <cppunit/extensions/HelperMacros.h>
11 #include "ninkawrapper.hpp"
12 
13 using namespace std;
14 using namespace boost;
15 using namespace boost::assign;
16 
17 namespace CPPUNIT_NS
18 {
19  template <>
20  struct assertion_traits<LicenseMatch>
21  {
22  static bool equal(const LicenseMatch& m1, const LicenseMatch& m2)
23  {
24  return m1.getLicenseName() == m2.getLicenseName() && m1.getPercentage() == m2.getPercentage();
25  }
26 
27  static string toString(const LicenseMatch& m)
28  {
29  boost::format format("LicenseMatch(licenseName=\"%s\", percentage=\"%u\")");
30  return str(format % m.getLicenseName() % m.getPercentage());
31  }
32  };
33 };
34 
35 class NinkaWrapperTest : public CPPUNIT_NS::TestFixture
36 {
37  CPPUNIT_TEST_SUITE(NinkaWrapperTest);
38  CPPUNIT_TEST(test_extractLicensesFromNinkaResult);
39  CPPUNIT_TEST(test_extractLicensePartFromNinkaResult);
40  CPPUNIT_TEST(test_splitLicensePart);
41  CPPUNIT_TEST(test_createMatches);
42  CPPUNIT_TEST(test_mapLicenseFromNinkaToFossology);
43  CPPUNIT_TEST_SUITE_END();
44 
45 public:
46  void test_extractLicensesFromNinkaResult()
47  {
48  string ninkaResult("filename;UNKNOWN,LGPLv3+;more;fields\n");
49 
50  vector<string> licenses = extractLicensesFromNinkaResult(ninkaResult);
51 
52  CPPUNIT_ASSERT_EQUAL(2L, (long) licenses.size());
53  CPPUNIT_ASSERT_EQUAL(string("UNKNOWN"), licenses[0]);
54  CPPUNIT_ASSERT_EQUAL(string("LGPLv3+"), licenses[1]);
55  }
56 
57  void test_extractLicensePartFromNinkaResult()
58  {
59  string licensePart;
60 
61  // valid output - single line w/ additional fields
62  licensePart = extractLicensePartFromNinkaResult("filename;license1,license2;more;fields\n");
63  CPPUNIT_ASSERT_EQUAL(string("license1,license2"), licensePart);
64 
65  // valid output - single line w/o additional fields
66  licensePart = extractLicensePartFromNinkaResult("filename;NONE\n");
67  CPPUNIT_ASSERT_EQUAL(string("NONE"), licensePart);
68 
69  // invalid output - no output at all
70  licensePart = extractLicensePartFromNinkaResult("");
71  CPPUNIT_ASSERT_EQUAL(string(""), licensePart);
72 
73  // invalid output - only the filename
74  licensePart = extractLicensePartFromNinkaResult("filename;\n");
75  CPPUNIT_ASSERT_EQUAL(string(""), licensePart);
76 
77  // invalid output - no license
78  licensePart = extractLicensePartFromNinkaResult("filename;;more;fields\n");
79  CPPUNIT_ASSERT_EQUAL(string(""), licensePart);
80 
81  // invalid output - multiple lines
82  licensePart = extractLicensePartFromNinkaResult("filename;license;more;fields\nanother line\n");
83  CPPUNIT_ASSERT_EQUAL(string("license"), licensePart);
84  }
85 
86  void test_splitLicensePart()
87  {
88  vector<string> licenses;
89 
90  // no license
91  licenses = splitLicensePart("");
92  CPPUNIT_ASSERT_EQUAL(0L, (long) licenses.size());
93 
94  // single license
95  licenses = splitLicensePart("NONE");
96  CPPUNIT_ASSERT_EQUAL(1L, (long) licenses.size());
97  CPPUNIT_ASSERT_EQUAL(string("NONE"), licenses[0]);
98 
99  // multiple licenses
100  licenses = splitLicensePart("LGPLv3+,Apachev1.0");
101  CPPUNIT_ASSERT_EQUAL(2L, (long) licenses.size());
102  CPPUNIT_ASSERT_EQUAL(string("LGPLv3+"), licenses[0]);
103  CPPUNIT_ASSERT_EQUAL(string("Apachev1.0"), licenses[1]);
104  }
105 
106  void test_createMatches()
107  {
108  vector<LicenseMatch> matches;
109 
110  // special case: NONE should have a percentage of 0
111  matches = createMatches(list_of("NONE"));
112  CPPUNIT_ASSERT_EQUAL(1L, (long) matches.size());
113  CPPUNIT_ASSERT_EQUAL(LicenseMatch("No_license_found", 0), matches[0]);
114 
115  // special case: UNKNOWN should have a percentage of 0
116  matches = createMatches(list_of("UNKNOWN"));
117  CPPUNIT_ASSERT_EQUAL(1L, (long) matches.size());
118  CPPUNIT_ASSERT_EQUAL(LicenseMatch("UnclassifiedLicense", 0), matches[0]);
119 
120  // normal case: a known license should have a percentage of 100
121  matches = createMatches(list_of("LGPLv3+")("Apachev1.0"));
122  CPPUNIT_ASSERT_EQUAL(2L, (long) matches.size());
123  CPPUNIT_ASSERT_EQUAL(LicenseMatch("LGPL-3.0+", 100), matches[0]);
124  CPPUNIT_ASSERT_EQUAL(LicenseMatch("Apache-1.0", 100), matches[1]);
125  }
126 
127  void test_mapLicenseFromNinkaToFossology()
128  {
129  // mapping: special cases
130  CPPUNIT_ASSERT_EQUAL(string("No_license_found"), mapLicenseFromNinkaToFossology(string("NONE")));
131  CPPUNIT_ASSERT_EQUAL(string("UnclassifiedLicense"), mapLicenseFromNinkaToFossology(string("UNKNOWN")));
132 
133  // mapping: input = output
134  CPPUNIT_ASSERT_EQUAL(string(""), mapLicenseFromNinkaToFossology(string("")));
135  CPPUNIT_ASSERT_EQUAL(string("something"), mapLicenseFromNinkaToFossology(string("something")));
136  };
137 };
138 
139 CPPUNIT_TEST_SUITE_REGISTRATION(NinkaWrapperTest);