1
1
package com .browserstack .local ;
2
2
3
3
import org .apache .commons .io .FileUtils ;
4
-
4
+ import java .io .IOException ;
5
+ import java .io .BufferedReader ;
6
+ import java .io .InputStreamReader ;
5
7
import java .io .File ;
6
8
import java .net .URL ;
9
+ import java .util .regex .Pattern ;
7
10
8
11
class LocalBinary {
9
12
@@ -24,6 +27,7 @@ class LocalBinary {
24
27
LocalBinary () throws LocalException {
25
28
initialize ();
26
29
getBinary ();
30
+ checkBinary ();
27
31
}
28
32
29
33
private void initialize () throws LocalException {
@@ -45,6 +49,46 @@ private void initialize() throws LocalException {
45
49
httpPath = BIN_URL + binFileName ;
46
50
}
47
51
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
+
48
92
private void getBinary () throws LocalException {
49
93
String destParentDir = getAvailableDirectory ();
50
94
binaryPath = destParentDir + "/BrowserStackLocal" ;
0 commit comments