Exemplo n.º 1
0
        static Boolean RunWebDAVTests(WebDAVClient c)
        {
            autoResetEvent = new AutoResetEvent(false);

            // Generate unique string to test with.
            string basepath           = Path.GetRandomFileName() + '/';
            string tempFilePath       = Path.GetTempFileName();
            string uploadTestFilePath = @"c:\windows\notepad.exe";
            //string uploadTestFilePath = @"c:\windows\explorer.exe";
            // string uploadTestFilePath = @"c:\windows\setuplog.txt";
            string uploadTestFileName = Path.GetFileName(uploadTestFilePath);

            c.CreateDirComplete += new CreateDirCompleteDel(c_CreateDirComplete);
            c.CreateDir(basepath);
            autoResetEvent.WaitOne();
            Debug.WriteLine("CreateDir passed");

            c.ListComplete += new ListCompleteDel(c_ListComplete);
            c.List(basepath);
            autoResetEvent.WaitOne();
            if (_files.Count != 0)
            {
                return(false);
            }
            Debug.WriteLine("List passed");

            c.UploadComplete += new UploadCompleteDel(c_UploadComplete);
            c.Upload(uploadTestFilePath, basepath + uploadTestFileName);
            autoResetEvent.WaitOne();
            Debug.WriteLine("Upload 1/2 passed");
            c.List(basepath);
            autoResetEvent.WaitOne();
            if (_files.Count != 1)
            {
                return(false);
            }
            Debug.WriteLine("Upload 2/2 passed");

            autoResetEvent      = new AutoResetEvent(false);
            c.DownloadComplete += new DownloadCompleteDel(c_DownloadComplete);
            c.Download(basepath + uploadTestFileName, tempFilePath);
            autoResetEvent.WaitOne();
            Debug.WriteLine("Download 1/2 passed");
            HashAlgorithm h = HashAlgorithm.Create("SHA1");

            byte[] localhash;
            byte[] remotehash;
            using (FileStream fs = new FileStream(uploadTestFilePath, FileMode.Open))
            {
                localhash = h.ComputeHash(fs);
            }
            using (FileStream fs = new FileStream(tempFilePath, FileMode.Open))
            {
                remotehash = h.ComputeHash(fs);
            }
            for (int i = 0; i < localhash.Length; i++)
            {
                if (localhash[i] != remotehash[i])
                {
                    return(false);
                }
            }
            Debug.WriteLine("Download 2/2 passed");

            c.DeleteComplete += new DeleteCompleteDel(c_DeleteComplete);
            c.Delete(basepath + uploadTestFileName);
            autoResetEvent.WaitOne();
            Debug.WriteLine("Delete 1/2 passed");

            c.List(basepath);
            autoResetEvent.WaitOne();
            if (_files.Count != 0)
            {
                return(false);
            }
            Debug.WriteLine("Delete 2/2 passed");

            return(true);
        }