OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [log/] [syslog/] [syslog_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
package syslog
5
 
6
import (
7
        "io"
8
        "log"
9
        "net"
10
        "testing"
11
        "time"
12
)
13
 
14
var serverAddr string
15
 
16
func runSyslog(c net.PacketConn, done chan<- string) {
17
        var buf [4096]byte
18
        var rcvd string = ""
19
        for {
20
                n, _, err := c.ReadFrom(buf[0:])
21
                if err != nil || n == 0 {
22
                        break
23
                }
24
                rcvd += string(buf[0:n])
25
        }
26
        done <- rcvd
27
}
28
 
29
func startServer(done chan<- string) {
30
        c, e := net.ListenPacket("udp", "127.0.0.1:0")
31
        if e != nil {
32
                log.Fatalf("net.ListenPacket failed udp :0 %v", e)
33
        }
34
        serverAddr = c.LocalAddr().String()
35
        c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
36
        go runSyslog(c, done)
37
}
38
 
39
func skipNetTest(t *testing.T) bool {
40
        if testing.Short() {
41
                // Depends on syslog daemon running, and sometimes it's not.
42
                t.Logf("skipping syslog test during -short")
43
                return true
44
        }
45
        return false
46
}
47
 
48
func TestNew(t *testing.T) {
49
        if skipNetTest(t) {
50
                return
51
        }
52
        s, err := New(LOG_INFO, "")
53
        if err != nil {
54
                t.Fatalf("New() failed: %s", err)
55
        }
56
        // Don't send any messages.
57
        s.Close()
58
}
59
 
60
func TestNewLogger(t *testing.T) {
61
        if skipNetTest(t) {
62
                return
63
        }
64
        f := NewLogger(LOG_INFO, 0)
65
        if f == nil {
66
                t.Error("NewLogger() failed")
67
        }
68
}
69
 
70
func TestDial(t *testing.T) {
71
        if skipNetTest(t) {
72
                return
73
        }
74
        l, err := Dial("", "", LOG_ERR, "syslog_test")
75
        if err != nil {
76
                t.Fatalf("Dial() failed: %s", err)
77
        }
78
        l.Close()
79
}
80
 
81
func TestUDPDial(t *testing.T) {
82
        done := make(chan string)
83
        startServer(done)
84
        l, err := Dial("udp", serverAddr, LOG_INFO, "syslog_test")
85
        if err != nil {
86
                t.Fatalf("syslog.Dial() failed: %s", err)
87
        }
88
        msg := "udp test"
89
        l.Info(msg)
90
        expected := "<6>syslog_test: udp test\n"
91
        rcvd := <-done
92
        if rcvd != expected {
93
                t.Fatalf("s.Info() = '%q', but wanted '%q'", rcvd, expected)
94
        }
95
}
96
 
97
func TestWrite(t *testing.T) {
98
        done := make(chan string)
99
        startServer(done)
100
        l, err := Dial("udp", serverAddr, LOG_ERR, "syslog_test")
101
        if err != nil {
102
                t.Fatalf("syslog.Dial() failed: %s", err)
103
        }
104
        msg := "write test"
105
        _, err = io.WriteString(l, msg)
106
        if err != nil {
107
                t.Fatalf("WriteString() failed: %s", err)
108
        }
109
        expected := "<3>syslog_test: write test\n"
110
        rcvd := <-done
111
        if rcvd != expected {
112
                t.Fatalf("s.Info() = '%q', but wanted '%q'", rcvd, expected)
113
        }
114
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.