FOSSology  4.7.1
Open Source License Compliance by Open Source Software
test_DoctoredBuffer.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2014 Siemens AG
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
11 #include <stdbool.h>
12 
13 #include "nomos.h"
14 #include "doctorBuffer_utils.h"
15 #include <stdio.h> /* printf, scanf, NULL */
16 #include <stdlib.h> /* malloc, free, rand */
17 #include <CUnit/CUnit.h>
18 
19 #include <stdarg.h>
20 #include "nomos_utils.h"
21 
22 #include "nomos.h"
23 #include "util.h"
24 #include "list.h"
25 #include "licenses.h"
26 #include "process.h"
27 #include "nomos_regex.h"
28 #include "_autodefs.h"
29 
38 {
39  licenseInit();
40  char* buf, *fer;
41 
42  fer = g_strdup_printf("//Th- is is a li-\n// cence of the test string");
43  buf = g_strdup_printf("This is a li cence of the test string");
44  printf("%s \n", buf);
45  printf("%s \n", fer);
46  initializeCurScan(&cur);
47  doctorBuffer(fer, 0, 0, 0);
48  printf("%s \n", buf);
49  printf("%s \n", fer);
50  CU_ASSERT_STRING_EQUAL(buf, fer);
51  freeAndClearScan(&cur);
52  g_free(buf);
53  g_free(fer);
54 }
55 
64 {
65 
66  licenseInit();
67  char *fer, *cfer;
68  fer = g_strdup_printf("//This is the test string");
69  cfer= g_strdup( fer);
70  printf("%s \n", fer);
71  initializeCurScan(&cur);
72  doctorBuffer(fer, 0, 0, 0);
73  printf("Before %d, after %d", (int) strlen(cfer), (int) strlen(fer));
74 
75  for (size_t i = 0; i < strlen(fer); i++)
76  {
77  CU_ASSERT_EQUAL(*(fer + i), *(cfer + uncollapsePosition(i, cur.docBufferPositionsAndOffsets)));
78  }
79 
80  g_free(cfer);
81  g_free(fer);
82 }
83 
87 static void report_Match(char* buf)
88 {
89  printf("I have %i matches \n", cur.theMatches->len);
90  for (guint i = 0; i < cur.theMatches->len; ++i)
91  {
93  for (guint k=0; k < licenceAndMatch->matchPositions->len; ++k) {
95  licenceAndMatch->matchPositions, k);
96  printf("Match from %d to %d: ", PaT->start, PaT->end);
97 
98  for (int j = PaT->start; j < PaT->end; ++j)
99  {
100  printf("%c", *(buf + j));
101  }
102 
103  printf("\n");
104  }
105  }
106 }
107 
117 {
118 
119  licenseInit();
120  char *buf, *undoc, *filename;
121  filename = buf = (char*)malloc(3000);
122  sprintf(filename, "%s/NomosTestfiles/WXwindows/WXwindows.txt", TESTDATADIR);
123  int f = open(filename, O_RDONLY);
124  int whatIread = read(f, buf, 3000);
125  close(f);
126 
127  CU_ASSERT_EQUAL(whatIread, 2496);
128  undoc = g_strdup(buf);
129 
130  printf("\n%s\n", undoc);
131  fflush(stdout);
132  int licence_index = _PHR_WXWINDOWS;
133  int licence_index2 = _LT_LGPLref1;
134 
135  initializeCurScan(&cur);
136  cur.currentLicenceIndex=0;
137  g_array_append_val(cur.indexList, licence_index);
138  g_array_append_val(cur.indexList, licence_index2);
139  addLicence(cur.theMatches, "WXwindows");
140  idxGrep_recordPosition(licence_index, undoc, REG_ICASE | REG_EXTENDED);
141  idxGrep_recordPosition(licence_index2, undoc, REG_ICASE | REG_EXTENDED);
142  report_Match(undoc);
143  freeAndClearScan(&cur);
144 
145  initializeCurScan(&cur);
146  cur.currentLicenceIndex=0;
147  g_array_append_val(cur.indexList, licence_index);
148  g_array_append_val(cur.indexList, licence_index2);
149  addLicence(cur.theMatches, "WXwindows");
150  doctorBuffer(buf, 0, 0, 0);
151 
152  printf("\n%s\n", buf);
153  idxGrep_recordPositionDoctored(licence_index, buf, REG_ICASE | REG_EXTENDED);
154  idxGrep_recordPositionDoctored(licence_index2, buf, REG_ICASE | REG_EXTENDED);
155  report_Match(undoc);
156  freeAndClearScan(&cur);
157 
158  initializeCurScan(&cur);
159  cur.currentLicenceIndex=0;
160  g_array_append_val(cur.indexList, licence_index);
161  g_array_append_val(cur.indexList, licence_index2);
162  addLicence(cur.theMatches, "WXwindows");
163  idxGrep_recordPosition(licence_index, buf, REG_ICASE | REG_EXTENDED);
164  idxGrep_recordPosition(licence_index2, buf, REG_ICASE | REG_EXTENDED);
165  report_Match(buf);
166  freeAndClearScan(&cur);
167 
168  free(buf);
169  g_free(undoc);
170 
171 }
172 
179 
190 {
191  initializeCurScan(&cur);
192  char* textBuffer = g_strdup_printf("&quot the big\t(C) and long\\n &quot\\s-1234,"
193  " test &copy; string \n con-\n// tains losts; of . <string test> &nbsp;"
194  " <body> \" compli-\n cated /* COMMENT s and funny */ Words as it \n "
195  "mimi-cs printf(\"Licence\"); and so on\n &quot \n ");
196  removeHtmlComments(textBuffer);
197  char* te22Buffer = g_strdup_printf(" quot the big\t(C) and long\\n quot\\s-1234,"
198  " test &copy string \n con-\n// tains losts; of . <string test nbsp "
199  " body \" compli-\n cated /* COMMENT s and funny */ Words as it \n "
200  "mimi-cs printf(\"Licence\"); and so on\n quot \n ");
201  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
202  g_free(textBuffer);
203  g_free(te22Buffer);
204 }
205 
213 {
214  initializeCurScan(&cur);
215  char* textBuffer = g_strdup_printf(" quot the big\t(C) and long\\n quot\\s-1234,"
216  " test &copy string \n con-\n// tains losts; of . <string test nbsp "
217  " body \" compli-\n cated /* COMMENT s and funny */ Words as it \n "
218  "mimi-cs printf(\"Licence\"); and so on\n quot \n ");
219 
220  removeLineComments(textBuffer);
221 
222  char* te22Buffer = g_strdup_printf(" quot the big\t(C) and long\\n quot\\s-1234,"
223  " test &copy string \n con-\n\377\377 tains losts; of . <string test nbsp"
224  " body \" compli-\n cated /* COMMENT s and funny */ Words as it \n "
225  "mimi-cs printf(\"Licence\"); and so on\n quot \n ");
226  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
227  g_free(textBuffer);
228  g_free(te22Buffer);
229 }
230 
237 {
238  initializeCurScan(&cur);
239  char* textBuffer=g_strdup_printf("(8) (89) -9.- A %%!PS-Adobe-3.0 (12) EPSF-3.0 --8. -9.- A");
240 
241  cleanUpPostscript(textBuffer);
242 
243  char* te22Buffer = g_strdup_printf(" %%!PS-Adobe-3.0 (12) EPSF-3.0 ");
244  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
245  g_free(textBuffer);
246  g_free(te22Buffer);
247 }
248 
249 
258 {
259  initializeCurScan(&cur);
260  char* textBuffer= g_strdup_printf(" quot the big\t(C) and long\\n quot\\s-1234, test"
261  " &copy string \n con-\n\377\377 tains losts; of . <string test nbsp "
262  " body \" compli-\n cated /* COMMENT s and funny */ Words as it \n "
263  "mimi-cs printf(\"Licence\"); and so on\n quot \n ");
264 
266 
267  char* te22Buffer = g_strdup_printf(" quot the big\t(C) and long quot , test"
268  " &copy string \n con-\n\377\377 tains losts; of . <string test nbsp "
269  " body \" compli-\n cated /* COMMENT s and funny */ Words as it \n "
270  "mimi-cs printf(\"Licence\"); and so on\n quot \n ");
271  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
272  g_free(textBuffer);
273  g_free(te22Buffer);
274 }
275 
287 {
288  initializeCurScan(&cur);
289  char* textBuffer;
290  int isCR = NO; // isCR switches the replacment of ';' and '.', in the project it is always NO
291 
292  textBuffer = g_strdup_printf(" quot the big\t(C) and long quot , test"
293  " &copy string \n con-\n\377\377 tains losts; of . <string test nbsp "
294  " body \" compli-\n cated /* COMMENT s and funny */ Words as it \n "
295  "mimi-cs printf(\"Licence\"); and so on\n quot \n ");
296 
298 
299  char* te22Buffer = g_strdup_printf(" quot the big (C) and long quot test"
300  " &copy string con- \377\377 tains losts of \377 test nbsp "
301  " body compli- cated / COMMENT s and funny / Words as it "
302  "mimi-cs printf Licence and so on quot ");
303  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
304  g_free(textBuffer);
305  g_free(te22Buffer);
306 
307 }
308 
309 
324 {
325  initializeCurScan(&cur);
326  char* buf, *fer;
327  fer= g_strdup_printf("This- is the-test str- ing");
328  buf= g_strdup_printf("This\377\377is the-test str\377\377ing");
329  dehyphen(fer);
330  CU_ASSERT_STRING_EQUAL(buf, fer);
331  g_free(buf);
332  g_free(fer);
333 }
334 
342 {
343  initializeCurScan(&cur);
344  char* textBuffer= g_strdup_printf(" quot the big (C) and long quot , "
345  "test &copy string con- \377\377 tains losts of \377 test"
346  " nbsp body compli- cated / COMMENT s and funny / Words as it "
347  "mimi-cs printf Licence and so on quot ");
348  dehyphen(textBuffer);
349 
350  char* te22Buffer = g_strdup_printf(" quot the big (C) and long quot , test &copy "
351  "string con- \377\377 tains losts of \377 test nbsp body "
352  "compli\377\377\377cated / COMMENT s and funny / Words as it "
353  "mimi-cs printf Licence and so on quot ");
354  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
355  g_free(textBuffer);
356  g_free(te22Buffer);
357 
358 }
359 
367 {
368  initializeCurScan(&cur);
369  char* textBuffer= g_strdup_printf(" quot the big (C) and long quot , "
370  "test &copy string con- \377\377 tains losts of \377 test "
371  "nbsp body compli\377\377\377cated / COMMENT s and funny / Words as it "
372  "mimi-cs printf Licence and so on quot ");
374  removePunctuation(textBuffer);
375 
376  char* te22Buffer = g_strdup_printf(" quot the big (C) and long quot , test "
377  "&copy string con \377\377 tains losts of \377 test nbsp body "
378  "compli\377\377\377cated COMMENT s and funny Words as it "
379  "mimi-cs printf Licence and so on quot ");
380  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
381  g_free(textBuffer);
382  g_free(te22Buffer);
383 }
384 
396 {
397  initializeCurScan(&cur);
398  char* textBuffer= g_strdup_printf(" quot the big (C) and long quot , test &copy "
399  "string con \377\377 tains losts of \377 test nbsp body "
400  "compli\377\377\377cated COMMENT s and funny Words as it "
401  "mimi-cs printf Licence and so on quot ");
402  ignoreFunctionCalls(textBuffer);
403 
404  char* te22Buffer = g_strdup_printf(" quot the big (C) and long quot , test &copy "
405  "string con \377\377 tains losts of \377 test nbsp body "
406  "compli\377\377\377cated COMMENT s and funny Words as it "
407  "mimi-cs Licence and so on quot ");
408  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
409  g_free(textBuffer);
410  g_free(te22Buffer);
411 }
412 
421 {
422  initializeCurScan(&cur);
423  char* textBuffer= g_strdup_printf(" quot the big (C) and long quot , test &copy "
424  "string con \377\377 tains losts of \377 test nbsp body "
425  "compli\377\377\377cated COMMENT s and funny Words as it "
426  "mimi-cs Licence and so on quot ");
427  convertSpaceToInvisible(textBuffer);
428 
429  char* te22Buffer = g_strdup_printf(" quot the big (C) and long \377\377\377quot \377\377\377\377\377\377, "
430  "test &copy \377string \377\377con \377\377\377\377tains losts \377of \377\377\377\377\377\377\377\377\377\377"
431  "test \377\377nbsp \377\377\377body \377\377\377compli\377\377\377cated \377\377\377COMMENT s "
432  "and funny \377\377\377Words as it \377\377mimi-cs \377\377\377\377\377\377\377\377Licence \377\377\377"
433  "and so on \377\377quot \377\377");
434 // printf("%s\n", te22Buffer);
435  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
436  g_free(textBuffer);
437  g_free(te22Buffer);
438 }
439 
446 {
447  initializeCurScan(&cur);
448  char* textBuffer= g_strdup_printf(" quot the big (C) and long \377\377\377quot \377\377\377\377\377\377, "
449  "test &copy \377string \377\377con \377\377\377\377tains losts \377of \377\377\377\377\377\377\377\377\377\377"
450  "test \377\377nbsp \377\377\377body \377\377\377compli\377\377\377cated \377\377\377COMMENT s "
451  "and funny \377\377\377Words as it \377\377mimi-cs \377\377\377\377\377\377\377\377Licence \377\377\377"
452  "and so on \377\377quot \377\377");
453 
454  compressDoctoredBuffer(textBuffer);
455 
456  char* te22Buffer = g_strdup_printf(" quot the big (C) and long quot , test &copy string "
457  "con tains losts of test nbsp body complicated COMMENT s and funny Words as "
458  "it mimi-cs Licence and so on quot ");
459 
460  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
461  g_free(textBuffer);
462  g_free(te22Buffer);
463 }
464 
465 CU_TestInfo doctorBuffer_testcases[] =
466 {
467 { "Testing doctorBuffer:", test_doctorBuffer },
468 { "Testing doctorBufer_uncollapse", test_doctorBuffer_uncollapse },
469 { "Testing doctorBuffer_fromFile", test_doctorBuffer_fromFile },
470 { "Testing removeHtmlComents:", test_1_removeHtmlComments },
471 { "Testing removeLineComments:", test_2_removeLineComments },
472 { "Testing cleanUpPostscript:", test_3_cleanUpPostscript },
473 { "Testing removeBackslashes:", test_4_removeBackslashesAndGTroffIndicators },
474 { "Testing convertWhitespace:", test_5_convertWhitespaceToSpaceAndRemoveSpecialChars },
475 { "Testing dehyphen:", test_6_dehyphen },
476 { "Testing dehyphen2:", test_6a_dehyphen },
477 { "Testing removePunctuation:", test_7_removePunctuation },
478 { "Testing ignoreFunctionCalls:", test_8_ignoreFunctionCalls },
479 { "Testing convertSpaceToInvisible:", test_9_convertSpaceToInvisible },
480 { "Testing compressDoctoredBuffer:", test_10_compressDoctoredBuffer },
481 CU_TEST_INFO_NULL };
482 
void dehyphen(char *buf)
void removeBackslashesAndGTroffIndicators(char *buf)
Remove groff/troff font-size indicators, the literal string backslash-n and all backslahes,...
void doctorBuffer(char *buf, int isML, int isPS, int isCR)
Convert a buffer of multiple stuff to text-only, separated by spaces.
int compressDoctoredBuffer(char *textBuffer)
garbage collect: eliminate all INVISIBLE characters in the buffer
void convertSpaceToInvisible(char *buf)
void removePunctuation(char *buf)
Clean up miscellaneous punctuation.
void removeLineComments(char *buf)
Remove comments that start at the beginning of a line.
void cleanUpPostscript(char *buf)
Remove newlines from buffer.
void convertWhitespaceToSpaceAndRemoveSpecialChars(char *buf, int isCR)
Convert white-space to real spaces, and remove unnecessary punctuation.
void ignoreFunctionCalls(char *buf)
Ignore function calls to print routines.
void removeHtmlComments(char *buf)
Remove HTML comments from buffer without removing comment text.
void licenseInit()
license initialization
Definition: licenses.c:70
Nomos header file.
#define NO
Definition: nomos.h:171
int idxGrep_recordPosition(int index, char *data, int flags)
compile a regex, perform the search and record findings
Definition: nomos_regex.c:220
int idxGrep_recordPositionDoctored(int index, char *data, int flags)
compile a regex, perform the search and record findings
Definition: nomos_regex.c:241
FUNCTION void freeAndClearScan(struct curScan *thisScan)
Clean-up all the per scan data structures, freeing any old data.
Definition: nomos_utils.c:953
FUNCTION void initializeCurScan(struct curScan *cur)
Initialize the scanner.
Definition: nomos_utils.c:938
FUNCTION void addLicence(GArray *theMatches, char *licenceName)
Add a license to the matches array.
Definition: nomos_utils.c:1003
FUNCTION MatchPositionAndType * getMatchfromHighlightInfo(GArray *in, int index)
Get the MatchPositionAndType for a given index in highlight array.
Definition: nomos_utils.c:913
FUNCTION LicenceAndMatchPositions * getLicenceAndMatchPositions(GArray *in, int index)
Get the LicenceAndMatchPositions for a given index in match array.
Definition: nomos_utils.c:925
GArray * matchPositions
Match positions.
Definition: nomos.h:379
int start
Start position of match.
Definition: nomos.h:370
int end
End position of match.
Definition: nomos.h:371
GArray * indexList
Definition: nomos.h:416
GArray * theMatches
Definition: nomos.h:417
void test_9_convertSpaceToInvisible()
Test for convertSpaceToInvisible()
void test_5_convertWhitespaceToSpaceAndRemoveSpecialChars()
Test for convertWhitespaceToSpaceAndRemoveSpecialChars()
void test_6a_dehyphen()
Test for dehyphen()
void test_10_compressDoctoredBuffer()
Test for compressDoctoredBuffer()
void test_7_removePunctuation()
Test for removePunctuation()
void test_doctorBuffer_uncollapse()
Test for uncollapsePosition()
void test_3_cleanUpPostscript()
Test for cleanUpPostscript()
static void report_Match(char *buf)
Helper function to match licenses and highlight info.
void test_1_removeHtmlComments()
Test for removeHtmlComments()
void test_2_removeLineComments()
Test for removeLineComments()
void test_6_dehyphen()
Test for dehyphen()
void test_doctorBuffer_fromFile()
Test for idxGrep_recordPosition()
void test_doctorBuffer()
Test for doctorBuffer()
void test_8_ignoreFunctionCalls()
Test for ignoreFunctionCalls()
void test_4_removeBackslashesAndGTroffIndicators()
Test for removeBackslashesAndGTroffIndicators()