Skip to content

Commit 7d5ff58

Browse files
committed
add more tests covering Nmap::buildCommand() and fix tests broken by introduction of -- into args (see #11)
1 parent 1d92463 commit 7d5ff58

3 files changed

Lines changed: 60 additions & 31 deletions

File tree

src/Nmap/Nmap.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public function __construct(
5555
$this->executor = $executor ?: new ProcessExecutor();
5656
$tmp = $outputFile ?? tempnam(sys_get_temp_dir(), 'nmap-scan-output.xml');
5757
if (!is_string($tmp)) {
58-
throw new \InvalidArgumentException("No outputFile parameter given, or not able to create one with tempnam, fs problem?");
58+
throw new \InvalidArgumentException(
59+
"No outputFile parameter given, or not able to create one with tempnam, fs problem?"
60+
);
5961
}
6062
$this->outputFile = $tmp;
6163
$this->executable = $executable;
@@ -110,7 +112,7 @@ public function buildCommand(array $targets, array $ports = []): array
110112
$options[] = $this->outputFile;
111113

112114
$options[] = '--'; // so targets are not options
113-
115+
114116
return array_merge([$this->executable], $options, $targets);
115117
}
116118

src/Nmap/XmlOutputParser.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function attemptFixInvalidFile(): bool
142142
if (!is_string($str)) {
143143
throw new \InvalidArgumentException("Could not get contents of file: {$this->xmlFile}");
144144
}
145-
145+
146146

147147
if (preg_match('%' . preg_quote(XmlOutputParser::$xmlCloseTag) . '\s+$%m', $str)) {
148148
return false;
@@ -213,7 +213,8 @@ public function parse(): array
213213
$host = new Host(
214214
self::parseAddresses($xmlHost),
215215
(string)$state,
216-
(isset($xmlHost->hostnames) && isset($xmlHost->hostnames->hostname)) ? self::parseHostnames($xmlHost->hostnames->hostname) : [],
216+
(isset($xmlHost->hostnames) && isset($xmlHost->hostnames->hostname)) ?
217+
self::parseHostnames($xmlHost->hostnames->hostname) : [],
217218
$port
218219
);
219220

tests/Nmap/Tests/NmapTest.php

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ public function tearDown(): void
1717
m::close();
1818
parent::tearDown();
1919

20-
if ($this->filesystem->exists($recoverDir = __DIR__.'/Fixtures/Validation/recovered')) {
20+
if ($this->filesystem->exists($recoverDir = __DIR__ . '/Fixtures/Validation/recovered')) {
2121
$this->filesystem->remove($recoverDir);
2222
}
2323
}
2424

2525
public function testScanBasic()
2626
{
27-
$outputFile = __DIR__.'/Fixtures/test_scan.xml';
27+
$outputFile = __DIR__ . '/Fixtures/test_scan.xml';
2828

29-
$expectedCommand = ["nmap", "-oX", $outputFile, 'williamdurand.fr'];
29+
$expectedCommand = ["nmap", "-oX", $outputFile, '--', 'williamdurand.fr'];
3030

3131
$executor = $this->getProcessExecutorMock($expectedCommand);
3232

@@ -71,9 +71,9 @@ public function testScanBasic()
7171

7272
public function testScanSpecifyingPorts()
7373
{
74-
$outputFile = __DIR__.'/Fixtures/test_scan_specifying_ports.xml';
74+
$outputFile = __DIR__ . '/Fixtures/test_scan_specifying_ports.xml';
7575

76-
$expectedCommand = ["nmap", "-p 21,22,80", "-oX", $outputFile, 'williamdurand.fr'];
76+
$expectedCommand = ["nmap", "-p 21,22,80", "-oX", $outputFile, '--', 'williamdurand.fr'];
7777

7878
$executor = $this->getProcessExecutorMock($expectedCommand);
7979

@@ -110,8 +110,8 @@ public function testScanSpecifyingPorts()
110110

111111
public function testScanWithOsDetection()
112112
{
113-
$outputFile = __DIR__.'/Fixtures/test_scan_with_os_detection.xml';
114-
$expectedCommand = ["nmap", "-O", "-oX", $outputFile, 'williamdurand.fr'];
113+
$outputFile = __DIR__ . '/Fixtures/test_scan_with_os_detection.xml';
114+
$expectedCommand = ["nmap", "-O", "-oX", $outputFile, '--', 'williamdurand.fr'];
115115

116116
$executor = $this->getProcessExecutorMock($expectedCommand);
117117

@@ -126,8 +126,8 @@ public function testScanWithOsDetection()
126126

127127
public function testScanWithServiceInfo()
128128
{
129-
$outputFile = __DIR__.'/Fixtures/test_scan_with_service_info.xml';
130-
$expectedCommand = ["nmap", "-sV", "-oX", $outputFile, 'williamdurand.fr'];
129+
$outputFile = __DIR__ . '/Fixtures/test_scan_with_service_info.xml';
130+
$expectedCommand = ["nmap", "-sV", "-oX", $outputFile, '--', 'williamdurand.fr'];
131131

132132
$executor = $this->getProcessExecutorMock($expectedCommand);
133133

@@ -151,8 +151,8 @@ public function testScanWithServiceInfo()
151151

152152
public function testScanWithVerbose()
153153
{
154-
$outputFile = __DIR__.'/Fixtures/test_scan_with_verbose.xml';
155-
$expectedCommand = ["nmap", "-v", "-oX", $outputFile, 'williamdurand.fr'];
154+
$outputFile = __DIR__ . '/Fixtures/test_scan_with_verbose.xml';
155+
$expectedCommand = ["nmap", "-v", "-oX", $outputFile, '--', 'williamdurand.fr'];
156156

157157
$executor = $this->getProcessExecutorMock($expectedCommand);
158158

@@ -167,8 +167,8 @@ public function testScanWithVerbose()
167167

168168
public function testPingScan()
169169
{
170-
$outputFile = __DIR__.'/Fixtures/test_ping_scan.xml';
171-
$expectedCommand = ["nmap", "-sn", "-oX", $outputFile, 'williamdurand.fr'];
170+
$outputFile = __DIR__ . '/Fixtures/test_ping_scan.xml';
171+
$expectedCommand = ["nmap", "-sn", "-oX", $outputFile, '--', 'williamdurand.fr'];
172172

173173
$executor = $this->getProcessExecutorMock($expectedCommand);
174174

@@ -182,8 +182,8 @@ public function testPingScan()
182182

183183
public function testScanWithoutReverseDNS()
184184
{
185-
$outputFile = __DIR__.'/Fixtures/test_ping_without_reverse_dns.xml';
186-
$expectedCommand = ["nmap", "-n", "-oX", $outputFile, 'williamdurand.fr'];
185+
$outputFile = __DIR__ . '/Fixtures/test_ping_without_reverse_dns.xml';
186+
$expectedCommand = ["nmap", "-n", "-oX", $outputFile, '--', 'williamdurand.fr'];
187187

188188
$executor = $this->getProcessExecutorMock($expectedCommand);
189189

@@ -197,8 +197,8 @@ public function testScanWithoutReverseDNS()
197197

198198
public function testScanWithTreatHostsAsOnline()
199199
{
200-
$outputFile = __DIR__.'/Fixtures/test_scan_with_verbose.xml';
201-
$expectedCommand = ["nmap", "-Pn", "-oX", $outputFile, 'williamdurand.fr'];
200+
$outputFile = __DIR__ . '/Fixtures/test_scan_with_verbose.xml';
201+
$expectedCommand = ["nmap", "-Pn", "-oX", $outputFile, '--', 'williamdurand.fr'];
202202

203203
$executor = $this->getProcessExecutorMock($expectedCommand);
204204

@@ -210,7 +210,7 @@ public function testScanWithTreatHostsAsOnline()
210210

211211
public function testScanWithUserTimeout()
212212
{
213-
$outputFile = __DIR__.'/Fixtures/test_scan.xml';
213+
$outputFile = __DIR__ . '/Fixtures/test_scan.xml';
214214
$timeout = 123;
215215

216216
$mock = m::mock(\Nmap\Util\ProcessExecutor::class);
@@ -269,23 +269,23 @@ function (array $args) {
269269

270270
public function testExistingXmlOutputFileCanBeParsed()
271271
{
272-
$hosts = Nmap::parseOutput(__DIR__.'/Fixtures/test_scan.xml');
272+
$hosts = Nmap::parseOutput(__DIR__ . '/Fixtures/test_scan.xml');
273273
$host = current($hosts);
274274
$this->assertCount(1, $hosts);
275275
$this->assertCount(5, $host->getPorts());
276276
}
277277

278278
public function testOutputValidationInvalid()
279279
{
280-
$parser = new XmlOutputParser(__DIR__.'/Fixtures/Validation/test_interrupted_invalid.xml');
281-
$dtd = __DIR__.'/Fixtures/Validation/nmap.dtd';
280+
$parser = new XmlOutputParser(__DIR__ . '/Fixtures/Validation/test_interrupted_invalid.xml');
281+
$dtd = __DIR__ . '/Fixtures/Validation/nmap.dtd';
282282
$this->assertStringContainsString('Premature end of data in tag nmaprun', $parser->validate($dtd));
283283
}
284284

285285
public function testOutputValidationValid()
286286
{
287-
$parser = new XmlOutputParser(__DIR__.'/Fixtures/Validation/test_completed_valid.xml');
288-
$dtd = __DIR__.'/Fixtures/Validation/nmap.dtd';
287+
$parser = new XmlOutputParser(__DIR__ . '/Fixtures/Validation/test_completed_valid.xml');
288+
$dtd = __DIR__ . '/Fixtures/Validation/nmap.dtd';
289289
$this->assertTrue($parser->validate($dtd));
290290
}
291291

@@ -296,28 +296,54 @@ public function testOutputDefaultDtdPath()
296296

297297
public function testOutputValidationValidByUsingDtdFallback()
298298
{
299-
$parser = new XmlOutputParser(__DIR__.'/Fixtures/Validation/test_completed_valid.xml');
299+
$parser = new XmlOutputParser(__DIR__ . '/Fixtures/Validation/test_completed_valid.xml');
300300
$this->assertTrue($parser->validate('notavalidpath'));
301301
}
302302

303303
public function testOutputFileRecovery()
304304
{
305305
// Invalid input
306-
$input = __DIR__.'/Fixtures/Validation/test_interrupted_invalid.xml';
306+
$input = __DIR__ . '/Fixtures/Validation/test_interrupted_invalid.xml';
307307
$parser = new XmlOutputParser($input);
308308
$this->assertTrue($parser->attemptFixInvalidFile());
309309

310310
// Assert recovery
311-
$output = __DIR__.'/Fixtures/Validation/recovered/test_interrupted_invalid.xml';
311+
$output = __DIR__ . '/Fixtures/Validation/recovered/test_interrupted_invalid.xml';
312312
$this->assertFileExists($output);
313313
$this->assertTrue(filesize($output) > filesize($input));
314314
$this->assertStringEndsWith(XmlOutputParser::$xmlCloseTag, file_get_contents($output));
315315
}
316316

317317
public function testOutputFileRecoveryOnValidFile()
318318
{
319-
$parser = new XmlOutputParser(__DIR__.'/Fixtures/Validation/test_completed_valid.xml');
319+
$parser = new XmlOutputParser(__DIR__ . '/Fixtures/Validation/test_completed_valid.xml');
320320
$this->assertFalse($parser->attemptFixInvalidFile());
321321
}
322322

323+
public function testBuildCommand()
324+
{
325+
326+
$x = (new Nmap(null, '/tmp/test'))->buildCommand([], []);
327+
$this->assertEquals(['nmap', '-oX', '/tmp/test', '--'], $x);
328+
329+
$x = (new Nmap(null, '/tmp/test'))->buildCommand(['facebook.com'], []);
330+
$this->assertEquals(['nmap', '-oX', '/tmp/test', '--', 'facebook.com'], $x);
331+
332+
$x = (new Nmap(null, '/tmp/test'))->buildCommand(['facebook.com'], [80]);
333+
$this->assertEquals(['nmap', '-p 80', '-oX', '/tmp/test', '--', 'facebook.com'], $x);
334+
335+
$x = (new Nmap(null, '/tmp/test'))->buildCommand(['facebook.com'], [80, 443]);
336+
$this->assertEquals(['nmap', '-p 80,443', '-oX', '/tmp/test', '--', 'facebook.com'], $x);
337+
338+
339+
$x = (new Nmap(null, '/tmp/test'))->buildCommand(['example.com', 'facebook.com'], [80, 443]);
340+
341+
$this->assertEquals(['nmap', '-p 80,443', '-oX', '/tmp/test', '--', 'example.com', 'facebook.com'], $x);
342+
343+
$x = (new Nmap(null, '/tmp/test'))
344+
->disableReverseDNS(true)
345+
->buildCommand(['example.com', 'facebook.com'], [80, 443]);
346+
$this->assertEquals(['nmap', '-p 80,443', '-n', '-oX', '/tmp/test', '--', 'example.com', 'facebook.com'], $x);
347+
}
348+
323349
}

0 commit comments

Comments
 (0)