|
1 | 1 | package syslog |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "net" |
4 | 5 | "testing" |
5 | 6 | "time" |
6 | 7 | ) |
7 | 8 |
|
| 9 | +func TestMessageNetSrc(t *testing.T) { |
| 10 | + tcpAddress, err := net.ResolveTCPAddr("tcp", "localhost:1234") |
| 11 | + |
| 12 | + if err != nil { |
| 13 | + t.Error("could not resolve TCP address") |
| 14 | + } |
| 15 | + |
| 16 | + m := &Message{time.Now(), tcpAddress, 0, 0, time.Now(), "", "", "", "", ""} |
| 17 | + if m.NetSrc() != "127.0.0.1" { |
| 18 | + t.Errorf("expected 127.0.0.1, got %s", m.NetSrc()) |
| 19 | + } |
| 20 | + |
| 21 | + udpAddress, err := net.ResolveUDPAddr("udp", "localhost:1234") |
| 22 | + |
| 23 | + if err != nil { |
| 24 | + t.Error("could not resolve UDP address") |
| 25 | + } |
| 26 | + |
| 27 | + m.Source = udpAddress |
| 28 | + if m.NetSrc() != "127.0.0.1" { |
| 29 | + t.Errorf("expected 127.0.0.1, got %s", m.NetSrc()) |
| 30 | + } |
| 31 | + |
| 32 | + unixAddress, err := net.ResolveUnixAddr("unix", "/tmp/str") |
| 33 | + |
| 34 | + if err != nil { |
| 35 | + t.Error("could not resolve unix address") |
| 36 | + } |
| 37 | + |
| 38 | + m.Source = unixAddress |
| 39 | + if m.NetSrc() != "/tmp/str" { |
| 40 | + t.Errorf("expected /tmp/str, got %s", m.NetSrc()) |
| 41 | + } |
| 42 | + |
| 43 | + unknownAddress, err := net.ResolveIPAddr("ip4", "localhost") |
| 44 | + |
| 45 | + if err != nil { |
| 46 | + t.Error("could not resolve unknown address") |
| 47 | + } |
| 48 | + |
| 49 | + m.Source = unknownAddress |
| 50 | + if m.NetSrc() != "127.0.0.1" { |
| 51 | + t.Errorf("expected 127.0.0.1, got %s", m.NetSrc()) |
| 52 | + } |
| 53 | +} |
| 54 | + |
8 | 55 | func TestMessageFormat(t *testing.T) { |
9 | | - m := &Message{time.Now(), 34, time.Now(), "localhost", "test", "hello world"} |
| 56 | + tcpAddress, err := net.ResolveTCPAddr("tcp", "localhost:1234") |
| 57 | + |
| 58 | + if err != nil { |
| 59 | + t.Errorf("could not resolve TCP address") |
| 60 | + } |
| 61 | + |
| 62 | + m := &Message{ |
| 63 | + time.Now(), |
| 64 | + tcpAddress, |
| 65 | + 0, |
| 66 | + 0, |
| 67 | + time.Now(), |
| 68 | + "localhost", |
| 69 | + "TEST", |
| 70 | + "hello world", |
| 71 | + "", |
| 72 | + "", |
| 73 | + } |
10 | 74 |
|
11 | 75 | timeLayout := "2006-01-02 15:04:05" |
12 | | - expectedOutput := m.Time.Format(timeLayout) + " test: hello world" |
| 76 | + expectedOutput := m.Time.Format(timeLayout) + " localhost TESThello world" |
13 | 77 | if m.String() != expectedOutput { |
14 | 78 | t.Errorf("expected '" + expectedOutput + "', got '" + m.String() + "'.") |
15 | 79 | } |
|
0 commit comments