unstaged_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package scan_test
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/zricethezav/gitleaks/v7/config"
  8. "github.com/zricethezav/gitleaks/v7/options"
  9. "github.com/zricethezav/gitleaks/v7/scan"
  10. )
  11. func TestUnstaged(t *testing.T) {
  12. err := moveDotGit("dotGit", ".git")
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. defer moveDotGit(".git", "dotGit")
  17. tests := []struct {
  18. description string
  19. opts options.Options
  20. wantPath string
  21. fileToChange string
  22. change string
  23. empty bool
  24. }{
  25. {
  26. description: "basic repo with unstagged change containing a secret",
  27. opts: options.Options{
  28. Path: filepath.Join(repoBasePath, "basic"),
  29. Report: filepath.Join(expectPath, "basic", "results_unstaged.json.got"),
  30. ReportFormat: "json",
  31. Unstaged: true,
  32. },
  33. wantPath: filepath.Join(expectPath, "basic", "results_unstaged.json"),
  34. fileToChange: filepath.Join(repoBasePath, "basic", "secrets.py"),
  35. change: "\nadded_aws_access_key_id='AKIAIO5FODNN7DXAMPLE'\n",
  36. },
  37. {
  38. description: "basic repo with unstagged change not containing a secret",
  39. opts: options.Options{
  40. Path: filepath.Join(repoBasePath, "basic"),
  41. Report: filepath.Join(expectPath, "basic", "results_unstaged.json.got"),
  42. ReportFormat: "json",
  43. Unstaged: true,
  44. },
  45. empty: true,
  46. fileToChange: filepath.Join(repoBasePath, "basic", "secrets.py"),
  47. change: "\nnice_variable='is_nice''\n",
  48. },
  49. }
  50. for _, test := range tests {
  51. var old []byte
  52. if test.fileToChange != "" {
  53. old, err = ioutil.ReadFile(test.fileToChange)
  54. if err != nil {
  55. t.Error(err)
  56. }
  57. altered, err := os.OpenFile(test.fileToChange,
  58. os.O_WRONLY|os.O_APPEND, 0644)
  59. if err != nil {
  60. t.Error(err)
  61. }
  62. _, err = altered.WriteString(test.change)
  63. if err != nil {
  64. t.Error(err)
  65. }
  66. }
  67. cfg, err := config.NewConfig(test.opts)
  68. if err != nil {
  69. t.Error(err)
  70. }
  71. scanner, err := scan.NewScanner(test.opts, cfg)
  72. if err != nil {
  73. t.Fatal(test.description, err)
  74. }
  75. scannerReport, err := scanner.Scan()
  76. if err != nil {
  77. t.Fatal(test.description, err)
  78. }
  79. err = scan.WriteReport(scannerReport, test.opts, cfg)
  80. if err != nil {
  81. t.Error(test.description, err)
  82. }
  83. if test.empty {
  84. if len(scannerReport.Leaks) != 0 {
  85. t.Errorf("%s wanted no leaks but got some instead: %+v", test.description, scannerReport.Leaks)
  86. }
  87. }
  88. if test.wantPath != "" {
  89. err := fileCheck(test.wantPath, test.opts.Report)
  90. if err != nil {
  91. t.Error(test.description, err)
  92. }
  93. }
  94. err = ioutil.WriteFile(test.fileToChange, old, 0)
  95. if err != nil {
  96. t.Error(err)
  97. }
  98. }
  99. }