Skip to content

Commit

Permalink
changes test names and adds file not found test
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarroquin committed Sep 27, 2024
1 parent faac6d7 commit cea3ce6
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 35 deletions.
114 changes: 81 additions & 33 deletions src/FlaUI.WebDriver.UITests/DeviceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,84 +14,132 @@ namespace FlaUI.WebDriver.UITests
public class DeviceTests
{
[Test]
public void PushPullFile()
public void PushFile_FileDoesNotExist_FileIsPushed()
{
var driverOptions = FlaUIAppiumDriverOptions.RootApp();
using var driver = new WindowsDriver(WebDriverFixture.WebDriverUrl, driverOptions);

var file = Path.GetTempFileName();
var data_to_write = "hello world";

// file was upload to temp directory
File.Delete(file);
var data_to_write = "aaaaaaaaaaaaaaa";
driver.PushFile(file, data_to_write);
Assert.That(File.ReadAllText(file) == data_to_write);

// pull the file back
var pulled_data = Encoding.UTF8.GetString(driver.PullFile(file));
Assert.That(pulled_data == data_to_write);
File.Delete(file);
}


[Test]
public void PullFolder()
public void PushFile_FileExist_FileIsOverwritten()
{
var driverOptions = FlaUIAppiumDriverOptions.RootApp();
using var driver = new WindowsDriver(WebDriverFixture.WebDriverUrl, driverOptions);

var dir = Path.Join(Path.GetTempPath(), "FlaUiWDPullDir");
for (int i = 0; i < 10; i++)
{
driver.PushFile(Path.Join(dir, i.ToString()), $"{i} data");
}
var zip_bytes = driver.PullFolder(dir);
using var memoryStream = new MemoryStream(zip_bytes);
using var zip = new ZipArchive(memoryStream);

for (int i = 0; i < 10; i++)
{
var entry = zip.GetEntry(i.ToString());
using var entry_stream = new StreamReader(entry.Open());
Assert.That(entry_stream.ReadToEnd() == $"{i} data");
}
Directory.Delete(dir, true);
var file = Path.GetTempFileName();
File.WriteAllText(file, "aaaaaaaaaaaaaaaaaaaa");
var data_to_write = "bbbbbbbbbbbbbbbbbbb";
driver.PushFile(file, data_to_write);
Assert.That(File.ReadAllText(file) == data_to_write);
}

[Test]
public void PushFile_PathEmpty()
public void PushFile_PathEmpty_ErrorRaised()
{
var driverOptions = FlaUIAppiumDriverOptions.RootApp();
using var driver = new WindowsDriver(WebDriverFixture.WebDriverUrl, driverOptions);

Assert.That(
() => driver.PushFile("", "hello world"),
Throws.TypeOf<WebDriverException>().With.Message.EqualTo("Parameter path must be provided in the request.")
);

}

[Test]
public void PullFile_PathEmpty()
public void PullFile_FileDoesNotExist_ErrorRaised()
{
var driverOptions = FlaUIAppiumDriverOptions.RootApp();
using var driver = new WindowsDriver(WebDriverFixture.WebDriverUrl, driverOptions);
var file = Path.GetTempFileName();
File.Delete(file);
Assert.That(
() => driver.PullFile(file),
Throws.TypeOf<WebDriverException>().With.Message.EqualTo($"File {file} does not exist.")
);
}


[Test]
public void PullFile_PathEmpty_ErrorRaised()
{
var driverOptions = FlaUIAppiumDriverOptions.RootApp();
using var driver = new WindowsDriver(WebDriverFixture.WebDriverUrl, driverOptions);
Assert.That(
() => driver.PullFile(""),
Throws.TypeOf<WebDriverException>().With.Message.EqualTo("Parameter path must be provided in the request.")
);

}


[Test]
public void PullFolder_PathEmpty()
public void PullFile_FileExists_ContentPulled()
{
var driverOptions = FlaUIAppiumDriverOptions.RootApp();
using var driver = new WindowsDriver(WebDriverFixture.WebDriverUrl, driverOptions);
var file = Path.GetTempFileName();
var content = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
File.WriteAllText(file, content);
var remoteContent = Encoding.UTF8.GetString(driver.PullFile(file));
Assert.That(remoteContent == content);

}

[Test]
public void PullFolder_FolderDoesNotExists_ErrorRaised()
{
var tempDir = Path.Join(Path.GetTempPath(), System.Guid.NewGuid().ToString());
var driverOptions = FlaUIAppiumDriverOptions.RootApp();
using var driver = new WindowsDriver(WebDriverFixture.WebDriverUrl, driverOptions);
Assert.That(
() => driver.PullFolder(tempDir),
Throws.TypeOf<WebDriverException>().With.Message.EqualTo($"File {tempDir} does not exist.")
);
}


[Test]
public void PullFolder_PathEmpty_ErrorRaised()
{
var driverOptions = FlaUIAppiumDriverOptions.RootApp();
using var driver = new WindowsDriver(WebDriverFixture.WebDriverUrl, driverOptions);
Assert.That(
() => driver.PullFolder(""),
Throws.TypeOf<WebDriverException>().With.Message.EqualTo("Parameter path must be provided in the request.")
Throws.TypeOf<WebDriverException>().With.Message.EqualTo($"Parameter path must be provided in the request.")
);
}


[Test]
public void PullFolder_FolderExists_ContentPulled()
{
var driverOptions = FlaUIAppiumDriverOptions.RootApp();
using var driver = new WindowsDriver(WebDriverFixture.WebDriverUrl, driverOptions);

var dir = Path.Join(Path.GetTempPath(), System.Guid.NewGuid().ToString());
for (int i = 0; i < 10; i++)
{
driver.PushFile(Path.Join(dir, i.ToString()), $"{i} data");
}
var zipBytes = driver.PullFolder(dir);
using var memoryStream = new MemoryStream(zipBytes);
using var zip = new ZipArchive(memoryStream);

for (int i = 0; i < 10; i++)
{
var entry = zip.GetEntry(i.ToString());
using var entryStream = new StreamReader(entry.Open());
Assert.That(entryStream.ReadToEnd() == $"{i} data");
}
Directory.Delete(dir, true);
}



}
}
18 changes: 16 additions & 2 deletions src/FlaUI.WebDriver/Controllers/DeviceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ private static ActionResult MissingParameter(string parameterName)
});
}

private static ActionResult FileNotFound(string fileName)
{
return WebDriverResult.NotFound(new ErrorResponse()
{
ErrorCode = "File Not Found",
Message = $"File {fileName} does not exist."
});
}

[HttpPost("push_file")]
public async Task<ActionResult> PushFile([FromBody] PushFileRequest request)
{
Expand Down Expand Up @@ -55,6 +64,9 @@ public async Task<ActionResult> PullFile([FromBody] PullFileRequest request)
{
return MissingParameter("path");
}
if (!System.IO.File.Exists(request.Path)) {

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
return FileNotFound(request.Path);
}
var data = await System.IO.File.ReadAllBytesAsync(request.Path);

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
return WebDriverResult.Success(Convert.ToBase64String(data));
}
Expand All @@ -66,6 +78,10 @@ public ActionResult PullFolder([FromBody] PullFileRequest request)
{
return MissingParameter("path");
}
if (!Directory.Exists(request.Path))

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
{
return FileNotFound(request.Path);
}
byte[] bytes;
using (var ms = new MemoryStream())
{
Expand All @@ -76,7 +92,5 @@ public ActionResult PullFolder([FromBody] PullFileRequest request)

return WebDriverResult.Success(Convert.ToBase64String(bytes));
}


}
}

0 comments on commit cea3ce6

Please sign in to comment.