FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_utils.cc
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2024 Siemens AG
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
10 #include "CompatibilityUtils.hpp"
11 
12 using namespace std;
13 
14 ostream& operator<<(ostream& out, const std::set<std::string>& s)
15 {
16  bool first = true;
17  out << "<";
18  for (auto& a: s)
19  {
20  if (!first)
21  {
22  out << ", " << a;
23  }
24  else
25  {
26  first = false;
27  out << a;
28  }
29  }
30  out << ">";
31  return out;
32 }
33 
34 ostream& operator<<(ostream& out,
35  const std::tuple<
36  std::string, std::string, std::string, std::string>& t)
37 {
38  out << "<" << std::get<0>(t) << ", " << std::get<1>(t) << ", "
39  << std::get<2>(t) << ", " << std::get<3>(t) << ">";
40  return out;
41 }
42 
43 #include <cppunit/TestFixture.h>
44 #include <cppunit/extensions/HelperMacros.h>
45 
50 class TestUtility : public CPPUNIT_NS::TestFixture
51 {
52  CPPUNIT_TEST_SUITE(TestUtility);
53  CPPUNIT_TEST(testLicensesToSet);
54  CPPUNIT_TEST(testRuleLoader);
55  CPPUNIT_TEST_SUITE_END();
56 protected:
57  void testLicensesToSet();
58  void testRuleLoader();
59 } ;
60 
70 {
71  const std::string licenseNormalCase = "GPL-2.0-only AND LGPL-2.1-or-later";
72  const std::set<std::string> licenseSetNormalCase = {"GPL-2.0-only", "LGPL-2.1-or-later"};
73 
74  CPPUNIT_ASSERT_EQUAL_MESSAGE("Set should contain 2 licenses",
75  licenseSetNormalCase, mainLicenseToSet(licenseNormalCase));
76 
77  const std::string licenseDuplicate = "GPL-2.0-only AND GPL-2.0-only";
78  const std::set<std::string> licenseSetDuplicate = {"GPL-2.0-only"};
79 
80  CPPUNIT_ASSERT_EQUAL_MESSAGE("Set should not contain duplicates",
81  licenseSetDuplicate, mainLicenseToSet(licenseDuplicate));
82 
83  const std::string licenseInvalidEnd = "GPL-2.0-only AND GPL-2.0-only AND ";
84  const std::set<std::string> licenseSetInvalidEnd = {"GPL-2.0-only"};
85 
86  CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be no empty string in set",
87  licenseSetInvalidEnd, mainLicenseToSet(licenseInvalidEnd));
88 
89  const std::string licenseEmpty = "";
90  const std::set<std::string> licenseSetEmpty = {};
91 
92  CPPUNIT_ASSERT_EQUAL_MESSAGE("Empty licenseList should return empty set",
93  licenseSetEmpty, mainLicenseToSet(licenseEmpty));
94 }
95 
96 void TestUtility::testRuleLoader()
97 {
98  std::string const test_yaml_loc = TESTDATADIR "/comp-rules-test.yaml";
99  auto const rule_list = initialize_rule_list(test_yaml_loc);
100 
101  const std::tuple<string, string, string, string> known_gpl_tuple =
102  make_tuple("GPL-2.0-only", "", "LGPL-2.1-or-later", "");
103 
104  auto result_check = rule_list.find(known_gpl_tuple);
105 
106  CPPUNIT_ASSERT_MESSAGE("Unable to find tuple with name from YAML",
107  result_check != rule_list.end());
108 
109  CPPUNIT_ASSERT_EQUAL_MESSAGE("Should find a known name tuple from YAML",
110  known_gpl_tuple, result_check->first);
111  CPPUNIT_ASSERT_EQUAL_MESSAGE("Invalid compatibility for name tuple from YAML",
112  true, result_check->second);
113 
114  const std::tuple<string, string, string, string> known_type_tuple =
115  make_tuple("", "Strong Copyleft", "", "Weak Copyleft");
116 
117  result_check = rule_list.find(known_type_tuple);
118 
119  CPPUNIT_ASSERT_MESSAGE("Unable to find tuple with type from YAML",
120  result_check != rule_list.end());
121 
122  CPPUNIT_ASSERT_EQUAL_MESSAGE("Should find a known type tuple from YAML",
123  known_type_tuple, result_check->first);
124  CPPUNIT_ASSERT_EQUAL_MESSAGE("Invalid compatibility for type tuple from YAML",
125  false, result_check->second);
126 
127  const std::tuple<string, string, string, string> default_tuple =
128  make_tuple("~", "~", "~", "~");
129 
130  result_check = rule_list.find(default_tuple);
131 
132  CPPUNIT_ASSERT_MESSAGE("Unable to find default rule from YAML",
133  result_check != rule_list.end());
134 
135  CPPUNIT_ASSERT_EQUAL_MESSAGE("Should find the default tuple from YAML",
136  default_tuple, result_check->first);
137  CPPUNIT_ASSERT_EQUAL_MESSAGE("Invalid compatibility for default tuple from YAML",
138  false, result_check->second);
139 }
140 
141 CPPUNIT_TEST_SUITE_REGISTRATION( TestUtility );
map< tuple< string, string, string, string >, bool > initialize_rule_list(const string &file_location)
Read YAML file of rules and parse it as map.
std::set< std::string > mainLicenseToSet(const string &mainLicense)
Test helper/utility functions.
Definition: test_utils.cc:51
void testLicensesToSet()
Test utility function mainLicenseToSet.
Definition: test_utils.cc:69
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:37
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