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