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,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