manager_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package manager
  2. import (
  3. "crypto/rand"
  4. "fmt"
  5. "github.com/zricethezav/gitleaks/v5/config"
  6. "github.com/zricethezav/gitleaks/v5/options"
  7. "io"
  8. "testing"
  9. )
  10. // TODO
  11. // add more substantial tests... but since literally every pkg uses manager
  12. // these tests are kind of redundant
  13. func TestSendReceiveLeaks(t *testing.T) {
  14. tests := []struct {
  15. leaksToAdd int
  16. goRoutines int
  17. }{
  18. {
  19. leaksToAdd: 10,
  20. },
  21. {
  22. leaksToAdd: 1000,
  23. },
  24. }
  25. for _, test := range tests {
  26. opts := options.Options{}
  27. cfg, _ := config.NewConfig(opts)
  28. m, _ := NewManager(opts, cfg)
  29. for i := 0; i < test.leaksToAdd; i++ {
  30. // we are testing the sync of sending/receiving leaks so we need
  31. // the hash generation in sendLeaks to be unique for each iteration
  32. // so I'm just setting the offender string as a uuid
  33. m.SendLeaks(Leak{Offender: newUUID()})
  34. }
  35. got := m.GetLeaks()
  36. if len(got) != test.leaksToAdd {
  37. t.Errorf("got %d, wanted %d leaks", len(got), test.leaksToAdd)
  38. }
  39. }
  40. }
  41. func TestSendReceiveMeta(t *testing.T) {
  42. tests := []struct {
  43. scanTime int64
  44. patchTime int64
  45. cloneTime int64
  46. regexTime int64
  47. iterations int
  48. }{
  49. {
  50. scanTime: 1000,
  51. patchTime: 1000,
  52. cloneTime: 1000,
  53. regexTime: 1000,
  54. iterations: 100,
  55. },
  56. }
  57. for _, test := range tests {
  58. opts := options.Options{}
  59. cfg, _ := config.NewConfig(opts)
  60. m, _ := NewManager(opts, cfg)
  61. for i := 0; i < test.iterations; i++ {
  62. m.RecordTime(ScanTime(test.scanTime))
  63. m.RecordTime(PatchTime(test.patchTime))
  64. m.RecordTime(CloneTime(test.cloneTime))
  65. m.RecordTime(RegexTime{
  66. Regex: "regex",
  67. Time: test.regexTime,
  68. })
  69. m.RecordTime(RegexTime{
  70. Regex: "regex2",
  71. Time: test.regexTime,
  72. })
  73. }
  74. md := m.GetMetadata()
  75. if md.cloneTime != test.cloneTime*int64(test.iterations) {
  76. t.Errorf("clone time mismatch, got %d, wanted %d",
  77. md.cloneTime, test.cloneTime*int64(test.iterations))
  78. }
  79. if md.ScanTime != test.scanTime*int64(test.iterations) {
  80. t.Errorf("scan time mismatch, got %d, wanted %d",
  81. md.ScanTime, test.scanTime*int64(test.iterations))
  82. }
  83. if md.patchTime != test.patchTime*int64(test.iterations) {
  84. t.Errorf("clone time mismatch, got %d, wanted %d",
  85. md.patchTime, test.patchTime*int64(test.iterations))
  86. }
  87. }
  88. }
  89. // newUUID generates a random UUID according to RFC 4122
  90. // Ripped from https://play.golang.org/p/4FkNSiUDMg
  91. func newUUID() string {
  92. uuid := make([]byte, 16)
  93. io.ReadFull(rand.Reader, uuid)
  94. // variant bits; see section 4.1.1
  95. uuid[8] = uuid[8]&^0xc0 | 0x80
  96. // version 4 (pseudo-random); see section 4.1.3
  97. uuid[6] = uuid[6]&^0xf0 | 0x40
  98. return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
  99. }