|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + tuifirewall "github.com/tui-tools/tui-firewall" |
| 9 | + "github.com/tui-tools/tui-firewall/internal/ufw" |
| 10 | + "github.com/tui-tools/tui-kit/compat" |
| 11 | + "github.com/tui-tools/tui-kit/manifest" |
| 12 | + "github.com/tui-tools/tui-kit/theme" |
| 13 | +) |
| 14 | + |
| 15 | +// The embedded manifest is the source the header reads, so it has to parse and |
| 16 | +// describe the backend the tool actually drives. |
| 17 | +func TestEmbeddedManifestDeclaresUfw(t *testing.T) { |
| 18 | + m, err := manifest.Load(tuifirewall.ManifestJSON) |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("the embedded tool.json does not parse: %v", err) |
| 21 | + } |
| 22 | + if m.Name != toolName { |
| 23 | + t.Errorf("manifest name = %q, want %q", m.Name, toolName) |
| 24 | + } |
| 25 | + backend, ok := m.Backend("ufw") |
| 26 | + if !ok { |
| 27 | + t.Fatal("no ufw backend in the manifest") |
| 28 | + } |
| 29 | + if len(backend.VersionCommand) == 0 || backend.Minimum == "" { |
| 30 | + t.Errorf("ufw backend is incomplete: %+v", backend) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// --demo must never probe the host: the version on screen would describe a |
| 35 | +// machine the demo is not driving. |
| 36 | +func TestProbeCompatSkipsDemo(t *testing.T) { |
| 37 | + got := probeCompat(context.Background(), "ufw", true) |
| 38 | + if got.Backend != "" || got.Version != "" { |
| 39 | + t.Errorf("demo probe = %+v, want the zero result", got) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// A backend the manifest does not describe (firewalld today) is not an error, |
| 44 | +// it is simply nothing to show. |
| 45 | +func TestProbeCompatUnknownBackend(t *testing.T) { |
| 46 | + got := probeCompat(context.Background(), "nftables", false) |
| 47 | + if got.Backend != "" { |
| 48 | + t.Errorf("unknown backend = %+v, want the zero result", got) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// The whole point of the feature: the version reaches the header. |
| 53 | +func TestHeaderShowsTheBackendVersion(t *testing.T) { |
| 54 | + a, _ := newTestApp(t, 100, 30) |
| 55 | + if got := a.View(); !strings.Contains(got, "ufw 0.36.2") { |
| 56 | + t.Errorf("the header should carry the probed version, got:\n%s", got) |
| 57 | + } |
| 58 | + |
| 59 | + a.backendCompat = testCompat(t, "ufw 0.37.0") |
| 60 | + if got := a.View(); !strings.Contains(got, "ufw 0.37.0 (untested)") { |
| 61 | + t.Errorf("an untested version should say so, got:\n%s", got) |
| 62 | + } |
| 63 | + |
| 64 | + a.backendCompat = testCompat(t, "ufw 0.35") |
| 65 | + if got := a.View(); !strings.Contains(got, "below minimum 0.36") { |
| 66 | + t.Errorf("a version below the minimum should say so, got:\n%s", got) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// A tool that could not read the version still renders a header. |
| 71 | +func TestHeaderWithoutAProbe(t *testing.T) { |
| 72 | + a, _ := newTestApp(t, 100, 30) |
| 73 | + a.backendCompat = compat.Result{} |
| 74 | + _ = theme.TokyoNight() |
| 75 | + if got := a.View(); strings.Contains(got, "backend:") { |
| 76 | + t.Errorf("no probe means no backend fact, got:\n%s", got) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// Rule comments are a ufw 0.35 feature. The form must drop the field on an |
| 81 | +// older machine rather than build a command ufw refuses. |
| 82 | +func TestRuleCommentsAreGatedOnTheUfwVersion(t *testing.T) { |
| 83 | + a, _ := newTestApp(t, 100, 30) |
| 84 | + if !a.caps.SupportsComments { |
| 85 | + t.Error("0.36.2 supports rule comments") |
| 86 | + } |
| 87 | + |
| 88 | + old := newApp(ufw.NewFake(), theme.FromPalette(theme.TokyoNight()), |
| 89 | + testCompat(t, "ufw 0.34")) |
| 90 | + if old.caps.SupportsComments { |
| 91 | + t.Error("0.34 has no rule comments, so the form must not offer them") |
| 92 | + } |
| 93 | +} |
0 commit comments