VirtualBox

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

Last change on this file since 4837 was 725, checked in by vboxsync, 17 years ago

gcc generates ud2a if a non POD object is attempted passed to printf. added missing .get().

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

© 2023 Oracle
ContactPrivacy policyTerms of Use