1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-09 22:21:29 +00:00

Normalize the Ndiff exit codes.

0 if the scans are equal,
	1 if they differ, and
	2 for runtime errors.
Add tests and man page documentation.
This commit is contained in:
david
2009-07-30 14:40:46 +00:00
parent 24b5c338b6
commit 05e5348b57
4 changed files with 115 additions and 4 deletions

View File

@@ -2,6 +2,7 @@
# Unit tests for Ndiff.
import subprocess
import unittest
import xml.dom.minidom
import StringIO
@@ -688,4 +689,43 @@ def host_apply_diff(host, diff):
host.script_results[host.script_results.index(sr_a)] = sr_b
host.script_results.sort()
def call_quiet(args, **kwargs):
"""Run a command with subprocess.call and hide its output."""
return subprocess.call(args, stdout = subprocess.PIPE,
stderr = subprocess.STDOUT, **kwargs)
class exit_code_test(unittest.TestCase):
NDIFF = "./ndiff"
def test_exit_equal(self):
"""Test that the exit code is 0 when the diff is empty."""
for format in ("--text", "--xml"):
code = call_quiet([self.NDIFF, format,
"test-scans/simple.xml", "test-scans/simple.xml"])
self.assertEqual(code, 0)
# Should be independent of verbosity.
for format in ("--text", "--xml"):
code = call_quiet([self.NDIFF, "-v", format,
"test-scans/simple.xml", "test-scans/simple.xml"])
self.assertEqual(code, 0)
def test_exit_different(self):
"""Test that the exit code is 1 when the diff is not empty."""
for format in ("--text", "--xml"):
code = call_quiet([self.NDIFF, format,
"test-scans/simple.xml", "test-scans/complex.xml"])
self.assertEqual(code, 1)
def test_exit_error(self):
"""Test that the exit code is 2 when there is an error."""
code = call_quiet([self.NDIFF])
self.assertEqual(code, 2)
code = call_quiet([self.NDIFF, "test-scans/simple.xml"])
self.assertEqual(code, 2)
code = call_quiet([self.NDIFF, "test-scans/simple.xml",
"test-scans/nonexistent.xml"])
self.assertEqual(code, 2)
code = call_quiet([self.NDIFF, "--nothing"])
self.assertEqual(code, 2)
unittest.main()