harbor run -t openthoughts/tasktrove-inferredbugs-sandboxes-verifier-inferredbugs-2970harbor download openthoughts/tasktrove-inferredbugs-sandboxes-verifier-inferredbugs-2970Project: AppiumTestDistribution Bug ID: 63 Language: java
Before (Buggy File):
package com.appium.manager;
import com.appium.utils.Api;
import com.appium.utils.CapabilityManager;
import com.appium.utils.OSType;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thoughtworks.device.Device;
import okhttp3.Response;
import org.json.JSONObject;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class RemoteAppiumManager implements IAppiumManager {
@Override
public void destroyAppiumNode(String host) throws IOException {
new Api().getResponse("http://" + host + ":4567"
+ "/appium/stop").body().string();
}
@Override
public String getRemoteWDHubIP(String host) throws IOException {
String hostIP = "http://" + host;
String appiumRunningPort = new JSONObject(new Api().getResponse(hostIP + ":4567"
+ "/appium/isRunning").body().string()).get("port").toString();
return hostIP + ":" + appiumRunningPort + "/wd/hub";
}
@Override
public void startAppiumServer(String host) throws Exception {
System.out.println(
"**************************************************************************\n");
System.out.println("Starting Appium Server on host " + host);
System.out.println(
"**************************************************************************\n");
String serverPath = CapabilityManager.getInstance().getAppiumServerPath(host);
String serverPort = CapabilityManager.getInstance().getAppiumServerPort(host);
if (serverPath == null
&& serverPort == null) {
System.out.println("Picking Default Path for AppiumServiceBuilder");
new Api().getResponse("http://" + host + ":4567"
+ "/appium/start").body().string();
} else if (serverPath != null && serverPort != null) {
System.out.println("Picking UserSpecified Path & Port for AppiumServiceBuilder");
new Api().getResponse("http://" + host + ":4567"
+ "/appium/start?URL=" + serverPath
+ "&PORT=" + serverPort).body().string();
} else if (serverPath != null) {
System.out.println("Picking UserSpecified Path "
+ "& Using default Port for AppiumServiceBuilder");
new Api().getResponse("http://" + host + ":4567"
+ "/appium/start?URL=" + serverPath).body().string();
} else if (serverPort != null) {
System.out.println("Picking Default Path & User Port for AppiumServiceBuilder");
new Api().getResponse("http://" + host + ":4567"
+ "/appium/start?PORT=" + serverPort).body().string();
}
boolean status = Boolean.getBoolean(new JSONObject(new Api().getResponse("http://" + host + ":4567"
+ "/appium/isRunning").body().string()).get("status").toString());
if (status) {
System.out.println(
"***************************************************************\n");
System.out.println("Appium Server started successfully on " + host);
System.out.println(
"****************************************************************\n");
}
}
@Override
public List<Device> getDevices(String machineIP, String platform) throws Exception {
ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
List<Device> devices = new ArrayList<>();
if (platform.equalsIgnoreCase(OSType.ANDROID.name())
|| platform.equalsIgnoreCase(OSType.BOTH.name())) {
List<Device> androidDevices = Arrays.asList(mapper.readValue(new URL(
"http://" + machineIP + ":4567/devices/android"),
Device[].class));
Optional.ofNullable(androidDevices).ifPresent(devices::addAll);
}
if (platform.equalsIgnoreCase(OSType.iOS.name())
|| platform.equalsIgnoreCase(OSType.BOTH.name())) {
if (CapabilityManager.getInstance().isApp()) {
if (CapabilityManager.getInstance().isSimulatorAppPresentInCapsJson()) {
List<Device> bootedSims = Arrays.asList(mapper.readValue(new URL(
"http://" + machineIP + ":4567/devices/ios/bootedSims"),
Device[].class));
Optional.ofNullable(bootedSims).ifPresent(devices::addAll);
}
if (CapabilityManager.getInstance().isRealDeviceAppPresentInCapsJson()) {
List<Device> iOSRealDevices = Arrays.asList(mapper.readValue(new URL(
"http://" + machineIP + ":4567/devices/ios/realDevices"),
Device[].class));
Optional.ofNullable(iOSRealDevices).ifPresent(devices::addAll);
}
} else {
List<Device> iOSDevices = Arrays.asList(mapper.readValue(new URL(
"http://" + machineIP + ":4567/devices/ios/realDevices"),
Device[].class));
Optional.ofNullable(iOSDevices).ifPresent(devices::addAll);
}
}
return devices;
}
@Override
public Device getSimulator(String machineIP, String deviceName, String os) throws IOException {
ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String url = String.format("http://%s:4567/device/ios/simulator"
+ "?simulatorName=%s&simulatorOSVersion=%s",
machineIP, URLEncoder.encode(deviceName, "UTF-8"),
URLEncoder.encode(os, "UTF-8"));
Device device = mapper.readValue(new URL(url),
Device.class);
return device;
}
@Override
public int getAvailablePort(String hostMachine) throws IOException {
String url = String.format("http://%s:4567/machine/availablePort", hostMachine);
Response response = new Api().getResponse(url);
return Integer.parseInt(response.body().string());
}
@Override
public int startIOSWebKitProxy(String host) throws Exception {
int port = getAvailablePort(host);
ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String url = String.format("http://%s:4567/devices/ios/webkitproxy/start"
+ "?udid=%s&port=%s", host,
AppiumDeviceManager.getAppiumDevice().getDevice().getUdid(),
port);
Response response = new Api().getResponse(url);
AppiumDeviceManager.getAppiumDevice().setWebkitProcessID(response.body().string());
return port;
}
@Override
public void destoryIOSWebKitProxy(String host) throws Exception {
if (AppiumDeviceManager.getAppiumDevice().getWebkitProcessID() != null) {
ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String url = String.format("http://%s:4567/devices/ios/webkitproxy/stop"
+ "?processID=%s", host,
AppiumDeviceManager.getAppiumDevice().getWebkitProcessID());
new Api().getResponse(url);
AppiumDeviceManager.getAppiumDevice().setWebkitProcessID(null);
}
}
}
PROVER
ERROR
NULL_DEREFERENCE
object returned by __new(com.appium.utils.Api).getResponse(__new(java.lang.StringBuilder).append("http://").append(host).append(":4567/appium/start?URL=").append(serverPath).toString()).body() at line 60.
HIGH
user
void RemoteAppiumManager.startAppiumServer(String)
com.appium.manager.RemoteAppiumManager.startAppiumServer(java.lang.String):void.b7751a79d9bad4033b77786f9f0fc7a1
src/main/java/com/appium/manager/RemoteAppiumManager.java
RemoteAppiumManager.java|startAppiumServer|NULL_DEREFERENCE
21bbf00bf52cbb29c85ade37dbbc751f
2ed7a5b0817f7f7df30803b1b6081095
Null Dereference
Fix the bug in the code.
Write the complete, corrected version of the file to: /app/src/main/java/com/appium/manager/RemoteAppiumManager.java.
The directory does not exist yet. You must create it and write the file:
mkdir -p /app/src/main/java/com/appium/manager
cat > /app/src/main/java/com/appium/manager/RemoteAppiumManager.java << 'ENDOFFILE'
<full corrected file content here>
ENDOFFILE