Skip to content

Checking binary after download and retrying once if it is corrupt. #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 3, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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