Skip to content

Commit

Permalink
Merge pull request #18 from browserstack/check_corrupt_binary
Browse files Browse the repository at this point in the history
Checking binary after download and retrying once if it is corrupt.
  • Loading branch information
AnkurGel authored Jan 3, 2017
2 parents 98402b1 + 379d1a9 commit 2a69f8c
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/main/java/com/browserstack/local/LocalBinary.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.browserstack.local;

import org.apache.commons.io.FileUtils;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.File;
import java.net.URL;
import java.util.regex.Pattern;

class LocalBinary {

Expand All @@ -24,6 +27,7 @@ class LocalBinary {
LocalBinary() throws LocalException {
initialize();
getBinary();
checkBinary();
}

private void initialize() throws LocalException {
Expand All @@ -45,6 +49,46 @@ private void initialize() throws LocalException {
httpPath = BIN_URL + binFileName;
}

private void checkBinary() throws LocalException{
boolean binaryWorking = validateBinary();

if(!binaryWorking){
File binary_file = new File(binaryPath);
if (binary_file.exists()) {
binary_file.delete();
}
getBinary();
if(!validateBinary()){
throw new LocalException("BrowserStackLocal binary is corrupt");
}
}
}

private boolean validateBinary() throws LocalException{
Process process;
try {

process = new ProcessBuilder(binaryPath,"--version").start();

BufferedReader stdoutbr = new BufferedReader(new InputStreamReader(process.getInputStream()));
String stdout="",line="";

while ((line = stdoutbr.readLine()) != null) {
stdout += line;
}
process.waitFor();

boolean validBinary = Pattern.matches("BrowserStack Local version \\d+\\.\\d+", stdout);

return validBinary;
}catch(IOException ex){
throw new LocalException(ex.toString());
}
catch(InterruptedException ex){
throw new LocalException(ex.toString());
}
}

private void getBinary() throws LocalException {
String destParentDir = getAvailableDirectory();
binaryPath = destParentDir + "/BrowserStackLocal";
Expand Down

0 comments on commit 2a69f8c

Please sign in to comment.