gitleaks_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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, whitelist repo",
  266. numLeaks: 0,
  267. expectedErrMsg: "",
  268. configPath: path.Join(configsDir, "repo"),
  269. },
  270. {
  271. testOpts: Options{
  272. GithubOrg: "gitleakstestorg",
  273. ExcludeForks: true,
  274. },
  275. description: "test github org, exclude forks",
  276. numLeaks: 0,
  277. expectedErrMsg: "",
  278. },
  279. }
  280. g := goblin.Goblin(t)
  281. for _, test := range tests {
  282. g.Describe("TestRun", func() {
  283. g.It(test.description, func() {
  284. if test.configPath != "" {
  285. os.Setenv("GITLEAKS_CONFIG", test.configPath)
  286. }
  287. opts = test.testOpts
  288. leaks, err := run()
  289. if err != nil {
  290. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  291. }
  292. g.Assert(len(leaks)).Equal(test.numLeaks)
  293. })
  294. })
  295. }
  296. }
  297. func TestWriteReport(t *testing.T) {
  298. tmpDir, _ := ioutil.TempDir("", "reportDir")
  299. reportJSON := path.Join(tmpDir, "report.json")
  300. reportCSV := path.Join(tmpDir, "report.csv")
  301. defer os.RemoveAll(tmpDir)
  302. leaks := []Leak{
  303. {
  304. Line: "eat",
  305. Commit: "your",
  306. Offender: "veggies",
  307. Type: "and",
  308. Message: "get",
  309. Author: "some",
  310. File: "sleep",
  311. Branch: "thxu",
  312. },
  313. }
  314. var tests = []struct {
  315. leaks []Leak
  316. reportFile string
  317. fileName string
  318. description string
  319. testOpts Options
  320. }{
  321. {
  322. leaks: leaks,
  323. reportFile: reportJSON,
  324. fileName: "report.json",
  325. description: "can we write a file",
  326. testOpts: Options{
  327. Report: reportJSON,
  328. },
  329. },
  330. {
  331. leaks: leaks,
  332. reportFile: reportCSV,
  333. fileName: "report.csv",
  334. description: "can we write a file",
  335. testOpts: Options{
  336. Report: reportCSV,
  337. CSV: true,
  338. },
  339. },
  340. }
  341. g := goblin.Goblin(t)
  342. for _, test := range tests {
  343. g.Describe("TestWriteReport", func() {
  344. g.It(test.description, func() {
  345. opts = test.testOpts
  346. writeReport(test.leaks)
  347. f, _ := os.Stat(test.reportFile)
  348. g.Assert(f.Name()).Equal(test.fileName)
  349. })
  350. })
  351. }
  352. }
  353. func testTomlLoader() string {
  354. tmpDir, _ := ioutil.TempDir("", "whiteListConfigs")
  355. ioutil.WriteFile(path.Join(tmpDir, "regex"), []byte(testWhitelistRegex), 0644)
  356. ioutil.WriteFile(path.Join(tmpDir, "branch"), []byte(testWhitelistBranch), 0644)
  357. ioutil.WriteFile(path.Join(tmpDir, "commit"), []byte(testWhitelistCommit), 0644)
  358. ioutil.WriteFile(path.Join(tmpDir, "file"), []byte(testWhitelistFile), 0644)
  359. ioutil.WriteFile(path.Join(tmpDir, "repo"), []byte(testWhitelistRepo), 0644)
  360. return tmpDir
  361. }
  362. func TestAuditRepo(t *testing.T) {
  363. var leaks []Leak
  364. err := loadToml()
  365. configsDir := testTomlLoader()
  366. defer os.RemoveAll(configsDir)
  367. if err != nil {
  368. panic(err)
  369. }
  370. leaksR, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  371. URL: "https://github.com/gitleakstest/gronit.git",
  372. })
  373. if err != nil {
  374. panic(err)
  375. }
  376. leaksRepo := &RepoDescriptor{
  377. repository: leaksR,
  378. name: "gronit",
  379. }
  380. cleanR, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  381. URL: "https://github.com/gitleakstest/h1domains.git",
  382. })
  383. if err != nil {
  384. panic(err)
  385. }
  386. cleanRepo := &RepoDescriptor{
  387. repository: cleanR,
  388. name: "h1domains",
  389. }
  390. var tests = []struct {
  391. testOpts Options
  392. description string
  393. expectedErrMsg string
  394. numLeaks int
  395. repo *RepoDescriptor
  396. whiteListFiles []*regexp.Regexp
  397. whiteListCommits map[string]bool
  398. whiteListBranches []string
  399. whiteListRepos []string
  400. whiteListRegexes []*regexp.Regexp
  401. configPath string
  402. }{
  403. {
  404. repo: leaksRepo,
  405. description: "two leaks present",
  406. numLeaks: 2,
  407. },
  408. {
  409. repo: leaksRepo,
  410. description: "two leaks present limit goroutines",
  411. numLeaks: 2,
  412. testOpts: Options{
  413. MaxGoRoutines: 4,
  414. },
  415. },
  416. {
  417. repo: leaksRepo,
  418. description: "audit specific bad branch",
  419. numLeaks: 2,
  420. testOpts: Options{
  421. Branch: "master",
  422. },
  423. },
  424. {
  425. repo: leaksRepo,
  426. description: "audit specific good branch",
  427. numLeaks: 0,
  428. testOpts: Options{
  429. Branch: "dev",
  430. },
  431. },
  432. {
  433. repo: leaksRepo,
  434. description: "audit all branch",
  435. numLeaks: 6,
  436. testOpts: Options{
  437. AuditAllRefs: true,
  438. },
  439. },
  440. {
  441. repo: leaksRepo,
  442. description: "audit all branch whitelist 1",
  443. numLeaks: 4,
  444. testOpts: Options{
  445. AuditAllRefs: true,
  446. },
  447. whiteListBranches: []string{
  448. "origin/master",
  449. },
  450. },
  451. {
  452. repo: leaksRepo,
  453. description: "two leaks present whitelist AWS.. no leaks",
  454. whiteListRegexes: []*regexp.Regexp{
  455. regexp.MustCompile("AKIA"),
  456. },
  457. numLeaks: 0,
  458. },
  459. {
  460. repo: leaksRepo,
  461. description: "two leaks present limit goroutines",
  462. numLeaks: 2,
  463. },
  464. {
  465. repo: cleanRepo,
  466. description: "no leaks present",
  467. numLeaks: 0,
  468. },
  469. {
  470. repo: leaksRepo,
  471. description: "two leaks present whitelist go files",
  472. whiteListFiles: []*regexp.Regexp{
  473. regexp.MustCompile(".go"),
  474. },
  475. numLeaks: 0,
  476. },
  477. {
  478. repo: leaksRepo,
  479. description: "two leaks present whitelist bad commit",
  480. whiteListCommits: map[string]bool{
  481. "eaeffdc65b4c73ccb67e75d96bd8743be2c85973": true,
  482. },
  483. numLeaks: 1,
  484. },
  485. {
  486. repo: leaksRepo,
  487. description: "redact",
  488. testOpts: Options{
  489. Redact: true,
  490. },
  491. numLeaks: 2,
  492. },
  493. {
  494. repo: leaksRepo,
  495. description: "toml whitelist regex",
  496. configPath: path.Join(configsDir, "regex"),
  497. numLeaks: 0,
  498. },
  499. {
  500. repo: leaksRepo,
  501. description: "toml whitelist branch",
  502. configPath: path.Join(configsDir, "branch"),
  503. testOpts: Options{
  504. AuditAllRefs: true,
  505. },
  506. numLeaks: 4,
  507. },
  508. {
  509. repo: leaksRepo,
  510. description: "toml whitelist file",
  511. configPath: path.Join(configsDir, "file"),
  512. numLeaks: 0,
  513. },
  514. {
  515. repo: leaksRepo,
  516. description: "toml whitelist commit",
  517. configPath: path.Join(configsDir, "commit"),
  518. numLeaks: 1,
  519. },
  520. {
  521. repo: leaksRepo,
  522. description: "audit whitelist repo",
  523. numLeaks: 0,
  524. whiteListRepos: []string{
  525. "gronit",
  526. },
  527. },
  528. {
  529. repo: leaksRepo,
  530. description: "toml whitelist repo",
  531. numLeaks: 0,
  532. configPath: path.Join(configsDir, "repo"),
  533. },
  534. {
  535. repo: leaksRepo,
  536. description: "leaks present with entropy",
  537. testOpts: Options{
  538. Entropy: 4.7,
  539. },
  540. numLeaks: 7,
  541. },
  542. {
  543. repo: leaksRepo,
  544. description: "Audit until specific commit",
  545. numLeaks: 1,
  546. testOpts: Options{
  547. Commit: "f6839959b7bbdcd23008f1fb16f797f35bcd3a0c",
  548. },
  549. },
  550. {
  551. repo: leaksRepo,
  552. description: "commit depth = 1, no leaks",
  553. numLeaks: 0,
  554. testOpts: Options{
  555. Depth: 1,
  556. },
  557. },
  558. {
  559. repo: leaksRepo,
  560. description: "commit depth = 2, one leak",
  561. numLeaks: 1,
  562. testOpts: Options{
  563. Depth: 2,
  564. },
  565. },
  566. }
  567. whiteListCommits = make(map[string]bool)
  568. g := goblin.Goblin(t)
  569. for _, test := range tests {
  570. g.Describe("TestAuditRepo", func() {
  571. g.It(test.description, func() {
  572. opts = test.testOpts
  573. // settin da globs
  574. if test.whiteListFiles != nil {
  575. whiteListFiles = test.whiteListFiles
  576. } else {
  577. whiteListFiles = nil
  578. }
  579. if test.whiteListCommits != nil {
  580. whiteListCommits = test.whiteListCommits
  581. } else {
  582. whiteListCommits = nil
  583. }
  584. if test.whiteListBranches != nil {
  585. whiteListBranches = test.whiteListBranches
  586. } else {
  587. whiteListBranches = nil
  588. }
  589. if test.whiteListRegexes != nil {
  590. whiteListRegexes = test.whiteListRegexes
  591. } else {
  592. whiteListRegexes = nil
  593. }
  594. if test.whiteListRepos != nil {
  595. whiteListRepos = test.whiteListRepos
  596. } else {
  597. whiteListRepos = nil
  598. }
  599. // config paths
  600. if test.configPath != "" {
  601. os.Setenv("GITLEAKS_CONFIG", test.configPath)
  602. loadToml()
  603. }
  604. leaks, err = auditGitRepo(test.repo)
  605. if opts.Redact {
  606. g.Assert(leaks[0].Offender).Equal("REDACTED")
  607. }
  608. g.Assert(len(leaks)).Equal(test.numLeaks)
  609. })
  610. })
  611. }
  612. }
  613. func TestOptionGuard(t *testing.T) {
  614. var tests = []struct {
  615. testOpts Options
  616. githubToken bool
  617. description string
  618. expectedErrMsg string
  619. expectedErrMsgFuzzy string
  620. }{
  621. {
  622. testOpts: Options{},
  623. description: "default no opts",
  624. expectedErrMsg: "",
  625. },
  626. {
  627. testOpts: Options{
  628. IncludePrivate: true,
  629. GithubOrg: "fakeOrg",
  630. },
  631. description: "private org no githubtoken",
  632. expectedErrMsg: "user/organization private repos require env var GITHUB_TOKEN to be set",
  633. githubToken: false,
  634. },
  635. {
  636. testOpts: Options{
  637. IncludePrivate: true,
  638. GithubUser: "fakeUser",
  639. },
  640. description: "private user no githubtoken",
  641. expectedErrMsg: "user/organization private repos require env var GITHUB_TOKEN to be set",
  642. githubToken: false,
  643. },
  644. {
  645. testOpts: Options{
  646. IncludePrivate: true,
  647. GithubUser: "fakeUser",
  648. GithubOrg: "fakeOrg",
  649. },
  650. description: "double owner",
  651. expectedErrMsg: "github user and organization set",
  652. },
  653. {
  654. testOpts: Options{
  655. IncludePrivate: true,
  656. GithubOrg: "fakeOrg",
  657. OwnerPath: "/dev/null",
  658. },
  659. description: "local and remote target",
  660. expectedErrMsg: "github organization set and local owner path",
  661. },
  662. {
  663. testOpts: Options{
  664. IncludePrivate: true,
  665. GithubUser: "fakeUser",
  666. OwnerPath: "/dev/null",
  667. },
  668. description: "local and remote target",
  669. expectedErrMsg: "github user set and local owner path",
  670. },
  671. {
  672. testOpts: Options{
  673. GithubUser: "fakeUser",
  674. SingleSearch: "*/./....",
  675. },
  676. description: "single search invalid regex gaurd",
  677. expectedErrMsgFuzzy: "unable to compile regex: */./...., ",
  678. },
  679. {
  680. testOpts: Options{
  681. GithubUser: "fakeUser",
  682. SingleSearch: "mystring",
  683. },
  684. description: "single search regex gaurd",
  685. expectedErrMsg: "",
  686. },
  687. {
  688. testOpts: Options{
  689. GithubOrg: "fakeOrg",
  690. Entropy: 9,
  691. },
  692. description: "Invalid entropy level guard",
  693. expectedErrMsg: "The maximum level of entropy is 8",
  694. },
  695. }
  696. g := goblin.Goblin(t)
  697. for _, test := range tests {
  698. g.Describe("Test Option Gaurd", func() {
  699. g.It(test.description, func() {
  700. os.Clearenv()
  701. opts = test.testOpts
  702. if test.githubToken {
  703. os.Setenv("GITHUB_TOKEN", "fakeToken")
  704. }
  705. err := optsGuard()
  706. if err != nil {
  707. if test.expectedErrMsgFuzzy != "" {
  708. g.Assert(strings.Contains(err.Error(), test.expectedErrMsgFuzzy)).Equal(true)
  709. } else {
  710. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  711. }
  712. } else {
  713. g.Assert("").Equal(test.expectedErrMsg)
  714. }
  715. })
  716. })
  717. }
  718. }
  719. func TestLoadToml(t *testing.T) {
  720. tmpDir, _ := ioutil.TempDir("", "gitleaksTestConfigDir")
  721. defer os.RemoveAll(tmpDir)
  722. err := ioutil.WriteFile(path.Join(tmpDir, "gitleaksConfig"), []byte(defaultConfig), 0644)
  723. if err != nil {
  724. panic(err)
  725. }
  726. configPath := path.Join(tmpDir, "gitleaksConfig")
  727. noConfigPath := path.Join(tmpDir, "gitleaksConfigNope")
  728. var tests = []struct {
  729. testOpts Options
  730. description string
  731. configPath string
  732. expectedErrMsg string
  733. singleSearch bool
  734. }{
  735. {
  736. testOpts: Options{
  737. ConfigPath: configPath,
  738. },
  739. description: "path to config",
  740. },
  741. {
  742. testOpts: Options{},
  743. description: "env var path to no config",
  744. singleSearch: true,
  745. },
  746. {
  747. testOpts: Options{
  748. ConfigPath: noConfigPath,
  749. },
  750. description: "no path to config",
  751. expectedErrMsg: fmt.Sprintf("no gitleaks config at %s", noConfigPath),
  752. },
  753. {
  754. testOpts: Options{},
  755. description: "env var path to config",
  756. configPath: configPath,
  757. expectedErrMsg: "",
  758. },
  759. {
  760. testOpts: Options{},
  761. description: "env var path to no config",
  762. configPath: noConfigPath,
  763. expectedErrMsg: fmt.Sprintf("problem loading config: open %s: no such file or directory", noConfigPath),
  764. },
  765. }
  766. g := goblin.Goblin(t)
  767. for _, test := range tests {
  768. g.Describe("TestLoadToml", func() {
  769. g.It(test.description, func() {
  770. opts = test.testOpts
  771. if test.singleSearch {
  772. singleSearchRegex = regexp.MustCompile("test")
  773. } else {
  774. singleSearchRegex = nil
  775. }
  776. if test.configPath != "" {
  777. os.Setenv("GITLEAKS_CONFIG", test.configPath)
  778. } else {
  779. os.Clearenv()
  780. }
  781. err := loadToml()
  782. if err != nil {
  783. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  784. } else {
  785. g.Assert("").Equal(test.expectedErrMsg)
  786. }
  787. })
  788. })
  789. }
  790. }
  791. func BenchmarkAuditRepo1Proc(b *testing.B) {
  792. loadToml()
  793. opts.MaxGoRoutines = 1
  794. benchmarkRepo = getBenchmarkRepo()
  795. for n := 0; n < b.N; n++ {
  796. auditGitRepo(benchmarkRepo)
  797. }
  798. }
  799. func BenchmarkAuditRepo2Proc(b *testing.B) {
  800. loadToml()
  801. opts.MaxGoRoutines = 2
  802. benchmarkRepo = getBenchmarkRepo()
  803. for n := 0; n < b.N; n++ {
  804. auditGitRepo(benchmarkRepo)
  805. }
  806. }
  807. func BenchmarkAuditRepo4Proc(b *testing.B) {
  808. loadToml()
  809. opts.MaxGoRoutines = 4
  810. benchmarkRepo = getBenchmarkRepo()
  811. for n := 0; n < b.N; n++ {
  812. auditGitRepo(benchmarkRepo)
  813. }
  814. }
  815. func BenchmarkAuditRepo8Proc(b *testing.B) {
  816. loadToml()
  817. opts.MaxGoRoutines = 8
  818. benchmarkRepo = getBenchmarkRepo()
  819. for n := 0; n < b.N; n++ {
  820. auditGitRepo(benchmarkRepo)
  821. }
  822. }
  823. func BenchmarkAuditRepo10Proc(b *testing.B) {
  824. loadToml()
  825. opts.MaxGoRoutines = 10
  826. benchmarkRepo = getBenchmarkRepo()
  827. for n := 0; n < b.N; n++ {
  828. auditGitRepo(benchmarkRepo)
  829. }
  830. }
  831. func BenchmarkAuditRepo100Proc(b *testing.B) {
  832. loadToml()
  833. opts.MaxGoRoutines = 100
  834. benchmarkRepo = getBenchmarkRepo()
  835. for n := 0; n < b.N; n++ {
  836. auditGitRepo(benchmarkRepo)
  837. }
  838. }
  839. func BenchmarkAuditRepo1000Proc(b *testing.B) {
  840. loadToml()
  841. opts.MaxGoRoutines = 1000
  842. benchmarkRepo = getBenchmarkRepo()
  843. for n := 0; n < b.N; n++ {
  844. auditGitRepo(benchmarkRepo)
  845. }
  846. }
  847. func BenchmarkAuditLeakRepo1Proc(b *testing.B) {
  848. loadToml()
  849. opts.MaxGoRoutines = 1
  850. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  851. for n := 0; n < b.N; n++ {
  852. auditGitRepo(benchmarkRepo)
  853. }
  854. }
  855. func BenchmarkAuditLeakRepo2Proc(b *testing.B) {
  856. loadToml()
  857. opts.MaxGoRoutines = 2
  858. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  859. for n := 0; n < b.N; n++ {
  860. auditGitRepo(benchmarkRepo)
  861. }
  862. }
  863. func BenchmarkAuditLeakRepo4Proc(b *testing.B) {
  864. loadToml()
  865. opts.MaxGoRoutines = 4
  866. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  867. for n := 0; n < b.N; n++ {
  868. auditGitRepo(benchmarkRepo)
  869. }
  870. }
  871. func BenchmarkAuditLeakRepo8Proc(b *testing.B) {
  872. loadToml()
  873. opts.MaxGoRoutines = 8
  874. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  875. for n := 0; n < b.N; n++ {
  876. auditGitRepo(benchmarkRepo)
  877. }
  878. }
  879. func BenchmarkAuditLeakRepo10Proc(b *testing.B) {
  880. loadToml()
  881. opts.MaxGoRoutines = 10
  882. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  883. for n := 0; n < b.N; n++ {
  884. auditGitRepo(benchmarkRepo)
  885. }
  886. }
  887. func BenchmarkAuditLeakRepo100Proc(b *testing.B) {
  888. loadToml()
  889. opts.MaxGoRoutines = 100
  890. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  891. for n := 0; n < b.N; n++ {
  892. auditGitRepo(benchmarkRepo)
  893. }
  894. }
  895. func BenchmarkAuditLeakRepo1000Proc(b *testing.B) {
  896. loadToml()
  897. opts.MaxGoRoutines = 1000
  898. benchmarkLeaksRepo = getBenchmarkLeaksRepo()
  899. for n := 0; n < b.N; n++ {
  900. auditGitRepo(benchmarkRepo)
  901. }
  902. }