VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/tests/nsIFileTest.cpp@ 101856

Last change on this file since 101856 was 101856, checked in by vboxsync, 14 months ago

libs/xpcom: Get rid of nsMemoryImpl.{cpp,h} and nsIMemory and repalce it with direct calls to RTMem* APIs, bugref:10545 [fix]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 KB
Line 
1#include "nsILocalFile.h"
2#if 0 /* too new */
3#include "nsStringGlue.h"
4#else
5#include "nsString.h"
6#endif
7
8#include <stdio.h>
9#include "nsXPCOM.h"
10#include "nsIComponentManager.h"
11#include "nsIComponentRegistrar.h"
12#include "nsIServiceManager.h"
13
14#include "nsComponentManagerUtils.h"
15#include "nsCOMPtr.h"
16
17void Passed();
18void Failed(const char* explanation = nsnull);
19void Inspect();
20void Banner(const char* bannerString);
21
22void VerifyResult(nsresult rv)
23{
24 if (NS_FAILED(rv))
25 {
26 Failed("rv failed");
27 printf("rv = %d\n", rv);
28 }
29}
30//----------------------------------------------------------------------------
31void Banner(const char* bannerString)
32//----------------------------------------------------------------------------
33{
34 printf("---------------------------\n");
35 printf("%s\n", bannerString);
36 printf("---------------------------\n");
37}
38
39//----------------------------------------------------------------------------
40void Passed()
41//----------------------------------------------------------------------------
42{
43 printf("Test passed.");
44}
45
46//----------------------------------------------------------------------------
47void Failed(const char* explanation)
48//----------------------------------------------------------------------------
49{
50 printf("ERROR : Test failed.\n");
51 printf("REASON: %s.\n", explanation);
52}
53
54//----------------------------------------------------------------------------
55void Inspect()
56//----------------------------------------------------------------------------
57{
58 printf("^^^^^^^^^^ PLEASE INSPECT OUTPUT FOR ERRORS\n");
59}
60
61void GetPaths(nsILocalFile* file)
62{
63 nsresult rv;
64 nsCAutoString pathName;
65
66 printf("Getting Path\n");
67
68 rv = file->GetNativePath(pathName);
69 VerifyResult(rv);
70
71 printf("filepath: %s\n", pathName.get());
72}
73
74void InitTest(const char* creationPath, const char* appendPath)
75{
76 nsILocalFile* file = nsnull;
77 nsresult rv = CallCreateInstance(NS_LOCAL_FILE_CONTRACTID, &file);
78
79
80 if (NS_FAILED(rv) || (!file))
81 {
82 printf("create nsILocalFile failed\n");
83 return;
84 }
85
86 nsCAutoString leafName;
87
88 Banner("InitWithPath");
89 printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
90
91 rv = file->InitWithNativePath(nsDependentCString(creationPath));
92 VerifyResult(rv);
93
94 printf("Getting Filename\n");
95 rv = file->GetNativeLeafName(leafName);
96 printf(" %s\n", leafName.get());
97 VerifyResult(rv);
98
99 printf("Appending %s \n", appendPath);
100 rv = file->AppendNative(nsDependentCString(appendPath));
101 VerifyResult(rv);
102
103 printf("Getting Filename\n");
104 rv = file->GetNativeLeafName(leafName);
105 printf(" %s\n", leafName.get());
106 VerifyResult(rv);
107
108 GetPaths(file);
109
110
111 printf("Check For Existence\n");
112
113 PRBool exists;
114 file->Exists(&exists);
115
116 NS_RELEASE(file);
117
118 if (exists)
119 printf("Yup!\n");
120 else
121 printf("no.\n");
122}
123
124
125void CreationTest(const char* creationPath, const char* appendPath,
126 PRInt32 whatToCreate, PRInt32 perm)
127{
128 nsresult rv;
129 nsCOMPtr<nsILocalFile> file =
130 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
131
132 if (NS_FAILED(rv) || (!file))
133 {
134 printf("create nsILocalFile failed\n");
135 return;
136 }
137
138 Banner("Creation Test");
139 printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
140
141 rv = file->InitWithNativePath(nsDependentCString(creationPath));
142 VerifyResult(rv);
143
144 printf("Appending %s\n", appendPath);
145 rv = file->AppendRelativeNativePath(nsDependentCString(appendPath));
146 VerifyResult(rv);
147
148 printf("Check For Existence\n");
149
150 PRBool exists;
151 file->Exists(&exists);
152
153 if (exists)
154 printf("Yup!\n");
155 else
156 printf("no.\n");
157
158
159 rv = file->Create(whatToCreate, perm);
160 VerifyResult(rv);
161
162 rv = file->Exists(&exists);
163 VerifyResult(rv);
164
165
166 if (!exists)
167 {
168 Failed("Did not create file system object!");
169 return;
170 }
171
172}
173
174void CreateUniqueTest(const char* creationPath, const char* appendPath,
175 PRInt32 whatToCreate, PRInt32 perm)
176{
177 nsresult rv;
178 nsCOMPtr<nsILocalFile> file =
179 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
180
181 if (NS_FAILED(rv) || (!file))
182 {
183 printf("create nsILocalFile failed\n");
184 return;
185 }
186
187 Banner("Creation Test");
188 printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
189
190 rv = file->InitWithNativePath(nsDependentCString(creationPath));
191 VerifyResult(rv);
192
193 printf("Appending %s\n", appendPath);
194 rv = file->AppendNative(nsDependentCString(appendPath));
195 VerifyResult(rv);
196
197 printf("Check For Existence\n");
198
199 PRBool exists;
200 file->Exists(&exists);
201
202 if (exists)
203 printf("Yup!\n");
204 else
205 printf("no.\n");
206
207
208 rv = file->CreateUnique(whatToCreate, perm);
209 VerifyResult(rv);
210
211 rv = file->Exists(&exists);
212 VerifyResult(rv);
213
214
215 if (!exists)
216 {
217 Failed("Did not create file system object!");
218 return;
219 }
220
221}
222
223
224void
225CopyTest(const char *testFile, const char *targetDir)
226{
227 printf("start copy test\n");
228
229 nsresult rv;
230 nsCOMPtr<nsILocalFile> file =
231 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
232
233 if (NS_FAILED(rv) || (!file))
234 {
235 printf("create nsILocalFile failed\n");
236 return;
237 }
238
239 rv = file->InitWithNativePath(nsDependentCString(testFile));
240 VerifyResult(rv);
241
242 nsCOMPtr<nsILocalFile> dir =
243 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
244
245 if (NS_FAILED(rv) || (!dir))
246 {
247 printf("create nsILocalFile failed\n");
248 return;
249 }
250
251 rv = dir->InitWithNativePath(nsDependentCString(targetDir));
252 VerifyResult(rv);
253
254 rv = file->CopyTo(dir, EmptyString());
255 VerifyResult(rv);
256
257 printf("end copy test\n");
258}
259
260void
261DeletionTest(const char* creationPath, const char* appendPath, PRBool recursive)
262{
263 nsresult rv;
264 nsCOMPtr<nsILocalFile> file =
265 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
266
267 if (NS_FAILED(rv) || (!file))
268 {
269 printf("create nsILocalFile failed\n");
270 return;
271 }
272
273 Banner("Deletion Test");
274 printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
275
276 rv = file->InitWithNativePath(nsDependentCString(creationPath));
277 VerifyResult(rv);
278
279 printf("Appending %s\n", appendPath);
280 rv = file->AppendNative(nsDependentCString(appendPath));
281 VerifyResult(rv);
282
283 printf("Check For Existance\n");
284
285 PRBool exists;
286 file->Exists(&exists);
287
288 if (exists)
289 printf("Yup!\n");
290 else
291 printf("no.\n");
292
293 rv = file->Remove(recursive);
294 VerifyResult(rv);
295
296 rv = file->Exists(&exists);
297 VerifyResult(rv);
298
299 if (exists)
300 {
301 Failed("Did not create delete system object!");
302 return;
303 }
304
305}
306
307void
308MoveTest(const char *testFile, const char *targetDir)
309{
310 Banner("Move Test");
311
312 printf("start move test\n");
313
314 nsresult rv;
315 nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
316
317 if (!file)
318 {
319 printf("create nsILocalFile failed\n");
320 return;
321 }
322
323 rv = file->InitWithNativePath(nsDependentCString(testFile));
324 VerifyResult(rv);
325
326 nsCOMPtr<nsILocalFile> dir(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
327
328 if (!dir)
329 {
330 printf("create nsILocalFile failed\n");
331 return;
332 }
333
334 rv = dir->InitWithNativePath(nsDependentCString(targetDir));
335 VerifyResult(rv);
336
337 rv = file->MoveToNative(dir, NS_LITERAL_CSTRING("newtemp"));
338 VerifyResult(rv);
339 if (NS_FAILED(rv))
340 {
341 printf("MoveToNative() test Failed.\n");
342 }
343 printf("end move test\n");
344}
345
346// move up the number of directories in moveUpCount, then append "foo/bar"
347void
348NormalizeTest(const char *testPath, int moveUpCount,
349 const char *expected)
350{
351 Banner("Normalize Test");
352
353 nsresult rv;
354 nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
355
356 if (!file)
357 {
358 printf("create nsILocalFile failed\n");
359 return;
360 }
361
362 rv = file->InitWithNativePath(nsDependentCString(testPath));
363 VerifyResult(rv);
364
365 nsCOMPtr<nsIFile> parent;
366 nsAutoString path;
367 for (int i=0; i < moveUpCount; i++)
368 {
369 rv = file->GetParent(getter_AddRefs(parent));
370 VerifyResult(rv);
371 rv = parent->GetPath(path);
372 VerifyResult(rv);
373 rv = file->InitWithPath(path);
374 VerifyResult(rv);
375 }
376
377 if (!parent) {
378 printf("Getting parent failed!\n");
379 return;
380 }
381
382 rv = parent->Append(NS_LITERAL_STRING("foo"));
383 VerifyResult(rv);
384 rv = parent->Append(NS_LITERAL_STRING("bar"));
385 VerifyResult(rv);
386
387 rv = parent->Normalize();
388 VerifyResult(rv);
389
390 nsCAutoString newPath;
391 rv = parent->GetNativePath(newPath);
392 VerifyResult(rv);
393
394 nsCOMPtr<nsILocalFile>
395 expectedFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
396
397 if (!expectedFile)
398 {
399 printf("create nsILocalFile failed\n");
400 return;
401 }
402 rv = expectedFile->InitWithNativePath(nsDependentCString(expected));
403 VerifyResult(rv);
404
405 rv = expectedFile->Normalize();
406 VerifyResult(rv);
407
408 nsCAutoString expectedPath;
409 rv = expectedFile->GetNativePath(expectedPath);
410 VerifyResult(rv);
411
412 if (!newPath.Equals(expectedPath)) {
413 printf("ERROR: Normalize() test Failed!\n");
414 printf(" Got: %s\n", newPath.get());
415 printf("Expected: %s\n", expectedPath.get());
416 }
417
418 printf("end normalize test.\n");
419}
420
421int main(void)
422{
423 nsCOMPtr<nsIServiceManager> servMan;
424 NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
425 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
426 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
427 registrar->AutoRegister(nsnull);
428
429#if defined(XP_WIN) || defined(XP_OS2)
430 InitTest("c:\\temp\\", "sub1/sub2/"); // expect failure
431 InitTest("d:\\temp\\", "sub1\\sub2\\"); // expect failure
432
433 CreationTest("c:\\temp\\", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
434 DeletionTest("c:\\temp\\", "file.txt", PR_FALSE);
435
436 MoveTest("c:\\newtemp\\", "d:");
437
438 CreationTest("c:\\temp\\", "mumble\\a\\b\\c\\d\\e\\f\\g\\h\\i\\j\\k\\", nsIFile::DIRECTORY_TYPE, 0644);
439 DeletionTest("c:\\temp\\", "mumble", PR_TRUE);
440
441 CreateUniqueTest("c:\\temp\\", "foo", nsIFile::NORMAL_FILE_TYPE, 0644);
442 CreateUniqueTest("c:\\temp\\", "foo", nsIFile::NORMAL_FILE_TYPE, 0644);
443 CreateUniqueTest("c:\\temp\\", "bar.xx", nsIFile::DIRECTORY_TYPE, 0644);
444 CreateUniqueTest("c:\\temp\\", "bar.xx", nsIFile::DIRECTORY_TYPE, 0644);
445 DeletionTest("c:\\temp\\", "foo", PR_TRUE);
446 DeletionTest("c:\\temp\\", "foo-1", PR_TRUE);
447 DeletionTest("c:\\temp\\", "bar.xx", PR_TRUE);
448 DeletionTest("c:\\temp\\", "bar-1.xx", PR_TRUE);
449
450#else
451#ifdef XP_UNIX
452 InitTest("/tmp/", "sub1/sub2/"); // expect failure
453
454 CreationTest("/tmp", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
455 DeletionTest("/tmp/", "file.txt", PR_FALSE);
456
457 CreationTest("/tmp", "mumble/a/b/c/d/e/f/g/h/i/j/k/", nsIFile::DIRECTORY_TYPE, 0644);
458 DeletionTest("/tmp", "mumble", PR_TRUE);
459
460 CreationTest("/tmp", "file", nsIFile::NORMAL_FILE_TYPE, 0644);
461 CopyTest("/tmp/file", "/tmp/newDir");
462 MoveTest("/tmp/file", "/tmp/newDir/anotherNewDir");
463 DeletionTest("/tmp", "newDir", PR_TRUE);
464
465 CreationTest("/tmp", "qux/quux", nsIFile::NORMAL_FILE_TYPE, 0644);
466 CreationTest("/tmp", "foo/bar", nsIFile::NORMAL_FILE_TYPE, 0644);
467 NormalizeTest("/tmp/qux/quux/..", 1, "/tmp/foo/bar");
468 DeletionTest("/tmp", "qux", PR_TRUE);
469 DeletionTest("/tmp", "foo", PR_TRUE);
470
471#endif /* XP_UNIX */
472#endif /* XP_WIN || XP_OS2 */
473 return 0;
474}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette