Back to index

lightning-sunbird  0.9+nobinonly
nsIFileTest.cpp
Go to the documentation of this file.
00001 #include "nsILocalFile.h"
00002 #include "nsString.h"
00003 
00004 #include <stdio.h>
00005 #include "nsXPCOM.h"
00006 #include "nsIComponentManager.h"
00007 #include "nsIComponentRegistrar.h"
00008 #include "nsIServiceManager.h"
00009 #include "nsIMemory.h"
00010 #include "nsXPIDLString.h"
00011 
00012 void Passed();
00013 void Failed(const char* explanation = nsnull);
00014 void Inspect();
00015 void Banner(const char* bannerString);
00016 
00017 void VerifyResult(nsresult rv)
00018 {
00019     if (NS_FAILED(rv))
00020     {
00021         Failed("rv failed");
00022         printf("rv = %d\n", rv);
00023     }
00024 }
00025 //----------------------------------------------------------------------------
00026 void Banner(const char* bannerString)
00027 //----------------------------------------------------------------------------
00028 {
00029     printf("---------------------------\n");
00030     printf("%s\n", bannerString);
00031     printf("---------------------------\n");
00032 }
00033 
00034 //----------------------------------------------------------------------------
00035 void Passed()
00036 //----------------------------------------------------------------------------
00037 {
00038     printf("Test passed.");
00039 }
00040 
00041 //----------------------------------------------------------------------------
00042 void Failed(const char* explanation)
00043 //----------------------------------------------------------------------------
00044 {
00045     printf("ERROR : Test failed.\n");
00046     printf("REASON: %s.\n", explanation);
00047 }
00048 
00049 //----------------------------------------------------------------------------
00050 void Inspect()
00051 //----------------------------------------------------------------------------
00052 {
00053     printf("^^^^^^^^^^ PLEASE INSPECT OUTPUT FOR ERRORS\n");
00054 }
00055 
00056 void GetPaths(nsILocalFile* file)
00057 {
00058     nsresult rv;
00059     nsCAutoString pathName;
00060 
00061     printf("Getting Path\n");
00062 
00063     rv = file->GetNativePath(pathName);
00064     VerifyResult(rv);
00065     
00066     printf("filepath: %s\n", pathName.get());
00067 }
00068 
00069 void InitTest(const char* creationPath, const char* appendPath)
00070 {
00071     nsILocalFile* file = nsnull;
00072     nsresult rv = CallCreateInstance(NS_LOCAL_FILE_CONTRACTID, &file);
00073     
00074     if (NS_FAILED(rv) || (!file)) 
00075     {
00076         printf("create nsILocalFile failed\n");
00077         return;
00078     }
00079 
00080     nsCAutoString leafName;
00081 
00082     Banner("InitWithPath");
00083     printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
00084 
00085     rv = file->InitWithNativePath(nsDependentCString(creationPath));
00086     VerifyResult(rv);
00087     
00088     printf("Getting Filename\n");
00089     rv = file->GetNativeLeafName(leafName);
00090     printf(" %s\n", leafName.get());
00091     VerifyResult(rv);
00092 
00093     printf("Appending %s \n", appendPath);
00094     rv = file->AppendNative(nsDependentCString(appendPath));
00095     VerifyResult(rv);
00096 
00097     printf("Getting Filename\n");
00098     rv = file->GetNativeLeafName(leafName);
00099     printf(" %s\n", leafName.get());
00100     VerifyResult(rv);
00101 
00102     GetPaths(file);
00103 
00104     
00105     printf("Check For Existence\n");
00106 
00107     PRBool exists;
00108     file->Exists(&exists);
00109 
00110     if (exists)
00111         printf("Yup!\n");
00112     else
00113         printf("no.\n");
00114 }
00115 
00116 
00117 void CreationTest(const char* creationPath, const char* appendPath,
00118                 PRInt32 whatToCreate, PRInt32 perm)
00119 {
00120     nsresult rv;
00121     nsCOMPtr<nsILocalFile> file =
00122         do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
00123 
00124     if (NS_FAILED(rv) || (!file)) 
00125     {
00126         printf("create nsILocalFile failed\n");
00127         return;
00128     }
00129 
00130     Banner("Creation Test");
00131     printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
00132 
00133     rv = file->InitWithNativePath(nsDependentCString(creationPath));
00134     VerifyResult(rv);
00135  
00136     printf("Appending %s\n", appendPath);
00137     rv = file->AppendRelativeNativePath(nsDependentCString(appendPath));
00138     VerifyResult(rv);
00139     
00140     printf("Check For Existence\n");
00141 
00142     PRBool exists;
00143     file->Exists(&exists);
00144 
00145     if (exists)
00146         printf("Yup!\n");
00147     else
00148         printf("no.\n");
00149 
00150 
00151     rv = file->Create(whatToCreate, perm);  
00152     VerifyResult(rv);
00153 
00154     rv = file->Exists(&exists);
00155     VerifyResult(rv);
00156 
00157     
00158     if (!exists)
00159     {
00160         Failed("Did not create file system object!");
00161         return;
00162     }
00163     
00164 }    
00165 
00166 void CreateUniqueTest(const char* creationPath, const char* appendPath,
00167                  PRInt32 whatToCreate, PRInt32 perm)
00168 {
00169     nsresult rv;
00170     nsCOMPtr<nsILocalFile> file =
00171         do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
00172 
00173     if (NS_FAILED(rv) || (!file)) 
00174     {
00175         printf("create nsILocalFile failed\n");
00176         return;
00177     }
00178 
00179     Banner("Creation Test");
00180     printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
00181 
00182     rv = file->InitWithNativePath(nsDependentCString(creationPath));
00183     VerifyResult(rv);
00184  
00185     printf("Appending %s\n", appendPath);
00186     rv = file->AppendNative(nsDependentCString(appendPath));
00187     VerifyResult(rv);
00188     
00189     printf("Check For Existence\n");
00190 
00191     PRBool exists;
00192     file->Exists(&exists);
00193 
00194     if (exists)
00195         printf("Yup!\n");
00196     else
00197         printf("no.\n");
00198 
00199 
00200     rv = file->CreateUnique(whatToCreate, perm);  
00201     VerifyResult(rv);
00202 
00203     rv = file->Exists(&exists);
00204     VerifyResult(rv);
00205 
00206     
00207     if (!exists)
00208     {
00209         Failed("Did not create file system object!");
00210         return;
00211     }
00212     
00213 }    
00214 
00215 
00216 void
00217 CopyTest(const char *testFile, const char *targetDir)
00218 {
00219   printf("start copy test\n");
00220 
00221   nsresult rv;
00222   nsCOMPtr<nsILocalFile> file =
00223       do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
00224     
00225   if (NS_FAILED(rv) || (!file)) 
00226   {
00227     printf("create nsILocalFile failed\n");
00228     return;
00229   }
00230 
00231   rv = file->InitWithNativePath(nsDependentCString(testFile));
00232   VerifyResult(rv);
00233   
00234   nsCOMPtr<nsILocalFile> dir =
00235       do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
00236 
00237   if (NS_FAILED(rv) || (!dir)) 
00238   {
00239     printf("create nsILocalFile failed\n");
00240     return;
00241   }
00242 
00243   rv = dir->InitWithNativePath(nsDependentCString(targetDir));
00244   VerifyResult(rv);
00245 
00246   rv = file->CopyTo(dir, EmptyString());
00247   VerifyResult(rv);
00248 
00249   printf("end copy test\n");
00250 }
00251     
00252 void
00253 DeletionTest(const char* creationPath, const char* appendPath, PRBool recursive)
00254 {
00255     nsresult rv;
00256     nsCOMPtr<nsILocalFile> file =
00257             do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
00258     
00259     if (NS_FAILED(rv) || (!file)) 
00260     {
00261         printf("create nsILocalFile failed\n");
00262         return;
00263     }
00264 
00265     Banner("Deletion Test");
00266     printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
00267 
00268     rv = file->InitWithNativePath(nsDependentCString(creationPath));
00269     VerifyResult(rv);
00270  
00271     printf("Appending %s\n", appendPath);
00272     rv = file->AppendNative(nsDependentCString(appendPath));
00273     VerifyResult(rv);
00274     
00275     printf("Check For Existance\n");
00276 
00277     PRBool exists;
00278     file->Exists(&exists);
00279 
00280     if (exists)
00281         printf("Yup!\n");
00282     else
00283         printf("no.\n");
00284 
00285     rv = file->Remove(recursive);  
00286     VerifyResult(rv);
00287 
00288     rv = file->Exists(&exists);
00289     VerifyResult(rv);
00290     
00291     if (exists)
00292     {
00293         Failed("Did not create delete system object!");
00294         return;
00295     }
00296     
00297 }
00298 
00299 void 
00300 MoveTest(const char *testFile, const char *targetDir)
00301 {
00302   Banner("Move Test");
00303 
00304   printf("start move test\n");
00305 
00306   nsresult rv;
00307   nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
00308      
00309   if (!file) 
00310   {
00311     printf("create nsILocalFile failed\n");
00312     return;
00313   }
00314 
00315   rv = file->InitWithNativePath(nsDependentCString(testFile));
00316   VerifyResult(rv);
00317   
00318   nsCOMPtr<nsILocalFile> dir(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
00319  
00320   if (!dir) 
00321   {
00322     printf("create nsILocalFile failed\n");
00323     return;
00324   }
00325 
00326   rv = dir->InitWithNativePath(nsDependentCString(targetDir));
00327   VerifyResult(rv);
00328 
00329   rv = file->MoveToNative(dir, NS_LITERAL_CSTRING("newtemp"));
00330   VerifyResult(rv);
00331   if (NS_FAILED(rv))
00332   {
00333     printf("MoveToNative() test Failed.\n");
00334   }
00335   printf("end move test\n");
00336 }
00337 
00338 
00339 
00340 int main(void)
00341 {
00342     nsCOMPtr<nsIServiceManager> servMan;
00343     NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00344     nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00345     NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00346     registrar->AutoRegister(nsnull);
00347   
00348 #if defined(XP_WIN) || defined(XP_OS2)
00349     InitTest("c:\\temp\\", "sub1/sub2/"); // expect failure
00350     InitTest("d:\\temp\\", "sub1\\sub2\\"); // expect failure
00351 
00352     CreationTest("c:\\temp\\", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
00353     DeletionTest("c:\\temp\\", "file.txt", PR_FALSE);
00354 
00355     MoveTest("c:\\newtemp\\", "d:");
00356 
00357     CreationTest("c:\\temp\\", "mumble\\a\\b\\c\\d\\e\\f\\g\\h\\i\\j\\k\\", nsIFile::DIRECTORY_TYPE, 0644);
00358     DeletionTest("c:\\temp\\", "mumble", PR_TRUE);
00359 
00360     CreateUniqueTest("c:\\temp\\", "foo", nsIFile::NORMAL_FILE_TYPE, 0644);
00361     CreateUniqueTest("c:\\temp\\", "foo", nsIFile::NORMAL_FILE_TYPE, 0644);
00362     CreateUniqueTest("c:\\temp\\", "bar.xx", nsIFile::DIRECTORY_TYPE, 0644);
00363     CreateUniqueTest("c:\\temp\\", "bar.xx", nsIFile::DIRECTORY_TYPE, 0644);
00364     DeletionTest("c:\\temp\\", "foo", PR_TRUE);
00365     DeletionTest("c:\\temp\\", "foo-1", PR_TRUE);
00366     DeletionTest("c:\\temp\\", "bar.xx", PR_TRUE);
00367     DeletionTest("c:\\temp\\", "bar-1.xx", PR_TRUE);
00368 
00369 #else
00370 #ifdef XP_UNIX
00371     InitTest("/tmp/", "sub1/sub2/"); // expect failure
00372     
00373     CreationTest("/tmp", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
00374     DeletionTest("/tmp/", "file.txt", PR_FALSE);
00375     
00376     CreationTest("/tmp", "mumble/a/b/c/d/e/f/g/h/i/j/k/", nsIFile::DIRECTORY_TYPE, 0644);
00377     DeletionTest("/tmp", "mumble", PR_TRUE);
00378 
00379     CreationTest("/tmp", "file", nsIFile::NORMAL_FILE_TYPE, 0644);
00380     CopyTest("/tmp/file", "/tmp/newDir");
00381     MoveTest("/tmp/file", "/tmp/newDir/anotherNewDir");
00382     DeletionTest("/tmp", "newDir", PR_TRUE);
00383 
00384 #endif /* XP_UNIX */
00385 #endif /* XP_WIN || XP_OS2 */
00386     return 0;
00387 }