access_test.go 895 B

1234567891011121314151617181920212223242526272829303132333435
  1. package log
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "strings"
  6. "testing"
  7. "time"
  8. v2testing "github.com/v2ray/v2ray-core/testing"
  9. "github.com/v2ray/v2ray-core/testing/assert"
  10. )
  11. func TestAccessLog(t *testing.T) {
  12. v2testing.Current(t)
  13. filename := "/tmp/test_access_log.log"
  14. InitAccessLogger(filename)
  15. _, err := os.Stat(filename)
  16. assert.Error(err).IsNil()
  17. Access("test_from", "test_to", AccessAccepted, "test_reason")
  18. <-time.After(2 * time.Second)
  19. accessLoggerInstance.(*fileAccessLogger).close()
  20. accessLoggerInstance = &noOpAccessLogger{}
  21. content, err := ioutil.ReadFile(filename)
  22. assert.Error(err).IsNil()
  23. assert.Bool(strings.Contains(string(content), "test_from")).IsTrue()
  24. assert.Bool(strings.Contains(string(content), "test_to")).IsTrue()
  25. assert.Bool(strings.Contains(string(content), "test_reason")).IsTrue()
  26. assert.Bool(strings.Contains(string(content), "accepted")).IsTrue()
  27. }