10 #include <libfocunit.h>
17 void test_ignoreLicense_withGoodLicense() {
20 notIgnored.shortname =
"testLicense";
21 char* text_ptr = g_strdup(
"this is a real license.");
22 notIgnored.tokens = tokenize(text_ptr, DELIMITERS);
24 CU_ASSERT_FALSE(isIgnoredLicense(¬Ignored));
26 tokens_free(notIgnored.tokens);
30 void test_ignoreLicense_withGoodLicenseBranch2() {
33 notIgnored.shortname =
"testLicense";
34 char* text_ptr = g_strdup(
"Licence by Me.");
35 notIgnored.tokens = tokenize(text_ptr, DELIMITERS);
37 CU_ASSERT_FALSE(isIgnoredLicense(¬Ignored));
39 tokens_free(notIgnored.tokens);
43 void test_ignoreLicense_withNomosLicense() {
46 notIgnored.shortname =
"testLicense";
47 char* text_ptr = g_strdup(
"License by Nomos.");
48 notIgnored.tokens = tokenize(text_ptr, DELIMITERS);
50 CU_ASSERT_TRUE(isIgnoredLicense(¬Ignored));
52 tokens_free(notIgnored.tokens);
56 void test_ignoreLicense_withIgnoredName() {
59 notIgnored.shortname =
"Void";
60 char* text_ptr = g_strdup(
"a good license text");
61 notIgnored.tokens = tokenize(text_ptr, DELIMITERS);
63 CU_ASSERT_TRUE(isIgnoredLicense(¬Ignored));
65 notIgnored.shortname =
"No_license_found";
67 CU_ASSERT_TRUE(isIgnoredLicense(¬Ignored));
69 tokens_free(notIgnored.tokens);
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);
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);
88 void _addLic(GArray* lics,
int id,
const char* text) {
91 .tokens = tokenize(text,
"^")
93 g_array_append_val(lics, toAdd);
96 void test_indexLicenses() {
97 GArray* licenseArray = g_array_new(FALSE, FALSE,
sizeof(
License));
99 GArray* textTokens = tokenize(
"a^b^c^d^e^f",
"^");
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");
106 Licenses* indexedLicenses = buildLicenseIndexes(licenseArray, 4, 2);
108 CU_ASSERT_EQUAL(licenseArray, indexedLicenses->licenses);
110 _assertLicIds(getShortLicenseArray(indexedLicenses), 1, 17);
112 CU_ASSERT_PTR_NULL(getLicenseArrayFor(indexedLicenses, 0, textTokens, 0));
114 _assertLicIds(getLicenseArrayFor(indexedLicenses, 0, textTokens, 1), 1, 18);
115 _assertLicIds(getLicenseArrayFor(indexedLicenses, 1, textTokens, 1), 2, 19, 20);
117 licenses_free(indexedLicenses);
119 tokens_free(textTokens);
122 void assertTokens(GArray* tokens, ...) {
124 va_start(expptr, tokens);
126 char* expected = va_arg(expptr,
char*);
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");
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);
140 expected = va_arg(expptr,
char*);
147 void test_extractLicenses_Ignored() {
150 char* noLic =
"No_license_found";
152 PGresult* licensesResult = fo_dbManager_Exec_printf(
dbManager,
153 "select rf_pk, rf_shortname from license_ref where rf_shortname = '%s'",
156 CU_ASSERT_PTR_NOT_NULL_FATAL(licensesResult);
159 CU_ASSERT_EQUAL(licenses->licenses->len, 0);
161 licenses_free(licenses);
162 PQclear(licensesResult);
165 void test_extractLicenses_One() {
168 char* gpl3 =
"GPL-3.0";
170 PGresult* licensesResult = fo_dbManager_Exec_printf(
dbManager,
171 "select rf_pk, rf_shortname from license_ref where rf_shortname = '%s'",
174 CU_ASSERT_PTR_NOT_NULL_FATAL(licensesResult);
177 GArray* licenseArray = licenses->licenses;
178 CU_ASSERT_EQUAL_FATAL(licenseArray->len, 1);
181 CU_ASSERT_STRING_EQUAL(
license.shortname, gpl3);
184 "gnu",
"general",
"public",
"license",
"version",
"3", NULL);
186 licenses_free(licenses);
187 PQclear(licensesResult);
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;
194 return (aLen < bLen) - (aLen > bLen);
197 void sortLicenses(GArray* licenses) {
198 g_array_sort(licenses, lengthInverseComparator);
201 void test_extractLicenses_Two() {
204 char* gpl3 =
"GPL-3.0";
205 char* gpl2 =
"GPL-2.0";
207 PGresult* licensesResult = queryAllLicenses(
dbManager);
209 CU_ASSERT_PTR_NOT_NULL_FATAL(licensesResult);
211 PQclear(licensesResult);
213 GArray* licenseArray = licenses->licenses;
214 CU_ASSERT_EQUAL_FATAL(licenseArray->len, 2);
216 sortLicenses(licenseArray);
221 CU_ASSERT_STRING_EQUAL(license0.shortname, gpl3);
222 CU_ASSERT_STRING_EQUAL(license1.shortname, gpl2);
224 assertTokens(license0.tokens,
225 "gnu",
"general",
"public",
"license",
"version",
"3", NULL);
226 assertTokens(license1.tokens,
227 "gnu",
"general",
"public",
"license",
"version",
"2", NULL);
229 licenses_free(licenses);
232 #define doOrReturnError(fmt, ...) do {\
233 PGresult* copy = fo_dbManager_Exec_printf(dbManager, fmt, #__VA_ARGS__); \
241 int license_setUpFunc() {
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)",);
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)",);
258 int license_tearDownFunc() {
263 doOrReturnError(
"DROP TABLE license_ref",);
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},
fo_dbManager * dbManager
fo_dbManager object