1 | #include "nsILocalFile.h"
|
---|
2 | #include "nsString.h"
|
---|
3 |
|
---|
4 | #include <stdio.h>
|
---|
5 | #include "nsXPCOM.h"
|
---|
6 | #include "nsIComponentManager.h"
|
---|
7 | #include "nsIComponentRegistrar.h"
|
---|
8 | #include "nsIServiceManager.h"
|
---|
9 | #include "nsIMemory.h"
|
---|
10 | #include "nsXPIDLString.h"
|
---|
11 |
|
---|
12 | void Passed();
|
---|
13 | void Failed(const char* explanation = nsnull);
|
---|
14 | void Inspect();
|
---|
15 | void Banner(const char* bannerString);
|
---|
16 |
|
---|
17 | void VerifyResult(nsresult rv)
|
---|
18 | {
|
---|
19 | if (NS_FAILED(rv))
|
---|
20 | {
|
---|
21 | Failed("rv failed");
|
---|
22 | printf("rv = %d\n", rv);
|
---|
23 | }
|
---|
24 | }
|
---|
25 | //----------------------------------------------------------------------------
|
---|
26 | void Banner(const char* bannerString)
|
---|
27 | //----------------------------------------------------------------------------
|
---|
28 | {
|
---|
29 | printf("---------------------------\n");
|
---|
30 | printf("%s\n", bannerString);
|
---|
31 | printf("---------------------------\n");
|
---|
32 | }
|
---|
33 |
|
---|
34 | //----------------------------------------------------------------------------
|
---|
35 | void Passed()
|
---|
36 | //----------------------------------------------------------------------------
|
---|
37 | {
|
---|
38 | printf("Test passed.");
|
---|
39 | }
|
---|
40 |
|
---|
41 | //----------------------------------------------------------------------------
|
---|
42 | void Failed(const char* explanation)
|
---|
43 | //----------------------------------------------------------------------------
|
---|
44 | {
|
---|
45 | printf("ERROR : Test failed.\n");
|
---|
46 | printf("REASON: %s.\n", explanation);
|
---|
47 | }
|
---|
48 |
|
---|
49 | //----------------------------------------------------------------------------
|
---|
50 | void Inspect()
|
---|
51 | //----------------------------------------------------------------------------
|
---|
52 | {
|
---|
53 | printf("^^^^^^^^^^ PLEASE INSPECT OUTPUT FOR ERRORS\n");
|
---|
54 | }
|
---|
55 |
|
---|
56 | void GetPaths(nsILocalFile* file)
|
---|
57 | {
|
---|
58 | nsresult rv;
|
---|
59 | nsCAutoString pathName;
|
---|
60 |
|
---|
61 | printf("Getting Path\n");
|
---|
62 |
|
---|
63 | rv = file->GetNativePath(pathName);
|
---|
64 | VerifyResult(rv);
|
---|
65 |
|
---|
66 | printf("filepath: %s\n", pathName.get());
|
---|
67 | }
|
---|
68 |
|
---|
69 | void InitTest(const char* creationPath, const char* appendPath)
|
---|
70 | {
|
---|
71 | nsILocalFile* file = nsnull;
|
---|
72 | nsresult rv = nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID,
|
---|
73 | nsnull,
|
---|
74 | NS_GET_IID(nsILocalFile),
|
---|
75 | (void**)&file);
|
---|
76 |
|
---|
77 | if (NS_FAILED(rv) || (!file))
|
---|
78 | {
|
---|
79 | printf("create nsILocalFile failed\n");
|
---|
80 | return;
|
---|
81 | }
|
---|
82 |
|
---|
83 | nsCAutoString leafName;
|
---|
84 |
|
---|
85 | Banner("InitWithPath");
|
---|
86 | printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
|
---|
87 |
|
---|
88 | rv = file->InitWithNativePath(nsDependentCString(creationPath));
|
---|
89 | VerifyResult(rv);
|
---|
90 |
|
---|
91 | printf("Getting Filename\n");
|
---|
92 | rv = file->GetNativeLeafName(leafName);
|
---|
93 | printf(" %s\n", leafName.get());
|
---|
94 | VerifyResult(rv);
|
---|
95 |
|
---|
96 | printf("Appending %s \n", appendPath);
|
---|
97 | rv = file->AppendNative(nsDependentCString(appendPath));
|
---|
98 | VerifyResult(rv);
|
---|
99 |
|
---|
100 | printf("Getting Filename\n");
|
---|
101 | rv = file->GetNativeLeafName(leafName);
|
---|
102 | printf(" %s\n", leafName.get());
|
---|
103 | VerifyResult(rv);
|
---|
104 |
|
---|
105 | GetPaths(file);
|
---|
106 |
|
---|
107 |
|
---|
108 | printf("Check For Existence\n");
|
---|
109 |
|
---|
110 | PRBool exists;
|
---|
111 | file->Exists(&exists);
|
---|
112 |
|
---|
113 | if (exists)
|
---|
114 | printf("Yup!\n");
|
---|
115 | else
|
---|
116 | printf("no.\n");
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | void CreationTest(const char* creationPath, const char* appendPath,
|
---|
121 | PRInt32 whatToCreate, PRInt32 perm)
|
---|
122 | {
|
---|
123 | nsCOMPtr<nsILocalFile> file;
|
---|
124 | nsresult rv =
|
---|
125 | nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID,
|
---|
126 | nsnull,
|
---|
127 | NS_GET_IID(nsILocalFile),
|
---|
128 | (void **)getter_AddRefs(file));
|
---|
129 |
|
---|
130 | if (NS_FAILED(rv) || (!file))
|
---|
131 | {
|
---|
132 | printf("create nsILocalFile failed\n");
|
---|
133 | return;
|
---|
134 | }
|
---|
135 |
|
---|
136 | Banner("Creation Test");
|
---|
137 | printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
|
---|
138 |
|
---|
139 | rv = file->InitWithNativePath(nsDependentCString(creationPath));
|
---|
140 | VerifyResult(rv);
|
---|
141 |
|
---|
142 | printf("Appending %s\n", appendPath);
|
---|
143 | rv = file->AppendRelativeNativePath(nsDependentCString(appendPath));
|
---|
144 | VerifyResult(rv);
|
---|
145 |
|
---|
146 | printf("Check For Existence\n");
|
---|
147 |
|
---|
148 | PRBool exists;
|
---|
149 | file->Exists(&exists);
|
---|
150 |
|
---|
151 | if (exists)
|
---|
152 | printf("Yup!\n");
|
---|
153 | else
|
---|
154 | printf("no.\n");
|
---|
155 |
|
---|
156 |
|
---|
157 | rv = file->Create(whatToCreate, perm);
|
---|
158 | VerifyResult(rv);
|
---|
159 |
|
---|
160 | rv = file->Exists(&exists);
|
---|
161 | VerifyResult(rv);
|
---|
162 |
|
---|
163 |
|
---|
164 | if (!exists)
|
---|
165 | {
|
---|
166 | Failed("Did not create file system object!");
|
---|
167 | return;
|
---|
168 | }
|
---|
169 |
|
---|
170 | }
|
---|
171 |
|
---|
172 | void CreateUniqueTest(const char* creationPath, const char* appendPath,
|
---|
173 | PRInt32 whatToCreate, PRInt32 perm)
|
---|
174 | {
|
---|
175 | nsCOMPtr<nsILocalFile> file;
|
---|
176 | nsresult rv =
|
---|
177 | nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID,
|
---|
178 | nsnull,
|
---|
179 | NS_GET_IID(nsILocalFile),
|
---|
180 | (void **)getter_AddRefs(file));
|
---|
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 |
|
---|
225 | void
|
---|
226 | CopyTest(const char *testFile, const char *targetDir)
|
---|
227 | {
|
---|
228 | nsCOMPtr<nsILocalFile> file;
|
---|
229 | nsCOMPtr<nsILocalFile> dir;
|
---|
230 |
|
---|
231 | printf("start copy test\n");
|
---|
232 |
|
---|
233 | nsresult rv =
|
---|
234 | nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID, NULL,
|
---|
235 | NS_GET_IID(nsILocalFile),
|
---|
236 | (void**)getter_AddRefs(file));
|
---|
237 |
|
---|
238 | if (NS_FAILED(rv) || (!file))
|
---|
239 | {
|
---|
240 | printf("create nsILocalFile failed\n");
|
---|
241 | return;
|
---|
242 | }
|
---|
243 |
|
---|
244 | rv = file->InitWithNativePath(nsDependentCString(testFile));
|
---|
245 | VerifyResult(rv);
|
---|
246 |
|
---|
247 | rv = nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID, NULL,
|
---|
248 | NS_GET_IID(nsILocalFile),
|
---|
249 | (void**)getter_AddRefs(dir));
|
---|
250 |
|
---|
251 | if (NS_FAILED(rv) || (!dir))
|
---|
252 | {
|
---|
253 | printf("create nsILocalFile failed\n");
|
---|
254 | return;
|
---|
255 | }
|
---|
256 |
|
---|
257 | rv = dir->InitWithNativePath(nsDependentCString(targetDir));
|
---|
258 | VerifyResult(rv);
|
---|
259 |
|
---|
260 | rv = file->CopyTo(dir, EmptyString());
|
---|
261 | VerifyResult(rv);
|
---|
262 |
|
---|
263 | printf("end copy test\n");
|
---|
264 | }
|
---|
265 |
|
---|
266 | void
|
---|
267 | DeletionTest(const char* creationPath, const char* appendPath, PRBool recursive)
|
---|
268 | {
|
---|
269 | nsCOMPtr<nsILocalFile> file;
|
---|
270 | nsresult rv =
|
---|
271 | nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID, NULL,
|
---|
272 | NS_GET_IID(nsILocalFile),
|
---|
273 | (void**)getter_AddRefs(file));
|
---|
274 |
|
---|
275 | if (NS_FAILED(rv) || (!file))
|
---|
276 | {
|
---|
277 | printf("create nsILocalFile failed\n");
|
---|
278 | return;
|
---|
279 | }
|
---|
280 |
|
---|
281 | Banner("Deletion Test");
|
---|
282 | printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
|
---|
283 |
|
---|
284 | rv = file->InitWithNativePath(nsDependentCString(creationPath));
|
---|
285 | VerifyResult(rv);
|
---|
286 |
|
---|
287 | printf("Appending %s\n", appendPath);
|
---|
288 | rv = file->AppendNative(nsDependentCString(appendPath));
|
---|
289 | VerifyResult(rv);
|
---|
290 |
|
---|
291 | printf("Check For Existance\n");
|
---|
292 |
|
---|
293 | PRBool exists;
|
---|
294 | file->Exists(&exists);
|
---|
295 |
|
---|
296 | if (exists)
|
---|
297 | printf("Yup!\n");
|
---|
298 | else
|
---|
299 | printf("no.\n");
|
---|
300 |
|
---|
301 | rv = file->Remove(recursive);
|
---|
302 | VerifyResult(rv);
|
---|
303 |
|
---|
304 | rv = file->Exists(&exists);
|
---|
305 | VerifyResult(rv);
|
---|
306 |
|
---|
307 | if (exists)
|
---|
308 | {
|
---|
309 | Failed("Did not create delete system object!");
|
---|
310 | return;
|
---|
311 | }
|
---|
312 |
|
---|
313 | }
|
---|
314 |
|
---|
315 | void
|
---|
316 | MoveTest(const char *testFile, const char *targetDir)
|
---|
317 | {
|
---|
318 | Banner("Move Test");
|
---|
319 |
|
---|
320 | printf("start move test\n");
|
---|
321 |
|
---|
322 | nsresult rv;
|
---|
323 | nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
|
---|
324 |
|
---|
325 | if (!file)
|
---|
326 | {
|
---|
327 | printf("create nsILocalFile failed\n");
|
---|
328 | return;
|
---|
329 | }
|
---|
330 |
|
---|
331 | rv = file->InitWithNativePath(nsDependentCString(testFile));
|
---|
332 | VerifyResult(rv);
|
---|
333 |
|
---|
334 | nsCOMPtr<nsILocalFile> dir(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
|
---|
335 |
|
---|
336 | if (!dir)
|
---|
337 | {
|
---|
338 | printf("create nsILocalFile failed\n");
|
---|
339 | return;
|
---|
340 | }
|
---|
341 |
|
---|
342 | rv = dir->InitWithNativePath(nsDependentCString(targetDir));
|
---|
343 | VerifyResult(rv);
|
---|
344 |
|
---|
345 | rv = file->MoveToNative(dir, NS_LITERAL_CSTRING("newtemp"));
|
---|
346 | VerifyResult(rv);
|
---|
347 | if (NS_FAILED(rv))
|
---|
348 | {
|
---|
349 | printf("MoveToNative() test Failed.\n");
|
---|
350 | }
|
---|
351 | printf("end move test\n");
|
---|
352 | }
|
---|
353 |
|
---|
354 |
|
---|
355 |
|
---|
356 | int main(void)
|
---|
357 | {
|
---|
358 | nsCOMPtr<nsIServiceManager> servMan;
|
---|
359 | NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
|
---|
360 | nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
|
---|
361 | NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
|
---|
362 | registrar->AutoRegister(nsnull);
|
---|
363 |
|
---|
364 | #if defined(XP_WIN) || defined(XP_OS2)
|
---|
365 | InitTest("c:\\temp\\", "sub1/sub2/");
|
---|
366 | InitTest("d:\\temp\\", "sub1\\sub2\\");
|
---|
367 |
|
---|
368 | CreationTest("c:\\temp\\", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
|
---|
369 | DeletionTest("c:\\temp\\", "file.txt", PR_FALSE);
|
---|
370 |
|
---|
371 | MoveTest("c:\\newtemp\\", "d:");
|
---|
372 |
|
---|
373 | CreationTest("c:\\temp\\", "mumble\\a\\b\\c\\d\\e\\f\\g\\h\\i\\j\\k\\", nsIFile::DIRECTORY_TYPE, 0644);
|
---|
374 | DeletionTest("c:\\temp\\", "mumble", PR_TRUE);
|
---|
375 |
|
---|
376 | CreateUniqueTest("c:\\temp\\", "foo", nsIFile::NORMAL_FILE_TYPE, 0644);
|
---|
377 | CreateUniqueTest("c:\\temp\\", "foo", nsIFile::NORMAL_FILE_TYPE, 0644);
|
---|
378 | CreateUniqueTest("c:\\temp\\", "bar.xx", nsIFile::DIRECTORY_TYPE, 0644);
|
---|
379 | CreateUniqueTest("c:\\temp\\", "bar.xx", nsIFile::DIRECTORY_TYPE, 0644);
|
---|
380 | DeletionTest("c:\\temp\\", "foo", PR_TRUE);
|
---|
381 | DeletionTest("c:\\temp\\", "foo-1", PR_TRUE);
|
---|
382 | DeletionTest("c:\\temp\\", "bar.xx", PR_TRUE);
|
---|
383 | DeletionTest("c:\\temp\\", "bar-1.xx", PR_TRUE);
|
---|
384 |
|
---|
385 | #else
|
---|
386 | #ifdef XP_UNIX
|
---|
387 | InitTest("/tmp/", "sub1/sub2/");
|
---|
388 |
|
---|
389 | CreationTest("/tmp", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
|
---|
390 | DeletionTest("/tmp/", "file.txt", PR_FALSE);
|
---|
391 |
|
---|
392 | CreationTest("/tmp", "mumble/a/b/c/d/e/f/g/h/i/j/k/", nsIFile::DIRECTORY_TYPE, 0644);
|
---|
393 | DeletionTest("/tmp", "mumble", PR_TRUE);
|
---|
394 | CopyTest("/tmp/test.txt", "/tmp/foo");
|
---|
395 |
|
---|
396 | #endif /* XP_UNIX */
|
---|
397 | #endif /* XP_WIN || XP_OS2 */
|
---|
398 | return 0;
|
---|
399 | }
|
---|