FOSSology  4.7.1
Open Source License Compliance by Open Source Software
test_scanners.cc
1 /*
2  SPDX-FileCopyrightText: © 2014-15, 2018 Siemens AG
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include <cppunit/TestFixture.h>
8 #include <cppunit/extensions/HelperMacros.h>
9 
10 #include "regex.hpp"
11 #include "regscan.hpp"
12 #include "copyrightUtils.hpp"
13 #include "cleanEntries.hpp"
14 #include <list>
15 #include <cstring>
16 #include <ostream>
17 
18 using namespace std;
19 
25 ostream& operator<<(ostream& out, const list<match>& l)
26 {
27  for (auto m = l.begin(); m != l.end(); ++m)
28  out << '[' << m->start << ':' << m->end << ':' << m->type << ']';
29  return out;
30 }
31 
35 const char testContent[] = "© 2007 Hugh Jackman\n\n"
36  "Copyright 2004 my company\n\n"
37  "Copyrights by any strange people\n\n"
38  "(C) copyright 2007-2011, 2013 my favourite company Google\n\n"
39  "(C) 2007-2011, 2013 my favourite company Google\n\n"
40  "if (c) { return -1 } \n\n"
41  "Written by: me, myself and Irene.\n\n"
42  "Authors all the people at ABC\n\n"
43  "<author>Author1</author>"
44  "<head>All the people</head>"
45  "<author>Author1 Author2 Author3</author>"
46  "<author>Author4</author><b>example</b>"
47  "Apache\n\n"
48  "This file is protected under pants 1 , 2 ,3\n\n"
49  "Do not modify this document\n\n"
50  "the shuttle is a space vehicle designed by NASA\n\n"
51  "visit http://mysite.org/FAQ or write to info@mysite.org\n\n"
52  "maintained by benjamin drieu <benj@debian.org>\n\n"
53  "* Copyright (c) 1989, 1993\n" // Really just one newline here!
54  "* The Regents of the University of California. All rights reserved.\n\n"
55  "to be licensed as a whole"
56  "/* Most of the following tests are stolen from RCS 5.7's src/conf.sh. */";
57 
58 class scannerTestSuite : public CPPUNIT_NS :: TestFixture {
59  CPPUNIT_TEST_SUITE (scannerTestSuite);
60  CPPUNIT_TEST (copyscannerTest);
61  CPPUNIT_TEST (copyscannerDotPrefixedNameTest);
62  CPPUNIT_TEST (copyscannerBracketedYearTest);
63  CPPUNIT_TEST (copyscannerBracketedYearNoiseTest);
64  CPPUNIT_TEST (copyscannerBracketedYearBoundariesTest);
65  CPPUNIT_TEST (copyscannerCopySymByHolderTest);
66  CPPUNIT_TEST (copyscannerCopySymPunctuatedHolderTest);
67  CPPUNIT_TEST (copyscannerBareKeywordDiscardTest);
68  CPPUNIT_TEST (copyscannerCopyrightedStatementTest);
69  CPPUNIT_TEST (copyscannerCamelCaseHolderTest);
70  CPPUNIT_TEST (copyscannerBinaryNoiseTest);
71  CPPUNIT_TEST (copyscannerSpdxFullLineTest);
72  CPPUNIT_TEST (copyscannerSpdxArrayTest);
73  CPPUNIT_TEST (copyscannerProseExceptionTest);
74  CPPUNIT_TEST (regAuthorTest);
75  CPPUNIT_TEST (regIpraTest);
76  CPPUNIT_TEST (regEccTest);
77  CPPUNIT_TEST (regUrlTest);
78  CPPUNIT_TEST (regEmailTest);
79  CPPUNIT_TEST (regEmailReservedDomainTest);
80  CPPUNIT_TEST (regKeywordTest);
81  CPPUNIT_TEST (cleanEntries);
82 
83  CPPUNIT_TEST_SUITE_END ();
84 
85 private:
93  void scannerTest (const scanner& sc, const char* content, const string& type, list<const char*> expectedStrings)
94  {
95  list<match> matches;
96  list<match> expected;
97  sc.ScanString(content, matches);
98 
99  for (auto s = expectedStrings.begin(); s != expectedStrings.end(); ++s)
100  {
101  const char * p = strstr(content, *s);
102  if (p)
103  {
104  int pos = p - content;
105  expected.push_back(match(pos, pos+strlen(*s), type));
106  }
107  // else: expected string is not contained in original string
108  }
109  CPPUNIT_ASSERT_EQUAL(expected, matches);
110  }
111 
112 protected:
121  {
122  // Test copyright matcher
124 
125  scannerTest(sc, testContent, "statement", { "© 2007 Hugh Jackman",
126  "Copyright 2004 my company",
127  "Copyrights by any strange people",
128  "(C) copyright 2007-2011, 2013 my favourite company Google",
129  "(C) 2007-2011, 2013 my favourite company Google",
130  "Copyright (c) 1989, 1993\n* The Regents of the University of California."
131  });
132  }
133 
139  {
141  // © followed by a dot-prefixed name (.NET) must be fully preserved
142  const char* content1 = "Copyright \xc2\xa9 .NET Foundation and contributors\n";
143  scannerTest(sc, content1, "statement",
144  {"Copyright \xc2\xa9 .NET Foundation and contributors"});
145 
146  // Standalone © (no 'copyright' keyword) with dot-prefixed name
147  const char* content2 = "\xc2\xa9 .NET Foundation and contributors\n";
148  scannerTest(sc, content2, "statement",
149  {"\xc2\xa9 .NET Foundation and contributors"});
150 
151  // Year-qualified form must still work
152  const char* content3 = "Copyright \xc2\xa9 2021 .NET Foundation\n";
153  scannerTest(sc, content3, "statement",
154  {"Copyright \xc2\xa9 2021 .NET Foundation"});
155  }
156 
162  {
164  const char* valid[] = {
165  "Copyright (c) [2021] [Marvin Countryman]",
166  "Copyright (C) [1999] Marvin Countryman",
167  "Copyright (c)[2021] [Marvin Countryman]",
168  "Copyright (c)\t[ 2021 ] [Marvin Countryman]",
169  "Copyright (c) [\t2021\t] [Marvin Countryman]",
170  "Copyright \xc2\xa9 [2021] [Marvin Countryman]",
171  "Copyright &copy; [2021] [Marvin Countryman]",
172  "Copyrighted (C) [2021] [Marvin Countryman]",
173  "Copyright (c) 2021 [Marvin Countryman]",
174  "Copyright [2021] [Marvin Countryman]",
175  "Copyright (c) 2021 Marvin Countryman"
176  };
177 
178  for (const char* statement : valid)
179  {
180  for (const char* ending : {"", "\n"})
181  {
182  list<match> matches;
183  sc.ScanString(string(statement) + ending, matches);
184  const string message = string("Expected full active match for: ") + statement;
185  CPPUNIT_ASSERT_EQUAL_MESSAGE(message, (size_t)1, matches.size());
186  const match& finding = matches.front();
187  CPPUNIT_ASSERT_MESSAGE(message, finding.is_enabled);
188  CPPUNIT_ASSERT_EQUAL_MESSAGE(message, string("statement"), finding.type);
189  CPPUNIT_ASSERT_EQUAL_MESSAGE(message, 0, finding.start);
190  CPPUNIT_ASSERT_EQUAL_MESSAGE(message, (int)strlen(statement), finding.end);
191  }
192  }
193  }
194 
200  {
202  const char* noise[] = {
203  "Copyright (c) [year] [name]\n",
204  "Copyright (c) [yyyy] [name]\n",
205  "Copyright (c) [Marvin Countryman]\n",
206  "Copyright (c) [c] [name]\n",
207  "Copyright (c) [] [name]\n",
208  "Copyright (c) [202] [name]\n",
209  "Copyright (c) [20210] [name]\n",
210  "Copyright (c) [2021suffix] [name]\n",
211  "Copyright (c) [2021 [name]\n",
212  "Copyright (c) [[2021]] [name]\n",
213  "Copyright (c) {year} {name}\n",
214  "Copyright (c) <year> <name>\n",
215  "Copyright (c) $year $name\n",
216  "Copyright (c) %year% %name%\n"
217  };
218 
219  for (const char* statement : noise)
220  {
221  list<match> matches;
222  sc.ScanString(statement, matches);
223  for (const auto& finding : matches)
224  CPPUNIT_ASSERT_MESSAGE(
225  string("Expected no active match for: ") + statement,
226  !finding.is_enabled);
227  }
228  }
229 
235  {
237  const char* content =
238  "// Copyright (c) [2021] [Marvin Countryman]. All rights reserved.\n"
239  "// Copyright (c) 2022 Another Company\n";
240  scannerTest(sc, content, "statement", {
241  "Copyright (c) [2021] [Marvin Countryman].",
242  "Copyright (c) 2022 Another Company"
243  });
244 
245  list<match> matches;
246  sc.ScanString(content, matches);
247  for (const auto& finding : matches)
248  CPPUNIT_ASSERT_MESSAGE("Both copyright statements must stay active",
249  finding.is_enabled);
250  }
251 
258  {
260  const char* valid[] = {
261  "Copyright(c) by Mei Qingguang",
262  "Copyright (c) by Mei Qingguang",
263  "Copyright (C) by Heiko Eissfeldt",
264  "Copyright \xc2\xa9 by Mei Qingguang"
265  };
266 
267  for (const char* statement : valid)
268  {
269  for (const char* ending : {"", "\n"})
270  {
271  list<match> matches;
272  sc.ScanString(string(statement) + ending, matches);
273  const string message = string("Expected full active match for: ") + statement;
274  CPPUNIT_ASSERT_EQUAL_MESSAGE(message, (size_t)1, matches.size());
275  const match& finding = matches.front();
276  CPPUNIT_ASSERT_MESSAGE(message, finding.is_enabled);
277  CPPUNIT_ASSERT_EQUAL_MESSAGE(message, 0, finding.start);
278  CPPUNIT_ASSERT_EQUAL_MESSAGE(message, (int)strlen(statement), finding.end);
279  }
280  }
281 
282  // Full MIT header: the grant text must be stripped, holder kept.
283  const char* mit =
284  "This software is licensed under the MIT License.\n\n"
285  "Copyright(c) by Mei Qingguang\n\n"
286  "Permission is hereby granted, free of charge, to any person obtaining a copy\n"
287  "of this software and associated documentation files (the \"Software\"), to deal\n"
288  "in the Software without restriction.\n";
289  scannerTest(sc, mit, "statement", {"Copyright(c) by Mei Qingguang"});
290 
291  // "by" alone still leaves nothing to keep.
292  const char* bare[] = {"Copyright (c) by\n", "(c) by\n"};
293  for (const char* statement : bare)
294  {
295  list<match> matches;
296  sc.ScanString(statement, matches);
297  for (const auto& finding : matches)
298  CPPUNIT_ASSERT_MESSAGE(
299  string("Expected no active match for: ") + statement,
300  !finding.is_enabled);
301  }
302  }
303 
311  {
313  const char* valid[] = {
314  "Copyright (c), Data Geekery GmbH, contact@datageekery.com",
315  "Copyright (c), Data Geekery GmbH",
316  "Copyright (C): Data Geekery GmbH",
317  "Copyright (c); Data Geekery GmbH",
318  "Copyright (c),Data Geekery GmbH",
319  "Copyright \xc2\xa9, Data Geekery GmbH",
320  "Copyright (c), 2021 Data Geekery GmbH",
321  "Copyright (c), by Data Geekery GmbH"
322  };
323 
324  for (const char* statement : valid)
325  {
326  for (const char* ending : {"", "\n"})
327  {
328  list<match> matches;
329  sc.ScanString(string(statement) + ending, matches);
330  const string message = string("Expected full active match for: ") + statement;
331  CPPUNIT_ASSERT_EQUAL_MESSAGE(message, (size_t)1, matches.size());
332  const match& finding = matches.front();
333  CPPUNIT_ASSERT_MESSAGE(message, finding.is_enabled);
334  CPPUNIT_ASSERT_EQUAL_MESSAGE(message, string("statement"), finding.type);
335  CPPUNIT_ASSERT_EQUAL_MESSAGE(message, 0, finding.start);
336  CPPUNIT_ASSERT_EQUAL_MESSAGE(message, (int)strlen(statement), finding.end);
337  }
338  }
339 
340  // Full Apache header: license grant stripped, holder kept.
341  const char* apache =
342  "/*\n"
343  " * Copyright (c), Data Geekery GmbH, contact@datageekery.com\n"
344  " *\n"
345  " * Licensed under the Apache License, Version 2.0 (the \"License\");\n"
346  " * you may not use this file except in compliance with the License.\n"
347  " */\n"
348  "package com.fasterxml.jackson.module.blackbird.util;\n";
349  scannerTest(sc, apache, "statement",
350  {"Copyright (c), Data Geekery GmbH, contact@datageekery.com"});
351 
352  // Punctuation followed by noise must still be stripped/discarded.
353  const char* noise[] = {
354  "Copyright (c), 0x1234\n",
355  "Copyright (c), {year} {name}\n",
356  "Copyright (c),\n"
357  };
358  for (const char* statement : noise)
359  {
360  list<match> matches;
361  sc.ScanString(statement, matches);
362  for (const auto& finding : matches)
363  CPPUNIT_ASSERT_MESSAGE(
364  string("Expected no active match for: ") + statement,
365  !finding.is_enabled);
366  }
367  }
368 
374  {
376 
377  // All bare-keyword variants must produce NO match
378  const char* bare[] = {
379  "copyright\n",
380  "Copyright\n",
381  "COPYRIGHT\n",
382  "Copyrights\n",
383  nullptr
384  };
385  for (int i = 0; bare[i]; ++i)
386  {
387  list<match> matches;
388  sc.ScanString(bare[i], matches);
389  CPPUNIT_ASSERT_MESSAGE(
390  string("Expected no match for bare keyword: ") + bare[i],
391  matches.empty());
392  }
393 
394  // Strings with real content must still produce exactly one match each
395  const char* valid[] = {
396  "Copyright 2021 .NET Foundation\n",
397  "Copyright (c) 2004 My Company\n",
398  "Copyright \xc2\xa9 .NET Foundation and contributors\n",
399  nullptr
400  };
401  for (int i = 0; valid[i]; ++i)
402  {
403  list<match> matches;
404  sc.ScanString(valid[i], matches);
405  CPPUNIT_ASSERT_MESSAGE(
406  string("Expected one match for valid copyright: ") + valid[i],
407  matches.size() == 1);
408  }
409  }
410 
417  {
419 
420  // All must produce exactly one active match
421  const char* valid[] = {
422  "Copyrighted (C) 1994 Normunds Saumanis (normunds@rx.tech.swh.lv)\n",
423  "Copyrighted (C) 1994, 1995, 1996 Normunds Saumanis (normunds@fi.ibm.com)\n",
424  "copyrighted (C) 1993 by Hartmut Schirmer\n",
425  "copyrighted 1992 by Mark Adler version c10p1, 10 January 1993\n",
426  "copyrighted 1990 Mark Adler\n",
427  "Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)\n",
428  "Copyright (C) Torres Martinez\n",
429  "Copyright (C) Tobias Schmidt\n",
430  "Copyright (C) Tomas Novak\n",
431  nullptr
432  };
433  for (int i = 0; valid[i]; ++i)
434  {
435  list<match> matches;
436  sc.ScanString(valid[i], matches);
437  // Must have at least one match and every match must be active
438  CPPUNIT_ASSERT_MESSAGE(
439  string("Expected active match for: ") + valid[i],
440  !matches.empty() && matches.front().is_enabled);
441  }
442  }
443 
454  {
456 
457  // Holder names beginning with "to" in any casing must stay active
458  const char* holders[] = {
459  "Copyright TOSHIBA CORPORATION, 2022. Part of the SW360 Portal Project.\n",
460  "Copyright Toshiba Software Development (Vietnam) Co., Ltd., 2022. Part of the SW360 Portal Project.\n",
461  "Copyright (C) TOSHIBA CORPORATION, 2025. Part of the SW360 Frontend Project.\n",
462  "Copyright (C) Toshiba Software Development (Vietnam) Co., Ltd., 2025. Part of the SW360 Frontend Project.\n",
463  "Copyright TOSHIBA TEC CORPORATION, 2023\n",
464  "Copyright TORONTO DOMINION BANK, 2019\n",
465  "Copyright TOYOTA MOTOR CORPORATION, 2018\n",
466  "Copyright TOMTOM INTERNATIONAL BV, 2021\n",
467  nullptr
468  };
469  for (int i = 0; holders[i]; ++i)
470  {
471  list<match> matches;
472  sc.ScanString(holders[i], matches);
473  CPPUNIT_ASSERT_MESSAGE(
474  string("Expected active match for holder: ") + holders[i],
475  !matches.empty() && matches.front().is_enabled);
476  }
477 
478  // Genuine camelCase identifiers must still be deactivated
479  const char* identifiers[] = {
480  "Copyright.ToString() returns the assembly attribute\n",
481  "copyright.ToArray().Select(x => x.Holder)\n",
482  "Copyright ToUpperInvariant helper for holder names\n",
483  nullptr
484  };
485  for (int i = 0; identifiers[i]; ++i)
486  {
487  list<match> matches;
488  sc.ScanString(identifiers[i], matches);
489  bool hasActive = !matches.empty() && matches.front().is_enabled;
490  CPPUNIT_ASSERT_MESSAGE(
491  string("Expected no active match for identifier: ") + identifiers[i],
492  !hasActive);
493  }
494  }
495 
502  {
504 
505  // Each string is © + short ASCII run + non-ASCII noise.
506  // None must produce an active match.
507  const char* noise[] = {
508  // 3-char ASCII prefix then non-ASCII
509  "\xc2\xa9 sjw\xc2\xa8noise\n",
510  "\xc2\xa9 OMr\xc2\xa5more\n",
511  "\xc2\xa9 tGa\xc3\x89garbage\n",
512  // 4-char ASCII prefix then non-ASCII
513  "\xc2\xa9 ZgU,\xc2\xb5garbage\n",
514  "\xc2\xa9 VJs0\xc3\x93noise\n",
515  // 7-char ASCII prefix then non-ASCII
516  "\xc2\xa9 NuXnHl{\xc2\xa4" "noise\n",
517  // 8-char ASCII prefix then non-ASCII
518  "\xc2\xa9 KtCtdy\x22s\xc3\xa8noise\n",
519  nullptr
520  };
521 
522  for (int i = 0; noise[i]; ++i)
523  {
524  list<match> matches;
525  sc.ScanString(noise[i], matches);
526  bool hasActive = !matches.empty() && matches.front().is_enabled;
527  CPPUNIT_ASSERT_MESSAGE(
528  string("Expected no active match for binary noise string #") + to_string(i),
529  !hasActive);
530  }
531  }
532 
539  {
541 
542  // Normal closed array: 1 deactivated header + 3 active elements
543  {
544  const char content[] =
545  "SPDX-FileCopyrightText = [\n"
546  "\"2026 Fraunhofer-Institut f\xC3\xBCr Produktionstechnik und Automatisierung IPA\",\n"
547  "\"2026 Hilscher Gesellschaft f\xC3\xBCr Systemautomation mbH\",\n"
548  "\"2026 Siemens AG\",\n"
549  "]\n";
550 
551  list<match> matches;
552  sc.ScanString(content, matches);
553 
554  CPPUNIT_ASSERT_EQUAL_MESSAGE("Expected 4 matches total", (size_t)4, matches.size());
555 
556  auto it = matches.begin();
557  CPPUNIT_ASSERT_MESSAGE("Header must be inactive", !it->is_enabled);
558  ++it;
559  for (int i = 1; i <= 3; ++i, ++it)
560  CPPUNIT_ASSERT_MESSAGE(
561  string("Array element ") + to_string(i) + " must be active", it->is_enabled);
562  }
563 
564  // Malformed array (no closing ]): scan must continue past the block and
565  // still detect copyright statements that follow it.
566  {
567  const char content[] =
568  "SPDX-FileCopyrightText = [\n"
569  "\"2026 Company A\",\n"
570  "\"2026 Company B\",\n"
571  // no closing ]
572  "Copyright 2021 Google LLC\n";
573 
574  list<match> matches;
575  sc.ScanString(content, matches);
576 
577  bool foundGoogle = false;
578  for (auto& m : matches) {
579  int len = m.end - m.start;
580  if (len > 0 && strncmp(content + m.start, "Copyright 2021 Google", 21) == 0)
581  foundGoogle = true;
582  }
583  CPPUNIT_ASSERT_MESSAGE(
584  "Copyright after unclosed SPDX array must still be detected", foundGoogle);
585  }
586  }
587 
595  {
597 
598  // Two lines with German umlauts (ü = \xC3\xBC), one plain ASCII line.
599  const char content[] =
600  "// SPDX-FileCopyrightText: 2026 Fraunhofer-Institut f\xC3\xBCr Produktionstechnik und Automatisierung IPA\n"
601  "// SPDX-FileCopyrightText: 2026 Hilscher Gesellschaft f\xC3\xBCr Systemautomation mbH\n"
602  "// SPDX-FileCopyrightText: 2026 Siemens AG\n";
603 
604  list<match> matches;
605  sc.ScanString(content, matches);
606 
607  CPPUNIT_ASSERT_EQUAL_MESSAGE("Expected 3 SPDX matches", (size_t)3, matches.size());
608 
609  // Verify each match is active and its end position reaches the line's '\n'
610  const char* lineStarts[3];
611  lineStarts[0] = strstr(content, "SPDX-FileCopyrightText: 2026 Fraunhofer");
612  lineStarts[1] = strstr(content, "SPDX-FileCopyrightText: 2026 Hilscher");
613  lineStarts[2] = strstr(content, "SPDX-FileCopyrightText: 2026 Siemens");
614 
615  int i = 0;
616  for (auto& m : matches)
617  {
618  CPPUNIT_ASSERT_MESSAGE(
619  string("Match ") + to_string(i) + " must be active",
620  m.is_enabled);
621 
622  int expectedStart = lineStarts[i] - content;
623  int expectedEnd = (int)(strchr(lineStarts[i], '\n') - content);
624 
625  CPPUNIT_ASSERT_EQUAL_MESSAGE(
626  string("Match ") + to_string(i) + " start",
627  expectedStart, m.start);
628  CPPUNIT_ASSERT_EQUAL_MESSAGE(
629  string("Match ") + to_string(i) + " end must reach line end",
630  expectedEnd, m.end);
631  ++i;
632  }
633  }
634 
649  {
651 
652  // All must produce NO active match (either deactivated or discarded)
653  const char* prose[] = {
654  "copyrights appearing in this test file\n",
655  "copyrights appears in the documentation\n",
656  "COPYRIGHT TO DETECT This section uses the standard header\n",
657  "copyright work that can be distributed\n",
658  "copyright protection under the terms of this License\n",
659  "copyrighted interfaces, the original copyright holder\n",
660  "copyright in the work, if the License is applied\n",
661  "copyright of this Package, but belong to whoever generated\n",
662  "copyright on material distributed under this License\n",
663  nullptr
664  };
665  for (int i = 0; prose[i]; ++i)
666  {
667  list<match> matches;
668  sc.ScanString(prose[i], matches);
669  bool hasActive = !matches.empty() && matches.front().is_enabled;
670  CPPUNIT_ASSERT_MESSAGE(
671  string("Expected no active match for prose: ") + prose[i],
672  !hasActive);
673  }
674 
675  // These must remain KEPT — year or (C)+year acts as holder separator
676  const char* valid[] = {
677  "Copyright (C) 2021 Toronto Inc.\n",
678  "Copyright 2021 Workday Inc.\n",
679  "Copyright (C) 2021 Interface Logic Inc.\n",
680  "Copyright (C) 2021 In-N-Out Burgers\n",
681  nullptr
682  };
683  for (int i = 0; valid[i]; ++i)
684  {
685  list<match> matches;
686  sc.ScanString(valid[i], matches);
687  CPPUNIT_ASSERT_MESSAGE(
688  string("Expected active match for: ") + valid[i],
689  !matches.empty() && matches.front().is_enabled);
690  }
691  }
692 
701  {
702  regexScanner sc("author", "copyright");
703  scannerTest(sc, testContent, "author", {
704  "Written by: me, myself and Irene.",
705  "Authors all the people at ABC",
706  "Author1",
707  "Author1 Author2 Author3",
708  "Author4",
709  "maintained by benjamin drieu <benj@debian.org>"
710  });
711  }
712 
720  void regIpraTest () {
721  regexScanner sc("ipra", "ipra");
722  scannerTest(sc, testContent, "ipra", { "US patents 1 , 2 ,3" });
723  }
724 
732  void regEccTest () {
733  regexScanner sc("ecc", "ecc");
734  scannerTest(sc, testContent, "ecc", { "space vehicle designed by NASA" });
735  }
736 
744  void regUrlTest () {
745  regexScanner sc("url", "copyright");
746  scannerTest(sc, testContent, "url", { "http://mysite.org/FAQ" });
747  }
748 
756  void regEmailTest () {
757  regexScanner sc("email", "copyright",1);
758  scannerTest(sc, testContent, "email", { "info@mysite.org", "benj@debian.org" });
759  }
760 
770  const char content[] =
771  "alice@example.com\n"
772  "<bob@example.net>\n"
773  "(carol@example.org)\n"
774  "dave@server.test\n"
775  "erin@host.invalid\n"
776  "grace@sub.example.com\n"
777  "frank@realcompany.de\n"
778  "john@myexample.com\n"
779  "maria@example.community\n"
780  "Contact paul@kernel.org.\n"
781  "sam@mail.co.uk\n";
782  regexScanner sc("email", "copyright", 1);
783  scannerTest(sc, content, "email", { "frank@realcompany.de",
784  "john@myexample.com", "maria@example.community", "paul@kernel.org",
785  "sam@mail.co.uk" });
786  }
787 
795  void regKeywordTest () {
796  regexScanner sc("keyword", "keyword");
797  scannerTest(sc, testContent, "keyword", {"patent", "licensed as", "stolen from"});
798  }
799 
808  void cleanEntries () {
809  // Binary content
810  string actualFileContent;
811  ReadFileToString("../testdata/testdata142", actualFileContent);
812 
813  vector<string> binaryStrings;
814  std::stringstream *ss = new std::stringstream(actualFileContent);
815  string temp;
816 
817  while (std::getline(*ss, temp)) {
818  binaryStrings.push_back(temp);
819  }
820 
821  // Simulate matches. Each line is a match
822  vector<match> matches;
823  int pos = 0;
824  int size = binaryStrings.size();
825  for (int i = 0; i < size; i++)
826  {
827  int length = binaryStrings[i].length();
828  matches.push_back(
829  match(pos, pos + length, "statement"));
830  pos += length + 1;
831  }
832 
833  // Expected data
834  string expectedFileContent;
835  ReadFileToString("../testdata/testdata142_exp", expectedFileContent);
836 
837  delete(ss);
838  ss = new std::stringstream(expectedFileContent);
839  vector<string> expectedStrings;
840  while (std::getline(*ss, temp)) {
841  expectedStrings.push_back(temp);
842  }
843 
844  vector<string> actualStrings;
845  for (size_t i = 0; i < matches.size(); i ++)
846  {
847  actualStrings.push_back(cleanMatch(actualFileContent, matches[i]));
848  }
849 
850  CPPUNIT_ASSERT(expectedStrings == actualStrings);
851  }
852 };
853 
854 CPPUNIT_TEST_SUITE_REGISTRATION( scannerTestSuite );
Implementation of scanner class for copyright.
Definition: copyscan.hpp:39
void ScanString(const string &s, list< match > &results) const
Scan a given string for copyright statements.
Definition: copyscan.cc:63
Provides a regex scanner using predefined regexs.
Definition: regscan.hpp:21
void copyscannerBracketedYearTest()
Preserve bracketed numeric years after a copyright symbol.
void copyscannerCamelCaseHolderTest()
Regression: REG_EXCEPTION_COPY carries a To[A-Z][a-zA-Z]{4,} branch to skip camelCase identifiers suc...
void copyscannerBracketedYearBoundariesTest()
Preserve offsets and separation from the following statement.
void regIpraTest()
Test Ipra scanner.
void copyscannerCopyrightedStatementTest()
Regression: "copyrighted" statements and names like "Tom" must not be falsely deactivated by REG_EXCE...
void copyscannerBinaryNoiseTest()
Regression: binary-file content with a short ASCII prefix before non-ASCII bytes must not be reported...
void regKeywordTest()
Test copyright scanner for keywords.
void copyscannerCopySymByHolderTest()
A copyright symbol followed by "by <holder>" without a year must not be stripped down to a bare keywo...
void copyscannerSpdxFullLineTest()
Regression: SPDX-FileCopyrightText entries must be detected as individual single-line statements and ...
void scannerTest(const scanner &sc, const char *content, const string &type, list< const char * > expectedStrings)
Runs scanner on content and check matches against expectedStrings.
void copyscannerBareKeywordDiscardTest()
Test that bare copyright keywords produce no matches.
void copyscannerDotPrefixedNameTest()
Test copyright scanner with dot-prefixed names like .NET Foundation.
void regEmailReservedDomainTest()
Test email scanner skips reserved domains (RFC 2606)
void regUrlTest()
Test copyright scanner for URL.
void regEmailTest()
Test copyright scanner for email.
void copyscannerTest()
Test copyright scanner.
void copyscannerCopySymPunctuatedHolderTest()
A copyright symbol separated from the holder by punctuation (e.g. "Copyright (c), Data Geekery GmbH")...
void copyscannerBracketedYearNoiseTest()
Brackets must not exempt placeholders or noise from cleanup.
void regEccTest()
Test ECC scanner.
void copyscannerProseExceptionTest()
Regression: license-prose strings that contain "copyright" as a common noun must be deactivated,...
void regAuthorTest()
Test copyright scanner for author.
void cleanEntries()
Test cleanMatch() to remove non-UTF8 text and extra spaces.
void copyscannerSpdxArrayTest()
Regression: SPDX-FileCopyrightText = [...] TOML array format must yield one active match per quoted e...
Abstract class to provide interface to scanners.
Definition: scanners.hpp:59
virtual void ScanString(const string &s, list< match > &results) const =0
Scan the given string and add matches to results.
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:37
int valid
If the information stored in buffer is valid.
bool ReadFileToString(const string &fileName, string &out)
Utility: read file to string from scanners.h.
Definition: scanners.cc:21
Store the results of a regex match.
Definition: scanners.hpp:28
const int start
Definition: scanners.hpp:35
const int end
Definition: scanners.hpp:35
bool is_enabled
Definition: scanners.hpp:46
std::ostream & operator<<(std::ostream &os, const std::vector< int > &x)
<< operator overload to appends a vector to an ostream object
Definition: testUtils.hpp:27