From 57446d75fa9d60eaa96f60ed1db0c48e079ea0aa Mon Sep 17 00:00:00 2001 From: VolodymyrBg Date: Tue, 18 Mar 2025 19:06:53 +0200 Subject: [PATCH] Create netstatcmd_test.go --- cmd/geth/netstatcmd_test.go | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 cmd/geth/netstatcmd_test.go diff --git a/cmd/geth/netstatcmd_test.go b/cmd/geth/netstatcmd_test.go new file mode 100644 index 0000000000..0373a45c91 --- /dev/null +++ b/cmd/geth/netstatcmd_test.go @@ -0,0 +1,62 @@ +// Copyright 2023 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see . + +package main + +import ( + "io" + "testing" + + "github.com/urfave/cli/v2" +) + +// Tests that the netstat command exists and has the correct flags +func TestNetstatCommand(t *testing.T) { + app := cli.NewApp() + // Use an io.Discard-like writer to avoid output during tests + app.Writer = io.Discard + app.Commands = []*cli.Command{netstatCommand} + + // Verify that the monitor flag is registered + var monitorFlagFound bool + for _, flag := range netstatCommand.Flags { + if flag.Names()[0] == monitorFlag.Name { + monitorFlagFound = true + break + } + } + if !monitorFlagFound { + t.Error("monitor flag not registered") + } + + // Test that the command help output works + if err := app.Run([]string{"geth", "netstat", "--help"}); err != nil { + t.Fatalf("netstat --help failed: %v", err) + } +} + +// Test that the connection direction detection works correctly +func TestConnectionDirection(t *testing.T) { + inbound := connectionDirection(true) + outbound := connectionDirection(false) + + if inbound != "Inbound (remote dialed us)" { + t.Errorf("expected 'Inbound (remote dialed us)', got '%s'", inbound) + } + if outbound != "Outbound (we dialed remote)" { + t.Errorf("expected 'Outbound (we dialed remote)', got '%s'", outbound) + } +}