VirtualBox

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

最後變更 在這個檔案從86424是 86424,由 vboxsync 提交於 4 年 前

xpcom/nsIFileTest: Leak fix. bugref:9841

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 11.3 KB
 
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 NS_RELEASE(file);
118
119 if (exists)
120 printf("Yup!\n");
121 else
122 printf("no.\n");
123}
124
125
126void CreationTest(const char* creationPath, const char* appendPath,
127 PRInt32 whatToCreate, PRInt32 perm)
128{
129 nsresult rv;
130 nsCOMPtr<nsILocalFile> file =
131 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
132
133 if (NS_FAILED(rv) || (!file))
134 {
135 printf("create nsILocalFile failed\n");
136 return;
137 }
138
139 Banner("Creation Test");
140 printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
141
142 rv = file->InitWithNativePath(nsDependentCString(creationPath));
143 VerifyResult(rv);
144
145 printf("Appending %s\n", appendPath);
146 rv = file->AppendRelativeNativePath(nsDependentCString(appendPath));
147 VerifyResult(rv);
148
149 printf("Check For Existence\n");
150
151 PRBool exists;
152 file->Exists(&exists);
153
154 if (exists)
155 printf("Yup!\n");
156 else
157 printf("no.\n");
158
159
160 rv = file->Create(whatToCreate, perm);
161 VerifyResult(rv);
162
163 rv = file->Exists(&exists);
164 VerifyResult(rv);
165
166
167 if (!exists)
168 {
169 Failed("Did not create file system object!");
170 return;
171 }
172
173}
174
175void CreateUniqueTest(const char* creationPath, const char* appendPath,
176 PRInt32 whatToCreate, PRInt32 perm)
177{
178 nsresult rv;
179 nsCOMPtr<nsILocalFile> file =
180 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
181
182 if (NS_FAILED(rv) || (!file))
183 {
184 printf("create nsILocalFile failed\n");
185 return;
186 }
187
188 Banner("Creation Test");
189 printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
190
191 rv = file->InitWithNativePath(nsDependentCString(creationPath));
192 VerifyResult(rv);
193
194 printf("Appending %s\n", appendPath);
195 rv = file->AppendNative(nsDependentCString(appendPath));
196 VerifyResult(rv);
197
198 printf("Check For Existence\n");
199
200 PRBool exists;
201 file->Exists(&exists);
202
203 if (exists)
204 printf("Yup!\n");
205 else
206 printf("no.\n");
207
208
209 rv = file->CreateUnique(whatToCreate, perm);
210 VerifyResult(rv);
211
212 rv = file->Exists(&exists);
213 VerifyResult(rv);
214
215
216 if (!exists)
217 {
218 Failed("Did not create file system object!");
219 return;
220 }
221
222}
223
224
225void
226CopyTest(const char *testFile, const char *targetDir)
227{
228 printf("start copy test\n");
229
230 nsresult rv;
231 nsCOMPtr<nsILocalFile> file =
232 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
233
234 if (NS_FAILED(rv) || (!file))
235 {
236 printf("create nsILocalFile failed\n");
237 return;
238 }
239
240 rv = file->InitWithNativePath(nsDependentCString(testFile));
241 VerifyResult(rv);
242
243 nsCOMPtr<nsILocalFile> dir =
244 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
245
246 if (NS_FAILED(rv) || (!dir))
247 {
248 printf("create nsILocalFile failed\n");
249 return;
250 }
251
252 rv = dir->InitWithNativePath(nsDependentCString(targetDir));
253 VerifyResult(rv);
254
255 rv = file->CopyTo(dir, EmptyString());
256 VerifyResult(rv);
257
258 printf("end copy test\n");
259}
260
261void
262DeletionTest(const char* creationPath, const char* appendPath, PRBool recursive)
263{
264 nsresult rv;
265 nsCOMPtr<nsILocalFile> file =
266 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
267
268 if (NS_FAILED(rv) || (!file))
269 {
270 printf("create nsILocalFile failed\n");
271 return;
272 }
273
274 Banner("Deletion Test");
275 printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
276
277 rv = file->InitWithNativePath(nsDependentCString(creationPath));
278 VerifyResult(rv);
279
280 printf("Appending %s\n", appendPath);
281 rv = file->AppendNative(nsDependentCString(appendPath));
282 VerifyResult(rv);
283
284 printf("Check For Existance\n");
285
286 PRBool exists;
287 file->Exists(&exists);
288
289 if (exists)
290 printf("Yup!\n");
291 else
292 printf("no.\n");
293
294 rv = file->Remove(recursive);
295 VerifyResult(rv);
296
297 rv = file->Exists(&exists);
298 VerifyResult(rv);
299
300 if (exists)
301 {
302 Failed("Did not create delete system object!");
303 return;
304 }
305
306}
307
308void
309MoveTest(const char *testFile, const char *targetDir)
310{
311 Banner("Move Test");
312
313 printf("start move test\n");
314
315 nsresult rv;
316 nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
317
318 if (!file)
319 {
320 printf("create nsILocalFile failed\n");
321 return;
322 }
323
324 rv = file->InitWithNativePath(nsDependentCString(testFile));
325 VerifyResult(rv);
326
327 nsCOMPtr<nsILocalFile> dir(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
328
329 if (!dir)
330 {
331 printf("create nsILocalFile failed\n");
332 return;
333 }
334
335 rv = dir->InitWithNativePath(nsDependentCString(targetDir));
336 VerifyResult(rv);
337
338 rv = file->MoveToNative(dir, NS_LITERAL_CSTRING("newtemp"));
339 VerifyResult(rv);
340 if (NS_FAILED(rv))
341 {
342 printf("MoveToNative() test Failed.\n");
343 }
344 printf("end move test\n");
345}
346
347// move up the number of directories in moveUpCount, then append "foo/bar"
348void
349NormalizeTest(const char *testPath, int moveUpCount,
350 const char *expected)
351{
352 Banner("Normalize Test");
353
354 nsresult rv;
355 nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
356
357 if (!file)
358 {
359 printf("create nsILocalFile failed\n");
360 return;
361 }
362
363 rv = file->InitWithNativePath(nsDependentCString(testPath));
364 VerifyResult(rv);
365
366 nsCOMPtr<nsIFile> parent;
367 nsAutoString path;
368 for (int i=0; i < moveUpCount; i++)
369 {
370 rv = file->GetParent(getter_AddRefs(parent));
371 VerifyResult(rv);
372 rv = parent->GetPath(path);
373 VerifyResult(rv);
374 rv = file->InitWithPath(path);
375 VerifyResult(rv);
376 }
377
378 if (!parent) {
379 printf("Getting parent failed!\n");
380 return;
381 }
382
383 rv = parent->Append(NS_LITERAL_STRING("foo"));
384 VerifyResult(rv);
385 rv = parent->Append(NS_LITERAL_STRING("bar"));
386 VerifyResult(rv);
387
388 rv = parent->Normalize();
389 VerifyResult(rv);
390
391 nsCAutoString newPath;
392 rv = parent->GetNativePath(newPath);
393 VerifyResult(rv);
394
395 nsCOMPtr<nsILocalFile>
396 expectedFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
397
398 if (!expectedFile)
399 {
400 printf("create nsILocalFile failed\n");
401 return;
402 }
403 rv = expectedFile->InitWithNativePath(nsDependentCString(expected));
404 VerifyResult(rv);
405
406 rv = expectedFile->Normalize();
407 VerifyResult(rv);
408
409 nsCAutoString expectedPath;
410 rv = expectedFile->GetNativePath(expectedPath);
411 VerifyResult(rv);
412
413 if (!newPath.Equals(expectedPath)) {
414 printf("ERROR: Normalize() test Failed!\n");
415 printf(" Got: %s\n", newPath.get());
416 printf("Expected: %s\n", expectedPath.get());
417 }
418
419 printf("end normalize test.\n");
420}
421
422int main(void)
423{
424 nsCOMPtr<nsIServiceManager> servMan;
425 NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
426 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
427 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
428 registrar->AutoRegister(nsnull);
429
430#if defined(XP_WIN) || defined(XP_OS2)
431 InitTest("c:\\temp\\", "sub1/sub2/"); // expect failure
432 InitTest("d:\\temp\\", "sub1\\sub2\\"); // expect failure
433
434 CreationTest("c:\\temp\\", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
435 DeletionTest("c:\\temp\\", "file.txt", PR_FALSE);
436
437 MoveTest("c:\\newtemp\\", "d:");
438
439 CreationTest("c:\\temp\\", "mumble\\a\\b\\c\\d\\e\\f\\g\\h\\i\\j\\k\\", nsIFile::DIRECTORY_TYPE, 0644);
440 DeletionTest("c:\\temp\\", "mumble", PR_TRUE);
441
442 CreateUniqueTest("c:\\temp\\", "foo", nsIFile::NORMAL_FILE_TYPE, 0644);
443 CreateUniqueTest("c:\\temp\\", "foo", nsIFile::NORMAL_FILE_TYPE, 0644);
444 CreateUniqueTest("c:\\temp\\", "bar.xx", nsIFile::DIRECTORY_TYPE, 0644);
445 CreateUniqueTest("c:\\temp\\", "bar.xx", nsIFile::DIRECTORY_TYPE, 0644);
446 DeletionTest("c:\\temp\\", "foo", PR_TRUE);
447 DeletionTest("c:\\temp\\", "foo-1", PR_TRUE);
448 DeletionTest("c:\\temp\\", "bar.xx", PR_TRUE);
449 DeletionTest("c:\\temp\\", "bar-1.xx", PR_TRUE);
450
451#else
452#ifdef XP_UNIX
453 InitTest("/tmp/", "sub1/sub2/"); // expect failure
454
455 CreationTest("/tmp", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
456 DeletionTest("/tmp/", "file.txt", PR_FALSE);
457
458 CreationTest("/tmp", "mumble/a/b/c/d/e/f/g/h/i/j/k/", nsIFile::DIRECTORY_TYPE, 0644);
459 DeletionTest("/tmp", "mumble", PR_TRUE);
460
461 CreationTest("/tmp", "file", nsIFile::NORMAL_FILE_TYPE, 0644);
462 CopyTest("/tmp/file", "/tmp/newDir");
463 MoveTest("/tmp/file", "/tmp/newDir/anotherNewDir");
464 DeletionTest("/tmp", "newDir", PR_TRUE);
465
466 CreationTest("/tmp", "qux/quux", nsIFile::NORMAL_FILE_TYPE, 0644);
467 CreationTest("/tmp", "foo/bar", nsIFile::NORMAL_FILE_TYPE, 0644);
468 NormalizeTest("/tmp/qux/quux/..", 1, "/tmp/foo/bar");
469 DeletionTest("/tmp", "qux", PR_TRUE);
470 DeletionTest("/tmp", "foo", PR_TRUE);
471
472#endif /* XP_UNIX */
473#endif /* XP_WIN || XP_OS2 */
474 return 0;
475}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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