public void StdFSDirCreateFileDupeTest() { File422 txt = root.CreateFile("file.txt"); File422 txtDupe = root.CreateFile("file.txt"); Assert.AreEqual(txt.Name, txtDupe.Name); }
public void MemFSDirCreateFileDupeTest() { File422 txt = root.CreateFile("file.txt"); File422 txtDupe = root.CreateFile("file.txt"); Assert.AreSame(txt, txtDupe); }
public void StdFSDirGetFilesTest() { Dir422 b = root.GetDir("two").GetDir("a").GetDir("b"); b.CreateFile("file1.txt"); b.CreateFile("file2.txt"); IList <File422> files = b.GetFiles(); Assert.AreEqual(2, files.Count); }
public void MemFSDirGetFilesTest() { Dir422 b = root.GetDir("two").GetDir("a").GetDir("b"); File422 file1 = b.CreateFile("file1.txt"); File422 file2 = b.CreateFile("file2.txt"); IList <File422> files = b.GetFiles(); Assert.AreEqual(2, files.Count); Assert.AreSame(file1, files[files.IndexOf(file1)]); Assert.AreSame(file2, files[files.IndexOf(file2)]); }
public void MReadWriteFile() { MemoryFileSystem mfs = new MemoryFileSystem(); Dir422 dir1 = mfs.GetRoot().CreateDir("Dir1"); File422 file1 = dir1.CreateFile("File1"); string datastring = "new data"; byte[] info = new UTF8Encoding(true).GetBytes(datastring); //Assert.NotNull(file1._stream); Stream open = file1.OpenReadWrite(); Assert.True(true); Assert.NotNull(open); open.Write(info, 0, info.Length); open.Close(); Stream read = file1.OpenReadWrite(); byte[] readIn = new byte[256]; read.Read(readIn, 0, 256); string str = System.Text.Encoding.Default.GetString(readIn).TrimEnd('\0'); StringAssert.AreEqualIgnoringCase(str, datastring); }
static int Main(string[] args) { StandardFileSystem sfs2 = StandardFileSystem.Create("/Users/drewm"); Dir422 dir1 = sfs2.GetRoot().GetDir("Desktop"); //Assert.NotNull(dir1.CreateFile("hello.txt")); //Assert.NotNull(dir1.CreateFile("Test.txt")); dir1.CreateFile("A"); string datastring = "new data"; byte[] info = new UTF8Encoding(true).GetBytes(datastring); File422 a = dir1.GetFile("A"); if (a is StdFSFile) { a = a as StdFSFile; FileStream fs = (FileStream)((StdFSFile)a).OpenReadWrite(); fs.Write(info, 0, info.Length); fs.Close(); } return(0); }
public void HandlePut(WebRequest req, Dir422 dir) { File422 file = dir.CreateFile(req.URI.Split('/', '\\').Last()); Stream outputStream = file.OpenReadWrite(); Stream inputStream = req._networkStream; inputStream.ReadTimeout = 1000; int BUFFER_SIZE = 8000; int bytesRead = 0; byte[] bytes = new byte[BUFFER_SIZE]; try { bytesRead = inputStream.Read(bytes, 0, bytes.Length); } catch { } while (bytesRead != 0) { // Translate data bytes to a ASCII string. outputStream.Write(bytes, 0, bytesRead); bytes = new byte[BUFFER_SIZE]; try { bytesRead = inputStream.Read(bytes, 0, bytes.Length); } catch { break; } Console.WriteLine(bytesRead.ToString()); } req.WriteHTMLResponse("Hello"); outputStream.Close(); inputStream.Close(); }
public void SDirTests() { StandardFileSystem sfs2 = StandardFileSystem.Create("/Users/drewm"); Dir422 dir1 = sfs2.GetRoot().GetDir("Desktop"); //Assert.NotNull(dir1.CreateFile("hello.txt")); //Assert.NotNull(dir1.CreateFile("Test.txt")); Assert.NotNull(dir1.CreateFile("A")); string datastring = "new data"; byte[] info = new UTF8Encoding(true).GetBytes(datastring); File422 a = dir1.GetFile("A"); if (a is StdFSFile) { Assert.NotNull(a); a = a as StdFSFile; FileStream fs = (FileStream)((StdFSFile)a).OpenReadWrite(); fs.Write(info, 0, info.Length); fs.Close(); } else { Assert.IsTrue(false); } }
public void StandardFileSystemContainsFileTest() { Dir422 c = root.GetDir("two").GetDir("a").GetDir("b").GetDir("c"); File422 txt = c.CreateFile("dummy.txt"); Assert.True(stdFS.Contains(txt)); }
public void StdFSDirCreateFileTest() { Dir422 b = root.GetDir("one").GetDir("a").GetDir("b"); File422 bFile = b.CreateFile("bFile.txt"); Assert.AreEqual("bFile.txt", bFile.Name); //references are different. Since not memory. we create new everytime. }
public void d_CreateContainsFile() { //Dir422 dir = new StdFSDir(rootDir, true); //dir.CreateFile("fooFile"); //Assert.AreEqual(true, dir.ContainsFile("fooFile", false)); root.CreateFile("fooFile"); Assert.AreEqual(true, root.ContainsFile("fooFile", false)); }
public void d_CreateContainFileRecursive() { Dir422 TestDir = root.GetDir("TestDir"); Dir422 BlahDir = TestDir.CreateDir("BlahDir"); BlahDir.CreateFile("blahFile"); Assert.AreEqual(true, root.ContainsFile("blahFile", true)); }
public void MemFSDirCreateFileTest() { Dir422 b = root.GetDir("one").GetDir("a").GetDir("b"); File422 bFile = b.CreateFile("bFile.txt"); Assert.AreEqual("bFile.txt", bFile.Name); Assert.AreSame(b, bFile.Parent); }
public void StdFSDirContainsFileRecursiveTest() { Dir422 c = root.GetDir("one").GetDir("a").GetDir("b").GetDir("c"); root.CreateFile("at_root.txt"); c.CreateFile("at_ones_c.txt"); Assert.True(root.ContainsFile("at_root.txt", true)); Assert.True(root.ContainsFile("at_ones_c.txt", true)); }
public void MemFSDirContainsFileNonRecursiveTest() { Dir422 c = root.GetDir("one").GetDir("a").GetDir("b").GetDir("c"); root.CreateFile("at_root.txt"); c.CreateFile("at_ones_c.txt"); Assert.True(root.ContainsFile("at_root.txt", false)); Assert.False(root.ContainsFile("at_ones_c.txt", false)); }
public void e_CreateContainsFileRecursive() { //Dir422 dir = new StdFSDir(rootDir + "/FooDir"); //dir.CreateFile("barFile"); //Dir422 dirParent = dir.Parent; //Assert.AreEqual(true, dirParent.ContainsFile("barFile", true)); Dir422 FooDir = root.GetDir("FooDir"); FooDir.CreateFile("barFile"); Assert.AreEqual(true, root.ContainsFile("barFile", true)); }
public void MCreateFile() { MemoryFileSystem mfs = new MemoryFileSystem(); Dir422 dir1 = mfs.GetRoot().CreateDir("Dir1"); File422 file1 = dir1.CreateFile("File1"); Assert.NotNull(dir1); Assert.NotNull(mfs.Contains(dir1)); Assert.False(mfs.GetRoot().ContainsFile("File1", false)); Assert.True(mfs.GetRoot().ContainsFile("File1", true)); }
public void f_GetDirMakeFile() { //Dir422 dir = new StdFSDir(rootDir + "/FooDir"); //Dir422 BarDir = dir.GetDir("BarDir"); //BarDir.CreateFile("barFile2"); //Assert.AreEqual(true, BarDir.ContainsFile("barFile2", true)); Dir422 FooDir = root.GetDir("FooDir"); Dir422 BarDir = FooDir.GetDir("BarDir"); BarDir.CreateFile("barFile2"); Assert.AreEqual(true, BarDir.ContainsFile("barFile2", true)); }
/* * Don’t allow overwriting existing files * Allow all (non-existing-file-overwriting) uploads provided the file name doesn’t contain reserve characters (/ and \) * Files can be uploaded to any directory that the service shares * Files must be uploaded into the current directory that the user is seeing the listing for within the browser * There is no limit on file sizes or content * You only need to support uploading 1 file at a time (1 per page refresh, as described below) * You do NOT need to support cross-origin requests or the OPTIONS method, which will have been discussed in class * */ void handlePUT(WebRequest req) { string[] path = req.URI.Split('/'); // The file already exists if (fileSystem.GetRoot().ContainsFile(path[path.Length - 1], true)) { req.WriteNotFoundResponse("File exists"); } // Make file else { Dir422 currentDir = fileSystem.GetRoot(); // Don't include last element because thats the file name for (int i = 2; i < path.Length - 1; i++) { // Fix '/' at end of URI if (path[i] != "") { currentDir = currentDir.GetDir(path[i]); } } File422 file = currentDir.CreateFile(path[path.Length - 1]); using (Stream stream = file.OpenReadWrite()) { byte[] buffer = new byte[1024 * 8]; int bytesRead = 0, size = int.Parse(req.Headers["content-length"]), totalRead = 0; while (totalRead < size) { bytesRead = req.Body.Read(buffer, 0, (size - totalRead > buffer.Length) ? buffer.Length : size - totalRead); totalRead += bytesRead; Console.WriteLine(totalRead < size); stream.Write(buffer, 0, (bytesRead < size) ? bytesRead : size); } } Console.WriteLine("File uploaded"); req.WriteHTMLResponse("file uploaded!"); } }
private void PUTHandler(WebRequest req, string name, Dir422 dir) { if (dir.ContainsFile(name, false) || name.Contains("\\") || name.Contains("/")) { string bodyMessage = "Invalid: File already exists or filename is invalid"; string template = "HTTP/1.1 400 Bad Request\r\n" + "Content-Type: text/html\r\n" + "Content-Length: {0}\r\n\r\n" + "{1}"; string response = string.Format(template, Encoding.ASCII.GetBytes(bodyMessage).Length, bodyMessage); byte[] responseBytes = Encoding.ASCII.GetBytes(response); req.WriteResponse(responseBytes, 0, responseBytes.Length); return; } File422 file = dir.CreateFile(name); FileStream fs = (FileStream)file.OpenReadWrite(); long fileSize = 0; Int64.TryParse(req.BodySize, out fileSize); int totalread = 0; while (true) { byte[] buf = new byte[4096]; int read = req.Body.Read(buf, 0, buf.Length); fs.Write(buf, 0, read); totalread += read; if ((read == 0) || read < buf.Length && totalread >= fileSize) { break; } } fs.Dispose(); fs.Close(); req.WriteHTMLResponse("Successfully uploaded file: " + name); }
private void UploadFile(WebRequest req) { string[] pieces = req.URI.Substring(ServiceURI.Length).Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); if (pieces == null || pieces.Length == 0) { req.WriteNotFoundResponse("404: Path error."); return; } Dir422 dir = r_fs.GetRoot(); for (int i = 0; i < pieces.Length - 1; i++) { string piece = pieces[i]; dir = dir.GetDir(piece); if (dir == null) { req.WriteNotFoundResponse("404: Path error."); return; } } File422 file = dir.GetFile(PercentDecoding(pieces[pieces.Length - 1])); if (file != null) { req.WriteHTMLResponse("<html> File Already Exists! </html>"); return; } File422 newFile = dir.CreateFile(PercentDecoding(pieces[pieces.Length - 1])); FileStream str = (newFile.OpenReadWrite() as FileStream); Stream reqStr = req.Body; byte[] buf = new byte[4096]; long len = req.Len; if (len < 4096) { reqStr.Read(buf, 0, (int)len); str.Write(buf, 0, (int)len); str.Close(); return; } int count = reqStr.Read(buf, 0, 4096); int totalRead = count; while (count != 0 && totalRead < len) { str.Write(buf, 0, count); buf = new byte[4096]; count = reqStr.Read(buf, 0, 4096); totalRead += count; } // If bytes were read last time, trim zeroes and write last bit if (count != 0) { str.Write(buf, 0, count); } str.Close(); req.WriteHTMLResponse("<html> Upload Successful! </html>"); return; }
public void c_CreateContainFile() { root.CreateFile("testFile"); Assert.AreEqual(true, root.ContainsFile("testFile", false)); }
public void RunTest(FileSys422 mySys) { Dir422 root = mySys.GetRoot(); //We should not be able to go above our root. Assert.IsNull(root.Parent); // Checking that we do not have a file Assert.IsFalse(root.ContainsFile("NewFile.txt", false)); //create the file root.CreateFile("NewFile.txt"); // Check that we can find it. Assert.IsTrue(root.ContainsFile("NewFile.txt", false)); // Same with directory Assert.IsFalse(root.ContainsDir("SubDir", false)); Dir422 subDir = root.CreateDir("SubDir"); Assert.IsTrue(root.ContainsDir("SubDir", false)); //Creating a file in a sub dir subDir.CreateFile("subText.txt"); // Testing the recursive methods on files Assert.IsFalse(root.ContainsFile("subText.txt", false)); Assert.IsTrue(root.ContainsFile("subText.txt", true)); //Testing recurcive method on dirs subDir.CreateDir("newSubDir"); Assert.IsFalse(root.ContainsDir("newSubDir", false)); Assert.IsTrue(root.ContainsDir("newSubDir", true)); //Checking getDir Dir422 recivedDir = root.GetDir("InvalidDir"); Assert.IsNull(recivedDir); recivedDir = root.GetDir("SubDir"); Assert.AreEqual("SubDir", recivedDir.Name); // Checking that if a file does not exist we return null, // otherwise we recived the file we wanted. File422 recidedFile = root.GetFile("InvalidFile"); Assert.IsNull(recidedFile); recidedFile = root.GetFile("NewFile.txt"); Assert.AreEqual("NewFile.txt", recidedFile.Name); //Checking the name validation function. // All of these methods use the same Validate Name method. Assert.IsNull(subDir.CreateFile("file/New.txt")); Assert.IsNull(subDir.CreateDir("file/New")); string bufString = "hello world"; byte[] buff = ASCIIEncoding.ASCII.GetBytes(bufString); var writeStream = recidedFile.OpenReadWrite(); writeStream.Write(buff, 0, 11); var readStream = recidedFile.OpenReadOnly(); Assert.IsNull(readStream); writeStream.Dispose(); readStream = recidedFile.OpenReadOnly(); Assert.IsNotNull(readStream); //First read 'hello ' from each stream byte[] readBuf = new byte[6]; readStream.Read(readBuf, 0, 6); Assert.AreEqual("hello ", ASCIIEncoding.ASCII.GetString(readBuf)); //Having two streams open for read var readStream2 = recidedFile.OpenReadOnly(); Assert.IsNotNull(readStream2); byte[] readBuf2 = new byte[6]; readStream2.Read(readBuf2, 0, 6); Assert.AreEqual("hello ", ASCIIEncoding.ASCII.GetString(readBuf2)); //Next read 'world' from each stream readBuf = new byte[5]; readStream.Read(readBuf, 0, 5); Assert.AreEqual("world", ASCIIEncoding.ASCII.GetString(readBuf)); readBuf2 = new byte[5]; readStream2.Read(readBuf2, 0, 5); Assert.AreEqual("world", ASCIIEncoding.ASCII.GetString(readBuf2)); //try to open a stream to write while there are streams open for read writeStream = recidedFile.OpenReadWrite(); Assert.IsNull(writeStream); //Close streams and try again readStream.Close(); readStream2.Close(); writeStream = recidedFile.OpenReadWrite(); Assert.IsNotNull(writeStream); }
private void DoUpload(WebRequest req, string percenDecoded) { string fileName = ""; Dir422 currentDir = null; if (percenDecoded == ServiceURI) { currentDir = r_sys.GetRoot(); } else { // The pluse one is the '/' after the '/files' so we remove '/files/' string[] pathPeices = percenDecoded.Substring(ServiceURI.Length + 1).Split('/'); currentDir = r_sys.GetRoot(); for (int i = 0; i < pathPeices.Length - 1; i++) { currentDir = currentDir.GetDir(pathPeices[i]); if (currentDir == null) { req.WriteNotFoundResponse(string.Empty); return; } } fileName = pathPeices [pathPeices.Length - 1]; } File422 newFile = currentDir.CreateFile(fileName); if (newFile == null) { // We could not create a new file return; } Stream s = newFile.OpenReadWrite(); if (s == null) { return; } byte[] bodyBytes = new byte[1024]; long bytesRead = 0; //Read all that we can. while (bytesRead < req.BodyLength) { // We need to read chuncks at a time since read wants an int // as the count, and we may loose data if we cast a long to an int. int read = req.Body.Read(bodyBytes, 0, 1024); bytesRead += read; if (read == 0) { break; } s.Write(bodyBytes, 0, read); Array.Clear(bodyBytes, 0, bodyBytes.Length); } s.Close(); req.WriteHTMLResponse(""); return; }