public void TC001_FileServerTest() { string user = Credentials.GenerateCredential(); string password = Credentials.GenerateCredential(); string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); Directory.CreateDirectory(tempPath); File.WriteAllText(Path.Combine(tempPath, "test.txt"), "this is a test"); int port = NetworkInterface.GrabEphemeralPort(); FileServer fs = new FileServer(port, tempPath, "foobar", user, password); fs.Start(); WebClient client = new WebClient(); NetworkCredential credentials = new NetworkCredential(user, password); client.Credentials = credentials; byte[] data = client.DownloadData(String.Format("http://{0}:{1}/foobar/test.txt", "localhost", port)); ASCIIEncoding encoding = new ASCIIEncoding(); string retrievedContents = encoding.GetString(data); Assert.IsTrue(retrievedContents.Contains("this is a test")); //Thread.Sleep(6000000); fs.Stop(); }
public void TC004_FileNamesWithSpacesFileServerTest() { string user = Credentials.GenerateCredential(); string password = Credentials.GenerateCredential(); string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); Directory.CreateDirectory(tempPath); Directory.CreateDirectory(Path.Combine(tempPath, "testDir with space")); File.WriteAllText(Path.Combine(tempPath, "test with space.txt"), "this is a test"); File.WriteAllText(Path.Combine(tempPath, "test2.txt"), "this is a test"); int port = NetworkInterface.GrabEphemeralPort(); FileServer fs = new FileServer(port, tempPath, "foobar", user, password); fs.Start(); WebClient client = new WebClient(); NetworkCredential credentials = new NetworkCredential(user, password); client.Credentials = credentials; byte[] gooddata = client.DownloadData(String.Format("http://{0}:{1}/foobar/test with space.txt", "localhost", port)); ASCIIEncoding encoding = new ASCIIEncoding(); string retrievedContents = encoding.GetString(gooddata); Assert.IsTrue(retrievedContents.Contains("this is a test")); Trace.WriteLine(String.Format("http://{2}:{3}@{0}:{1}/foobar/../", NetworkInterface.GetLocalIPAddress(), port, user, password)); try { Uri dl = new Uri(String.Format("http://{0}:{1}/foobar/useless/../../", "localhost", port)); byte[] data = client.DownloadData(dl); } catch { return; } finally { fs.Stop(); } Assert.Fail(); }
public void Start(string dropletsPath) { this.filerStartTimer = new System.Timers.Timer(1000); this.filerStartTimer.AutoReset = false; this.filerStartTimer.Elapsed += new System.Timers.ElapsedEventHandler(delegate(object sender, System.Timers.ElapsedEventArgs args) { bool success = false; try { this.fileViewerServer = new FileServer(this.Port, dropletsPath, @"/droplets", this.Credentials[0], this.Credentials[1]); this.fileViewerServer.Start(); Logger.Info(Strings.FileServiceStartedOnPort, this.Port); this.startAttempts += 1; success = true; } catch (Exception ex) { Logger.Fatal(Strings.FilerServiceFailedToStart, this.Port, ex.ToString()); this.startAttempts += 1; if (this.startAttempts >= 5) { Logger.Fatal(Strings.GivingUpOnTryingToStartFiler); this.OnStartError(null, null); } } if (success) { this.IsRunning = true; this.filerStartTimer.Enabled = false; this.filerStartTimer = null; } else { this.filerStartTimer.Enabled = true; } }); this.filerStartTimer.Enabled = true; }