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