syncthing/cmd/syncthing/auditservice_test.go

55 lines
1.3 KiB
Go
Raw Normal View History

2015-04-25 11:21:47 +02:00
// Copyright (C) 2015 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
2015-04-25 11:21:47 +02:00
package main
import (
"bytes"
"strings"
"testing"
"time"
2015-08-06 11:29:25 +02:00
"github.com/syncthing/syncthing/lib/events"
2015-04-25 11:21:47 +02:00
)
func TestAuditService(t *testing.T) {
buf := new(bytes.Buffer)
2015-12-23 16:31:12 +01:00
service := newAuditService(buf)
2015-04-25 11:21:47 +02:00
// Event sent before start, will not be logged
events.Default.Log(events.ConfigSaved, "the first event")
2015-04-25 11:21:47 +02:00
2015-12-23 16:31:12 +01:00
go service.Serve()
service.WaitForStart()
2015-04-25 11:21:47 +02:00
// Event that should end up in the audit log
events.Default.Log(events.ConfigSaved, "the second event")
2015-04-25 11:21:47 +02:00
// We need to give the events time to arrive, since the channels are buffered etc.
time.Sleep(10 * time.Millisecond)
2015-12-23 16:31:12 +01:00
service.Stop()
service.WaitForStop()
2015-04-25 11:21:47 +02:00
// This event should not be logged, since we have stopped.
events.Default.Log(events.ConfigSaved, "the third event")
2015-04-25 11:21:47 +02:00
result := string(buf.Bytes())
t.Log(result)
if strings.Contains(result, "first event") {
t.Error("Unexpected first event")
}
if !strings.Contains(result, "second event") {
t.Error("Missing second event")
}
if strings.Contains(result, "third event") {
t.Error("Missing third event")
}
}