|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.zaproxy.zap.extension.selenium.internal; |
| 19 | + |
| 20 | +import static java.util.stream.Collectors.toList; |
| 21 | +import static org.openqa.selenium.Platform.MAC; |
| 22 | +import static org.openqa.selenium.Platform.UNIX; |
| 23 | +import static org.openqa.selenium.Platform.WINDOWS; |
| 24 | + |
| 25 | +import java.io.File; |
| 26 | +import java.io.IOException; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Optional; |
| 33 | +import java.util.stream.Collectors; |
| 34 | +import java.util.stream.Stream; |
| 35 | +import org.openqa.selenium.Platform; |
| 36 | +import org.openqa.selenium.WebDriverException; |
| 37 | +import org.openqa.selenium.firefox.FirefoxDriver; |
| 38 | +import org.openqa.selenium.os.ExecutableFinder; |
| 39 | + |
| 40 | +/** |
| 41 | + * A wrapper around Firefox's binary. This allows us to locate the binary in a portable way. |
| 42 | + */ |
| 43 | +public class FirefoxBinary { |
| 44 | + |
| 45 | + private final String executable; |
| 46 | + |
| 47 | + public FirefoxBinary() { |
| 48 | + String systemBinary = locateFirefoxBinaryFromSystemProperty(); |
| 49 | + if (systemBinary != null) { |
| 50 | + executable = systemBinary; |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + String platformBinary = locateFirefoxBinariesFromPlatform().findFirst().orElse(null); |
| 55 | + if (platformBinary != null) { |
| 56 | + executable = platformBinary; |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + throw new WebDriverException( |
| 61 | + "Cannot find firefox binary in PATH. " |
| 62 | + + "Make sure firefox is installed. OS appears to be: " |
| 63 | + + Platform.getCurrent()); |
| 64 | + } |
| 65 | + |
| 66 | + public String getFile() { |
| 67 | + return executable; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Locates the firefox binary from a system property. Will throw an exception if the binary cannot |
| 72 | + * be found. |
| 73 | + */ |
| 74 | + static String locateFirefoxBinaryFromSystemProperty() { |
| 75 | + String binaryName = System.getProperty(FirefoxDriver.SystemProperty.BROWSER_BINARY); |
| 76 | + if (binaryName == null) return null; |
| 77 | + |
| 78 | + File binary = new File(binaryName); |
| 79 | + if (binary.exists() && !binary.isDirectory()) return binary.getAbsolutePath(); |
| 80 | + |
| 81 | + Platform current = Platform.getCurrent(); |
| 82 | + if (current.is(WINDOWS)) { |
| 83 | + if (!binaryName.endsWith(".exe")) { |
| 84 | + binaryName += ".exe"; |
| 85 | + } |
| 86 | + |
| 87 | + } else if (current.is(MAC)) { |
| 88 | + if (!binaryName.endsWith(".app")) { |
| 89 | + binaryName += ".app"; |
| 90 | + } |
| 91 | + binaryName += "/Contents/MacOS/firefox"; |
| 92 | + } |
| 93 | + |
| 94 | + binary = new File(binaryName); |
| 95 | + if (binary.exists()) return binary.getAbsolutePath(); |
| 96 | + |
| 97 | + throw new WebDriverException( |
| 98 | + String.format( |
| 99 | + "'%s' property set, but unable to locate the requested binary: %s", |
| 100 | + FirefoxDriver.SystemProperty.BROWSER_BINARY, binaryName)); |
| 101 | + } |
| 102 | + |
| 103 | + /** Locates the firefox binary by platform. */ |
| 104 | + private static Stream<String> locateFirefoxBinariesFromPlatform() { |
| 105 | + List<String> executables = new ArrayList<>(); |
| 106 | + |
| 107 | + Platform current = Platform.getCurrent(); |
| 108 | + if (current.is(WINDOWS)) { |
| 109 | + executables.addAll( |
| 110 | + Stream.of( |
| 111 | + "Mozilla Firefox\\firefox.exe", |
| 112 | + "Firefox Developer Edition\\firefox.exe", |
| 113 | + "Nightly\\firefox.exe") |
| 114 | + .map(FirefoxBinary::getPathsInProgramFiles) |
| 115 | + .flatMap(List::stream) |
| 116 | + .map(File::new) |
| 117 | + .filter(File::exists) |
| 118 | + .map(File::getAbsolutePath) |
| 119 | + .collect(toList())); |
| 120 | + |
| 121 | + } else if (current.is(MAC)) { |
| 122 | + // system |
| 123 | + File binary = new File("/Applications/Firefox.app/Contents/MacOS/firefox"); |
| 124 | + if (binary.exists()) { |
| 125 | + executables.add(binary.getAbsolutePath()); |
| 126 | + } |
| 127 | + |
| 128 | + // user home |
| 129 | + binary = new File(System.getProperty("user.home") + binary.getAbsolutePath()); |
| 130 | + if (binary.exists()) { |
| 131 | + executables.add(binary.getAbsolutePath()); |
| 132 | + } |
| 133 | + |
| 134 | + } else if (current.is(UNIX)) { |
| 135 | + String systemFirefoxBin = new ExecutableFinder().find("firefox"); |
| 136 | + if (systemFirefoxBin != null) { |
| 137 | + executables.add(new File(systemFirefoxBin).getAbsolutePath()); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + String systemFirefox = new ExecutableFinder().find("firefox"); |
| 142 | + if (systemFirefox != null) { |
| 143 | + Path firefoxPath = new File(systemFirefox).toPath(); |
| 144 | + if (Files.isSymbolicLink(firefoxPath)) { |
| 145 | + try { |
| 146 | + Path realPath = firefoxPath.toRealPath(); |
| 147 | + File file = realPath.getParent().resolve("firefox").toFile(); |
| 148 | + if (file.exists()) { |
| 149 | + executables.add(file.getAbsolutePath()); |
| 150 | + } |
| 151 | + } catch (IOException e) { |
| 152 | + // ignore this path |
| 153 | + } |
| 154 | + |
| 155 | + } else { |
| 156 | + executables.add(new File(systemFirefox).getAbsolutePath()); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return executables.stream(); |
| 161 | + } |
| 162 | + |
| 163 | + private static List<String> getPathsInProgramFiles(final String childPath) { |
| 164 | + return Stream.of(getProgramFilesPath(), getProgramFiles86Path()) |
| 165 | + .map(parent -> new File(parent, childPath).getAbsolutePath()) |
| 166 | + .collect(Collectors.toList()); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Returns the path to the Windows Program Files. On non-English versions, this is not necessarily |
| 171 | + * "C:\Program Files". |
| 172 | + * |
| 173 | + * @return the path to the Windows Program Files |
| 174 | + */ |
| 175 | + private static String getProgramFilesPath() { |
| 176 | + return getEnvVarPath("ProgramFiles", "C:\\Program Files").replace(" (x86)", ""); |
| 177 | + } |
| 178 | + |
| 179 | + private static String getProgramFiles86Path() { |
| 180 | + return getEnvVarPath("ProgramFiles(x86)", "C:\\Program Files (x86)"); |
| 181 | + } |
| 182 | + |
| 183 | + private static String getEnvVarPath(final String envVar, final String defaultValue) { |
| 184 | + return getEnvVarIgnoreCase(envVar) |
| 185 | + .map(File::new) |
| 186 | + .filter(File::exists) |
| 187 | + .map(File::getAbsolutePath) |
| 188 | + .orElseGet(() -> new File(defaultValue).getAbsolutePath()); |
| 189 | + } |
| 190 | + |
| 191 | + private static Optional<String> getEnvVarIgnoreCase(String var) { |
| 192 | + return System.getenv().entrySet().stream() |
| 193 | + .filter(e -> e.getKey().equalsIgnoreCase(var)) |
| 194 | + .findFirst() |
| 195 | + .map(Map.Entry::getValue); |
| 196 | + } |
| 197 | +} |
0 commit comments