|
| 1 | +package zeroconf |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestRegister(t *testing.T) { |
| 10 | + expected := ServiceEntry{ |
| 11 | + ServiceRecord: ServiceRecord{ |
| 12 | + Instance: "instance", |
| 13 | + Service: "service", |
| 14 | + Domain: "domain", |
| 15 | + }, |
| 16 | + Port: 1337, |
| 17 | + Text: []string{"txtv=0", "lo=1", "la=2"}, |
| 18 | + } |
| 19 | + server, err := Register(expected.Instance, expected.Service, expected.Domain, expected.Port, expected.Text, nil) |
| 20 | + if err != nil { |
| 21 | + t.Fatal(err) |
| 22 | + } |
| 23 | + server.Shutdown() |
| 24 | + |
| 25 | + hostname, err := os.Hostname() |
| 26 | + if err != nil { |
| 27 | + t.Fatal(err) |
| 28 | + } |
| 29 | + expected.HostName = hostname + ".domain." |
| 30 | + |
| 31 | + checkServiceEntry(t, *server.service, expected) |
| 32 | +} |
| 33 | + |
| 34 | +func TestRegisterProxy(t *testing.T) { |
| 35 | + expected := ServiceEntry{ |
| 36 | + ServiceRecord: ServiceRecord{ |
| 37 | + Instance: "instance", |
| 38 | + Service: "service", |
| 39 | + Domain: "domain", |
| 40 | + }, |
| 41 | + Port: 1337, |
| 42 | + HostName: "proxy.domain.", |
| 43 | + Text: []string{"txtv=0", "lo=1", "la=2"}, |
| 44 | + } |
| 45 | + server, err := RegisterProxy(expected.Instance, expected.Service, expected.Domain, expected.Port, expected.HostName, nil, expected.Text, nil) |
| 46 | + if err != nil { |
| 47 | + t.Fatal(err) |
| 48 | + } |
| 49 | + server.Shutdown() |
| 50 | + |
| 51 | + checkServiceEntry(t, *server.service, expected) |
| 52 | +} |
| 53 | + |
| 54 | +func checkServiceEntry(t *testing.T, got, expected ServiceEntry) { |
| 55 | + t.Helper() |
| 56 | + if got.Instance != expected.Instance { |
| 57 | + t.Fatalf("Expected Instance is %s, but got %s", expected.Instance, got.Instance) |
| 58 | + } |
| 59 | + if got.Service != expected.Service { |
| 60 | + t.Fatalf("Expected Service is %s, but got %s", expected.Service, got.Service) |
| 61 | + } |
| 62 | + if got.Domain != expected.Domain { |
| 63 | + t.Fatalf("Expected Domain is %s, but got %s", expected.Domain, got.Domain) |
| 64 | + } |
| 65 | + if got.Port != expected.Port { |
| 66 | + t.Fatalf("Expected Port is %d, but got %d", expected.Port, got.Port) |
| 67 | + } |
| 68 | + if strings.Join(got.Text, " ") != strings.Join(expected.Text, " ") { |
| 69 | + t.Fatalf("Expected Text is %s, but got %s", expected.Text, got.Text) |
| 70 | + } |
| 71 | + if got.HostName != expected.HostName { |
| 72 | + t.Fatalf("Expected HostName is %s, but got %s", expected.HostName, got.HostName) |
| 73 | + } |
| 74 | + if len(got.AddrIPv4) == 0 && len(got.AddrIPv6) == 0 { |
| 75 | + t.Fatal("Unexpected empty IPV4 and IPV6") |
| 76 | + } |
| 77 | +} |
0 commit comments