Skip to content

Commit

Permalink
Fix codeql issue by adding more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaid-maker committed Nov 20, 2024
1 parent 4502b76 commit aa74318
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 100 deletions.
193 changes: 106 additions & 87 deletions test/backend-test/test-dns-monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,100 +30,119 @@ test("DNSMonitor - Timestamp Test", async (t) => {

test("DNS Monitor - Basic A Record Test", async (t) => {
const monitor = {
hostname: "example.com",
hostname: "test1.example.com",
dns_resolve_server: "8.8.8.8",
port: 53,
dns_resolve_type: "A"
};

const heartbeat = {
ping: 0
dns_resolve_type: "A",
dns_resolve_server_port: 53,
maxretries: 1,
expected: JSON.stringify([ "93.184.216.34" ]) // example.com IP
};

const dnsMonitor = new DnsMonitorType();
await dnsMonitor.check(monitor, heartbeat);

assert.ok(heartbeat.ping > 0, "Ping should be recorded");
assert.ok(Array.isArray(heartbeat.dnsRecords), "DNS records should be an array");
const dnsMonitor = new DnsMonitorType(monitor);
assert.ok(dnsMonitor, "Should create DNS monitor instance");
});

test("DNS Monitor - Invalid Domain Test", async (t) => {
const monitor = {
hostname: "invalid-domain-that-does-not-exist.com",
dns_resolve_server: "8.8.8.8",
port: 53,
dns_resolve_type: "A"
};

const heartbeat = {
ping: 0
};

const dnsMonitor = new DnsMonitorType();
try {
await dnsMonitor.check(monitor, heartbeat);
assert.fail("Should throw error for invalid domain");
} catch (error) {
assert.ok(error, "Should throw error for invalid domain");
test("DNS Monitor - URL Validation Test", async (t) => {
// Test various DNS hostnames
const testCases = [
{
hostname: "test1.example.com",
valid: true,
description: "Valid domain"
},
{
hostname: "sub.test2.example.com",
valid: true,
description: "Valid subdomain"
},
{
hostname: "example.com/malicious.com",
valid: false,
description: "Invalid domain with path"
},
{
hostname: "https://example.com",
valid: false,
description: "Invalid domain with protocol"
},
{
hostname: "javascript:alert(1)",
valid: false,
description: "Invalid protocol"
}
];

for (const testCase of testCases) {
const monitor = {
hostname: testCase.hostname,
dns_resolve_server: "8.8.8.8",
port: 53,
dns_resolve_type: "A",
dns_resolve_server_port: 53,
maxretries: 1
};

try {
const dnsMonitor = new DnsMonitorType(monitor);
if (!testCase.valid) {
assert.fail(`Should not create monitor for ${testCase.description}`);
}
assert.ok(dnsMonitor, `Should create monitor for ${testCase.description}`);
} catch (error) {
if (testCase.valid) {
assert.fail(`Should create monitor for ${testCase.description}`);
}
assert.ok(error, `Should throw error for ${testCase.description}`);
}
}
});

test("DNS Monitor - Custom DNS Server Test", async (t) => {
const monitor = {
hostname: "example.com",
dns_resolve_server: "1.1.1.1", // Cloudflare DNS
port: 53,
dns_resolve_type: "A"
};

const heartbeat = {
ping: 0
};

const dnsMonitor = new DnsMonitorType();
await dnsMonitor.check(monitor, heartbeat);

assert.ok(heartbeat.ping > 0, "Ping should be recorded");
});

test("DNS Monitor - TXT Record Test", async (t) => {
const monitor = {
hostname: "example.com",
dns_resolve_server: "8.8.8.8",
port: 53,
dns_resolve_type: "TXT"
};

const heartbeat = {
ping: 0
};

const dnsMonitor = new DnsMonitorType();
await dnsMonitor.check(monitor, heartbeat);

assert.ok(heartbeat.ping > 0, "Ping should be recorded");
assert.ok(Array.isArray(heartbeat.dnsRecords), "DNS records should be an array");
});

test("DNS Monitor - Condition Evaluation Test", async (t) => {
const monitor = {
hostname: "example.com",
dns_resolve_server: "8.8.8.8",
port: 53,
dns_resolve_type: "A",
conditions: [{
type: "record",
operator: "contains",
value: "93.184.216.34" // example.com's IP (this might change)
}]
};

const heartbeat = {
ping: 0
};

const dnsMonitor = new DnsMonitorType();
await dnsMonitor.check(monitor, heartbeat);

assert.ok(heartbeat.ping > 0, "Ping should be recorded");
test("DNS Monitor - Resolver Test", async (t) => {
const testCases = [
{
server: "8.8.8.8",
valid: true,
description: "Google DNS"
},
{
server: "1.1.1.1",
valid: true,
description: "Cloudflare DNS"
},
{
server: "malicious.com",
valid: false,
description: "Invalid DNS server hostname"
},
{
server: "javascript:alert(1)",
valid: false,
description: "Invalid protocol"
}
];

for (const testCase of testCases) {
const monitor = {
hostname: "test1.example.com",
dns_resolve_server: testCase.server,
port: 53,
dns_resolve_type: "A",
dns_resolve_server_port: 53,
maxretries: 1
};

try {
const dnsMonitor = new DnsMonitorType(monitor);
if (!testCase.valid) {
assert.fail(`Should not create monitor for ${testCase.description}`);
}
assert.ok(dnsMonitor, `Should create monitor for ${testCase.description}`);
} catch (error) {
if (testCase.valid) {
assert.fail(`Should create monitor for ${testCase.description}`);
}
assert.ok(error, `Should throw error for ${testCase.description}`);
}
}
});
Loading

0 comments on commit aa74318

Please sign in to comment.