1 /* Copyright (C) 2012 Red Hat
2 
3  This file is part of IcedTea.
4 
5  IcedTea is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2, or (at your option)
8  any later version.
9 
10  IcedTea is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with IcedTea; see the file COPYING.  If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301 USA.
19 
20  Linking this library statically or dynamically with other modules is
21  making a combined work based on this library.  Thus, the terms and
22  conditions of the GNU General Public License cover the whole
23  combination.
24 
25  As a special exception, the copyright holders of this library give you
26  permission to link this library with independent modules to produce an
27  executable, regardless of the license terms of these independent
28  modules, and to copy and distribute the resulting executable under
29  terms of your choice, provided that you also meet, for each linked
30  independent module, the terms and conditions of the license of that
31  module.  An independent module is a module which is not derived from
32  or based on this library.  If you modify this library, you may extend
33  this exception to your version of the library, but you are not
34  obligated to do so.  If you do not wish to do so, delete this
35  exception statement from your version. */
36 
37 // The test runner.
38 // Note that all modules compiled with the TEST macro will append tests to
39 // a global test list, that is accessible via Test::GetTestList().
40 #include <cstdio>
41 
42 #include <UnitTest++.h>
43 #include <TestReporter.h>
44 
45 #include <npfunctions.h>
46 
47 #include "IcedTeaNPPlugin.h"
48 #include "browser_mock.h"
49 #include "MemoryLeakDetector.h"
50 #include "checked_allocations.h"
51 
52 using namespace UnitTest;
53 
full_testname(const TestDetails & details)54 static std::string full_testname(const TestDetails& details) {
55     std::string suite = details.suiteName;
56     if (suite == "DefaultSuite") {
57         return details.testName;
58     } else {
59         return suite + "." + details.testName;
60     }
61 }
62 
63 // Important for testing purposes of eg leaks between tests
reset_global_state()64 static void reset_global_state() {
65     browsermock_clear_state();
66     MemoryLeakDetector::reset_global_state();
67 }
68 
69 class IcedteaWebUnitTestReporter: public TestReporter {
70 public:
71 
IcedteaWebUnitTestReporter()72     IcedteaWebUnitTestReporter() {
73         // Unfortunately, there is no 'ReportSuccess'
74         // We use 'did_finish_correctly' to track successes
75         did_finish_correctly = false;
76     }
77 
ReportTestStart(const TestDetails & test)78     virtual void ReportTestStart(const TestDetails& test) {
79         reset_global_state();
80         pretest_allocs = cpp_unfreed_allocations();
81         did_finish_correctly = true;
82     }
83 
ReportFailure(const TestDetails & details,char const * failure)84     virtual void ReportFailure(const TestDetails& details,
85             char const* failure) {
86         std::string testname = full_testname(details);
87 
88         printf("FAILED: %s line %d (%s)\n", testname.c_str(),
89                 details.lineNumber, failure);
90 
91         did_finish_correctly = false;
92     }
93 
ReportTestFinish(const TestDetails & details,float secondsElapsed)94     virtual void ReportTestFinish(const TestDetails& details,
95             float secondsElapsed) {
96 
97         reset_global_state();
98         int posttest_allocs = cpp_unfreed_allocations();
99         std::string testname = full_testname(details);
100 
101         if (browsermock_unfreed_allocations() > 0) {
102             printf("*** WARNING: %s has a memory leak! %d more NPAPI allocations than frees!\n",
103                     testname.c_str(), browsermock_unfreed_allocations());
104         }
105         if (posttest_allocs > pretest_allocs) {
106             printf("*** WARNING: %s has a memory leak! %d more operator 'new' allocations than 'delete's!\n",
107                     testname.c_str(), posttest_allocs - pretest_allocs);
108         }
109 
110         if (did_finish_correctly) {
111             printf("Passed: %s\n", testname.c_str());
112         }
113     }
114 
ReportSummary(int totalTestCount,int failedTestCount,int failureCount,float secondsElapsed)115     virtual void ReportSummary(int totalTestCount, int failedTestCount,
116             int failureCount, float secondsElapsed) {
117 
118         if (failedTestCount > 0) {
119             printf("TEST SUITE FAILURE: Not all tests have passed!\n");
120         }
121 
122         printf("Total tests run: %d\n", totalTestCount);
123         printf("Test results: passed: %d; failed: %d\n",
124                 totalTestCount - failedTestCount, failedTestCount);
125     }
126 
127 private:
128     int pretest_allocs;
129     bool did_finish_correctly;
130 };
131 
run_icedtea_web_unit_tests()132 static int run_icedtea_web_unit_tests() {
133     IcedteaWebUnitTestReporter reporter;
134     TestRunner runner(reporter);
135 
136     return runner.RunTestsIf(Test::GetTestList(), NULL /*All suites*/,
137             True() /*All tests*/, 0 /*No time limit*/);
138 }
139 
140 /* Spawns the Java-side of the plugin, create request processing threads,
141  * and sets up a mocked 'browser' environment. */
initialize_plugin_components()142 static void initialize_plugin_components() {
143     NPNetscapeFuncs mocked_browser_functions = browsermock_create_table();
144     NPPluginFuncs unused_plugin_functions;
145     memset(&unused_plugin_functions, 0, sizeof(NPPluginFuncs));
146     unused_plugin_functions.size = sizeof(NPPluginFuncs);
147 
148     NP_Initialize (&mocked_browser_functions, &unused_plugin_functions);
149     start_jvm_if_needed();
150 }
151 
main()152 int main() {
153     initialize_plugin_components();
154 
155     int exitcode = run_icedtea_web_unit_tests();
156 
157     return exitcode;
158 }
159