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