From adf663f8aa00385e281b5de1d8b2389959552de3 Mon Sep 17 00:00:00 2001 From: AdityaHirapara Date: Mon, 14 Mar 2022 14:58:07 +0530 Subject: [PATCH 1/7] Assign binary name and path based on os --- .../BrowserStackLocal/BrowserStackTunnel.cs | 14 ++++++++++---- .../IntegrationTests.cs | 2 -- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs index 4d985f1..4da45ee 100644 --- a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs +++ b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs @@ -14,10 +14,16 @@ public enum LocalState { Idle, Connecting, Connected, Error, Disconnected }; public class BrowserStackTunnel : IDisposable { - static readonly string binaryName = "BrowserStackLocal.exe"; - static readonly string downloadURL = "https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal.exe"; + static readonly OperatingSystem os = Environment.OSVersion; + static readonly string binaryName = os.Platform.ToString() == "Unix" ? "BrowserStackLocal" : "BrowserStackLocal.exe"; + static readonly string downloadURL = os.Platform.ToString() == "Unix" ? + "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-darwin-x64" : + "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal.exe"; + static readonly string homepath = os.Platform.ToString() == "Unix" ? + Environment.GetFolderPath(Environment.SpecialFolder.Personal) : + Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"); public static readonly string[] basePaths = new string[] { - Path.Combine(Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"), ".browserstack"), + Path.Combine(homepath, ".browserstack"), Directory.GetCurrentDirectory(), Path.GetTempPath() }; @@ -60,7 +66,7 @@ public virtual void fallbackPaths() { if (basePathsIndex >= basePaths.Length - 1) { - throw new Exception("Binary not found or failed to launch. Make sure that BrowserStackLocal.exe is not already running."); + throw new Exception("Binary not found or failed to launch. Make sure that BrowserStackLocal is not already running."); } basePathsIndex++; binaryAbsolute = Path.Combine(basePaths[basePathsIndex], binaryName); diff --git a/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs b/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs index e2d6c81..c639d13 100644 --- a/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs +++ b/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs @@ -18,8 +18,6 @@ public class IntegrationTests new KeyValuePair("key", accesskey), new KeyValuePair("verbose", "true"), new KeyValuePair("forcelocal", "true"), - new KeyValuePair("binarypath", "C:\\Users\\Admin\\Desktop\\BrowserStackLocal.exe"), - new KeyValuePair("logfile", "C:\\Users\\Admin\\Desktop\\local.log"), }; [Test] From 5522a8a870c5f9c429003f4aba0f24858636b2fe Mon Sep 17 00:00:00 2001 From: AdityaHirapara Date: Mon, 14 Mar 2022 16:32:44 +0530 Subject: [PATCH 2/7] Fix existing unit tests --- .../BrowserStackTunnelTests.cs | 23 +++++++++++-------- .../BrowserStackLocal/BrowserStackTunnel.cs | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/BrowserStackLocal/BrowserStackLocal Unit Tests/BrowserStackTunnelTests.cs b/BrowserStackLocal/BrowserStackLocal Unit Tests/BrowserStackTunnelTests.cs index dd88bb6..2a32216 100644 --- a/BrowserStackLocal/BrowserStackLocal Unit Tests/BrowserStackTunnelTests.cs +++ b/BrowserStackLocal/BrowserStackLocal Unit Tests/BrowserStackTunnelTests.cs @@ -13,6 +13,11 @@ namespace BrowserStack_Unit_Tests [TestClass] public class BrowserStackTunnelTests { + static readonly OperatingSystem os = Environment.OSVersion; + static readonly string homepath = os.Platform.ToString() == "Unix" ? + Environment.GetFolderPath(Environment.SpecialFolder.Personal) : + Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"); + static readonly string binaryName = os.Platform.ToString() == "Unix" ? "BrowserStackLocal-darwin-x64" : "BrowserStackLocal.exe"; private TunnelClass tunnel; [TestMethod] public void TestInitialState() @@ -33,8 +38,8 @@ public void TestBinaryPathOnNull() { tunnel = new TunnelClass(); tunnel.addBinaryPath(null); - string expectedPath = Path.Combine(Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"), ".browserstack"); - expectedPath = Path.Combine(expectedPath, "BrowserStackLocal.exe"); + string expectedPath = Path.Combine(homepath, ".browserstack"); + expectedPath = Path.Combine(expectedPath, binaryName); Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath); } [TestMethod] @@ -42,8 +47,8 @@ public void TestBinaryPathOnEmpty() { tunnel = new TunnelClass(); tunnel.addBinaryPath(""); - string expectedPath = Path.Combine(Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"), ".browserstack"); - expectedPath = Path.Combine(expectedPath, "BrowserStackLocal.exe"); + string expectedPath = Path.Combine(homepath, ".browserstack"); + expectedPath = Path.Combine(expectedPath, binaryName); Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath); } [TestMethod] @@ -55,18 +60,18 @@ public void TestBinaryPathOnFallback() Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath); tunnel.fallbackPaths(); - expectedPath = Path.Combine(Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"), ".browserstack"); - expectedPath = Path.Combine(expectedPath, "BrowserStackLocal.exe"); + expectedPath = Path.Combine(homepath, ".browserstack"); + expectedPath = Path.Combine(expectedPath, binaryName); Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath); tunnel.fallbackPaths(); expectedPath = Directory.GetCurrentDirectory(); - expectedPath = Path.Combine(expectedPath, "BrowserStackLocal.exe"); + expectedPath = Path.Combine(expectedPath, binaryName); Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath); tunnel.fallbackPaths(); expectedPath = Path.GetTempPath(); - expectedPath = Path.Combine(expectedPath, "BrowserStackLocal.exe"); + expectedPath = Path.Combine(expectedPath, binaryName); Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath); } [TestMethod] @@ -79,7 +84,7 @@ public void TestBinaryPathOnNoMoreFallback() tunnel.fallbackPaths(); Assert.Throws(typeof(Exception), new TestDelegate(testFallbackException), - "Binary not found or failed to launch. Make sure that BrowserStackLocal.exe is not already running." + "Binary not found or failed to launch. Make sure that BrowserStackLocal is not already running." ); } [TestMethod] diff --git a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs index 4da45ee..52898a2 100644 --- a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs +++ b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs @@ -15,7 +15,7 @@ public enum LocalState { Idle, Connecting, Connected, Error, Disconnected }; public class BrowserStackTunnel : IDisposable { static readonly OperatingSystem os = Environment.OSVersion; - static readonly string binaryName = os.Platform.ToString() == "Unix" ? "BrowserStackLocal" : "BrowserStackLocal.exe"; + static readonly string binaryName = os.Platform.ToString() == "Unix" ? "BrowserStackLocal-darwin-x64" : "BrowserStackLocal.exe"; static readonly string downloadURL = os.Platform.ToString() == "Unix" ? "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-darwin-x64" : "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal.exe"; From f5a0db5ead274f108552795b4bcf19f5d647cbbd Mon Sep 17 00:00:00 2001 From: AdityaHirapara Date: Mon, 14 Mar 2022 17:47:39 +0530 Subject: [PATCH 3/7] Change permission for mac binary after download --- .../BrowserStackLocal/BrowserStackTunnel.cs | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs index 52898a2..cd5c190 100644 --- a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs +++ b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs @@ -88,10 +88,27 @@ public void downloadBinary() throw new Exception("Error accessing file " + binaryAbsolute); } - DirectoryInfo dInfo = new DirectoryInfo(binaryAbsolute); - DirectorySecurity dSecurity = dInfo.GetAccessControl(); - dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow)); - dInfo.SetAccessControl(dSecurity); + if (os.Platform.ToString() == "Unix") + { + try + { + using (Process proc = Process.Start("/bin/bash", $"-c \"chmod 0755 {binaryAbsolute}\"")) + { + proc.WaitForExit(); + } + } + catch + { + throw new Exception("Error in changing permission for file " + binaryAbsolute); + } + } + else + { + DirectoryInfo dInfo = new DirectoryInfo(binaryAbsolute); + DirectorySecurity dSecurity = dInfo.GetAccessControl(); + dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow)); + dInfo.SetAccessControl(dSecurity); + } } public virtual void Run(string accessKey, string folder, string logFilePath, string processType) From 917b7a396f2d726c975180c54bc3b900f5ff3003 Mon Sep 17 00:00:00 2001 From: AdityaHirapara Date: Mon, 14 Mar 2022 18:18:25 +0530 Subject: [PATCH 4/7] Refactor --- .../BrowserStackLocal/BrowserStackTunnel.cs | 57 +++++++++++-------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs index cd5c190..9ee3147 100644 --- a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs +++ b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs @@ -14,12 +14,11 @@ public enum LocalState { Idle, Connecting, Connected, Error, Disconnected }; public class BrowserStackTunnel : IDisposable { - static readonly OperatingSystem os = Environment.OSVersion; - static readonly string binaryName = os.Platform.ToString() == "Unix" ? "BrowserStackLocal-darwin-x64" : "BrowserStackLocal.exe"; - static readonly string downloadURL = os.Platform.ToString() == "Unix" ? + static readonly string binaryName = isDarwin() ? "BrowserStackLocal-darwin-x64" : "BrowserStackLocal.exe"; + static readonly string downloadURL = isDarwin() ? "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-darwin-x64" : "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal.exe"; - static readonly string homepath = os.Platform.ToString() == "Unix" ? + static readonly string homepath = isDarwin() ? Environment.GetFolderPath(Environment.SpecialFolder.Personal) : Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"); public static readonly string[] basePaths = new string[] { @@ -38,6 +37,12 @@ public class BrowserStackTunnel : IDisposable Process process = null; + static Boolean isDarwin() + { + OperatingSystem os = Environment.OSVersion; + return os.Platform.ToString() == "Unix"; + } + public virtual void addBinaryPath(string binaryAbsolute) { if (binaryAbsolute == null || binaryAbsolute.Trim().Length == 0) @@ -71,35 +76,21 @@ public virtual void fallbackPaths() basePathsIndex++; binaryAbsolute = Path.Combine(basePaths[basePathsIndex], binaryName); } - public void downloadBinary() - { - string binaryDirectory = Path.Combine(this.binaryAbsolute, ".."); - //string binaryAbsolute = Path.Combine(binaryDirectory, binaryName); - - Directory.CreateDirectory(binaryDirectory); - - using (var client = new WebClient()) - { - client.DownloadFile(downloadURL, this.binaryAbsolute); - } - - if (!File.Exists(binaryAbsolute)) - { - throw new Exception("Error accessing file " + binaryAbsolute); - } - if (os.Platform.ToString() == "Unix") + public void modifyBinaryPermission() + { + if (isDarwin()) { try { - using (Process proc = Process.Start("/bin/bash", $"-c \"chmod 0755 {binaryAbsolute}\"")) + using (Process proc = Process.Start("/bin/bash", $"-c \"chmod 0755 {this.binaryAbsolute}\"")) { proc.WaitForExit(); } } catch { - throw new Exception("Error in changing permission for file " + binaryAbsolute); + throw new Exception("Error in changing permission for file " + this.binaryAbsolute); } } else @@ -111,6 +102,26 @@ public void downloadBinary() } } + public void downloadBinary() + { + string binaryDirectory = Path.Combine(this.binaryAbsolute, ".."); + //string binaryAbsolute = Path.Combine(binaryDirectory, binaryName); + + Directory.CreateDirectory(binaryDirectory); + + using (var client = new WebClient()) + { + client.DownloadFile(downloadURL, this.binaryAbsolute); + } + + if (!File.Exists(binaryAbsolute)) + { + throw new Exception("Error accessing file " + binaryAbsolute); + } + + modifyBinaryPermission(); + } + public virtual void Run(string accessKey, string folder, string logFilePath, string processType) { string arguments = "-d " + processType + " "; From 039862bacbf523d3c99e9ce0de50d097c222633a Mon Sep 17 00:00:00 2001 From: AdityaHirapara Date: Mon, 14 Mar 2022 18:18:57 +0530 Subject: [PATCH 5/7] Fix integration test to be run on mac --- .../BrowserStackLocalIntegrationTests/IntegrationTests.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs b/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs index c639d13..bc942af 100644 --- a/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs +++ b/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs @@ -11,6 +11,8 @@ namespace BrowserStackLocalIntegrationTests { public class IntegrationTests { + static readonly OperatingSystem os = Environment.OSVersion; + static readonly string binaryName = os.Platform.ToString() == "Unix" ? "BrowserStackLocal-darwin-x64" : "BrowserStackLocal.exe"; private static string username = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME"); private static string accesskey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY"); @@ -31,7 +33,7 @@ void startWithOptions() } Assert.DoesNotThrow(new TestDelegate(startWithOptions)); - Process[] binaryInstances = Process.GetProcessesByName("BrowserStackLocal"); + Process[] binaryInstances = Process.GetProcessesByName(binaryName); Assert.AreNotEqual(binaryInstances.Length, 0); IWebDriver driver; @@ -52,7 +54,7 @@ void startWithOptions() driver.Quit(); local.stop(); - binaryInstances = Process.GetProcessesByName("BrowserStackLocal"); + binaryInstances = Process.GetProcessesByName(binaryName); Assert.AreEqual(binaryInstances.Length, 0); } @@ -74,7 +76,7 @@ public void TestBinaryState() [TearDown] public void TestCleanup() { - foreach(Process p in Process.GetProcessesByName("BrowserStackLocal")) + foreach(Process p in Process.GetProcessesByName(binaryName)) { p.Kill(); } From 486cd6223474b75bc49d5fe9735ba024f08280b4 Mon Sep 17 00:00:00 2001 From: AdityaHirapara Date: Mon, 14 Mar 2022 18:29:12 +0530 Subject: [PATCH 6/7] Fix integration test --- .../BrowserStackLocalIntegrationTests/IntegrationTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs b/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs index bc942af..c0b1e39 100644 --- a/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs +++ b/BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs @@ -12,7 +12,7 @@ namespace BrowserStackLocalIntegrationTests public class IntegrationTests { static readonly OperatingSystem os = Environment.OSVersion; - static readonly string binaryName = os.Platform.ToString() == "Unix" ? "BrowserStackLocal-darwin-x64" : "BrowserStackLocal.exe"; + static readonly string binaryName = os.Platform.ToString() == "Unix" ? "BrowserStackLocal-darwin-x64" : "BrowserStackLocal"; private static string username = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME"); private static string accesskey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY"); From 6206a26fa73f19042b8f7f42c3a57f2528059572 Mon Sep 17 00:00:00 2001 From: AdityaHirapara Date: Tue, 6 Dec 2022 16:52:49 +0530 Subject: [PATCH 7/7] Throw error on disconnected state as well --- BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs index df9c3f7..fa319e7 100644 --- a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs +++ b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs @@ -192,6 +192,7 @@ private void RunProcess(string arguments, string processType) else if (connectionState.ToString().ToLower().Equals("disconnected")) { SetTunnelState(LocalState.Disconnected); + throw new Exception("Error while executing BrowserStackLocal " + processType + " " + e.Data); } else {