80
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description: Tests for function server
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
#include <e32base.h>
|
|
19 |
#include "TestHarness.h"
|
|
20 |
#include "testutils.h"
|
|
21 |
|
|
22 |
#include "mjvmnativeportlayer.h"
|
|
23 |
#include "mjavafile.h"
|
|
24 |
|
|
25 |
TEST_GROUP(TestJvmPortPriority)
|
|
26 |
{
|
|
27 |
TEST_SETUP()
|
|
28 |
{
|
|
29 |
}
|
|
30 |
|
|
31 |
TEST_TEARDOWN()
|
|
32 |
{
|
|
33 |
}
|
|
34 |
};
|
|
35 |
|
|
36 |
TEST_GROUP(TestJvmPort)
|
|
37 |
{
|
|
38 |
TEST_SETUP()
|
|
39 |
{
|
|
40 |
}
|
|
41 |
|
|
42 |
TEST_TEARDOWN()
|
|
43 |
{
|
|
44 |
}
|
|
45 |
};
|
|
46 |
|
|
47 |
_LIT(KTestFileName, "c:\\javaFileTest.txt");
|
|
48 |
_LIT(KTestReplaceFileName, "c:\\javaFileReplaceTest.txt");
|
|
49 |
_LIT(KTestDrmFileName1, "c:\\test.dm");
|
|
50 |
_LIT(KTestDrmFileName2, "c:\\test.dcf");
|
|
51 |
_LIT(KTestJarFileName, "c:\\test.jar");
|
|
52 |
_LIT(KTestDrmEncryptedFileName, "c:\\testDrm.dcf");
|
|
53 |
_LIT(KTestDrmEncryptedInstallerFileName, "c:\\testDrm.jar");
|
|
54 |
|
|
55 |
_LIT8(KTestText, "Testing..");
|
|
56 |
_LIT8(KTestText2, "Testing2..");
|
|
57 |
_LIT8(KJarText, "This is a jar file.");
|
|
58 |
_LIT8(KDrmText, "This is a drm protected file.");
|
|
59 |
_LIT8(KDM_CONTENT, "This is a dm file.");
|
|
60 |
_LIT8(KDCF_CONTENT, "This is a dcf file.");
|
|
61 |
|
|
62 |
TEST_GROUP(TestJavaFile)
|
|
63 |
{
|
|
64 |
RFs fs;
|
|
65 |
javaruntime::MJvmNativePortLayer* port;
|
|
66 |
javaruntime::MJavaFile* file;
|
|
67 |
TEST_SETUP()
|
|
68 |
{
|
|
69 |
port = 0;
|
|
70 |
fs.Connect();
|
|
71 |
fs.Delete(KTestFileName);
|
|
72 |
fs.Delete(KTestReplaceFileName);
|
|
73 |
}
|
|
74 |
|
|
75 |
TEST_TEARDOWN()
|
|
76 |
{
|
|
77 |
delete port;
|
|
78 |
delete file;
|
|
79 |
fs.Close();
|
|
80 |
}
|
|
81 |
};
|
|
82 |
// #undef CHECKWITHNOTE
|
|
83 |
// #define CHECKWITHNOTE(x, y) CHECK(x)
|
|
84 |
void checkPriority(const TThreadPriority& expected,
|
|
85 |
const TThreadPriority& recieved)
|
|
86 |
{
|
|
87 |
CHECKWITHNOTE(expected == recieved, "Priority check failed. Expecting: "<<expected<<", received: "<<recieved);
|
|
88 |
}
|
|
89 |
|
|
90 |
// Testing priority table.
|
|
91 |
TEST(TestJvmPortPriority, priorityTest)
|
|
92 |
{
|
|
93 |
int i = 0;
|
|
94 |
checkPriority(EPriorityMuchLess, javaruntime::GetSingleThreadPriority(i++));
|
|
95 |
checkPriority(EPriorityMuchLess, javaruntime::GetSingleThreadPriority(i++));
|
|
96 |
checkPriority(EPriorityMuchLess, javaruntime::GetSingleThreadPriority(i++));
|
|
97 |
checkPriority(EPriorityMuchLess, javaruntime::GetSingleThreadPriority(i++));
|
|
98 |
checkPriority(EPriorityMuchLess, javaruntime::GetSingleThreadPriority(i++));
|
|
99 |
checkPriority(EPriorityLess, javaruntime::GetSingleThreadPriority(i++));
|
|
100 |
checkPriority(EPriorityLess, javaruntime::GetSingleThreadPriority(i++));
|
|
101 |
checkPriority(EPriorityLess, javaruntime::GetSingleThreadPriority(i++));
|
|
102 |
checkPriority(EPriorityLess, javaruntime::GetSingleThreadPriority(i++));
|
|
103 |
checkPriority(EPriorityNormal, javaruntime::GetSingleThreadPriority(i++));
|
|
104 |
checkPriority(EPriorityNormal, javaruntime::GetSingleThreadPriority(i++));
|
|
105 |
checkPriority(EPriorityMore, javaruntime::GetSingleThreadPriority(i++));
|
|
106 |
|
|
107 |
}
|
|
108 |
void getPort(javaruntime::MJvmNativePortLayer*& port)
|
|
109 |
{
|
|
110 |
TRAPD(err, port = javaruntime::GetPortLayerL());
|
|
111 |
CHECKWITHNOTE(err == KErrNone, "Error in getting MJvmNativePortLayer: "<<err);
|
|
112 |
CHECKWITHNOTE(port, "Got null pointer when getting port layer");
|
|
113 |
}
|
|
114 |
|
|
115 |
void getFileAndPort(javaruntime::MJvmNativePortLayer*& port, javaruntime::MJavaFile*& file)
|
|
116 |
{
|
|
117 |
if (port == 0)
|
|
118 |
{
|
|
119 |
getPort(port);
|
|
120 |
}
|
|
121 |
TRAPD(err, file = port->GetJavaFileL());
|
|
122 |
CHECKWITHNOTE(err == KErrNone, "Error in getting MJavaFile: "<<err);
|
|
123 |
CHECKWITHNOTE(file, "Got null pointer when getting Java file");
|
|
124 |
}
|
|
125 |
|
|
126 |
_LIT(KOutpuFullDir,"c:\\logs\\java\\");
|
|
127 |
_LIT(KOutpuFullDir2,"c:\\logs\\java2\\");
|
|
128 |
_LIT(KConsole,"c:\\logs\\java\\vmconsole.txt");
|
|
129 |
|
|
130 |
// Testing log writings works.
|
|
131 |
TEST(TestJvmPort, logWritingTest)
|
|
132 |
{
|
|
133 |
RFs fs;
|
|
134 |
fs.Connect();
|
|
135 |
TUint dummy;
|
|
136 |
TBool dirExists = fs.Att(KOutpuFullDir, dummy) == KErrNone;
|
|
137 |
|
|
138 |
TInt res;
|
|
139 |
|
|
140 |
// If the c:\logs\java directory exixts rename it to java2
|
|
141 |
if (dirExists)
|
|
142 |
{
|
|
143 |
res = fs.Rename(KOutpuFullDir, KOutpuFullDir2);
|
|
144 |
CHECKWITHNOTE(res == KErrNone, "Rename failed: "<<res);
|
|
145 |
}
|
|
146 |
|
|
147 |
// Testing that writing to nonexisting dir works.
|
|
148 |
javaruntime::MJvmNativePortLayer* port;
|
|
149 |
getPort(port);
|
|
150 |
TRAP(res, port->WriteToStdoutL(_L8("StdOutTestNonExisting\n")));
|
|
151 |
CHECKWITHNOTE(res == KErrNone, "Error in writing to std.out: "<<res);
|
|
152 |
TRAP(res, port->WriteToStderrL(_L8("StdErrTestNonExisting\n")));
|
|
153 |
CHECKWITHNOTE(res == KErrNone, "Error in writing to std.err: "<<res);
|
|
154 |
|
|
155 |
CHECKWITHNOTE(fs.Att(KOutpuFullDir, dummy) != KErrNone, "The logs\\java dir should not be present: ");
|
|
156 |
|
|
157 |
delete port;
|
|
158 |
port = 0;
|
|
159 |
|
|
160 |
// Create c:\logs\java directory.
|
|
161 |
res = fs.MkDir(KOutpuFullDir);
|
|
162 |
CHECKWITHNOTE(res == KErrNone, "MkDir failed: "<<res);
|
|
163 |
|
|
164 |
// Needs to reinit the port, because the logs dir exists now.
|
|
165 |
getPort(port);
|
|
166 |
TRAP(res, port->WriteToStdoutL(_L8("StdOutTest\n")));
|
|
167 |
CHECKWITHNOTE(res == KErrNone, "Error in writing to std.out: "<<res);
|
|
168 |
TRAP(res, port->WriteToStderrL(_L8("StdErrTest\n")));
|
|
169 |
CHECKWITHNOTE(res == KErrNone, "Error in writing to std.err: "<<res);
|
|
170 |
|
|
171 |
// Check that the vmconsole.txt contains correct content.
|
|
172 |
RFile file;
|
|
173 |
res = file.Open(fs, KConsole, EFileShareReadersOrWriters);
|
|
174 |
CHECKWITHNOTE(res == KErrNone, "file open failed: "<<res);
|
|
175 |
TBuf8<30> bufR;
|
|
176 |
res = file.Read(bufR);
|
|
177 |
CHECKWITHNOTE(res == KErrNone, "file read failed: "<<res);
|
|
178 |
file.Close();
|
|
179 |
res = bufR.Compare(_L8("StdOutTest\nStdErrTest\n"));
|
|
180 |
CHECKWITHNOTE(res == 0, "Compare failed: "<<res);
|
|
181 |
|
|
182 |
// Delete the vmconsole.txt. RFileLogger seems to keep the file open for
|
|
183 |
// a while.
|
|
184 |
int tryCount = 0;
|
|
185 |
do
|
|
186 |
{
|
|
187 |
res = fs.Delete(KConsole);
|
|
188 |
if (res == KErrInUse)
|
|
189 |
{
|
|
190 |
tryCount++;
|
|
191 |
User::After(500 * 1000);
|
|
192 |
}
|
|
193 |
}
|
|
194 |
while (res == KErrInUse && tryCount < 200);
|
|
195 |
|
|
196 |
CHECKWITHNOTE(res == 0, "Delete failed: "<<res);
|
|
197 |
|
|
198 |
// Delete c:\logs\java directory.
|
|
199 |
res = fs.RmDir(KOutpuFullDir);;
|
|
200 |
CHECKWITHNOTE(res == 0, "RmDir failed: "<<res);
|
|
201 |
|
|
202 |
// If the c:\logs\java directory exixted before the tests, return it back.
|
|
203 |
if (dirExists)
|
|
204 |
{
|
|
205 |
res = fs.Rename(KOutpuFullDir2, KOutpuFullDir);
|
|
206 |
CHECKWITHNOTE(res == 0, "Rename2 failed: "<<res);
|
|
207 |
}
|
|
208 |
fs.Close();
|
|
209 |
delete port;
|
|
210 |
}
|
|
211 |
|
|
212 |
// Testing that opening a file works ok and when trying to re-open KErrInUse
|
|
213 |
// is returned.
|
|
214 |
TEST(TestJavaFile, fileOpen)
|
|
215 |
{
|
|
216 |
getFileAndPort(port, file);
|
|
217 |
int res = file->Open(fs, KTestJarFileName, EFileRead);
|
|
218 |
CHECKWITHNOTE(res == KErrNone, "Failed to open file: "<<res);
|
|
219 |
|
|
220 |
res = file->Open(fs, KTestJarFileName, EFileRead);
|
|
221 |
CHECKWITHNOTE(res == KErrInUse, "Open, unexpected result: "<<res);
|
|
222 |
file->Close();
|
|
223 |
}
|
|
224 |
|
|
225 |
// Testing that opening unexisting files.
|
|
226 |
TEST(TestJavaFile, fileOpenUnexisting)
|
|
227 |
{
|
|
228 |
getFileAndPort(port, file);
|
|
229 |
int res = file->Open(fs, _L("c:\\nonexisting.txt"), EFileRead);
|
|
230 |
CHECKWITHNOTE(res == KErrNotFound, "Open, unexpected result: "<<res);
|
|
231 |
|
|
232 |
res = file->Open(fs, _L("c:\\nonexisting.dcf"), EFileRead);
|
|
233 |
CHECKWITHNOTE(res == KErrNotFound, "Open2, unexpected result: "<<res);
|
|
234 |
|
|
235 |
res = file->Open(fs, _L("c:\\nonexisting.txt"), EFileRead);
|
|
236 |
CHECKWITHNOTE(res == KErrNotFound, "Open3, unexpected result: "<<res);
|
|
237 |
file->Close();
|
|
238 |
|
|
239 |
res = file->Open(fs, _L("c:\\nonexisting.dcf"), EFileRead);
|
|
240 |
CHECKWITHNOTE(res == KErrNotFound, "Open4, unexpected result: "<<res);
|
|
241 |
|
|
242 |
file->Close();
|
|
243 |
}
|
|
244 |
|
|
245 |
// Testing that creation of file to unaccessable location fails.
|
|
246 |
TEST(TestJavaFile, fileCreateUnAccessible)
|
|
247 |
{
|
|
248 |
getFileAndPort(port, file);
|
|
249 |
int res = file->Create(fs, _L("z:\\nonexisting.txt"), EFileRead);
|
|
250 |
CHECKWITHNOTE(res == KErrAccessDenied, "Create, unexpected result: "<<res);
|
|
251 |
file->Close();
|
|
252 |
}
|
|
253 |
|
|
254 |
// Testing that Replacing of file to unaccessable location fails.
|
|
255 |
TEST(TestJavaFile, fileReplaceUnAccessible)
|
|
256 |
{
|
|
257 |
getFileAndPort(port, file);
|
|
258 |
int res = file->Replace(fs, _L("z:\\nonexisting.txt"), EFileRead);
|
|
259 |
CHECKWITHNOTE(res == KErrAccessDenied, "Replace, unexpected result: "<<res);
|
|
260 |
file->Close();
|
|
261 |
}
|
|
262 |
|
|
263 |
// Testing that file creation succceeds. As a side test testing that Open,
|
|
264 |
// Create and replace will fail when using file object that is in use.
|
|
265 |
TEST(TestJavaFile, fileCreate1)
|
|
266 |
{
|
|
267 |
getFileAndPort(port, file);
|
|
268 |
int res = file->Create(fs, KTestFileName, EFileRead);
|
|
269 |
CHECKWITHNOTE(res == KErrNone, "Failed to create file: "<<res);
|
|
270 |
TUint dummy;
|
|
271 |
CHECKWITHNOTE(fs.Att(KTestFileName, dummy) == KErrNone, "File does't exist");
|
|
272 |
|
|
273 |
res = file->Open(fs, KTestFileName, EFileRead);
|
|
274 |
CHECKWITHNOTE(res == KErrInUse, "ReOpen, unexpected result: "<<res);
|
|
275 |
|
|
276 |
res = file->Replace(fs, KTestFileName, EFileRead);
|
|
277 |
CHECKWITHNOTE(res == KErrInUse, "ReReplace, unexpected result: "<<res);
|
|
278 |
|
|
279 |
res = file->Create(fs, KTestFileName, EFileRead);
|
|
280 |
CHECKWITHNOTE(res == KErrInUse, "ReCreate, unexpected result: "<<res);
|
|
281 |
|
|
282 |
file->Close();
|
|
283 |
}
|
|
284 |
|
|
285 |
// Testing read, write flush and seek for normal files.
|
|
286 |
TEST(TestJavaFile, fileCreateWriteReads)
|
|
287 |
{
|
|
288 |
getFileAndPort(port, file);
|
|
289 |
|
|
290 |
// Create the test file.
|
|
291 |
int res = file->Create(fs, KTestFileName, EFileRead);
|
|
292 |
CHECKWITHNOTE(res == KErrNone, "Failed to create file: "<<res);
|
|
293 |
TUint dummy;
|
|
294 |
CHECKWITHNOTE(fs.Att(KTestFileName, dummy) == KErrNone, "File does't exist");
|
|
295 |
|
|
296 |
TBuf8<15> bufR;
|
|
297 |
TBuf8<15> bufW;
|
|
298 |
bufW.Append(KTestText);
|
|
299 |
|
|
300 |
// Write content to buffer.
|
|
301 |
res = file->Write(bufW);
|
|
302 |
CHECKWITHNOTE(res == KErrNone, "Write failed: "<<res);
|
|
303 |
|
|
304 |
// Flush content to buffer.
|
|
305 |
res = file->Flush();
|
|
306 |
CHECKWITHNOTE(res == KErrNone, "Flush failed: "<<res);
|
|
307 |
|
|
308 |
// Set current position to the start of the file.
|
|
309 |
TInt pos = 0;
|
|
310 |
res = file->Seek(ESeekStart, pos);
|
|
311 |
CHECKWITHNOTE(res == KErrNone, "Seek failed: "<<res);
|
|
312 |
|
|
313 |
// Read the whole content of the file.
|
|
314 |
res = file->Read(bufR);
|
|
315 |
CHECKWITHNOTE(res == KErrNone, "Read failed: "<<res);
|
|
316 |
|
|
317 |
// Compare the the read and written are the same.
|
|
318 |
res = bufW.Compare(bufR);
|
|
319 |
CHECKWITHNOTE(res == 0, "Compare failed: "<<res);
|
|
320 |
|
|
321 |
// Read a portion of the content from the file.
|
|
322 |
bufR.Zero();
|
|
323 |
pos = 2;
|
|
324 |
res = file->Seek(ESeekStart, pos);
|
|
325 |
CHECKWITHNOTE(res == KErrNone, "Seek2 failed: "<<res);
|
|
326 |
res = file->Read(bufR, 5);
|
|
327 |
CHECKWITHNOTE(res == KErrNone, "Read2 failed: "<<res);
|
|
328 |
res = bufW.Mid(2,5).Compare(bufR);
|
|
329 |
CHECKWITHNOTE(res == 0, "Compare2 failed: "<<res);
|
|
330 |
|
|
331 |
// Read a portion of the content from the file. Testing
|
|
332 |
// ESeekEnd.
|
|
333 |
bufR.Zero();
|
|
334 |
pos = -6;
|
|
335 |
res = file->Seek(ESeekEnd, pos);
|
|
336 |
CHECKWITHNOTE(res == KErrNone, "Seek3 failed: "<<res);
|
|
337 |
res = file->Read(bufR, 4);
|
|
338 |
CHECKWITHNOTE(res == KErrNone, "Read3 failed: "<<res);
|
|
339 |
res = bufW.Mid(3,4).Compare(bufR);
|
|
340 |
CHECKWITHNOTE(res == 0, "Compare2 failed: "<<res);
|
|
341 |
|
|
342 |
file->Close();
|
|
343 |
}
|
|
344 |
|
|
345 |
// Testing all variants of Write methods, Size, and SetSize. As a side test
|
|
346 |
// testing that Open, Create and replace will fail when using file object
|
|
347 |
// that is in use.
|
|
348 |
TEST(TestJavaFile, fileCreateWritesRead)
|
|
349 |
{
|
|
350 |
getFileAndPort(port, file);
|
|
351 |
int res = file->Create(fs, KTestFileName, EFileRead);
|
|
352 |
CHECKWITHNOTE(res == KErrNone, "Failed to create file: "<<res);
|
|
353 |
TUint dummy;
|
|
354 |
CHECKWITHNOTE(fs.Att(KTestFileName, dummy) == KErrNone, "File does't exist");
|
|
355 |
|
|
356 |
res = file->Open(fs, KTestFileName, EFileRead);
|
|
357 |
CHECKWITHNOTE(res == KErrInUse, "ReOpen, unexpected result: "<<res);
|
|
358 |
|
|
359 |
res = file->Replace(fs, KTestFileName, EFileRead);
|
|
360 |
CHECKWITHNOTE(res == KErrInUse, "ReReplace, unexpected result: "<<res);
|
|
361 |
|
|
362 |
res = file->Create(fs, KTestFileName, EFileRead);
|
|
363 |
CHECKWITHNOTE(res == KErrInUse, "ReCreate, unexpected result: "<<res);
|
|
364 |
|
|
365 |
|
|
366 |
// Write some content to file.
|
|
367 |
TBuf8<45> bufR;
|
|
368 |
TBuf8<15> bufW;
|
|
369 |
bufW.Append(KTestText);
|
|
370 |
res = file->Write(bufW);
|
|
371 |
CHECKWITHNOTE(res == KErrNone, "Write failed: "<<res);
|
|
372 |
res = file->Write(bufW, 8); // Testing..Testing.
|
|
373 |
CHECKWITHNOTE(res == KErrNone, "Write failed2: "<<res);
|
|
374 |
res = file->Write(2, bufW); // TeTesting..sting.
|
|
375 |
CHECKWITHNOTE(res == KErrNone, "Write failed3: "<<res);
|
|
376 |
res = file->Write(8, bufW, 2); // TeTestinTe.sting.
|
|
377 |
CHECKWITHNOTE(res == KErrNone, "Write failed4: "<<res);
|
|
378 |
TInt pos = 0;
|
|
379 |
res = file->Seek(ESeekEnd, pos);
|
|
380 |
CHECKWITHNOTE(res == KErrNone, "Seek failed: "<<res);
|
|
381 |
res = file->Write(bufW, 4); // TeTestinTe.sting.Test
|
|
382 |
CHECKWITHNOTE(res == KErrNone, "Write failed5: "<<res);
|
|
383 |
file->Flush();
|
|
384 |
CHECKWITHNOTE(res == KErrNone, "Flush failed: "<<res);
|
|
385 |
|
|
386 |
// Set current position to the start of the file.
|
|
387 |
pos = 0;
|
|
388 |
res = file->Seek(ESeekStart, pos);
|
|
389 |
CHECKWITHNOTE(res == KErrNone, "Seek failed2: "<<res);
|
|
390 |
|
|
391 |
// Read the whole content of the file.
|
|
392 |
res = file->Read(bufR);
|
|
393 |
CHECKWITHNOTE(res == KErrNone, "Read failed: "<<res);
|
|
394 |
_LIT8(KTestRes, "TeTestinTe.sting.Test");
|
|
395 |
|
|
396 |
// Compare that the file content is correct.
|
|
397 |
res = bufR.Compare(KTestRes);
|
|
398 |
CHECKWITHNOTE(res == 0, "Compare failed: "<<res);
|
|
399 |
|
|
400 |
// Testing that Size works ok.
|
|
401 |
TInt size;
|
|
402 |
res = file->Size(size);
|
|
403 |
CHECKWITHNOTE(res == 0, "Size call failed: "<<res);
|
|
404 |
CHECKWITHNOTE(size == 21, "Size failed: "<<size);
|
|
405 |
|
|
406 |
// Testing that SetSize works ok.
|
|
407 |
res = file->SetSize(10);
|
|
408 |
CHECKWITHNOTE(size == 21, "SetSize failed: "<<res);
|
|
409 |
|
|
410 |
// Read the content ater size change.
|
|
411 |
bufR.Zero();
|
|
412 |
res = file->Seek(ESeekStart, pos);
|
|
413 |
res = file->Read(bufR);
|
|
414 |
CHECKWITHNOTE(res == KErrNone, "Read failed2: "<<res);
|
|
415 |
|
|
416 |
// The new size should be smaller that before SetSize.
|
|
417 |
pos = 0;
|
|
418 |
res = file->Size(size);
|
|
419 |
CHECKWITHNOTE(res == 0, "Size call failed2: "<<res);
|
|
420 |
CHECKWITHNOTE(size == 10, "Size failed2: "<<size);
|
|
421 |
|
|
422 |
// Compare that the file content is correct.
|
|
423 |
res = bufR.Compare(KTestRes().Left(10));
|
|
424 |
CHECKWITHNOTE(res == 0, "Compare failed: "<<res);
|
|
425 |
|
|
426 |
file->Close();
|
|
427 |
}
|
|
428 |
|
|
429 |
// Testing Replace method. As a side test testing that Open, Create and
|
|
430 |
// replace will fail when using file object that is in use.
|
|
431 |
TEST(TestJavaFile, fileReplace)
|
|
432 |
{
|
|
433 |
getFileAndPort(port, file);
|
|
434 |
|
|
435 |
// Checking that file that we are about to create doesn't exist.
|
|
436 |
TUint dummy;
|
|
437 |
CHECKWITHNOTE(fs.Att(KTestReplaceFileName, dummy) != KErrNone, "File exists before");
|
|
438 |
|
|
439 |
// Using Replace to create the file.
|
|
440 |
int res = file->Replace(fs, KTestReplaceFileName, EFileRead);
|
|
441 |
CHECKWITHNOTE(res == KErrNone, "Failed to Replace file: "<<res);
|
|
442 |
CHECKWITHNOTE(fs.Att(KTestReplaceFileName, dummy) == KErrNone, "File does't exist");
|
|
443 |
|
|
444 |
res = file->Open(fs, KTestReplaceFileName, EFileRead);
|
|
445 |
CHECKWITHNOTE(res == KErrInUse, "ReOpen, unexpected result: "<<res);
|
|
446 |
|
|
447 |
res = file->Replace(fs, KTestReplaceFileName, EFileRead);
|
|
448 |
CHECKWITHNOTE(res == KErrInUse, "ReReplace, unexpected result: "<<res);
|
|
449 |
|
|
450 |
res = file->Create(fs, KTestReplaceFileName, EFileRead);
|
|
451 |
CHECKWITHNOTE(res == KErrInUse, "ReCreate, unexpected result: "<<res);
|
|
452 |
|
|
453 |
// Write content to created file.
|
|
454 |
TBuf8<45> bufR;
|
|
455 |
TBuf8<15> bufW;
|
|
456 |
bufW.Append(KTestText);
|
|
457 |
res = file->Write(bufW);
|
|
458 |
CHECKWITHNOTE(res == KErrNone, "Write failed: "<<res);
|
|
459 |
|
|
460 |
// Read the content and compare that the content is correct.
|
|
461 |
TInt pos = 0;
|
|
462 |
res = file->Seek(ESeekStart, pos);
|
|
463 |
CHECKWITHNOTE(res == KErrNone, "Seek failed: "<<res);
|
|
464 |
res = file->Read(bufR);
|
|
465 |
CHECKWITHNOTE(res == KErrNone, "Read failed: "<<res);
|
|
466 |
res = bufR.Compare(KTestText);
|
|
467 |
CHECKWITHNOTE(res == 0, "Compare failed: "<<res);
|
|
468 |
file->Close();
|
|
469 |
|
|
470 |
// Replace the existing file.
|
|
471 |
res = file->Replace(fs, KTestReplaceFileName, EFileRead);
|
|
472 |
CHECKWITHNOTE(res == KErrNone, "Failed to Replace file2: "<<res);
|
|
473 |
CHECKWITHNOTE(fs.Att(KTestReplaceFileName, dummy) == KErrNone, "File does't exist2");
|
|
474 |
|
|
475 |
// Check that the file size is zero i.e. a new file.
|
|
476 |
TInt size;
|
|
477 |
res = file->Size(size);
|
|
478 |
CHECKWITHNOTE(res == 0, "Size call failed: "<<res);
|
|
479 |
CHECKWITHNOTE(size == 0, "Size failed: "<<size);
|
|
480 |
|
|
481 |
// Write content to recreated file.
|
|
482 |
bufW.Zero();
|
|
483 |
bufR.Zero();
|
|
484 |
bufW.Append(KTestText2);
|
|
485 |
res = file->Write(bufW);
|
|
486 |
CHECKWITHNOTE(res == KErrNone, "Write failed2: "<<res);
|
|
487 |
|
|
488 |
// Read the content and compare that the content is correct.
|
|
489 |
res = file->Seek(ESeekStart, pos);
|
|
490 |
CHECKWITHNOTE(res == KErrNone, "Seek failed2: "<<res);
|
|
491 |
res = file->Read(bufR);
|
|
492 |
CHECKWITHNOTE(res == KErrNone, "Read failed2: "<<res);
|
|
493 |
res = bufR.Compare(KTestText2);
|
|
494 |
CHECKWITHNOTE(res == 0, "Compare failed2: "<<res);
|
|
495 |
file->Close();
|
|
496 |
}
|
|
497 |
|
|
498 |
// Testing Lock and Unlock methods.
|
|
499 |
TEST(TestJavaFile, fileLock)
|
|
500 |
{
|
|
501 |
getFileAndPort(port, file);
|
|
502 |
|
|
503 |
// Creating two files objects pointing to same directory.
|
|
504 |
int res = file->Create(fs, KTestFileName, EFileWrite | EFileShareAny);
|
|
505 |
CHECKWITHNOTE(res == KErrNone, "Failed to Create file: "<<res);
|
|
506 |
|
|
507 |
javaruntime::MJavaFile* file2;
|
|
508 |
getFileAndPort(port, file2);
|
|
509 |
res = file2->Open(fs, KTestFileName, EFileRead | EFileShareAny);
|
|
510 |
CHECKWITHNOTE(res == KErrNone, "Failed to Open file: "<<res);
|
|
511 |
|
|
512 |
// Write some content.
|
|
513 |
res = file->Write(KTestText);
|
|
514 |
CHECKWITHNOTE(res == KErrNone, "Failed to write to file: "<<res);
|
|
515 |
|
|
516 |
// Write some content.
|
|
517 |
res = file->Lock(3, 4);
|
|
518 |
CHECKWITHNOTE(res == KErrNone, "Failed to lock the file: "<<res);
|
|
519 |
|
|
520 |
// Lock the area
|
|
521 |
TInt pos = 4;
|
|
522 |
res = file2->Seek(ESeekStart, pos);
|
|
523 |
CHECKWITHNOTE(res == KErrNone, "Seek failed: "<<res);
|
|
524 |
|
|
525 |
// Check that locked the area is unaccessable.
|
|
526 |
TBuf8<45> bufR;
|
|
527 |
res = file2->Read(bufR, 2);
|
|
528 |
CHECKWITHNOTE(res == KErrLocked, "Expecting locked when reading: "<<res);
|
|
529 |
|
|
530 |
// Unlock the area
|
|
531 |
res = file->UnLock(3, 4);
|
|
532 |
CHECKWITHNOTE(res == KErrNone, "Failed to unlock the file: "<<res);
|
|
533 |
|
|
534 |
// Check that unlocked the area is now accessable.
|
|
535 |
res = file2->Read(bufR, 2);
|
|
536 |
CHECKWITHNOTE(res == KErrNone, "Read failed: "<<res);
|
|
537 |
res = KTestText().Mid(4,2).Compare(bufR);
|
|
538 |
CHECKWITHNOTE(res == 0, "Compare failed: "<<res);
|
|
539 |
|
|
540 |
file->Close();
|
|
541 |
file2->Close();
|
|
542 |
delete file2;
|
|
543 |
}
|
|
544 |
|
|
545 |
|
|
546 |
// Testing that jar file can be opened. This test is for checking a branch in
|
|
547 |
// IsDrm() method.
|
|
548 |
TEST(TestJavaFile, openJar)
|
|
549 |
{
|
|
550 |
getFileAndPort(port, file);
|
|
551 |
|
|
552 |
// Open the jar file.
|
|
553 |
TInt res = file->Open(fs, KTestJarFileName, EFileRead);
|
|
554 |
CHECKWITHNOTE(res == KErrNone, "Open, unexpected result: "<<res);
|
|
555 |
|
|
556 |
// Read and compare content.
|
|
557 |
TBuf8<45> bufR;
|
|
558 |
res = file->Read(bufR);
|
|
559 |
CHECKWITHNOTE(res == KErrNone, "Read failed: "<<res);
|
|
560 |
res = bufR.Compare(KJarText);
|
|
561 |
CHECKWITHNOTE(res == 0, "Compare failed: "<<res);
|
|
562 |
file->Close();
|
|
563 |
}
|
|
564 |
|
|
565 |
// Testing the file open through CAF with files that are not DRM protected.
|
|
566 |
void testOpenCafFile(RFs& fs,
|
|
567 |
javaruntime::MJavaFile* file,
|
|
568 |
const TDesC& fileName,
|
|
569 |
const TDesC8& compareString)
|
|
570 |
{
|
|
571 |
// Open file using CAF.
|
|
572 |
TInt res = file->Open(fs, fileName, EFileRead);
|
|
573 |
CHECKWITHNOTE(res == KErrNone, "Failed to open file: "<<res);
|
|
574 |
|
|
575 |
// Read the content and compare.
|
|
576 |
TBuf8<45> bufR;
|
|
577 |
res = file->Read(bufR);
|
|
578 |
CHECKWITHNOTE(res == KErrNone, "Read failed: "<<res);
|
|
579 |
res = bufR.Compare(compareString);
|
|
580 |
CHECKWITHNOTE(res == 0, "Compare failed: "<<res);
|
|
581 |
|
|
582 |
// Read the partial content and compare. Using ESeekStart.
|
|
583 |
bufR.Zero();
|
|
584 |
TInt pos = 2;
|
|
585 |
res = file->Seek(ESeekStart, pos);
|
|
586 |
CHECKWITHNOTE(res == KErrNone, "Seek failed: "<<res);
|
|
587 |
res = file->Read(bufR, 5);
|
|
588 |
CHECKWITHNOTE(res == KErrNone, "Read2 failed: "<<res);
|
|
589 |
res = compareString.Mid(2,5).Compare(bufR);
|
|
590 |
CHECKWITHNOTE(res == 0, "Compare2 failed: "<<res);
|
|
591 |
|
|
592 |
// Read the partial content and compare. Using ESeekEnd.
|
|
593 |
bufR.Zero();
|
|
594 |
pos = -6;
|
|
595 |
res = file->Seek(ESeekEnd, pos);
|
|
596 |
CHECKWITHNOTE(res == KErrNone, "Seek3 failed: "<<res);
|
|
597 |
res = file->Read(bufR, 4);
|
|
598 |
CHECKWITHNOTE(res == KErrNone, "Read3 failed: "<<res);
|
|
599 |
res = compareString.Mid(compareString.Length() - 6, 4).Compare(bufR);
|
|
600 |
CHECKWITHNOTE(res == 0, "Compare2 failed: "<<res);
|
|
601 |
|
|
602 |
// Check that Size method works also in case of CAF.
|
|
603 |
TInt size;
|
|
604 |
res = file->Size(size);
|
|
605 |
CHECKWITHNOTE(res == 0, "Size call failed: "<<res);
|
|
606 |
CHECKWITHNOTE(size == compareString.Length(), "Size failed: "<<size);
|
|
607 |
|
|
608 |
file->Close();
|
|
609 |
}
|
|
610 |
|
|
611 |
|
|
612 |
// Testing CAF with .dm file extension.
|
|
613 |
TEST(TestJavaFile, readDrmDmFile)
|
|
614 |
{
|
|
615 |
getFileAndPort(port, file);
|
|
616 |
testOpenCafFile(fs, file, KTestDrmFileName1, KDM_CONTENT);
|
|
617 |
}
|
|
618 |
|
|
619 |
// Testing CAF with .dcf file extension.
|
|
620 |
TEST(TestJavaFile, readDrmDcfFile)
|
|
621 |
{
|
|
622 |
getFileAndPort(port, file);
|
|
623 |
testOpenCafFile(fs, file, KTestDrmFileName2, KDCF_CONTENT);
|
|
624 |
}
|
|
625 |
|
|
626 |
// Testing methods that are not supported by CAF. Expecting correct error codes.
|
|
627 |
TEST(TestJavaFile, readDrmNotSupportedMethods)
|
|
628 |
{
|
|
629 |
// Open file using CAF.
|
|
630 |
getFileAndPort(port, file);
|
|
631 |
TInt res = file->Open(fs, KTestDrmFileName1, EFileRead);
|
|
632 |
CHECKWITHNOTE(res == KErrNone, "Failed to open file: "<<res);
|
|
633 |
|
|
634 |
// Trying to re-open the file - this should fail.
|
|
635 |
res = file->Open(fs, KTestDrmFileName1, EFileRead);
|
|
636 |
CHECKWITHNOTE(res == KErrInUse, "Open, unexpected result: "<<res);
|
|
637 |
|
|
638 |
res = file->SetSize(10);
|
|
639 |
CHECKWITHNOTE(res == KErrNotSupported, "SetSize, unexpected result: "<<res);
|
|
640 |
|
|
641 |
_LIT8(KDummy, "dummy");
|
|
642 |
res = file->Write(KDummy);
|
|
643 |
CHECKWITHNOTE(res == KErrNotSupported, "Write, unexpected result: "<<res);
|
|
644 |
|
|
645 |
res = file->Write(KDummy, 1);
|
|
646 |
CHECKWITHNOTE(res == KErrNotSupported, "Write2, unexpected result: "<<res);
|
|
647 |
|
|
648 |
res = file->Write(1, KDummy);
|
|
649 |
CHECKWITHNOTE(res == KErrNotSupported, "Write3, unexpected result: "<<res);
|
|
650 |
|
|
651 |
res = file->Write(1, KDummy, 1);
|
|
652 |
CHECKWITHNOTE(res == KErrNotSupported, "Write4, unexpected result: "<<res);
|
|
653 |
|
|
654 |
res = file->Lock(1, 1);
|
|
655 |
CHECKWITHNOTE(res == KErrNotSupported, "Lock, unexpected result: "<<res);
|
|
656 |
|
|
657 |
res = file->UnLock(1, 1);
|
|
658 |
CHECKWITHNOTE(res == KErrNotSupported, "UnLock, unexpected result: "<<res);
|
|
659 |
|
|
660 |
res = file->Flush();
|
|
661 |
CHECKWITHNOTE(res == KErrNotSupported, "Flush, unexpected result: "<<res);
|
|
662 |
|
|
663 |
file->Close();
|
|
664 |
}
|
|
665 |
|
|
666 |
// Testing file reading that is DRM protected. Reading through CAF. This test
|
|
667 |
// requires that rights object is added to key store. See readme.txt for
|
|
668 |
// instructions.
|
|
669 |
TEST(TestJavaFile, readDrmProtectedFile)
|
|
670 |
{
|
|
671 |
// Open the DRM protected jar file.
|
|
672 |
getFileAndPort(port, file);
|
|
673 |
TInt res = file->Open(fs, KTestDrmEncryptedFileName, EFileRead);
|
|
674 |
CHECKWITHNOTE(res == KErrNone, "Failed to open file: "<<res);
|
|
675 |
|
|
676 |
// Read the content and compare.
|
|
677 |
TBuf8<45> bufR;
|
|
678 |
res = file->Read(bufR);
|
|
679 |
CHECKWITHNOTE(res == KErrNone, "Read failed: "<<res);
|
|
680 |
res = bufR.Compare(KDrmText);
|
|
681 |
CHECKWITHNOTE(res == 0, "Compare failed: "<<res);
|
|
682 |
|
|
683 |
file->Close();
|
|
684 |
|
|
685 |
// Open the DRM protected file having .jar extension. This will not be
|
|
686 |
// opened through CAF and therefore the content should not be in decrypted
|
|
687 |
// format.
|
|
688 |
res = file->Open(fs, KTestDrmEncryptedInstallerFileName, EFileRead);
|
|
689 |
CHECKWITHNOTE(res == KErrNone, "Failed to open file2: "<<res);
|
|
690 |
|
|
691 |
// Read the content.
|
|
692 |
bufR.Zero();
|
|
693 |
res = file->Read(bufR);
|
|
694 |
CHECKWITHNOTE(res == KErrNone, "Read failed2: "<<res);
|
|
695 |
|
|
696 |
// Check that we were not able to read the content as decrypted format.
|
|
697 |
res = bufR.Compare(KDrmText);
|
|
698 |
CHECKWITHNOTE(res != 0, "Compare failed2: "<<res);
|
|
699 |
file->Close();
|
|
700 |
}
|
|
701 |
|
|
702 |
// This test tests that the jar file that is DRM protected can be opended in
|
|
703 |
// decrypted format when the process name is JavaInstaller.
|
|
704 |
TEST(TestJavaFile, readDrmProtectedInstaller)
|
|
705 |
{
|
|
706 |
// OpenC uses statics and those will cause a memory allocations that will
|
|
707 |
// be interpreted as a leak.
|
|
708 |
EXPECT_N_LEAKS(33);
|
|
709 |
|
|
710 |
// Faking the process name to be JavaInstaller.
|
|
711 |
_LIT(KJavaInstallerProcessName, "JavaInstaller");
|
|
712 |
_LIT(KOrigProcessName, "testjvmport");
|
|
713 |
// Rename installer process and main thread.
|
|
714 |
TInt res = User::RenameProcess(KJavaInstallerProcessName);
|
|
715 |
|
|
716 |
// Open the DRM protected jar file.
|
|
717 |
getFileAndPort(port, file);
|
|
718 |
res = file->Open(fs, KTestDrmEncryptedInstallerFileName, EFileRead);
|
|
719 |
CHECKWITHNOTE(res == KErrNone, "Failed to open file: "<<res);
|
|
720 |
|
|
721 |
// Read the content and check that it was in decrypted format.
|
|
722 |
TBuf8<45> bufR;
|
|
723 |
res = file->Read(bufR);
|
|
724 |
CHECKWITHNOTE(res == KErrNone, "Read failed: "<<res);
|
|
725 |
res = bufR.Compare(KDrmText);
|
|
726 |
CHECKWITHNOTE(res == 0, "Compare failed: "<<res);
|
|
727 |
|
|
728 |
file->Close();
|
|
729 |
|
|
730 |
// Testing that opening of jar file that is not DRM protected succeeds.
|
|
731 |
res = file->Open(fs, KTestJarFileName, EFileRead);
|
|
732 |
CHECKWITHNOTE(res == KErrNone, "Failed to open file2: "<<res);
|
|
733 |
|
|
734 |
// Read the content and compare.
|
|
735 |
bufR.Zero();
|
|
736 |
res = file->Read(bufR);
|
|
737 |
CHECKWITHNOTE(res == KErrNone, "Read failed2: "<<res);
|
|
738 |
res = bufR.Compare(KJarText);
|
|
739 |
CHECKWITHNOTE(res == 0, "Compare failed2: "<<res);
|
|
740 |
file->Close();
|
|
741 |
|
|
742 |
// Testing that opening nonexisting file leads to error.
|
|
743 |
res = file->Open(fs, _L("c:\\nonexisting.jar"), EFileRead);
|
|
744 |
CHECKWITHNOTE(res == KErrNotFound, "Open, unexpected result: "<<res);
|
|
745 |
file->Close();
|
|
746 |
|
|
747 |
// Rename the process name to original.
|
|
748 |
User::RenameProcess(KOrigProcessName);
|
|
749 |
}
|