1package main23import (4 "net/http"5 "net/http/httptest"6 "os"7 "path/filepath"8 "strings"9 "testing"10)1112func TestResolveFeatures(t *testing.T) {13 for _, c := range []struct {14 antenna, hdhr, discovery string15 want features16 }{17 {"", "", "", features{antenna: true, hdhr: true, discovery: true}}, // the home server18 {"off", "", "", features{}}, // custom channels only19 {"OFF", "on", "", features{hdhr: true}}, // HTTP, no discovery20 {"off", "on", "on", features{hdhr: true, discovery: true}}, // and discovery21 {"on", "off", "on", features{antenna: true}}, // no discovery without the device22 {"true", "", "false", features{antenna: true, hdhr: true}}, // other spellings23 {" yes ", "1", "0", features{antenna: true, hdhr: true}},24 } {25 got, err := resolveFeatures(c.antenna, c.hdhr, c.discovery)26 if err != nil || got != c.want {27 t.Errorf("resolveFeatures(%q, %q, %q) = %+v, %v; want %+v", c.antenna, c.hdhr, c.discovery, got, err, c.want)28 }29 }30 if _, err := resolveFeatures("", "maybe", ""); err == nil || !strings.Contains(err.Error(), "AIRWAVES_HDHR") {31 t.Errorf("bad setting: %v", err)32 }33}3435func TestWritable(t *testing.T) {36 dir := filepath.Join(t.TempDir(), "data")37 if err := writable(dir); err != nil {38 t.Fatal(err)39 }40 if entries, _ := os.ReadDir(dir); len(entries) != 0 {41 t.Errorf("left %v behind", entries)42 }43 if os.Getuid() == 0 {44 t.Skip("root writes anywhere")45 }46 if err := os.Chmod(dir, 0o555); err != nil {47 t.Fatal(err)48 }49 defer os.Chmod(dir, 0o755)50 if err := writable(dir); err == nil || !strings.Contains(err.Error(), "chown") {51 t.Errorf("read-only folder: %v", err)52 }53}5455func TestHealthcheck(t *testing.T) {56 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {57 if r.URL.Path != "/healthz" {58 http.NotFound(w, r)59 }60 }))61 addr := strings.TrimPrefix(srv.URL, "http://")62 if err := healthcheck(addr); err != nil {63 t.Errorf("up: %v", err)64 }65 _, port, _ := strings.Cut(addr, ":")66 if err := healthcheck(":" + port); err != nil {67 t.Errorf("up, on all addresses: %v", err)68 }69 srv.Close()70 if err := healthcheck(addr); err == nil {71 t.Error("down, yet healthy")72 }73}