git » debian:golang-blitiri-go-systemd » commit 6973930

test: Add tests for some error conditions

author Alberto Bertogli
2020-05-28 21:26:28 UTC
committer Alberto Bertogli
2020-05-28 23:21:04 UTC
parent c26a65e81ff02d0c9d426395d9cce01b3d46e06c

test: Add tests for some error conditions

This patch adds two small tests that cover some error conditions that
were not covered before: being passed a bad file descriptor, and calling
OneListener/Listen with a name that doesn't have a listener.

systemd_test.go +20 -0

diff --git a/systemd_test.go b/systemd_test.go
index 6686795..e005b67 100644
--- a/systemd_test.go
+++ b/systemd_test.go
@@ -105,6 +105,26 @@ func TestNoFDs(t *testing.T) {
 	if ls, err := Files(); len(ls) != 0 || err != nil {
 		t.Errorf("Got a non-empty result: %v // %v", ls, err)
 	}
+	if l, err := OneListener("nothing"); l != nil || err != nil {
+		t.Errorf("Unexpected result: %v // %v", l, err)
+	}
+	if l, err := Listen("tcp", "&nothing"); l != nil || err == nil {
+		t.Errorf("Unexpected result: %v // %v", l, err)
+	}
+}
+
+func TestBadFDs(t *testing.T) {
+	f, err := os.Open(os.DevNull)
+	if err != nil {
+		t.Fatalf("Failed to open /dev/null: %v", err)
+	}
+	defer f.Close()
+
+	setenv(strconv.Itoa(os.Getpid()), "1")
+	firstFD = int(f.Fd())
+	if ls, err := Listeners(); len(ls) != 1 || err == nil {
+		t.Errorf("Got a non-empty result: %v // %v", ls, err)
+	}
 }
 
 // newListener creates a TCP listener.