gitleaks_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "regexp"
  8. "strings"
  9. "testing"
  10. "github.com/franela/goblin"
  11. git "gopkg.in/src-d/go-git.v4"
  12. "gopkg.in/src-d/go-git.v4/storage/memory"
  13. )
  14. const testWhitelistCommit = `
  15. [[regexes]]
  16. description = "AWS"
  17. regex = '''AKIA[0-9A-Z]{16}'''
  18. [whitelist]
  19. commits = [
  20. "eaeffdc65b4c73ccb67e75d96bd8743be2c85973",
  21. ]
  22. `
  23. const testWhitelistFile = `
  24. [[regexes]]
  25. description = "AWS"
  26. regex = '''AKIA[0-9A-Z]{16}'''
  27. [whitelist]
  28. files = [
  29. ".go",
  30. ]
  31. `
  32. const testWhitelistBranch = `
  33. [[regexes]]
  34. description = "AWS"
  35. regex = '''AKIA[0-9A-Z]{16}'''
  36. [whitelist]
  37. branches = [
  38. "origin/master",
  39. ]
  40. `
  41. const testWhitelistRegex = `
  42. [[regexes]]
  43. description = "AWS"
  44. regex = '''AKIA[0-9A-Z]{16}'''
  45. [whitelist]
  46. regexes= [
  47. "AKIA",
  48. ]
  49. `
  50. const testWhitelistRepo = `
  51. [[regexes]]
  52. description = "AWS"
  53. regex = '''AKIA[0-9A-Z]{16}'''
  54. [whitelist]
  55. repos = [
  56. "gronit",
  57. ]
  58. `
  59. var benchmarkRepo *RepoDescriptor
  60. var benchmarkLeaksRepo *RepoDescriptor
  61. func getBenchmarkLeaksRepo() *RepoDescriptor {
  62. if benchmarkLeaksRepo != nil {
  63. return benchmarkLeaksRepo
  64. }
  65. leaksR, _ := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  66. URL: "https://github.com/gitleakstest/gronit.git",
  67. })
  68. benchmarkLeaksRepo = &RepoDescriptor{
  69. repository: leaksR,
  70. }
  71. return benchmarkLeaksRepo
  72. }
  73. func getBenchmarkRepo() *RepoDescriptor {
  74. if benchmarkRepo != nil {
  75. return benchmarkRepo
  76. }
  77. bmRepo, _ := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  78. URL: "https://github.com/apple/swift-package-manager.git",
  79. })
  80. benchmarkRepo = &RepoDescriptor{
  81. repository: bmRepo,
  82. }
  83. return benchmarkRepo
  84. }
  85. func TestGetRepo(t *testing.T) {
  86. var err error
  87. dir, err = ioutil.TempDir("", "gitleaksTestRepo")
  88. defer os.RemoveAll(dir)
  89. if err != nil {
  90. panic(err)
  91. }
  92. _, err = git.PlainClone(dir, false, &git.CloneOptions{
  93. URL: "https://github.com/gitleakstest/gronit",
  94. })
  95. if err != nil {
  96. panic(err)
  97. }
  98. var tests = []struct {
  99. testOpts Options
  100. description string
  101. expectedErrMsg string
  102. }{
  103. {
  104. testOpts: Options{
  105. Repo: "https://github.com/gitleakstest/gronit",
  106. },
  107. description: "test plain clone remote repo",
  108. expectedErrMsg: "",
  109. },
  110. {
  111. testOpts: Options{
  112. Repo: "https://github.com/gitleakstest/gronit",
  113. Disk: true,
  114. },
  115. description: "test on disk clone remote repo",
  116. expectedErrMsg: "",
  117. },
  118. {
  119. testOpts: Options{
  120. RepoPath: dir,
  121. },
  122. description: "test local clone repo",
  123. expectedErrMsg: "",
  124. },
  125. {
  126. testOpts: Options{
  127. Repo: "https://github.com/gitleakstest/nope",
  128. },
  129. description: "test no repo",
  130. expectedErrMsg: "authentication required",
  131. },
  132. {
  133. testOpts: Options{
  134. Repo: "https://github.com/gitleakstest/private",
  135. IncludePrivate: true,
  136. },
  137. description: "test private repo",
  138. expectedErrMsg: "invalid auth method",
  139. },
  140. {
  141. testOpts: Options{
  142. Repo: "https://github.com/gitleakstest/private",
  143. IncludePrivate: true,
  144. Disk: true,
  145. },
  146. description: "test private repo",
  147. expectedErrMsg: "invalid auth method",
  148. },
  149. }
  150. g := goblin.Goblin(t)
  151. for _, test := range tests {
  152. g.Describe("TestGetRepo", func() {
  153. g.It(test.description, func() {
  154. opts = test.testOpts
  155. _, err := cloneRepo()
  156. if err != nil {
  157. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  158. }
  159. })
  160. })
  161. }
  162. }
  163. func TestRun(t *testing.T) {
  164. var err error
  165. configsDir := testTomlLoader()
  166. dir, err = ioutil.TempDir("", "gitleaksTestOwner")
  167. defer os.RemoveAll(dir)
  168. if err != nil {
  169. panic(err)
  170. }
  171. git.PlainClone(dir+"/gronit", false, &git.CloneOptions{
  172. URL: "https://github.com/gitleakstest/gronit",
  173. })
  174. git.PlainClone(dir+"/h1domains", false, &git.CloneOptions{
  175. URL: "https://github.com/gitleakstest/h1domains",
  176. })
  177. var tests = []struct {
  178. testOpts Options
  179. description string
  180. expectedErrMsg string
  181. whiteListRepos []string
  182. numLeaks int
  183. configPath string
  184. }{
  185. {
  186. testOpts: Options{
  187. GithubUser: "gitleakstest",
  188. },
  189. description: "test github user",
  190. numLeaks: 2,
  191. expectedErrMsg: "",
  192. },
  193. {
  194. testOpts: Options{
  195. GithubUser: "gitleakstest",
  196. Disk: true,
  197. },
  198. description: "test github user on disk ",
  199. numLeaks: 2,
  200. expectedErrMsg: "",
  201. },
  202. {
  203. testOpts: Options{
  204. GithubOrg: "gitleakstestorg",
  205. },
  206. description: "test github org",
  207. numLeaks: 2,
  208. expectedErrMsg: "",
  209. },
  210. {
  211. testOpts: Options{
  212. GithubOrg: "gitleakstestorg",
  213. Disk: true,
  214. },
  215. description: "test org on disk",
  216. numLeaks: 2,
  217. expectedErrMsg: "",
  218. },
  219. {
  220. testOpts: Options{
  221. OwnerPath: dir,
  222. },
  223. description: "test owner path",
  224. numLeaks: 2,
  225. expectedErrMsg: "",
  226. },
  227. {
  228. testOpts: Options{
  229. GithubOrg: "gitleakstestorg",
  230. IncludePrivate: true,
  231. SSHKey: "reallyreallyreallyreallywrongpath",
  232. },
  233. description: "test private org no ssh",
  234. numLeaks: 0,
  235. expectedErrMsg: "unable to generate ssh key: open reallyreallyreallyreallywrongpath: no such file or directory",
  236. },
  237. {
  238. testOpts: Options{
  239. Repo: "https://github.com/gitleakstest/gronit.git",
  240. },
  241. description: "test leak",
  242. numLeaks: 2,
  243. expectedErrMsg: "",
  244. },
  245. {
  246. testOpts: Options{
  247. Repo: "https://github.com/gitleakstest/h1domains.git",
  248. },
  249. description: "test clean",
  250. numLeaks: 0,
  251. expectedErrMsg: "",
  252. },
  253. {
  254. testOpts: Options{
  255. Repo: "https://github.com/gitleakstest/empty.git",
  256. },
  257. description: "test empty",
  258. numLeaks: 0,
  259. expectedErrMsg: "reference not found",
  260. },
  261. {
  262. testOpts: Options{
  263. GithubOrg: "gitleakstestorg",
  264. },
  265. description: "test github org",
  266. numLeaks: 0,
  267. expectedErrMsg: "",
  268. configPath: path.Join(configsDir, "repo"),
  269. },
  270. }
  271. g := goblin.Goblin(t)
  272. for _, test := range tests {
  273. g.Describe("TestRun", func() {
  274. g.It(test.description, func() {
  275. if test.configPath != "" {
  276. os.Setenv("GITLEAKS_CONFIG", test.configPath)
  277. }
  278. opts = test.testOpts
  279. leaks, err := run()
  280. if err != nil {
  281. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  282. }
  283. g.Assert(len(leaks)).Equal(test.numLeaks)
  284. })
  285. })
  286. }
  287. }
  288. func TestWriteReport(t *testing.T) {
  289. tmpDir, _ := ioutil.TempDir("", "reportDir")
  290. reportJSON := path.Join(tmpDir, "report.json")
  291. reportCSV := path.Join(tmpDir, "report.csv")
  292. defer os.RemoveAll(tmpDir)
  293. leaks := []Leak{
  294. {
  295. Line: "eat",
  296. Commit: "your",
  297. Offender: "veggies",
  298. Type: "and",
  299. Message: "get",
  300. Author: "some",
  301. File: "sleep",
  302. Branch: "thxu",
  303. },
  304. }
  305. var tests = []struct {
  306. leaks []Leak
  307. reportFile string
  308. fileName string
  309. description string
  310. testOpts Options
  311. }{
  312. {
  313. leaks: leaks,
  314. reportFile: reportJSON,
  315. fileName: "report.json",
  316. description: "can we write a file",
  317. testOpts: Options{
  318. Report: reportJSON,
  319. },
  320. },
  321. {
  322. leaks: leaks,
  323. reportFile: reportCSV,
  324. fileName: "report.csv",
  325. description: "can we write a file",
  326. testOpts: Options{
  327. Report: reportCSV,
  328. CSV: true,
  329. },
  330. },
  331. }
  332. g := goblin.Goblin(t)
  333. for _, test := range tests {
  334. g.Describe("TestWriteReport", func() {
  335. g.It(test.description, func() {
  336. opts = test.testOpts
  337. writeReport(test.leaks)
  338. f, _ := os.Stat(test.reportFile)
  339. g.Assert(f.Name()).Equal(test.fileName)
  340. })
  341. })
  342. }
  343. }
  344. func testTomlLoader() string {
  345. tmpDir, _ := ioutil.TempDir("", "whiteListConfigs")
  346. ioutil.WriteFile(path.Join(tmpDir, "regex"), []byte(testWhitelistRegex), 0644)
  347. ioutil.WriteFile(path.Join(tmpDir, "branch"), []byte(testWhitelistBranch), 0644)
  348. ioutil.WriteFile(path.Join(tmpDir, "commit"), []byte(testWhitelistCommit), 0644)
  349. ioutil.WriteFile(path.Join(tmpDir, "file"), []byte(testWhitelistFile), 0644)
  350. ioutil.WriteFile(path.Join(tmpDir, "repo"), []byte(testWhitelistRepo), 0644)
  351. return tmpDir
  352. }
  353. func TestAuditRepo(t *testing.T) {
  354. var leaks []Leak
  355. err := loadToml()
  356. configsDir := testTomlLoader()
  357. defer os.RemoveAll(configsDir)
  358. if err != nil {
  359. panic(err)
  360. }
  361. leaksR, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  362. URL: "https://github.com/gitleakstest/gronit.git",
  363. })
  364. if err != nil {
  365. panic(err)
  366. }
  367. leaksRepo := &RepoDescriptor{
  368. repository: leaksR,
  369. name: "gronit",
  370. }
  371. cleanR, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  372. URL: "https://github.com/gitleakstest/h1domains.git",
  373. })
  374. if err != nil {
  375. panic(err)
  376. }
  377. cleanRepo := &RepoDescriptor{
  378. repository: cleanR,
  379. name: "h1domains",
  380. }
  381. var tests = []struct {
  382. testOpts Options
  383. description string
  384. expectedErrMsg string
  385. numLeaks int
  386. repo *RepoDescriptor
  387. whiteListFiles []*regexp.Regexp
  388. whiteListCommits map[string]bool
  389. whiteListBranches []string
  390. whiteListRepos []string
  391. whiteListRegexes []*regexp.Regexp
  392. configPath string
  393. }{
  394. {
  395. repo: leaksRepo,
  396. description: "two leaks present",
  397. numLeaks: 2,
  398. },
  399. {
  400. repo: leaksRepo,
  401. description: "two leaks present limit goroutines",
  402. numLeaks: 2,
  403. testOpts: Options{
  404. MaxGoRoutines: 4,
  405. },
  406. },
  407. {
  408. repo: leaksRepo,
  409. description: "audit specific bad branch",
  410. numLeaks: 2,
  411. testOpts: Options{
  412. Branch: "master",
  413. },
  414. },
  415. {
  416. repo: leaksRepo,
  417. description: "audit specific good branch",
  418. numLeaks: 0,
  419. testOpts: Options{
  420. Branch: "dev",
  421. },
  422. },
  423. {
  424. repo: leaksRepo,
  425. description: "audit all branch",
  426. numLeaks: 6,
  427. testOpts: Options{
  428. AuditAllRefs: true,
  429. },
  430. },
  431. {
  432. repo: leaksRepo,
  433. description: "audit all branch whitelist 1",
  434. numLeaks: 4,
  435. testOpts: Options{
  436. AuditAllRefs: true,
  437. },
  438. whiteListBranches: []string{
  439. "origin/master",
  440. },
  441. },
  442. {
  443. repo: leaksRepo,
  444. description: "two leaks present whitelist AWS.. no leaks",
  445. whiteListRegexes: []*regexp.Regexp{
  446. regexp.MustCompile("AKIA"),
  447. },
  448. numLeaks: 0,
  449. },
  450. {
  451. repo: leaksRepo,
  452. description: "two leaks present limit goroutines",
  453. numLeaks: 2,
  454. },
  455. {
  456. repo: cleanRepo,
  457. description: "no leaks present",
  458. numLeaks: 0,
  459. },
  460. {
  461. repo: leaksRepo,
  462. description: "two leaks present whitelist go files",
  463. whiteListFiles: []*regexp.Regexp{
  464. regexp.MustCompile(".go"),
  465. },
  466. numLeaks: 0,
  467. },
  468. {
  469. // note this double counts the first commit since we are whitelisting
  470. // a "bad" first commit
  471. repo: leaksRepo,
  472. description: "two leaks present whitelist bad commit",
  473. whiteListCommits: map[string]bool{
  474. "eaeffdc65b4c73ccb67e75d96bd8743be2c85973": true,
  475. },
  476. numLeaks: 2,
  477. },
  478. {
  479. repo: leaksRepo,
  480. description: "redact",
  481. testOpts: Options{
  482. Redact: true,
  483. },
  484. numLeaks: 2,
  485. },
  486. {
  487. repo: leaksRepo,
  488. description: "toml whitelist regex",
  489. configPath: path.Join(configsDir, "regex"),
  490. numLeaks: 0,
  491. },
  492. {
  493. repo: leaksRepo,
  494. description: "toml whitelist branch",
  495. configPath: path.Join(configsDir, "branch"),
  496. testOpts: Options{
  497. AuditAllRefs: true,
  498. },
  499. numLeaks: 4,
  500. },
  501. {
  502. repo: leaksRepo,
  503. description: "toml whitelist file",
  504. configPath: path.Join(configsDir, "file"),
  505. numLeaks: 0,
  506. },
  507. {
  508. // note this double counts the first commit since we are whitelisting
  509. // a "bad" first commit
  510. repo: leaksRepo,
  511. description: "toml whitelist commit",
  512. configPath: path.Join(configsDir, "commit"),
  513. numLeaks: 2,
  514. },
  515. {
  516. repo: leaksRepo,
  517. description: "audit whitelist repo",
  518. numLeaks: 0,
  519. whiteListRepos: []string{
  520. "gronit",
  521. },
  522. },
  523. {
  524. repo: leaksRepo,
  525. description: "toml whitelist repo",
  526. numLeaks: 0,
  527. configPath: path.Join(configsDir, "repo"),
  528. },
  529. }
  530. whiteListCommits = make(map[string]bool)
  531. g := goblin.Goblin(t)
  532. for _, test := range tests {
  533. g.Describe("TestAuditRepo", func() {
  534. g.It(test.description, func() {
  535. opts = test.testOpts
  536. // settin da globs
  537. if test.whiteListFiles != nil {
  538. whiteListFiles = test.whiteListFiles
  539. } else {
  540. whiteListFiles = nil
  541. }
  542. if test.whiteListCommits != nil {
  543. whiteListCommits = test.whiteListCommits
  544. } else {
  545. whiteListCommits = nil
  546. }
  547. if test.whiteListBranches != nil {
  548. whiteListBranches = test.whiteListBranches
  549. } else {
  550. whiteListBranches = nil
  551. }
  552. if test.whiteListRegexes != nil {
  553. whiteListRegexes = test.whiteListRegexes
  554. } else {
  555. whiteListRegexes = nil
  556. }
  557. if test.whiteListRepos != nil {
  558. whiteListRepos = test.whiteListRepos
  559. } else {
  560. whiteListRepos = nil
  561. }
  562. // config paths
  563. if test.configPath != "" {
  564. os.Setenv("GITLEAKS_CONFIG", test.configPath)
  565. loadToml()
  566. }
  567. leaks, err = auditGitRepo(test.repo)
  568. if opts.Redact {
  569. g.Assert(leaks[0].Offender).Equal("REDACTED")
  570. }
  571. g.Assert(len(leaks)).Equal(test.numLeaks)
  572. })
  573. })
  574. }
  575. }
  576. func TestOptionGuard(t *testing.T) {
  577. var tests = []struct {
  578. testOpts Options
  579. githubToken bool
  580. description string
  581. expectedErrMsg string
  582. expectedErrMsgFuzzy string
  583. }{
  584. {
  585. testOpts: Options{},
  586. description: "default no opts",
  587. expectedErrMsg: "",
  588. },
  589. {
  590. testOpts: Options{
  591. IncludePrivate: true,
  592. GithubOrg: "fakeOrg",
  593. },
  594. description: "private org no githubtoken",
  595. expectedErrMsg: "user/organization private repos require env var GITHUB_TOKEN to be set",
  596. githubToken: false,
  597. },
  598. {
  599. testOpts: Options{
  600. IncludePrivate: true,
  601. GithubUser: "fakeUser",
  602. },
  603. description: "private user no githubtoken",
  604. expectedErrMsg: "user/organization private repos require env var GITHUB_TOKEN to be set",
  605. githubToken: false,
  606. },
  607. {
  608. testOpts: Options{
  609. IncludePrivate: true,
  610. GithubUser: "fakeUser",
  611. GithubOrg: "fakeOrg",
  612. },
  613. description: "double owner",
  614. expectedErrMsg: "github user and organization set",
  615. },
  616. {
  617. testOpts: Options{
  618. IncludePrivate: true,
  619. GithubOrg: "fakeOrg",
  620. OwnerPath: "/dev/null",
  621. },
  622. description: "local and remote target",
  623. expectedErrMsg: "github organization set and local owner path",
  624. },
  625. {
  626. testOpts: Options{
  627. IncludePrivate: true,
  628. GithubUser: "fakeUser",
  629. OwnerPath: "/dev/null",
  630. },
  631. description: "local and remote target",
  632. expectedErrMsg: "github user set and local owner path",
  633. },
  634. {
  635. testOpts: Options{
  636. GithubUser: "fakeUser",
  637. SingleSearch: "*/./....",
  638. },
  639. description: "single search invalid regex gaurd",
  640. expectedErrMsgFuzzy: "unable to compile regex: */./...., ",
  641. },
  642. {
  643. testOpts: Options{
  644. GithubUser: "fakeUser",
  645. SingleSearch: "mystring",
  646. },
  647. description: "single search regex gaurd",
  648. expectedErrMsg: "",
  649. },
  650. }
  651. g := goblin.Goblin(t)
  652. for _, test := range tests {
  653. g.Describe("Test Option Gaurd", func() {
  654. g.It(test.description, func() {
  655. os.Clearenv()
  656. opts = test.testOpts
  657. if test.githubToken {
  658. os.Setenv("GITHUB_TOKEN", "fakeToken")
  659. }
  660. err := optsGuard()
  661. if err != nil {
  662. if test.expectedErrMsgFuzzy != "" {
  663. g.Assert(strings.Contains(err.Error(), test.expectedErrMsgFuzzy)).Equal(true)
  664. } else {
  665. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  666. }
  667. } else {
  668. g.Assert("").Equal(test.expectedErrMsg)
  669. }
  670. })
  671. })
  672. }
  673. }
  674. func TestLoadToml(t *testing.T) {
  675. tmpDir, _ := ioutil.TempDir("", "gitleaksTestConfigDir")
  676. defer os.RemoveAll(tmpDir)
  677. err := ioutil.WriteFile(path.Join(tmpDir, "gitleaksConfig"), []byte(defaultConfig), 0644)
  678. if err != nil {
  679. panic(err)
  680. }
  681. configPath := path.Join(tmpDir, "gitleaksConfig")
  682. noConfigPath := path.Join(tmpDir, "gitleaksConfigNope")
  683. var tests = []struct {
  684. testOpts Options
  685. description string
  686. configPath string
  687. expectedErrMsg string
  688. singleSearch bool
  689. }{
  690. {
  691. testOpts: Options{
  692. ConfigPath: configPath,
  693. },
  694. description: "path to config",
  695. },
  696. {
  697. testOpts: Options{},
  698. description: "env var path to no config",
  699. singleSearch: true,
  700. },
  701. {
  702. testOpts: Options{
  703. ConfigPath: noConfigPath,
  704. },
  705. description: "no path to config",
  706. expectedErrMsg: fmt.Sprintf("no gitleaks config at %s", noConfigPath),
  707. },
  708. {
  709. testOpts: Options{},
  710. description: "env var path to config",
  711. configPath: configPath,
  712. expectedErrMsg: "",
  713. },
  714. {
  715. testOpts: Options{},
  716. description: "env var path to no config",
  717. configPath: noConfigPath,
  718. expectedErrMsg: fmt.Sprintf("problem loading config: open %s: no such file or directory", noConfigPath),
  719. },
  720. }
  721. g := goblin.Goblin(t)
  722. for _, test := range tests {
  723. g.Describe("TestLoadToml", func() {
  724. g.It(test.description, func() {
  725. opts = test.testOpts
  726. if test.singleSearch {
  727. singleSearchRegex = regexp.MustCompile("test")
  728. } else {
  729. singleSearchRegex = nil
  730. }
  731. if test.configPath != "" {
  732. os.Setenv("GITLEAKS_CONFIG", test.configPath)
  733. } else {
  734. os.Clearenv()
  735. }
  736. err := loadToml()
  737. if err != nil {
  738. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  739. } else {
  740. g.Assert("").Equal(test.expectedErrMsg)
  741. }
  742. })
  743. })
  744. }
  745. }
  746. func BenchmarkAuditRepo1Proc(b *testing.B) {
  747. loadToml()
  748. opts.MaxGoRoutines = 1
  749. benchmarkRepo = getBenchmarkRepo()
  750. for n := 0; n < b.N; n++ {
  751. auditGitRepo(benchmarkRepo)
  752. }
  753. }
  754. func BenchmarkAuditRepo2Proc(b *testing.B) {
  755. loadToml()
  756. opts.MaxGoRoutines = 2
  757. benchmarkRepo = getBenchmarkRepo()
  758. for n := 0; n < b.N; n++ {
  759. auditGitRepo(benchmarkRepo)
  760. }
  761. }
  762. func BenchmarkAuditRepo4Proc(b *testing.B) {
  763. loadToml()
  764. opts.MaxGoRoutines = 4
  765. benchmarkRepo = getBenchmarkRepo()
  766. for n := 0; n < b.N; n++ {
  767. auditGitRepo(benchmarkRepo)
  768. }
  769. }
  770. func BenchmarkAuditRepo8Proc(b *testing.B) {
  771. loadToml()
  772. opts.MaxGoRoutines = 8
  773. benchmarkRepo = getBenchmarkRepo()
  774. for n := 0; n < b.N; n++ {
  775. auditGitRepo(benchmarkRepo)
  776. }
  777. }
  778. func BenchmarkAuditRepo10Proc(b *testing.B) {
  779. loadToml()
  780. opts.MaxGoRoutines = 10
  781. benchmarkRepo = getBenchmarkRepo()
  782. for n := 0; n < b.N; n++ {
  783. auditGitRepo(benchmarkRepo)
  784. }
  785. }
  786. func BenchmarkAuditRepo100Proc(b *testing.B) {
  787. loadToml()
  788. opts.MaxGoRoutines = 100
  789. benchmarkRepo = getBenchmarkRepo()
  790. for n := 0; n < b.N; n++ {
  791. auditGitRepo(benchmarkRepo)
  792. }
  793. }
  794. func BenchmarkAuditRepo1000Proc(b *testing.B) {
  795. loadToml()
  796. opts.MaxGoRoutines = 1000
  797. benchmarkRepo = getBenchmarkRepo()
  798. for n := 0; n < b.N; n++ {
  799. auditGitRepo(benchmarkRepo)
  800. }
  801. }
  802. func BenchmarkAuditRepo10000Proc(b *testing.B) {
  803. loadToml()
  804. opts.MaxGoRoutines = 10000
  805. benchmarkRepo = getBenchmarkRepo()
  806. for n := 0; n < b.N; n++ {
  807. auditGitRepo(benchmarkRepo)
  808. }
  809. }
  810. func BenchmarkAuditRepo100000Proc(b *testing.B) {
  811. loadToml()
  812. opts.MaxGoRoutines = 100000
  813. benchmarkRepo = getBenchmarkRepo()
  814. for n := 0; n < b.N; n++ {
  815. auditGitRepo(benchmarkRepo)
  816. }
  817. }
  818. func BenchmarkAuditLeakRepo1Proc(b *testing.B) {
  819. loadToml()
  820. opts.MaxGoRoutines = 1
  821. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  822. for n := 0; n < b.N; n++ {
  823. auditGitRepo(benchmarkRepo)
  824. }
  825. }
  826. func BenchmarkAuditLeakRepo2Proc(b *testing.B) {
  827. loadToml()
  828. opts.MaxGoRoutines = 2
  829. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  830. for n := 0; n < b.N; n++ {
  831. auditGitRepo(benchmarkRepo)
  832. }
  833. }
  834. func BenchmarkAuditLeakRepo4Proc(b *testing.B) {
  835. loadToml()
  836. opts.MaxGoRoutines = 4
  837. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  838. for n := 0; n < b.N; n++ {
  839. auditGitRepo(benchmarkRepo)
  840. }
  841. }
  842. func BenchmarkAuditLeakRepo8Proc(b *testing.B) {
  843. loadToml()
  844. opts.MaxGoRoutines = 8
  845. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  846. for n := 0; n < b.N; n++ {
  847. auditGitRepo(benchmarkRepo)
  848. }
  849. }
  850. func BenchmarkAuditLeakRepo10Proc(b *testing.B) {
  851. loadToml()
  852. opts.MaxGoRoutines = 10
  853. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  854. for n := 0; n < b.N; n++ {
  855. auditGitRepo(benchmarkRepo)
  856. }
  857. }
  858. func BenchmarkAuditLeakRepo100Proc(b *testing.B) {
  859. loadToml()
  860. opts.MaxGoRoutines = 100
  861. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  862. for n := 0; n < b.N; n++ {
  863. auditGitRepo(benchmarkRepo)
  864. }
  865. }
  866. func BenchmarkAuditLeakRepo1000Proc(b *testing.B) {
  867. loadToml()
  868. opts.MaxGoRoutines = 1000
  869. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  870. for n := 0; n < b.N; n++ {
  871. auditGitRepo(benchmarkRepo)
  872. }
  873. }
  874. func BenchmarkAuditLeakRepo10000Proc(b *testing.B) {
  875. loadToml()
  876. opts.MaxGoRoutines = 10000
  877. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  878. for n := 0; n < b.N; n++ {
  879. auditGitRepo(benchmarkRepo)
  880. }
  881. }
  882. func BenchmarkAuditLeakRepo100000Proc(b *testing.B) {
  883. loadToml()
  884. opts.MaxGoRoutines = 100000
  885. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  886. for n := 0; n < b.N; n++ {
  887. auditGitRepo(benchmarkRepo)
  888. }
  889. }