18 #include <cppunit/TestFixture.h> 19 #include <cppunit/extensions/HelperMacros.h> 22 #include "regscan.hpp" 23 #include "copyrightUtils.hpp" 24 #include "cleanEntries.hpp" 36 ostream& operator<<(ostream& out, const list<match>& l)
38 for (
auto m = l.begin(); m != l.end(); ++m)
39 out <<
'[' << m->start <<
':' << m->end <<
':' << m->type <<
']';
46 const char testContent[] =
"© 2007 Hugh Jackman\n\n" 47 "Copyright 2004 my company\n\n" 48 "Copyrights by any strange people\n\n" 49 "(C) copyright 2007-2011, 2013 my favourite company Google\n\n" 50 "(C) 2007-2011, 2013 my favourite company Google\n\n" 51 "if (c) { return -1 } \n\n" 52 "Written by: me, myself and Irene.\n\n" 53 "Authors all the people at ABC\n\n" 54 "<author>Author1</author>" 55 "<head>All the people</head>" 56 "<author>Author1 Author2 Author3</author>" 57 "<author>Author4</author><b>example</b>" 59 "This file is protected under pants 1 , 2 ,3\n\n" 60 "Do not modify this document\n\n" 61 "the shuttle is a space vehicle designed by NASA\n\n" 62 "visit http://mysite.org/FAQ or write to info@mysite.org\n\n" 63 "maintained by benjamin drieu <benj@debian.org>\n\n" 64 "* Copyright (c) 1989, 1993\n" 65 "* The Regents of the University of California. All rights reserved.\n\n" 66 "to be licensed as a whole";
70 CPPUNIT_TEST (copyscannerTest);
71 CPPUNIT_TEST (regAuthorTest);
72 CPPUNIT_TEST (regEccTest);
73 CPPUNIT_TEST (regUrlTest);
74 CPPUNIT_TEST (regEmailTest);
75 CPPUNIT_TEST (regKeywordTest);
76 CPPUNIT_TEST (cleanEntries);
78 CPPUNIT_TEST_SUITE_END ();
88 void scannerTest (
const scanner& sc,
const char* content,
const string& type, list<const char*> expectedStrings)
94 for (
auto s = expectedStrings.begin();
s != expectedStrings.end(); ++
s)
96 const char * p = strstr(content, *
s);
99 int pos = p - content;
100 expected.push_back(
match(pos, pos+strlen(*
s), type));
104 CPPUNIT_ASSERT_EQUAL(expected, matches);
120 scannerTest(sc, testContent,
"statement", {
"© 2007 Hugh Jackman",
121 "Copyright 2004 my company",
122 "Copyrights by any strange people",
123 "(C) copyright 2007-2011, 2013 my favourite company Google",
124 "(C) 2007-2011, 2013 my favourite company Google",
125 "Copyright (c) 1989, 1993\n* The Regents of the University of California. All rights reserved." 139 scannerTest(sc, testContent,
"author", {
140 "Written by: me, myself and Irene.",
141 "Authors all the people at ABC",
143 "Author1 Author2 Author3",
145 "maintained by benjamin drieu <benj@debian.org>" 158 scannerTest(sc, testContent,
"ecc", {
"space vehicle designed by NASA" });
170 scannerTest(sc, testContent,
"url", {
"http://mysite.org/FAQ" });
182 scannerTest(sc, testContent,
"email", {
"info@mysite.org",
"benj@debian.org" });
194 scannerTest(sc, testContent,
"keyword", {
"patent",
"licensed as"});
207 string actualFileContent;
210 vector<string> binaryStrings;
211 std::stringstream *ss =
new std::stringstream(actualFileContent);
214 while (std::getline(*ss, temp)) {
215 binaryStrings.push_back(temp);
219 vector<match> matches;
221 int size = binaryStrings.size();
222 for (
int i = 0; i < size; i++)
224 int length = binaryStrings[i].length();
226 match(pos, pos + length,
"statement"));
231 string expectedFileContent;
235 ss =
new std::stringstream(expectedFileContent);
236 vector<string> expectedStrings;
237 while (std::getline(*ss, temp)) {
238 expectedStrings.push_back(temp);
241 vector<string> actualStrings;
242 for (
size_t i = 0; i < matches.size(); i ++)
244 actualStrings.push_back(
cleanMatch(actualFileContent, matches[i]));
247 CPPUNIT_ASSERT(expectedStrings == actualStrings);
bool ReadFileToString(const string &fileName, string &out)
Utility: read file to string from scanners.h.
Store the results of a regex match.
void regKeywordTest()
Test copyright scanner for keywords.
void regUrlTest()
Test copyright scanner for URL.
int s
The socket that the CLI will use to communicate.
virtual void ScanString(const string &s, list< match > &results) const =0
Scan the given string and add matches to results.
void regAuthorTest()
Test copyright scanner for author.
Provides a regex scanner using predefined regexs.
Abstract class to provide interface to scanners.
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 copyscannerTest()
Test copyright scanner.
void cleanEntries()
Test cleanMatch() to remove non-UTF8 text and extra spaces.
void regEmailTest()
Test copyright scanner for email.
void regEccTest()
Test ECC scanner.
Implementation of scanner class for copyright.
string cleanMatch(const string &sText, const match &m)
Clean the text based on type.