Skip to content

Commit 2a69f8c

Browse files
authored
Merge pull request #18 from browserstack/check_corrupt_binary
Checking binary after download and retrying once if it is corrupt.
2 parents 98402b1 + 379d1a9 commit 2a69f8c

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/main/java/com/browserstack/local/LocalBinary.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.browserstack.local;
22

33
import org.apache.commons.io.FileUtils;
4-
4+
import java.io.IOException;
5+
import java.io.BufferedReader;
6+
import java.io.InputStreamReader;
57
import java.io.File;
68
import java.net.URL;
9+
import java.util.regex.Pattern;
710

811
class LocalBinary {
912

@@ -24,6 +27,7 @@ class LocalBinary {
2427
LocalBinary() throws LocalException {
2528
initialize();
2629
getBinary();
30+
checkBinary();
2731
}
2832

2933
private void initialize() throws LocalException {
@@ -45,6 +49,46 @@ private void initialize() throws LocalException {
4549
httpPath = BIN_URL + binFileName;
4650
}
4751

52+
private void checkBinary() throws LocalException{
53+
boolean binaryWorking = validateBinary();
54+
55+
if(!binaryWorking){
56+
File binary_file = new File(binaryPath);
57+
if (binary_file.exists()) {
58+
binary_file.delete();
59+
}
60+
getBinary();
61+
if(!validateBinary()){
62+
throw new LocalException("BrowserStackLocal binary is corrupt");
63+
}
64+
}
65+
}
66+
67+
private boolean validateBinary() throws LocalException{
68+
Process process;
69+
try {
70+
71+
process = new ProcessBuilder(binaryPath,"--version").start();
72+
73+
BufferedReader stdoutbr = new BufferedReader(new InputStreamReader(process.getInputStream()));
74+
String stdout="",line="";
75+
76+
while ((line = stdoutbr.readLine()) != null) {
77+
stdout += line;
78+
}
79+
process.waitFor();
80+
81+
boolean validBinary = Pattern.matches("BrowserStack Local version \\d+\\.\\d+", stdout);
82+
83+
return validBinary;
84+
}catch(IOException ex){
85+
throw new LocalException(ex.toString());
86+
}
87+
catch(InterruptedException ex){
88+
throw new LocalException(ex.toString());
89+
}
90+
}
91+
4892
private void getBinary() throws LocalException {
4993
String destParentDir = getAvailableDirectory();
5094
binaryPath = destParentDir + "/BrowserStackLocal";

0 commit comments

Comments
 (0)