2015-10-10 17:41:45 -04:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-12-05 15:10:14 -05:00
|
|
|
"github.com/v2ray/v2ray-core/common/serial"
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
|
|
|
"github.com/v2ray/v2ray-core/testing/assert"
|
2015-10-10 17:41:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccessLog(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-10-10 17:41:45 -04:00
|
|
|
|
|
|
|
filename := "/tmp/test_access_log.log"
|
|
|
|
InitAccessLogger(filename)
|
|
|
|
_, err := os.Stat(filename)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
2016-01-18 06:31:27 -05:00
|
|
|
Access(serial.StringLiteral("test_from"), serial.StringLiteral("test_to"), AccessAccepted, serial.StringLiteral("test_reason"))
|
2015-10-10 17:41:45 -04:00
|
|
|
<-time.After(2 * time.Second)
|
|
|
|
|
2015-12-05 15:10:14 -05:00
|
|
|
accessLoggerInstance.(*fileLogWriter).close()
|
|
|
|
accessLoggerInstance = &noOpLogWriter{}
|
2015-10-10 17:41:45 -04:00
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(filename)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
2015-12-05 15:10:14 -05:00
|
|
|
contentStr := serial.StringLiteral(content)
|
|
|
|
assert.String(contentStr).Contains(serial.StringLiteral("test_from"))
|
|
|
|
assert.String(contentStr).Contains(serial.StringLiteral("test_to"))
|
|
|
|
assert.String(contentStr).Contains(serial.StringLiteral("test_reason"))
|
|
|
|
assert.String(contentStr).Contains(serial.StringLiteral("accepted"))
|
2015-10-10 17:41:45 -04:00
|
|
|
}
|