]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
test: cover TTL option validation 582/head
authorDarafei Praliaskouski <me@komzpa.net>
Thu, 7 May 2026 20:08:46 +0000 (00:08 +0400)
committerDarafei Praliaskouski <me@komzpa.net>
Thu, 7 May 2026 20:11:36 +0000 (00:11 +0400)
test/cmdparse.py

index ad6d23c8d0cbd6c3d4e8fe2f53b49fcdd0322d28..b3b8e8112c10a94748c0ebdbb6b9c84549fbee23 100755 (executable)
 #   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 #
 
-'''Test mtr-packet's command parsing.'''
+'''Test mtr and mtr-packet command parsing.'''
 
 
+import os
+import subprocess
 import time
 import unittest
 
 import mtrpacket
 
 
+PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+MTR = os.path.join(PROJECT_ROOT, 'mtr')
+
+
+class TestMtrCommandParse(unittest.TestCase):
+    '''Test cases with malformed mtr command-line arguments.'''
+
+    def run_mtr(self, *args):
+        return subprocess.run(
+            [MTR] + list(args),
+            stdout=subprocess.PIPE,
+            stderr=subprocess.PIPE,
+            universal_newlines=True,
+        )
+
+    def test_first_ttl_cannot_exceed_max_ttl(self):
+        'Test that conflicting first/max TTL options fail fast.'
+
+        reply = self.run_mtr('--first-ttl', '91', '--max-ttl', '90')
+
+        self.assertNotEqual(reply.returncode, 0)
+        self.assertIn(
+            'firstTTL(91) cannot be larger than maxTTL(90)',
+            reply.stderr,
+        )
+
+    def test_first_ttl_can_equal_max_ttl(self):
+        'Test that a single requested TTL depth remains valid.'
+
+        reply = self.run_mtr(
+            '--report',
+            '--report-cycles',
+            '1',
+            '--no-dns',
+            '--first-ttl',
+            '91',
+            '--max-ttl',
+            '91',
+            '127.0.0.1',
+        )
+
+        self.assertEqual(reply.returncode, 0)
+        self.assertEqual(reply.stderr, '')
+
+
 class TestCommandParse(mtrpacket.MtrPacketTest):
     '''Test cases with malformed commands and version checks'''