Skip to content

Commit

Permalink
Add WDA roundtrip test
Browse files Browse the repository at this point in the history
  • Loading branch information
qmfrederik committed Nov 26, 2022
1 parent 0febec5 commit ad77b7c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
54 changes: 51 additions & 3 deletions Melanzana.MachO.Tests/RoundtripTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using Xunit;
using System.IO;
using System.IO.Compression;
using System.Linq;
using Melanzana.Streams;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;

namespace Melanzana.MachO.Tests
{
public class RoundtripTests
{
private const string WebDriverAgentFileName = "WebDriverAgentRunner-Runner.zip";

private static void TestRoundtrip(Stream aOutStream)
{
var objectFile = MachReader.Read(aOutStream).Single();
Expand Down Expand Up @@ -34,7 +38,7 @@ private static void TestFatRoundtrip(Stream aOutStream)
outputStream.Seek(0, SeekOrigin.Begin);

MachWriter.Write(outputStream, objectFiles);
}
}
}

[Fact]
Expand Down Expand Up @@ -64,5 +68,49 @@ public void ExecutableRoundtrip()
var aOutStream = typeof(RoundtripTests).Assembly.GetManifestResourceStream("Melanzana.MachO.Tests.Data.rpath.out")!;
TestRoundtrip(aOutStream);
}

[Theory]
[InlineData("WebDriverAgentRunner-Runner.app/WebDriverAgentRunner-Runner")]
[InlineData("WebDriverAgentRunner-Runner.app/Frameworks/XCTAutomationSupport.framework/XCTAutomationSupport")]
[InlineData("WebDriverAgentRunner-Runner.app/Frameworks/XCTest.framework/XCTest")]
[InlineData("WebDriverAgentRunner-Runner.app/Frameworks/XCTestCore.framework/XCTestCore")]
[InlineData("WebDriverAgentRunner-Runner.app/Frameworks/XCUIAutomation.framework/XCUIAutomation")]
[InlineData("WebDriverAgentRunner-Runner.app/Frameworks/XCUnit.framework/XCUnit")]
[InlineData("WebDriverAgentRunner-Runner.app/PlugIns/WebDriverAgentRunner.xctest/WebDriverAgentRunner")]
[InlineData("WebDriverAgentRunner-Runner.app/PlugIns/WebDriverAgentRunner.xctest/Frameworks/WebDriverAgentLib.framework/WebDriverAgentLib")]
public async Task WebDriverAgentRunnerRoundtrip(string entryName)
{
await DownloadWebDriverAgent(WebDriverAgentFileName);

using (var bundleStream = File.OpenRead(WebDriverAgentFileName))
using (var zipArchive = new ZipArchive(bundleStream, ZipArchiveMode.Read))
using (var machObjectZipStream = zipArchive.GetEntry(entryName)!.Open())
using (var machObjectStream = new MemoryStream())
{
machObjectZipStream.CopyTo(machObjectStream);

if (MachReader.IsFatMach(machObjectStream))
{
TestFatRoundtrip(machObjectStream);
}
else
{
TestRoundtrip(machObjectStream);
}
}
}

private async Task DownloadWebDriverAgent(string path, string version = "v4.10.10")
{
if (!File.Exists(path))
{
using (var targetStream = File.Create(path))
using (var client = new HttpClient())
using (var sourceStream = await client.GetStreamAsync($"https://github.com/appium/WebDriverAgent/releases/download/{version}/WebDriverAgentRunner-Runner.zip"))
{
await sourceStream.CopyToAsync(targetStream);
}
}
}
}
}
5 changes: 4 additions & 1 deletion Melanzana.MachO.Tests/ValidatingStream.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Melanzana.Streams;
using System;
using System.IO;
using System.Linq;
using Xunit;

namespace Melanzana.MachO.Tests
Expand Down Expand Up @@ -62,7 +63,9 @@ public override void Write(byte[] buffer, int offset, int count)
}
else
{
Assert.Equal(expected, actual);
// Assert.Equal for large byte arrays does not perform particularly well;
// use SequenceEqual instead.
Assert.True(expected.SequenceEqual(actual));
}
}
}
Expand Down

0 comments on commit ad77b7c

Please sign in to comment.