11package com .browserstack .local ;
22
33import org .apache .commons .io .FileUtils ;
4-
4+ import java .io .IOException ;
5+ import java .io .BufferedReader ;
6+ import java .io .InputStreamReader ;
57import java .io .File ;
68import java .net .URL ;
9+ import java .util .regex .Pattern ;
710
811class 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,48 @@ 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+ System .out .println ("Validating binary at " + binaryPath );
69+ Process process ;
70+
71+ try {
72+
73+ process = new ProcessBuilder (binaryPath ,"--version" ).start ();
74+
75+ BufferedReader stdoutbr = new BufferedReader (new InputStreamReader (process .getInputStream ()));
76+ String stdout ="" ,line ="" ;
77+
78+ while ((line = stdoutbr .readLine ()) != null ) {
79+ stdout += line ;
80+ }
81+ process .waitFor ();
82+
83+ boolean validBinary = Pattern .matches ("BrowserStack Local version \\ d+\\ .\\ d+" , stdout );
84+
85+ return binaryValid ;
86+ }catch (IOException ex ){
87+ throw new LocalException (ex .toString ());
88+ }
89+ catch (InterruptedException ex ){
90+ throw new LocalException (ex .toString ());
91+ }
92+ }
93+
4894 private void getBinary () throws LocalException {
4995 String destParentDir = getAvailableDirectory ();
5096 binaryPath = destParentDir + "/BrowserStackLocal" ;
0 commit comments