FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_license.c
1 /*
2  Author: Daniele Fognini, Andreas Wuerl
3  SPDX-FileCopyrightText: © 2013-2017 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <libfocunit.h>
11 
12 #include "license.h"
13 #include "hash.h"
14 
15 extern fo_dbManager* dbManager;
16 
17 void test_ignoreLicense_withGoodLicense() {
18  License notIgnored;
19  notIgnored.refId = 0;
20  notIgnored.shortname = "testLicense";
21  char* text_ptr = g_strdup("this is a real license.");
22  notIgnored.tokens = tokenize(text_ptr, DELIMITERS);
23 
24  CU_ASSERT_FALSE(isIgnoredLicense(&notIgnored));
25 
26  tokens_free(notIgnored.tokens);
27  g_free(text_ptr);
28 }
29 
30 void test_ignoreLicense_withGoodLicenseBranch2() {
31  License notIgnored;
32  notIgnored.refId = 0;
33  notIgnored.shortname = "testLicense";
34  char* text_ptr = g_strdup("Licence by Me."); //same token length as an ignored
35  notIgnored.tokens = tokenize(text_ptr, DELIMITERS);
36 
37  CU_ASSERT_FALSE(isIgnoredLicense(&notIgnored));
38 
39  tokens_free(notIgnored.tokens);
40  g_free(text_ptr);
41 }
42 
43 void test_ignoreLicense_withNomosLicense() {
44  License notIgnored;
45  notIgnored.refId = 0;
46  notIgnored.shortname = "testLicense";
47  char* text_ptr = g_strdup("License by Nomos.");
48  notIgnored.tokens = tokenize(text_ptr, DELIMITERS);
49 
50  CU_ASSERT_TRUE(isIgnoredLicense(&notIgnored));
51 
52  tokens_free(notIgnored.tokens);
53  g_free(text_ptr);
54 }
55 
56 void test_ignoreLicense_withIgnoredName() {
57  License notIgnored;
58  notIgnored.refId = 0;
59  notIgnored.shortname = "Void";
60  char* text_ptr = g_strdup("a good license text");
61  notIgnored.tokens = tokenize(text_ptr, DELIMITERS);
62 
63  CU_ASSERT_TRUE(isIgnoredLicense(&notIgnored));
64 
65  notIgnored.shortname = "No_license_found";
66 
67  CU_ASSERT_TRUE(isIgnoredLicense(&notIgnored));
68 
69  tokens_free(notIgnored.tokens);
70  g_free(text_ptr);
71 }
72 
73 void _assertLicIds(const GArray* lics, unsigned int n, ...) {
74  CU_ASSERT_PTR_NOT_NULL_FATAL(lics);
75  CU_ASSERT_EQUAL_FATAL(lics->len, n);
76  va_list args;
77 
78  va_start(args, n);
79 
80  for (unsigned int i=0; i<n; i++) {
81  int expectedLicId = va_arg(args, int);
82  CU_ASSERT_EQUAL(license_index(lics, i)->refId, expectedLicId);
83  }
84 
85  va_end(args);
86 }
87 
88 void _addLic(GArray* lics, int id, const char* text) {
89  License toAdd = (License) {
90  .refId = id,
91  .tokens = tokenize(text, "^")
92  };
93  g_array_append_val(lics, toAdd);
94 }
95 
96 void test_indexLicenses() {
97  GArray* licenseArray = g_array_new(FALSE, FALSE, sizeof(License));
98 
99  GArray* textTokens = tokenize("a^b^c^d^e^f", "^");
100 
101  _addLic(licenseArray, 17, "b^c");
102  _addLic(licenseArray, 18, "b^c^d^e^f");
103  _addLic(licenseArray, 19, "1^b^c^d^e^f");
104  _addLic(licenseArray, 20, "2^b^c^d^e^f");
105 
106  Licenses* indexedLicenses = buildLicenseIndexes(licenseArray, 4, 2);
107 
108  CU_ASSERT_EQUAL(licenseArray, indexedLicenses->licenses);
109 
110  _assertLicIds(getShortLicenseArray(indexedLicenses), 1, 17); // lic 17 is a short lic
111 
112  CU_ASSERT_PTR_NULL(getLicenseArrayFor(indexedLicenses, 0, textTokens, 0)); // no lic matches the first 4 tokens of text
113 
114  _assertLicIds(getLicenseArrayFor(indexedLicenses, 0, textTokens, 1), 1, 18); // lic 18 matches tokens 1-5 of text
115  _assertLicIds(getLicenseArrayFor(indexedLicenses, 1, textTokens, 1), 2, 19, 20); // lic 19 and 20 matche tokens 1-5 of text with a 1 token head diff
116 
117  licenses_free(indexedLicenses);
118 
119  tokens_free(textTokens);
120 }
121 
122 void assertTokens(GArray* tokens, ...) {
123  va_list expptr;
124  va_start(expptr, tokens);
125 
126  char* expected = va_arg(expptr, char*);
127  size_t i = 0;
128  while (expected != NULL) {
129  if (i >= tokens->len) {
130  printf("ASSERT ERROR: tokens array has length %d, which is shorter that expected", tokens->len);
131  CU_FAIL("tokens array shorter than expected");
132  break;
133  }
134 
135  Token token = g_array_index(tokens, Token, i);
136  CU_ASSERT_EQUAL(token.hashedContent, hash(expected));
137  if (token.hashedContent != hash(expected)) {
138  printf("%u != hash(%s)\n", token.hashedContent, expected);
139  }
140  expected = va_arg(expptr, char*);
141  i++;
142  }
143 
144  va_end(expptr);
145 }
146 
147 void test_extractLicenses_Ignored() {
148  FO_ASSERT_PTR_NOT_NULL_FATAL(dbManager);
149 
150  char* noLic = "No_license_found";
151 
152  PGresult* licensesResult = fo_dbManager_Exec_printf(dbManager,
153  "select rf_pk, rf_shortname from license_ref where rf_shortname = '%s'",
154  noLic);
155 
156  CU_ASSERT_PTR_NOT_NULL_FATAL(licensesResult);
157 
158  Licenses* licenses = extractLicenses(dbManager, licensesResult, 0 , 0);
159  CU_ASSERT_EQUAL(licenses->licenses->len, 0);
160 
161  licenses_free(licenses);
162  PQclear(licensesResult);
163 }
164 
165 void test_extractLicenses_One() {
166  FO_ASSERT_PTR_NOT_NULL_FATAL(dbManager);
167 
168  char* gpl3 = "GPL-3.0";
169 
170  PGresult* licensesResult = fo_dbManager_Exec_printf(dbManager,
171  "select rf_pk, rf_shortname from license_ref where rf_shortname = '%s'",
172  gpl3);
173 
174  CU_ASSERT_PTR_NOT_NULL_FATAL(licensesResult);
175 
176  Licenses* licenses = extractLicenses(dbManager, licensesResult, 0, 0);
177  GArray* licenseArray = licenses->licenses;
178  CU_ASSERT_EQUAL_FATAL(licenseArray->len, 1);
179 
180  License license = g_array_index(licenseArray, License, 0);
181  CU_ASSERT_STRING_EQUAL(license.shortname, gpl3);
182 
183  assertTokens(license.tokens,
184  "gnu", "general", "public", "license", "version", "3", NULL);
185 
186  licenses_free(licenses);
187  PQclear(licensesResult);
188 }
189 
190 static gint lengthInverseComparator(const void* a, const void* b) {
191  size_t aLen = ((License*) a)->tokens->len;
192  size_t bLen = ((License*) b)->tokens->len;
193 
194  return (aLen < bLen) - (aLen > bLen);
195 }
196 
197 void sortLicenses(GArray* licenses) {
198  g_array_sort(licenses, lengthInverseComparator);
199 }
200 
201 void test_extractLicenses_Two() {
202  FO_ASSERT_PTR_NOT_NULL_FATAL(dbManager);
203 
204  char* gpl3 = "GPL-3.0";
205  char* gpl2 = "GPL-2.0";
206 
207  PGresult* licensesResult = queryAllLicenses(dbManager);
208 
209  CU_ASSERT_PTR_NOT_NULL_FATAL(licensesResult);
210  Licenses * licenses = extractLicenses(dbManager, licensesResult, 0, 0);
211  PQclear(licensesResult);
212 
213  GArray* licenseArray = licenses->licenses;
214  CU_ASSERT_EQUAL_FATAL(licenseArray->len, 2);
215 
216  sortLicenses(licenseArray);
217 
218  License license0 = g_array_index(licenseArray, License, 0);
219  License license1 = g_array_index(licenseArray, License, 1);
220 
221  CU_ASSERT_STRING_EQUAL(license0.shortname, gpl3);
222  CU_ASSERT_STRING_EQUAL(license1.shortname, gpl2);
223 
224  assertTokens(license0.tokens,
225  "gnu", "general", "public", "license", "version", "3", NULL);
226  assertTokens(license1.tokens,
227  "gnu", "general", "public", "license", "version", "2", NULL);
228 
229  licenses_free(licenses);
230 }
231 
232 #define doOrReturnError(fmt, ...) do {\
233  PGresult* copy = fo_dbManager_Exec_printf(dbManager, fmt, #__VA_ARGS__); \
234  if (!copy) {\
235  return 1; \
236  } else {\
237  PQclear(copy);\
238  }\
239 } while(0)
240 
241 int license_setUpFunc() {
242  if (!dbManager) {
243  return 1;
244  }
245 
246  if (!fo_dbManager_tableExists(dbManager, "license_ref")) {
247  doOrReturnError("CREATE TABLE license_ref(rf_pk int, rf_shortname text, rf_text text, rf_active bool, rf_detector_type int)",);
248  }
249 
250  doOrReturnError("INSERT INTO license_ref(rf_pk, rf_shortname, rf_text, rf_active ,rf_detector_type) "
251  "VALUES (1, 'GPL-3.0', 'gnu general public license version 3,', true, 1)",);
252  doOrReturnError("INSERT INTO license_ref(rf_pk, rf_shortname, rf_text, rf_active ,rf_detector_type) "
253  "VALUES (2, 'GPL-2.0', 'gnu general public license, version 2', true, 1)",);
254 
255  return 0;
256 }
257 
258 int license_tearDownFunc() {
259  if (!dbManager) {
260  return 1;
261  }
262 
263  doOrReturnError("DROP TABLE license_ref",);
264 
265  return 0;
266 }
267 
268 CU_TestInfo license_testcases[] = {
269  {"Testing not ignoring good license:", test_ignoreLicense_withGoodLicense},
270  {"Testing not ignoring good license2:", test_ignoreLicense_withGoodLicenseBranch2},
271  {"Testing ignoring nomos license text:", test_ignoreLicense_withNomosLicense},
272  {"Testing ignoring license text by Name:", test_ignoreLicense_withIgnoredName},
273  {"Testing extracting a license from DB:", test_extractLicenses_One},
274  {"Testing extracting two licenses from DB:", test_extractLicenses_Two},
275  {"Testing extracting an ignored license from DB:", test_extractLicenses_Ignored},
276  {"Testing indexing of licenses:", test_indexLicenses},
277  CU_TEST_INFO_NULL
278 };
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Definition: monk.h:55
Definition: monk.h:67
Definition: nomos.h:426