access_test.go 848 B

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