mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-18 07:18:38 +02:00
BUG: Fix missed cases in offline server handling (#2495)
There were two large holes in the existing offline server handling: 1. It didn't include IPs, so scripts that used IPs instead of hostnames would get exceptions thrown for "server not found." 2. Coverage was very low for non-Darknet APIs. Maybe most of them don't need to be covered, but many obvious ones like "ps", "killall" and "hasRootAccess" were missing. IMO the only reliable answer is one that enforces *all* are covered via the type system. To accomplish the second part, helpers.getServer() was changed to return null when a server is offline. This intentionally breaks a lot of its utility, which was to return a server unconditionally. To compensate, its utility was increased - it now also does unknown argument processing, allowing it to subsume a common line that all callers were repeating. Some callers switched to ctx.workerScript.getServer(), because they didn't actually need to be using helpers.getServer(). Similarly, a few callsites switched to GetServerOrThrow(), for the cases where it should be guaranteed that the server is valid. The rest are returning a default/failure response when the server is offline. (Except for contracts, which threw on failure already anyway.)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { AugmentationName, CompletedProgramName } from "@enums";
|
||||
import { Player } from "@player";
|
||||
import type { DarknetResult } from "@nsdefs";
|
||||
import { PlayerOwnedAugmentation } from "../../../src/Augmentation/PlayerOwnedAugmentation";
|
||||
import { addCacheToServer } from "../../../src/DarkNet/effects/cacheFiles";
|
||||
import { getDarkscapeNavigator } from "../../../src/DarkNet/effects/effects";
|
||||
@@ -35,8 +36,9 @@ import type { Result } from "@nsdefs";
|
||||
import { assertNonNullish } from "../../../src/utils/TypeAssertion";
|
||||
|
||||
const hostnameOfNonExistentServer = "fake-server";
|
||||
const errorMessageForNonExistentServer = `Server ${hostnameOfNonExistentServer} does not exist.`;
|
||||
const errorMessageForNonExistentServer = `Invalid host: '${hostnameOfNonExistentServer}'`;
|
||||
const hostnameForOfflineServer = "darknet-offline-server";
|
||||
const ipForOfflineServer = "0.0.0.0";
|
||||
|
||||
fixDoImportIssue();
|
||||
|
||||
@@ -45,7 +47,7 @@ beforeAll(() => {
|
||||
initStockMarket();
|
||||
});
|
||||
beforeEach(() => {
|
||||
DarknetState.offlineServers = [];
|
||||
DarknetState.offlineServers = new Set();
|
||||
setupBasicTestingEnvironment({ purchasePServer: true, purchaseHacknetServer: true });
|
||||
Player.sourceFiles.set(15, 1);
|
||||
getDarkscapeNavigator();
|
||||
@@ -1060,81 +1062,96 @@ describe("Non-darkweb darknet server", () => {
|
||||
|
||||
describe("Offline darknet server", () => {
|
||||
beforeEach(() => {
|
||||
DarknetState.offlineServers.push(hostnameForOfflineServer);
|
||||
DarknetState.offlineServers.add(hostnameForOfflineServer);
|
||||
DarknetState.offlineServers.add(ipForOfflineServer);
|
||||
});
|
||||
test("authenticate from home", async () => {
|
||||
const ns = getNsOnHome();
|
||||
const result = await ns.dnet.authenticate(hostnameForOfflineServer, "");
|
||||
async function testIpAndHostname(func: (host: string) => Promise<DarknetResult>) {
|
||||
let result = await func(hostnameForOfflineServer);
|
||||
expect(result.success).toStrictEqual(false);
|
||||
expect(result.code).toStrictEqual(ResponseCodeEnum.ServiceUnavailable);
|
||||
|
||||
result = await func(ipForOfflineServer);
|
||||
expect(result.success).toStrictEqual(false);
|
||||
expect(result.code).toStrictEqual(ResponseCodeEnum.ServiceUnavailable);
|
||||
}
|
||||
test("authenticate from home", async () => {
|
||||
const ns = getNsOnHome();
|
||||
await testIpAndHostname((host) => ns.dnet.authenticate(host, ""));
|
||||
});
|
||||
test("authenticate itself", async () => {
|
||||
const ns = getNsOnDarkWeb();
|
||||
const result = await ns.dnet.authenticate(hostnameForOfflineServer, "");
|
||||
expect(result.success).toStrictEqual(false);
|
||||
expect(result.code).toStrictEqual(ResponseCodeEnum.ServiceUnavailable);
|
||||
await testIpAndHostname((host) => ns.dnet.authenticate(host, ""));
|
||||
});
|
||||
test("connectToSession from home", () => {
|
||||
test("connectToSession from home", async () => {
|
||||
const ns = getNsOnHome();
|
||||
const result = ns.dnet.connectToSession(hostnameForOfflineServer, "");
|
||||
expect(result.success).toStrictEqual(false);
|
||||
expect(result.code).toStrictEqual(ResponseCodeEnum.ServiceUnavailable);
|
||||
await testIpAndHostname((host) => Promise.resolve(ns.dnet.connectToSession(host, "")));
|
||||
});
|
||||
test("heartbleed from home", async () => {
|
||||
const ns = getNsOnHome();
|
||||
const result = await ns.dnet.heartbleed(hostnameForOfflineServer);
|
||||
expect(result.success).toStrictEqual(false);
|
||||
expect(result.code).toStrictEqual(ResponseCodeEnum.ServiceUnavailable);
|
||||
await testIpAndHostname((host) => ns.dnet.heartbleed(host));
|
||||
});
|
||||
test("getServer", () => {
|
||||
const ns = getNsOnDarkWeb();
|
||||
const server = ns.getServer(hostnameForOfflineServer);
|
||||
if (!("isOnline" in server)) {
|
||||
throw new Error("getServer does not return DarknetServerData");
|
||||
}
|
||||
expect(server.isOnline).toStrictEqual(false);
|
||||
let server = ns.getServer(hostnameForOfflineServer);
|
||||
expect(server).toHaveProperty("isOnline", false);
|
||||
expect(server.hostname).toBe(hostnameForOfflineServer);
|
||||
expect(server.ip).toBe("");
|
||||
|
||||
server = ns.getServer(ipForOfflineServer);
|
||||
expect(server).toHaveProperty("isOnline", false);
|
||||
expect(server.hostname).toBe("");
|
||||
expect(server.ip).toBe(ipForOfflineServer);
|
||||
});
|
||||
test("getServerAuthDetails", () => {
|
||||
const ns = getNsOnDarkWeb();
|
||||
const authDetails = ns.dnet.getServerAuthDetails(hostnameForOfflineServer);
|
||||
let authDetails = ns.dnet.getServerAuthDetails(hostnameForOfflineServer);
|
||||
expect(authDetails.isOnline).toStrictEqual(false);
|
||||
|
||||
authDetails = ns.dnet.getServerAuthDetails(ipForOfflineServer);
|
||||
expect(authDetails.isOnline).toStrictEqual(false);
|
||||
});
|
||||
test("packetCapture from home", async () => {
|
||||
const ns = getNsOnHome();
|
||||
const result = await ns.dnet.packetCapture(hostnameForOfflineServer);
|
||||
expect(result.success).toStrictEqual(false);
|
||||
expect(result.code).toStrictEqual(ResponseCodeEnum.ServiceUnavailable);
|
||||
await testIpAndHostname((host) => ns.dnet.packetCapture(host));
|
||||
});
|
||||
test("induceServerMigration", async () => {
|
||||
const ns = getNsOnDarkWeb();
|
||||
const result = await ns.dnet.induceServerMigration(hostnameForOfflineServer);
|
||||
expect(result.success).toStrictEqual(false);
|
||||
expect(result.code).toStrictEqual(ResponseCodeEnum.ServiceUnavailable);
|
||||
await testIpAndHostname((host) => ns.dnet.induceServerMigration(host));
|
||||
});
|
||||
test("isDarknetServer", () => {
|
||||
const ns = getNsOnDarkWeb();
|
||||
const result = ns.dnet.isDarknetServer(hostnameForOfflineServer);
|
||||
let result = ns.dnet.isDarknetServer(hostnameForOfflineServer);
|
||||
expect(result).toStrictEqual(false);
|
||||
|
||||
result = ns.dnet.isDarknetServer(ipForOfflineServer);
|
||||
expect(result).toStrictEqual(false);
|
||||
});
|
||||
test("memoryReallocation", async () => {
|
||||
const ns = getNsOnDarkWeb();
|
||||
const result = await ns.dnet.memoryReallocation(hostnameForOfflineServer);
|
||||
expect(result.success).toStrictEqual(false);
|
||||
expect(result.code).toStrictEqual(ResponseCodeEnum.ServiceUnavailable);
|
||||
await testIpAndHostname((host) => ns.dnet.memoryReallocation(host));
|
||||
});
|
||||
test("getBlockedRam", () => {
|
||||
const ns = getNsOnDarkWeb();
|
||||
const result = ns.dnet.getBlockedRam(hostnameForOfflineServer);
|
||||
let result = ns.dnet.getBlockedRam(hostnameForOfflineServer);
|
||||
expect(result).toStrictEqual(0);
|
||||
|
||||
result = ns.dnet.getBlockedRam(ipForOfflineServer);
|
||||
expect(result).toStrictEqual(0);
|
||||
});
|
||||
test("getDepth", () => {
|
||||
const ns = getNsOnDarkWeb();
|
||||
const result = ns.dnet.getDepth(hostnameForOfflineServer);
|
||||
let result = ns.dnet.getDepth(hostnameForOfflineServer);
|
||||
expect(result).toStrictEqual(-1);
|
||||
|
||||
result = ns.dnet.getDepth(ipForOfflineServer);
|
||||
expect(result).toStrictEqual(-1);
|
||||
});
|
||||
test("getServerRequiredCharismaLevel", () => {
|
||||
const ns = getNsOnDarkWeb();
|
||||
const result = ns.dnet.getServerRequiredCharismaLevel(hostnameForOfflineServer);
|
||||
let result = ns.dnet.getServerRequiredCharismaLevel(hostnameForOfflineServer);
|
||||
expect(result).toStrictEqual(-1);
|
||||
|
||||
result = ns.dnet.getServerRequiredCharismaLevel(ipForOfflineServer);
|
||||
expect(result).toStrictEqual(-1);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user