source: trunk/icedtea-web/tests/cpp-unit-tests/IcedTeaPluginUtilsTest.cc@ 443

Last change on this file since 443 was 427, checked in by dmik, 11 years ago

icedtea-web: Import version 1.5.1 from vendor.

File size: 9.3 KB
Line 
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#include <fstream>
38#include <UnitTest++.h>
39#include <stdio.h>
40
41#include <npapi.h>
42
43
44#include "browser_mock.h"
45#include "MemoryLeakDetector.h"
46
47#include "IcedTeaPluginUtils.h"
48#include "IcedTeaNPPlugin.h"
49
50
51void doDebugErrorRun();
52
53TEST(NPVariantAsString) {
54 NPVariant var;
55 STRINGZ_TO_NPVARIANT("test", var);
56
57 std::string cppstr = IcedTeaPluginUtilities::NPVariantAsString(var);
58 CHECK_EQUAL("test", cppstr);
59
60}
61
62TEST(NPStringCopy) {
63 std::string cppstr = "test";
64 NPString npstr = IcedTeaPluginUtilities::NPStringCopy(cppstr);
65
66 CHECK_EQUAL(4, npstr.UTF8Length);
67 CHECK_EQUAL("test", npstr.UTF8Characters);
68
69 // NPAPI states that browser allocation function should be used for NPString/NPVariant
70 CHECK_EQUAL(1, browsermock_unfreed_allocations());
71
72 browser_functions.memfree((void*) npstr.UTF8Characters);
73
74 CHECK_EQUAL(0, browsermock_unfreed_allocations());
75}
76
77TEST(NPVariantStringCopy) {
78 std::string cppstr = "test";
79 NPVariant npvar = IcedTeaPluginUtilities::NPVariantStringCopy(cppstr);
80
81 CHECK_EQUAL(NPVariantType_String, npvar.type);
82
83 CHECK_EQUAL(4, npvar.value.stringValue.UTF8Length);
84 CHECK_EQUAL("test", npvar.value.stringValue.UTF8Characters);
85
86 CHECK_EQUAL(1, browsermock_unfreed_allocations());
87
88 browser_functions.memfree((void*) npvar.value.stringValue.UTF8Characters);
89
90 CHECK_EQUAL(0, browsermock_unfreed_allocations());
91}
92
93TEST(NPIdentifierAsString) {
94 const char test_string[] = "foobar";
95 MemoryLeakDetector leak_detector;
96 /* Ensure destruction */{
97 std::string str = IcedTeaPluginUtilities::NPIdentifierAsString(
98 browser_functions.getstringidentifier(test_string));
99 CHECK_EQUAL(test_string, str);
100 }
101 CHECK_EQUAL(0, leak_detector.memory_leaks());
102}
103
104TEST(trim) {
105 std::string toBeTrimmed = std::string(" testX ");
106 IcedTeaPluginUtilities::trim (toBeTrimmed);
107 CHECK_EQUAL("testX", toBeTrimmed);
108
109 std::string toBeTrimmed2 = std::string(" \t testX\n");
110 IcedTeaPluginUtilities::trim (toBeTrimmed2);
111 CHECK_EQUAL("testX", toBeTrimmed2);
112
113 std::string toBeTrimmed3 = std::string(" \t \n te \n stX\n");
114 IcedTeaPluginUtilities::trim (toBeTrimmed3);
115 CHECK_EQUAL("te \n stX", toBeTrimmed3);
116}
117
118
119/* Creates a temporary file with the specified contents */
120static std::string temporary_file(const std::string& contents) {
121 std::string path = tmpnam(NULL); /* POSIX function, fine for test suite */
122 std::ofstream myfile;
123 myfile.open (path.c_str());
124 myfile << contents;
125 myfile.close();
126 return path;
127}
128
129
130TEST(file_exists) {
131 std::string f1 = temporary_file("dummy content");
132 bool a = IcedTeaPluginUtilities::file_exists(f1);
133 CHECK_EQUAL(a, true);
134
135 remove(f1.c_str());
136 bool b = IcedTeaPluginUtilities::file_exists(f1);
137 CHECK_EQUAL(b, false);
138
139 std::string dir = tmpnam(NULL);
140 const int PERMISSIONS_MASK = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; // 0755
141 bool created_dir = g_mkdir(dir.c_str(), PERMISSIONS_MASK);
142 CHECK_EQUAL(created_dir, false);
143 CHECK_EQUAL(IcedTeaPluginUtilities::file_exists(dir), true);
144}
145
146
147void doDebugErrorRun(int max) {
148 FILE* old1 = stdout;
149 FILE* old2 = stderr;
150 char* buf1 = " ";
151 char* buf2 = " ";
152 stdout = fmemopen (buf1, strlen (buf1), "rw");
153 stderr = fmemopen (buf2, strlen (buf2), "rw");
154
155 clock_t begin1, end1;
156 clock_t begin2, end2;
157 int i;
158 std::string hello = std::string("hello");
159 std::string eello = std::string("eello");
160
161 begin1 = clock();
162 for (i = 0 ; i < max ; i++ ) {
163 PLUGIN_DEBUG("hello \n");
164 PLUGIN_DEBUG("hello %s\n", hello.c_str());
165 PLUGIN_DEBUG("hello %d %d\n", 10 , 0.5);
166 PLUGIN_DEBUG("hello %s %s \n", hello.c_str() , hello.c_str());
167 PLUGIN_DEBUG("hello %s %d %s %d\n", hello.c_str() ,10, hello.c_str(), 0.5);
168 }
169 end1 = clock();
170 begin2 = clock();
171 for (i = 0 ; i < max ; i++ ) {
172 PLUGIN_ERROR("eello \n");
173 PLUGIN_ERROR("eello %s\n", eello.c_str());
174 PLUGIN_ERROR("eello %d %d\n", 10 , 0.5);
175 PLUGIN_ERROR("eello %s %s \n", eello.c_str() , eello.c_str());
176 PLUGIN_ERROR("eello %s %d %s %d\n", eello.c_str() ,10, eello.c_str(), 0.5);
177 }
178 end2 = clock();
179 fclose(stdout);
180 fclose(stderr);
181 stdout = old1;
182 stderr = old2;
183 long time_spent1 = ((end1 - begin1));
184 long time_spent2 = ((end2 - begin2));
185 fprintf (stdout, " PLUGIN_DEBUG %d, ", time_spent1);
186 fprintf (stdout, "PLUGIN_ERROR %d\n", time_spent2);
187}
188
189void doDebugErrorRun() {
190 doDebugErrorRun(1000000);
191}
192
193/*
194 *The family of PLUGIN_DEBUG_ERROR_PROFILING tests actually do not test.
195 *It is just messure that the mechanisms around do not break soething fataly.
196 */
197
198TEST(PLUGIN_DEBUG_ERROR_PROFILING_debug_on_headers_off) {
199 bool plugin_debug_backup = plugin_debug;
200 bool plugin_debug_headers_backup = plugin_debug_headers;
201 bool plugin_debug_console_backup = plugin_debug_to_console;
202 bool plugin_debug_system_backup = plugin_debug_to_system;
203 plugin_debug_to_console = false;
204 plugin_debug = true;
205 plugin_debug_to_system = false; //no need to torture system log in testing
206 doDebugErrorRun();
207 plugin_debug = plugin_debug_backup;
208 plugin_debug_headers = plugin_debug_headers_backup;
209 plugin_debug_to_console = plugin_debug_console_backup;
210 plugin_debug_to_system = plugin_debug_system_backup;
211}
212TEST(PLUGIN_DEBUG_ERROR_PROFILING_debug_off_headers_off) {
213 bool plugin_debug_backup = plugin_debug;
214 bool plugin_debug_headers_backup = plugin_debug_headers;
215 bool plugin_debug_console_backup = plugin_debug_to_console;
216 bool plugin_debug_system_backup = plugin_debug_to_system;
217 plugin_debug_to_console = false;
218 plugin_debug = false;
219 plugin_debug_to_system = false; //no need to torture system log in testing
220 doDebugErrorRun();
221 plugin_debug = plugin_debug_backup;
222 plugin_debug_headers = plugin_debug_headers_backup;
223 plugin_debug_to_console = plugin_debug_console_backup;
224 plugin_debug_to_system = plugin_debug_system_backup;
225}
226
227
228TEST(PLUGIN_DEBUG_ERROR_PROFILING_debug_on_headers_on) {
229 bool plugin_debug_backup = plugin_debug;
230 bool plugin_debug_headers_backup = plugin_debug_headers;
231 bool plugin_debug_console_backup = plugin_debug_to_console;
232 bool plugin_debug_system_backup = plugin_debug_to_system;
233 plugin_debug_to_console = false;
234 plugin_debug = true;
235 plugin_debug_headers = true;
236 plugin_debug_to_system = false; //no need to torture system log in testing
237 doDebugErrorRun();
238 plugin_debug = plugin_debug_backup;
239 plugin_debug_headers = plugin_debug_headers_backup;
240 plugin_debug_to_console = plugin_debug_console_backup;
241 plugin_debug_to_system = plugin_debug_system_backup;
242}
243
244TEST(PLUGIN_DEBUG_ERROR_PROFILING_debug_off_headers_on) {
245 bool plugin_debug_backup = plugin_debug;
246 bool plugin_debug_headers_backup = plugin_debug_headers;
247 bool plugin_debug_console_backup = plugin_debug_to_console;
248 bool plugin_debug_system_backup = plugin_debug_to_system;
249 plugin_debug_to_console = false;
250 plugin_debug = false;
251 plugin_debug_headers = true;
252 plugin_debug_to_system = false; //no need to torture system log in testing
253 doDebugErrorRun();
254 plugin_debug = plugin_debug_backup;
255 plugin_debug_headers = plugin_debug_headers_backup;
256 plugin_debug_to_console = plugin_debug_console_backup;
257 plugin_debug_to_system = plugin_debug_system_backup;
258}
259
260TEST(PLUGIN_DEBUG_ERROR_PROFILING_debug_on_headers_on_syslog_on) {
261 bool plugin_debug_backup = plugin_debug;
262 bool plugin_debug_headers_backup = plugin_debug_headers;
263 bool plugin_debug_console_backup = plugin_debug_to_console;
264 bool plugin_debug_system_backup = plugin_debug_to_system;
265 plugin_debug_to_console = false;
266 plugin_debug = true;
267 plugin_debug_headers = true;
268 plugin_debug_to_system = true;
269 doDebugErrorRun(50);
270 plugin_debug = plugin_debug_backup;
271 plugin_debug_headers = plugin_debug_headers_backup;
272 plugin_debug_to_console = plugin_debug_console_backup;
273 plugin_debug_to_system = plugin_debug_system_backup;
274}
275
Note: See TracBrowser for help on using the repository browser.