From a80093a7279cb2162db028f5672bd824040be658 Mon Sep 17 00:00:00 2001 From: V2Ray Date: Sat, 10 Oct 2015 23:41:45 +0200 Subject: [PATCH] Test case for access log --- common/log/access.go | 6 ++++++ common/log/access_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 common/log/access_test.go diff --git a/common/log/access.go b/common/log/access.go index 76b05a881..c3c68f7c5 100644 --- a/common/log/access.go +++ b/common/log/access.go @@ -33,6 +33,11 @@ type accessLog struct { type fileAccessLogger struct { queue chan *accessLog logger *log.Logger + file *os.File +} + +func (logger *fileAccessLogger) close() { + logger.file.Close() } func (logger *fileAccessLogger) Log(from, to string, status AccessStatus, reason string) { @@ -59,6 +64,7 @@ func newFileAccessLogger(path string) accessLogger { return &fileAccessLogger{ queue: make(chan *accessLog, 16), logger: log.New(file, "", log.Ldate|log.Ltime), + file: file, } } diff --git a/common/log/access_test.go b/common/log/access_test.go new file mode 100644 index 000000000..cb58cec96 --- /dev/null +++ b/common/log/access_test.go @@ -0,0 +1,34 @@ +package log + +import ( + "io/ioutil" + "os" + "strings" + "testing" + "time" + + "github.com/v2ray/v2ray-core/testing/unit" +) + +func TestAccessLog(t *testing.T) { + assert := unit.Assert(t) + + filename := "/tmp/test_access_log.log" + InitAccessLogger(filename) + _, err := os.Stat(filename) + assert.Error(err).IsNil() + + Access("test_from", "test_to", AccessAccepted, "test_reason") + <-time.After(2 * time.Second) + + accessLoggerInstance.(*fileAccessLogger).close() + accessLoggerInstance = &noOpAccessLogger{} + + content, err := ioutil.ReadFile(filename) + assert.Error(err).IsNil() + + assert.Bool(strings.Contains(string(content), "test_from")).IsTrue() + assert.Bool(strings.Contains(string(content), "test_to")).IsTrue() + assert.Bool(strings.Contains(string(content), "test_reason")).IsTrue() + assert.Bool(strings.Contains(string(content), "accepted")).IsTrue() +}