auditservice_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "bytes"
  9. "strings"
  10. "testing"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/events"
  13. )
  14. func TestAuditService(t *testing.T) {
  15. buf := new(bytes.Buffer)
  16. service := newAuditService(buf)
  17. // Event sent before start, will not be logged
  18. events.Default.Log(events.ConfigSaved, "the first event")
  19. go service.Serve()
  20. service.WaitForStart()
  21. // Event that should end up in the audit log
  22. events.Default.Log(events.ConfigSaved, "the second event")
  23. // We need to give the events time to arrive, since the channels are buffered etc.
  24. time.Sleep(10 * time.Millisecond)
  25. service.Stop()
  26. service.WaitForStop()
  27. // This event should not be logged, since we have stopped.
  28. events.Default.Log(events.ConfigSaved, "the third event")
  29. result := string(buf.Bytes())
  30. t.Log(result)
  31. if strings.Contains(result, "first event") {
  32. t.Error("Unexpected first event")
  33. }
  34. if !strings.Contains(result, "second event") {
  35. t.Error("Missing second event")
  36. }
  37. if strings.Contains(result, "third event") {
  38. t.Error("Missing third event")
  39. }
  40. }