20 #include <libfocunit.h> 27 void test_ignoreLicense_withGoodLicense() {
30 notIgnored.shortname =
"testLicense";
31 char* text_ptr = g_strdup(
"this is a real license.");
32 notIgnored.tokens = tokenize(text_ptr, DELIMITERS);
34 CU_ASSERT_FALSE(isIgnoredLicense(¬Ignored));
36 tokens_free(notIgnored.tokens);
40 void test_ignoreLicense_withGoodLicenseBranch2() {
43 notIgnored.shortname =
"testLicense";
44 char* text_ptr = g_strdup(
"Licence by Me.");
45 notIgnored.tokens = tokenize(text_ptr, DELIMITERS);
47 CU_ASSERT_FALSE(isIgnoredLicense(¬Ignored));
49 tokens_free(notIgnored.tokens);
53 void test_ignoreLicense_withNomosLicense() {
56 notIgnored.shortname =
"testLicense";
57 char* text_ptr = g_strdup(
"License by Nomos.");
58 notIgnored.tokens = tokenize(text_ptr, DELIMITERS);
60 CU_ASSERT_TRUE(isIgnoredLicense(¬Ignored));
62 tokens_free(notIgnored.tokens);
66 void test_ignoreLicense_withIgnoredName() {
69 notIgnored.shortname =
"Void";
70 char* text_ptr = g_strdup(
"a good license text");
71 notIgnored.tokens = tokenize(text_ptr, DELIMITERS);
73 CU_ASSERT_TRUE(isIgnoredLicense(¬Ignored));
75 notIgnored.shortname =
"No_license_found";
77 CU_ASSERT_TRUE(isIgnoredLicense(¬Ignored));
79 tokens_free(notIgnored.tokens);
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);
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);
98 void _addLic(GArray* lics,
int id,
const char* text) {
101 .tokens = tokenize(text,
"^")
103 g_array_append_val(lics, toAdd);
106 void test_indexLicenses() {
107 GArray* licenseArray = g_array_new(FALSE, FALSE,
sizeof(
License));
109 GArray* textTokens = tokenize(
"a^b^c^d^e^f",
"^");
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");
116 Licenses* indexedLicenses = buildLicenseIndexes(licenseArray, 4, 2);
118 CU_ASSERT_EQUAL(licenseArray, indexedLicenses->licenses);
120 _assertLicIds(getShortLicenseArray(indexedLicenses), 1, 17);
122 CU_ASSERT_PTR_NULL(getLicenseArrayFor(indexedLicenses, 0, textTokens, 0));
124 _assertLicIds(getLicenseArrayFor(indexedLicenses, 0, textTokens, 1), 1, 18);
125 _assertLicIds(getLicenseArrayFor(indexedLicenses, 1, textTokens, 1), 2, 19, 20);
127 licenses_free(indexedLicenses);
129 tokens_free(textTokens);
132 void assertTokens(GArray* tokens, ...) {
134 va_start(expptr, tokens);
136 char* expected = va_arg(expptr,
char*);
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");
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);
150 expected = va_arg(expptr,
char*);
157 void test_extractLicenses_Ignored() {
158 FO_ASSERT_PTR_NOT_NULL_FATAL(dbManager);
160 char* noLic =
"No_license_found";
162 PGresult* licensesResult = fo_dbManager_Exec_printf(dbManager,
163 "select rf_pk, rf_shortname from license_ref where rf_shortname = '%s'",
166 CU_ASSERT_PTR_NOT_NULL_FATAL(licensesResult);
168 Licenses* licenses = extractLicenses(dbManager, licensesResult, 0 , 0);
169 CU_ASSERT_EQUAL(licenses->licenses->len, 0);
171 licenses_free(licenses);
172 PQclear(licensesResult);
175 void test_extractLicenses_One() {
176 FO_ASSERT_PTR_NOT_NULL_FATAL(dbManager);
178 char* gpl3 =
"GPL-3.0";
180 PGresult* licensesResult = fo_dbManager_Exec_printf(dbManager,
181 "select rf_pk, rf_shortname from license_ref where rf_shortname = '%s'",
184 CU_ASSERT_PTR_NOT_NULL_FATAL(licensesResult);
186 Licenses* licenses = extractLicenses(dbManager, licensesResult, 0, 0);
187 GArray* licenseArray = licenses->licenses;
188 CU_ASSERT_EQUAL_FATAL(licenseArray->len, 1);
191 CU_ASSERT_STRING_EQUAL(license.shortname, gpl3);
193 assertTokens(license.tokens,
194 "gnu",
"general",
"public",
"license",
"version",
"3,", NULL);
196 licenses_free(licenses);
197 PQclear(licensesResult);
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;
204 return (aLen < bLen) - (aLen > bLen);
207 void sortLicenses(GArray* licenses) {
208 g_array_sort(licenses, lengthInverseComparator);
211 void test_extractLicenses_Two() {
212 FO_ASSERT_PTR_NOT_NULL_FATAL(dbManager);
214 char* gpl3 =
"GPL-3.0";
215 char* gpl2 =
"GPL-2.0";
217 PGresult* licensesResult = queryAllLicenses(dbManager);
219 CU_ASSERT_PTR_NOT_NULL_FATAL(licensesResult);
220 Licenses * licenses = extractLicenses(dbManager, licensesResult, 0, 0);
221 PQclear(licensesResult);
223 GArray* licenseArray = licenses->licenses;
224 CU_ASSERT_EQUAL_FATAL(licenseArray->len, 2);
226 sortLicenses(licenseArray);
231 CU_ASSERT_STRING_EQUAL(license0.shortname, gpl3);
232 CU_ASSERT_STRING_EQUAL(license1.shortname, gpl2);
234 assertTokens(license0.tokens,
235 "gnu",
"general",
"public",
"license",
"version",
"3,", NULL);
236 assertTokens(license1.tokens,
237 "gnu",
"general",
"public",
"license,",
"version",
"2", NULL);
239 licenses_free(licenses);
242 #define doOrReturnError(fmt, ...) do {\ 243 PGresult* copy = fo_dbManager_Exec_printf(dbManager, fmt, #__VA_ARGS__); \ 251 int license_setUpFunc() {
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)",);
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)",);
268 int license_tearDownFunc() {
273 doOrReturnError(
"DROP TABLE license_ref",);
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},
fo_dbManager * dbManager
fo_dbManager object