gitleaks_test.go 18 KB

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