|
| 1 | +#!/usr/bin/env bash |
| 2 | +source tests/common.bash |
| 3 | + |
| 4 | +# Skip if python3 not available (needed for JSON validation) |
| 5 | +if ! command -v python3 &>/dev/null; then |
| 6 | + echo "SKIP: python3 not available" >> $report |
| 7 | + exit 77 |
| 8 | +fi |
| 9 | + |
| 10 | +{ |
| 11 | + # Test 1: -J produces valid JSON |
| 12 | + output=$($lsof -J -p $$ 2>/dev/null) |
| 13 | + if ! echo "$output" | python3 -m json.tool > /dev/null 2>&1; then |
| 14 | + echo "FAIL: -J output is not valid JSON" >> $report |
| 15 | + exit 1 |
| 16 | + fi |
| 17 | + echo "PASS: -J produces valid JSON" >> $report |
| 18 | + |
| 19 | + # Test 2: -J output has lsof_version and processes keys |
| 20 | + echo "$output" | python3 -c " |
| 21 | +import sys, json |
| 22 | +d = json.load(sys.stdin) |
| 23 | +assert 'lsof_version' in d, 'missing lsof_version' |
| 24 | +assert 'processes' in d, 'missing processes' |
| 25 | +assert isinstance(d['processes'], list), 'processes is not a list' |
| 26 | +assert len(d['processes']) > 0, 'no processes found' |
| 27 | +" 2>&1 || { echo "FAIL: -J output structure invalid" >> $report; exit 1; } |
| 28 | + echo "PASS: -J output structure valid" >> $report |
| 29 | + |
| 30 | + # Test 3: -J -Fpcfn only includes selected fields |
| 31 | + output_sel=$($lsof -J -Fpcfn -p $$ 2>/dev/null) |
| 32 | + echo "$output_sel" | python3 -c " |
| 33 | +import sys, json |
| 34 | +d = json.load(sys.stdin) |
| 35 | +p = d['processes'][0] |
| 36 | +assert 'pid' in p, 'missing pid' |
| 37 | +assert 'command' in p, 'missing command' |
| 38 | +f = p['files'][0] |
| 39 | +allowed_file_keys = {'fd', 'name'} |
| 40 | +actual_file_keys = set(f.keys()) |
| 41 | +extra_keys = actual_file_keys - allowed_file_keys |
| 42 | +assert not extra_keys, 'unexpected file keys with -Fpcfn: %s' % extra_keys |
| 43 | +assert 'uid' not in p, 'uid should not be present with -Fpcfn' |
| 44 | +" 2>&1 || { echo "FAIL: -J field selection not working" >> $report; exit 1; } |
| 45 | + echo "PASS: -J field selection works" >> $report |
| 46 | + |
| 47 | + # Test 4: -J empty result produces valid JSON |
| 48 | + output_empty=$($lsof -J -p 999999 2>/dev/null) |
| 49 | + echo "$output_empty" | python3 -c " |
| 50 | +import sys, json |
| 51 | +d = json.load(sys.stdin) |
| 52 | +assert d['processes'] == [], 'empty result should have empty processes array' |
| 53 | +" 2>&1 || { echo "FAIL: -J empty result is not valid JSON" >> $report; exit 1; } |
| 54 | + echo "PASS: -J empty result is valid JSON" >> $report |
| 55 | + |
| 56 | + # Test 5: -J and -j are mutually exclusive |
| 57 | + if $lsof -J -j 2>/dev/null; then |
| 58 | + echo "FAIL: -J -j should error" >> $report |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | + echo "PASS: -J -j mutual exclusivity" >> $report |
| 62 | + |
| 63 | + # Test 6: -J and -t are mutually exclusive |
| 64 | + if $lsof -J -t 2>/dev/null; then |
| 65 | + echo "FAIL: -J -t should error" >> $report |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | + echo "PASS: -J -t mutual exclusivity" >> $report |
| 69 | + |
| 70 | + exit 0 |
| 71 | +} > $report 2>&1 |
0 commit comments