FOSSology  4.4.0
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  report_Match(undoc);
146 
147  initializeCurScan(&cur);
148  cur.currentLicenceIndex=0;
149  g_array_append_val(cur.indexList, licence_index);
150  g_array_append_val(cur.indexList, licence_index2);
151  addLicence(cur.theMatches, "WXwindows");
152  doctorBuffer(buf, 0, 0, 0);
153 
154  printf("\n%s\n", buf);
155  idxGrep_recordPositionDoctored(licence_index, buf, REG_ICASE | REG_EXTENDED);
156  idxGrep_recordPositionDoctored(licence_index2, buf, REG_ICASE | REG_EXTENDED);
157  report_Match(undoc);
158  freeAndClearScan(&cur);
159 
160  initializeCurScan(&cur);
161  cur.currentLicenceIndex=0;
162  g_array_append_val(cur.indexList, licence_index);
163  g_array_append_val(cur.indexList, licence_index2);
164  addLicence(cur.theMatches, "WXwindows");
165  idxGrep_recordPosition(licence_index, buf, REG_ICASE | REG_EXTENDED);
166  idxGrep_recordPosition(licence_index2, buf, REG_ICASE | REG_EXTENDED);
167  report_Match(buf);
168  freeAndClearScan(&cur);
169 
170  free(buf);
171  g_free(undoc);
172 
173 }
174 
181 
192 {
193  initializeCurScan(&cur);
194  char* textBuffer = g_strdup_printf("&quot the big\t(C) and long\\n &quot\\s-1234,"
195  " test &copy; string \n con-\n// tains losts; of . <string test> &nbsp;"
196  " <body> \" compli-\n cated /* COMMENT s and funny */ Words as it \n "
197  "mimi-cs printf(\"Licence\"); and so on\n &quot \n ");
198  removeHtmlComments(textBuffer);
199  char* te22Buffer = g_strdup_printf(" quot the big\t(C) and long\\n quot\\s-1234,"
200  " test &copy string \n con-\n// tains losts; of . <string test nbsp "
201  " body \" compli-\n cated /* COMMENT s and funny */ Words as it \n "
202  "mimi-cs printf(\"Licence\"); and so on\n quot \n ");
203  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
204  g_free(textBuffer);
205  g_free(te22Buffer);
206 }
207 
215 {
216  initializeCurScan(&cur);
217  char* textBuffer = g_strdup_printf(" quot the big\t(C) and long\\n quot\\s-1234,"
218  " test &copy string \n con-\n// tains losts; of . <string test nbsp "
219  " body \" compli-\n cated /* COMMENT s and funny */ Words as it \n "
220  "mimi-cs printf(\"Licence\"); and so on\n quot \n ");
221 
222  removeLineComments(textBuffer);
223 
224  char* te22Buffer = g_strdup_printf(" quot the big\t(C) and long\\n quot\\s-1234,"
225  " test &copy string \n con-\n\377\377 tains losts; of . <string test nbsp"
226  " body \" compli-\n cated /* COMMENT s and funny */ Words as it \n "
227  "mimi-cs printf(\"Licence\"); and so on\n quot \n ");
228  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
229  g_free(textBuffer);
230  g_free(te22Buffer);
231 }
232 
239 {
240  initializeCurScan(&cur);
241  char* textBuffer=g_strdup_printf("(8) (89) -9.- A %%!PS-Adobe-3.0 (12) EPSF-3.0 --8. -9.- A");
242 
243  cleanUpPostscript(textBuffer);
244 
245  char* te22Buffer = g_strdup_printf(" %%!PS-Adobe-3.0 (12) EPSF-3.0 ");
246  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
247  g_free(textBuffer);
248  g_free(te22Buffer);
249 }
250 
251 
260 {
261  initializeCurScan(&cur);
262  char* textBuffer= g_strdup_printf(" quot the big\t(C) and long\\n quot\\s-1234, test"
263  " &copy string \n con-\n\377\377 tains losts; of . <string test nbsp "
264  " body \" compli-\n cated /* COMMENT s and funny */ Words as it \n "
265  "mimi-cs printf(\"Licence\"); and so on\n quot \n ");
266 
268 
269  char* te22Buffer = g_strdup_printf(" quot the big\t(C) and long quot , test"
270  " &copy string \n con-\n\377\377 tains losts; of . <string test nbsp "
271  " body \" compli-\n cated /* COMMENT s and funny */ Words as it \n "
272  "mimi-cs printf(\"Licence\"); and so on\n quot \n ");
273  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
274  g_free(textBuffer);
275  g_free(te22Buffer);
276 }
277 
289 {
290  initializeCurScan(&cur);
291  char* textBuffer;
292  int isCR = NO; // isCR switches the replacment of ';' and '.', in the project it is always NO
293 
294  textBuffer = g_strdup_printf(" quot the big\t(C) and long quot , test"
295  " &copy string \n con-\n\377\377 tains losts; of . <string test nbsp "
296  " body \" compli-\n cated /* COMMENT s and funny */ Words as it \n "
297  "mimi-cs printf(\"Licence\"); and so on\n quot \n ");
298 
300 
301  char* te22Buffer = g_strdup_printf(" quot the big (C) and long quot test"
302  " &copy string con- \377\377 tains losts of \377 test nbsp "
303  " body compli- cated / COMMENT s and funny / Words as it "
304  "mimi-cs printf Licence and so on quot ");
305  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
306  g_free(textBuffer);
307  g_free(te22Buffer);
308 
309 }
310 
311 
326 {
327  initializeCurScan(&cur);
328  char* buf, *fer;
329  fer= g_strdup_printf("This- is the-test str- ing");
330  buf= g_strdup_printf("This\377\377is the-test str\377\377ing");
331  dehyphen(fer);
332  CU_ASSERT_STRING_EQUAL(buf, fer);
333  g_free(buf);
334  g_free(fer);
335 }
336 
344 {
345  initializeCurScan(&cur);
346  char* textBuffer= g_strdup_printf(" quot the big (C) and long quot , "
347  "test &copy string con- \377\377 tains losts of \377 test"
348  " nbsp body compli- cated / COMMENT s and funny / Words as it "
349  "mimi-cs printf Licence and so on quot ");
350  dehyphen(textBuffer);
351 
352  char* te22Buffer = g_strdup_printf(" quot the big (C) and long quot , test &copy "
353  "string con- \377\377 tains losts of \377 test nbsp body "
354  "compli\377\377\377cated / COMMENT s and funny / Words as it "
355  "mimi-cs printf Licence and so on quot ");
356  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
357  g_free(textBuffer);
358  g_free(te22Buffer);
359 
360 }
361 
369 {
370  initializeCurScan(&cur);
371  char* textBuffer= g_strdup_printf(" quot the big (C) and long quot , "
372  "test &copy string con- \377\377 tains losts of \377 test "
373  "nbsp body compli\377\377\377cated / COMMENT s and funny / Words as it "
374  "mimi-cs printf Licence and so on quot ");
376  removePunctuation(textBuffer);
377 
378  char* te22Buffer = g_strdup_printf(" quot the big (C) and long quot , test "
379  "&copy string con \377\377 tains losts of \377 test nbsp body "
380  "compli\377\377\377cated COMMENT s and funny Words as it "
381  "mimi-cs printf Licence and so on quot ");
382  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
383  g_free(textBuffer);
384  g_free(te22Buffer);
385 }
386 
398 {
399  initializeCurScan(&cur);
400  char* textBuffer= g_strdup_printf(" quot the big (C) and long quot , test &copy "
401  "string con \377\377 tains losts of \377 test nbsp body "
402  "compli\377\377\377cated COMMENT s and funny Words as it "
403  "mimi-cs printf Licence and so on quot ");
404  ignoreFunctionCalls(textBuffer);
405 
406  char* te22Buffer = g_strdup_printf(" quot the big (C) and long quot , test &copy "
407  "string con \377\377 tains losts of \377 test nbsp body "
408  "compli\377\377\377cated COMMENT s and funny Words as it "
409  "mimi-cs Licence and so on quot ");
410  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
411  g_free(textBuffer);
412  g_free(te22Buffer);
413 }
414 
423 {
424  initializeCurScan(&cur);
425  char* textBuffer= g_strdup_printf(" quot the big (C) and long quot , test &copy "
426  "string con \377\377 tains losts of \377 test nbsp body "
427  "compli\377\377\377cated COMMENT s and funny Words as it "
428  "mimi-cs Licence and so on quot ");
429  convertSpaceToInvisible(textBuffer);
430 
431  char* te22Buffer = g_strdup_printf(" quot the big (C) and long \377\377\377quot \377\377\377\377\377\377, "
432  "test &copy \377string \377\377con \377\377\377\377tains losts \377of \377\377\377\377\377\377\377\377\377\377"
433  "test \377\377nbsp \377\377\377body \377\377\377compli\377\377\377cated \377\377\377COMMENT s "
434  "and funny \377\377\377Words as it \377\377mimi-cs \377\377\377\377\377\377\377\377Licence \377\377\377"
435  "and so on \377\377quot \377\377");
436 // printf("%s\n", te22Buffer);
437  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
438  g_free(textBuffer);
439  g_free(te22Buffer);
440 }
441 
448 {
449  initializeCurScan(&cur);
450  char* textBuffer= g_strdup_printf(" quot the big (C) and long \377\377\377quot \377\377\377\377\377\377, "
451  "test &copy \377string \377\377con \377\377\377\377tains losts \377of \377\377\377\377\377\377\377\377\377\377"
452  "test \377\377nbsp \377\377\377body \377\377\377compli\377\377\377cated \377\377\377COMMENT s "
453  "and funny \377\377\377Words as it \377\377mimi-cs \377\377\377\377\377\377\377\377Licence \377\377\377"
454  "and so on \377\377quot \377\377");
455 
456  compressDoctoredBuffer(textBuffer);
457 
458  char* te22Buffer = g_strdup_printf(" quot the big (C) and long quot , test &copy string "
459  "con tains losts of test nbsp body complicated COMMENT s and funny Words as "
460  "it mimi-cs Licence and so on quot ");
461 
462  CU_ASSERT_STRING_EQUAL(te22Buffer, textBuffer);
463  g_free(textBuffer);
464  g_free(te22Buffer);
465 }
466 
467 CU_TestInfo doctorBuffer_testcases[] =
468 {
469 { "Testing doctorBuffer:", test_doctorBuffer },
470 { "Testing doctorBufer_uncollapse", test_doctorBuffer_uncollapse },
471 { "Testing doctorBuffer_fromFile", test_doctorBuffer_fromFile },
472 { "Testing removeHtmlComents:", test_1_removeHtmlComments },
473 { "Testing removeLineComments:", test_2_removeLineComments },
474 { "Testing cleanUpPostscript:", test_3_cleanUpPostscript },
475 { "Testing removeBackslashes:", test_4_removeBackslashesAndGTroffIndicators },
476 { "Testing convertWhitespace:", test_5_convertWhitespaceToSpaceAndRemoveSpecialChars },
477 { "Testing dehyphen:", test_6_dehyphen },
478 { "Testing dehyphen2:", test_6a_dehyphen },
479 { "Testing removePunctuation:", test_7_removePunctuation },
480 { "Testing ignoreFunctionCalls:", test_8_ignoreFunctionCalls },
481 { "Testing convertSpaceToInvisible:", test_9_convertSpaceToInvisible },
482 { "Testing compressDoctoredBuffer:", test_10_compressDoctoredBuffer },
483 CU_TEST_INFO_NULL };
484 
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:941
FUNCTION void initializeCurScan(struct curScan *cur)
Initialize the scanner.
Definition: nomos_utils.c:926
FUNCTION void addLicence(GArray *theMatches, char *licenceName)
Add a license to the matches array.
Definition: nomos_utils.c:991
FUNCTION MatchPositionAndType * getMatchfromHighlightInfo(GArray *in, int index)
Get the MatchPositionAndType for a given index in highlight array.
Definition: nomos_utils.c:901
FUNCTION LicenceAndMatchPositions * getLicenceAndMatchPositions(GArray *in, int index)
Get the LicenceAndMatchPositions for a given index in match array.
Definition: nomos_utils.c:913
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()