manager_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package manager
  2. import (
  3. "github.com/zricethezav/gitleaks/config"
  4. "github.com/zricethezav/gitleaks/options"
  5. "testing"
  6. )
  7. // TODO
  8. // add more substantial tests... but since literally every pkg uses manager
  9. // these tests are kind of redundant
  10. func TestSendReceiveLeaks(t *testing.T) {
  11. tests := []struct {
  12. leaksToAdd int
  13. goRoutines int
  14. }{
  15. {
  16. leaksToAdd: 10,
  17. },
  18. {
  19. leaksToAdd: 1000,
  20. },
  21. }
  22. for _, test := range tests {
  23. opts := options.Options{}
  24. cfg, _ := config.NewConfig(opts)
  25. m, _ := NewManager(opts, cfg)
  26. for i := 0; i < test.leaksToAdd; i++ {
  27. m.SendLeaks(Leak{})
  28. }
  29. got := m.GetLeaks()
  30. if len(got) != test.leaksToAdd {
  31. t.Errorf("got %d, wanted %d leaks", len(got), test.leaksToAdd)
  32. }
  33. }
  34. }
  35. func TestSendReceiveMeta(t *testing.T) {
  36. tests := []struct {
  37. auditTime int64
  38. patchTime int64
  39. cloneTime int64
  40. regexTime int64
  41. iterations int
  42. }{
  43. {
  44. auditTime: 1000,
  45. patchTime: 1000,
  46. cloneTime: 1000,
  47. regexTime: 1000,
  48. iterations: 100,
  49. },
  50. }
  51. for _, test := range tests {
  52. opts := options.Options{}
  53. cfg, _ := config.NewConfig(opts)
  54. m, _ := NewManager(opts, cfg)
  55. for i := 0; i < test.iterations; i++ {
  56. m.RecordTime(AuditTime(test.auditTime))
  57. m.RecordTime(PatchTime(test.patchTime))
  58. m.RecordTime(CloneTime(test.cloneTime))
  59. m.RecordTime(RegexTime{
  60. Regex: "regex",
  61. Time: test.regexTime,
  62. })
  63. m.RecordTime(RegexTime{
  64. Regex: "regex2",
  65. Time: test.regexTime,
  66. })
  67. }
  68. md := m.GetMetadata()
  69. if md.cloneTime != test.cloneTime*int64(test.iterations) {
  70. t.Errorf("clone time mismatch, got %d, wanted %d",
  71. md.cloneTime, test.cloneTime*int64(test.iterations))
  72. }
  73. if md.AuditTime != test.auditTime*int64(test.iterations) {
  74. t.Errorf("audit time mismatch, got %d, wanted %d",
  75. md.AuditTime, test.auditTime*int64(test.iterations))
  76. }
  77. if md.patchTime != test.patchTime*int64(test.iterations) {
  78. t.Errorf("clone time mismatch, got %d, wanted %d",
  79. md.patchTime, test.patchTime*int64(test.iterations))
  80. }
  81. }
  82. }