VirtualBox

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

最後變更 在這個檔案從103140是 103140,由 vboxsync 提交於 14 月 前

libs/xpcom: Some warning fixes about externally visible functions which should be static, bugref:3409

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 11.4 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
14#include "nsComponentManagerUtils.h"
15#include "nsCOMPtr.h"
16
17static void Passed();
18static void Failed(const char* explanation = nsnull);
19static void Inspect();
20static void Banner(const char* bannerString);
21
22static void VerifyResult(nsresult rv)
23{
24 if (NS_FAILED(rv))
25 {
26 Failed("rv failed");
27 printf("rv = %d\n", rv);
28 }
29}
30//----------------------------------------------------------------------------
31static void Banner(const char* bannerString)
32//----------------------------------------------------------------------------
33{
34 printf("---------------------------\n");
35 printf("%s\n", bannerString);
36 printf("---------------------------\n");
37}
38
39//----------------------------------------------------------------------------
40static void Passed()
41//----------------------------------------------------------------------------
42{
43 printf("Test passed.");
44}
45
46//----------------------------------------------------------------------------
47static void Failed(const char* explanation)
48//----------------------------------------------------------------------------
49{
50 printf("ERROR : Test failed.\n");
51 printf("REASON: %s.\n", explanation);
52}
53
54//----------------------------------------------------------------------------
55static void Inspect()
56//----------------------------------------------------------------------------
57{
58 printf("^^^^^^^^^^ PLEASE INSPECT OUTPUT FOR ERRORS\n");
59}
60
61static void 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
74static void 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
125static void 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
174static void 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
224static void CopyTest(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
259static void DeletionTest(const char* creationPath, const char* appendPath, PRBool recursive)
260{
261 nsresult rv;
262 nsCOMPtr<nsILocalFile> file =
263 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
264
265 if (NS_FAILED(rv) || (!file))
266 {
267 printf("create nsILocalFile failed\n");
268 return;
269 }
270
271 Banner("Deletion Test");
272 printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
273
274 rv = file->InitWithNativePath(nsDependentCString(creationPath));
275 VerifyResult(rv);
276
277 printf("Appending %s\n", appendPath);
278 rv = file->AppendNative(nsDependentCString(appendPath));
279 VerifyResult(rv);
280
281 printf("Check For Existance\n");
282
283 PRBool exists;
284 file->Exists(&exists);
285
286 if (exists)
287 printf("Yup!\n");
288 else
289 printf("no.\n");
290
291 rv = file->Remove(recursive);
292 VerifyResult(rv);
293
294 rv = file->Exists(&exists);
295 VerifyResult(rv);
296
297 if (exists)
298 {
299 Failed("Did not create delete system object!");
300 return;
301 }
302
303}
304
305static void MoveTest(const char *testFile, const char *targetDir)
306{
307 Banner("Move Test");
308
309 printf("start move test\n");
310
311 nsresult rv;
312 nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
313
314 if (!file)
315 {
316 printf("create nsILocalFile failed\n");
317 return;
318 }
319
320 rv = file->InitWithNativePath(nsDependentCString(testFile));
321 VerifyResult(rv);
322
323 nsCOMPtr<nsILocalFile> dir(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
324
325 if (!dir)
326 {
327 printf("create nsILocalFile failed\n");
328 return;
329 }
330
331 rv = dir->InitWithNativePath(nsDependentCString(targetDir));
332 VerifyResult(rv);
333
334 rv = file->MoveToNative(dir, NS_LITERAL_CSTRING("newtemp"));
335 VerifyResult(rv);
336 if (NS_FAILED(rv))
337 {
338 printf("MoveToNative() test Failed.\n");
339 }
340 printf("end move test\n");
341}
342
343// move up the number of directories in moveUpCount, then append "foo/bar"
344static void NormalizeTest(const char *testPath, int moveUpCount,
345 const char *expected)
346{
347 Banner("Normalize Test");
348
349 nsresult rv;
350 nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
351
352 if (!file)
353 {
354 printf("create nsILocalFile failed\n");
355 return;
356 }
357
358 rv = file->InitWithNativePath(nsDependentCString(testPath));
359 VerifyResult(rv);
360
361 nsCOMPtr<nsIFile> parent;
362 nsAutoString path;
363 for (int i=0; i < moveUpCount; i++)
364 {
365 rv = file->GetParent(getter_AddRefs(parent));
366 VerifyResult(rv);
367 rv = parent->GetPath(path);
368 VerifyResult(rv);
369 rv = file->InitWithPath(path);
370 VerifyResult(rv);
371 }
372
373 if (!parent) {
374 printf("Getting parent failed!\n");
375 return;
376 }
377
378 rv = parent->Append(NS_LITERAL_STRING("foo"));
379 VerifyResult(rv);
380 rv = parent->Append(NS_LITERAL_STRING("bar"));
381 VerifyResult(rv);
382
383 rv = parent->Normalize();
384 VerifyResult(rv);
385
386 nsCAutoString newPath;
387 rv = parent->GetNativePath(newPath);
388 VerifyResult(rv);
389
390 nsCOMPtr<nsILocalFile>
391 expectedFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
392
393 if (!expectedFile)
394 {
395 printf("create nsILocalFile failed\n");
396 return;
397 }
398 rv = expectedFile->InitWithNativePath(nsDependentCString(expected));
399 VerifyResult(rv);
400
401 rv = expectedFile->Normalize();
402 VerifyResult(rv);
403
404 nsCAutoString expectedPath;
405 rv = expectedFile->GetNativePath(expectedPath);
406 VerifyResult(rv);
407
408 if (!newPath.Equals(expectedPath)) {
409 printf("ERROR: Normalize() test Failed!\n");
410 printf(" Got: %s\n", newPath.get());
411 printf("Expected: %s\n", expectedPath.get());
412 }
413
414 printf("end normalize test.\n");
415}
416
417int main(void)
418{
419 nsCOMPtr<nsIServiceManager> servMan;
420 NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
421 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
422 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
423 registrar->AutoRegister(nsnull);
424
425#if defined(XP_WIN) || defined(XP_OS2)
426 InitTest("c:\\temp\\", "sub1/sub2/"); // expect failure
427 InitTest("d:\\temp\\", "sub1\\sub2\\"); // expect failure
428
429 CreationTest("c:\\temp\\", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
430 DeletionTest("c:\\temp\\", "file.txt", PR_FALSE);
431
432 MoveTest("c:\\newtemp\\", "d:");
433
434 CreationTest("c:\\temp\\", "mumble\\a\\b\\c\\d\\e\\f\\g\\h\\i\\j\\k\\", nsIFile::DIRECTORY_TYPE, 0644);
435 DeletionTest("c:\\temp\\", "mumble", PR_TRUE);
436
437 CreateUniqueTest("c:\\temp\\", "foo", nsIFile::NORMAL_FILE_TYPE, 0644);
438 CreateUniqueTest("c:\\temp\\", "foo", nsIFile::NORMAL_FILE_TYPE, 0644);
439 CreateUniqueTest("c:\\temp\\", "bar.xx", nsIFile::DIRECTORY_TYPE, 0644);
440 CreateUniqueTest("c:\\temp\\", "bar.xx", nsIFile::DIRECTORY_TYPE, 0644);
441 DeletionTest("c:\\temp\\", "foo", PR_TRUE);
442 DeletionTest("c:\\temp\\", "foo-1", PR_TRUE);
443 DeletionTest("c:\\temp\\", "bar.xx", PR_TRUE);
444 DeletionTest("c:\\temp\\", "bar-1.xx", PR_TRUE);
445
446#else
447#ifdef XP_UNIX
448 InitTest("/tmp/", "sub1/sub2/"); // expect failure
449
450 CreationTest("/tmp", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
451 DeletionTest("/tmp/", "file.txt", PR_FALSE);
452
453 CreationTest("/tmp", "mumble/a/b/c/d/e/f/g/h/i/j/k/", nsIFile::DIRECTORY_TYPE, 0644);
454 DeletionTest("/tmp", "mumble", PR_TRUE);
455
456 CreationTest("/tmp", "file", nsIFile::NORMAL_FILE_TYPE, 0644);
457 CopyTest("/tmp/file", "/tmp/newDir");
458 MoveTest("/tmp/file", "/tmp/newDir/anotherNewDir");
459 DeletionTest("/tmp", "newDir", PR_TRUE);
460
461 CreationTest("/tmp", "qux/quux", nsIFile::NORMAL_FILE_TYPE, 0644);
462 CreationTest("/tmp", "foo/bar", nsIFile::NORMAL_FILE_TYPE, 0644);
463 NormalizeTest("/tmp/qux/quux/..", 1, "/tmp/foo/bar");
464 DeletionTest("/tmp", "qux", PR_TRUE);
465 DeletionTest("/tmp", "foo", PR_TRUE);
466
467#endif /* XP_UNIX */
468#endif /* XP_WIN || XP_OS2 */
469 return 0;
470}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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