gitleaks_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. GithubUser: "gitleakstest",
  172. },
  173. description: "test github user",
  174. numLeaks: 2,
  175. expectedErrMsg: "",
  176. },
  177. {
  178. testOpts: Options{
  179. GithubUser: "gitleakstest",
  180. Disk: true,
  181. },
  182. description: "test github user on disk ",
  183. numLeaks: 2,
  184. expectedErrMsg: "",
  185. },
  186. {
  187. testOpts: Options{
  188. GithubOrg: "gitleakstestorg",
  189. },
  190. description: "test github org",
  191. numLeaks: 2,
  192. expectedErrMsg: "",
  193. },
  194. {
  195. testOpts: Options{
  196. GithubOrg: "gitleakstestorg",
  197. Disk: true,
  198. },
  199. description: "test org on disk",
  200. numLeaks: 2,
  201. expectedErrMsg: "",
  202. },
  203. {
  204. testOpts: Options{
  205. OwnerPath: dir,
  206. },
  207. description: "test owner path",
  208. numLeaks: 2,
  209. expectedErrMsg: "",
  210. },
  211. {
  212. testOpts: Options{
  213. Repo: "git@github.com:gitleakstest/gronit.git",
  214. SSHKey: "trash",
  215. },
  216. description: "test leak",
  217. numLeaks: 0,
  218. expectedErrMsg: "unable to generate ssh key: open trash: no such file or directory",
  219. },
  220. {
  221. testOpts: Options{
  222. Repo: "https://github.com/gitleakstest/gronit.git",
  223. },
  224. description: "test leak",
  225. numLeaks: 2,
  226. expectedErrMsg: "",
  227. },
  228. {
  229. testOpts: Options{
  230. Repo: "https://github.com/gitleakstest/h1domains.git",
  231. },
  232. description: "test clean",
  233. numLeaks: 0,
  234. expectedErrMsg: "",
  235. },
  236. {
  237. testOpts: Options{
  238. Repo: "https://github.com/gitleakstest/empty.git",
  239. },
  240. description: "test empty",
  241. numLeaks: 0,
  242. expectedErrMsg: "reference not found",
  243. },
  244. {
  245. testOpts: Options{
  246. GithubOrg: "gitleakstestorg",
  247. },
  248. description: "test github org, whitelist repo",
  249. numLeaks: 0,
  250. expectedErrMsg: "",
  251. configPath: path.Join(configsDir, "repo"),
  252. },
  253. {
  254. testOpts: Options{
  255. GithubOrg: "gitleakstestorg",
  256. ExcludeForks: true,
  257. },
  258. description: "test github org, exclude forks",
  259. numLeaks: 0,
  260. expectedErrMsg: "",
  261. },
  262. {
  263. testOpts: Options{
  264. GithubPR: "https://github.com/gitleakstest/gronit/pull/1",
  265. },
  266. description: "test github pr",
  267. numLeaks: 4,
  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. commitPerPage: 1,
  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. if test.commitPerPage != 0 {
  288. githubPages = test.commitPerPage
  289. }
  290. opts = test.testOpts
  291. leaks, err := run()
  292. if err != nil {
  293. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  294. }
  295. g.Assert(len(leaks)).Equal(test.numLeaks)
  296. githubPages = 100
  297. })
  298. })
  299. }
  300. }
  301. func TestWriteReport(t *testing.T) {
  302. tmpDir, _ := ioutil.TempDir("", "reportDir")
  303. reportJSON := path.Join(tmpDir, "report.json")
  304. reportJASON := path.Join(tmpDir, "report.jason")
  305. reportVOID := path.Join("thereIsNoWay", "thisReportWillGetWritten.json")
  306. reportCSV := path.Join(tmpDir, "report.csv")
  307. defer os.RemoveAll(tmpDir)
  308. leaks := []Leak{
  309. {
  310. Line: "eat",
  311. Commit: "your",
  312. Offender: "veggies",
  313. Type: "and",
  314. Message: "get",
  315. Author: "some",
  316. File: "sleep",
  317. Date: time.Now(),
  318. },
  319. }
  320. var tests = []struct {
  321. leaks []Leak
  322. reportFile string
  323. fileName string
  324. description string
  325. testOpts Options
  326. expectedErrMsg string
  327. }{
  328. {
  329. leaks: leaks,
  330. reportFile: reportJSON,
  331. fileName: "report.json",
  332. description: "can we write a json file",
  333. testOpts: Options{
  334. Report: reportJSON,
  335. },
  336. },
  337. {
  338. leaks: leaks,
  339. reportFile: reportCSV,
  340. fileName: "report.csv",
  341. description: "can we write a csv file",
  342. testOpts: Options{
  343. Report: reportCSV,
  344. },
  345. },
  346. {
  347. leaks: leaks,
  348. reportFile: reportJASON,
  349. fileName: "report.jason",
  350. description: "bad file",
  351. expectedErrMsg: "Report should be a .json or .csv file",
  352. testOpts: Options{
  353. Report: reportJASON,
  354. },
  355. },
  356. {
  357. leaks: leaks,
  358. reportFile: reportVOID,
  359. fileName: "report.jason",
  360. description: "bad dir",
  361. expectedErrMsg: "thereIsNoWay does not exist",
  362. testOpts: Options{
  363. Report: reportVOID,
  364. },
  365. },
  366. }
  367. g := goblin.Goblin(t)
  368. for _, test := range tests {
  369. g.Describe("TestWriteReport", func() {
  370. g.It(test.description, func() {
  371. opts = test.testOpts
  372. err := optsGuard()
  373. if err != nil {
  374. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  375. } else {
  376. writeReport(test.leaks)
  377. f, _ := os.Stat(test.reportFile)
  378. g.Assert(f.Name()).Equal(test.fileName)
  379. }
  380. })
  381. })
  382. }
  383. }
  384. func testTomlLoader() string {
  385. tmpDir, _ := ioutil.TempDir("", "whiteListConfigs")
  386. ioutil.WriteFile(path.Join(tmpDir, "regex"), []byte(testWhitelistRegex), 0644)
  387. ioutil.WriteFile(path.Join(tmpDir, "commit"), []byte(testWhitelistCommit), 0644)
  388. ioutil.WriteFile(path.Join(tmpDir, "file"), []byte(testWhitelistFile), 0644)
  389. ioutil.WriteFile(path.Join(tmpDir, "repo"), []byte(testWhitelistRepo), 0644)
  390. ioutil.WriteFile(path.Join(tmpDir, "entropy"), []byte(testEntropyRange), 0644)
  391. ioutil.WriteFile(path.Join(tmpDir, "badEntropy"), []byte(testBadEntropyRange), 0644)
  392. ioutil.WriteFile(path.Join(tmpDir, "badEntropy2"), []byte(testBadEntropyRange2), 0644)
  393. return tmpDir
  394. }
  395. func TestAuditRepo(t *testing.T) {
  396. var leaks []Leak
  397. err := loadToml()
  398. configsDir := testTomlLoader()
  399. defer os.RemoveAll(configsDir)
  400. if err != nil {
  401. panic(err)
  402. }
  403. leaksR, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  404. URL: "https://github.com/gitleakstest/gronit.git",
  405. })
  406. if err != nil {
  407. panic(err)
  408. }
  409. leaksRepo := &RepoDescriptor{
  410. repository: leaksR,
  411. name: "gronit",
  412. }
  413. cleanR, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  414. URL: "https://github.com/gitleakstest/h1domains.git",
  415. })
  416. if err != nil {
  417. panic(err)
  418. }
  419. cleanRepo := &RepoDescriptor{
  420. repository: cleanR,
  421. name: "h1domains",
  422. }
  423. var tests = []struct {
  424. testOpts Options
  425. description string
  426. expectedErrMsg string
  427. numLeaks int
  428. repo *RepoDescriptor
  429. whiteListFiles []*regexp.Regexp
  430. whiteListCommits map[string]bool
  431. whiteListRepos []*regexp.Regexp
  432. whiteListRegexes []*regexp.Regexp
  433. configPath string
  434. }{
  435. {
  436. repo: leaksRepo,
  437. description: "two leaks present",
  438. numLeaks: 2,
  439. },
  440. {
  441. repo: leaksRepo,
  442. description: "two leaks present limit goroutines",
  443. numLeaks: 2,
  444. testOpts: Options{
  445. Threads: 4,
  446. },
  447. },
  448. {
  449. repo: leaksRepo,
  450. description: "two leaks present whitelist AWS.. no leaks",
  451. whiteListRegexes: []*regexp.Regexp{
  452. regexp.MustCompile("AKIA"),
  453. },
  454. numLeaks: 0,
  455. },
  456. {
  457. repo: leaksRepo,
  458. description: "two leaks present limit goroutines",
  459. numLeaks: 2,
  460. },
  461. {
  462. repo: cleanRepo,
  463. description: "no leaks present",
  464. numLeaks: 0,
  465. },
  466. {
  467. repo: leaksRepo,
  468. description: "two leaks present whitelist go files",
  469. whiteListFiles: []*regexp.Regexp{
  470. regexp.MustCompile(".go"),
  471. },
  472. numLeaks: 0,
  473. },
  474. {
  475. repo: leaksRepo,
  476. description: "two leaks present whitelist bad commit",
  477. whiteListCommits: map[string]bool{
  478. "eaeffdc65b4c73ccb67e75d96bd8743be2c85973": true,
  479. },
  480. numLeaks: 1,
  481. },
  482. {
  483. repo: leaksRepo,
  484. description: "redact",
  485. testOpts: Options{
  486. Redact: true,
  487. },
  488. numLeaks: 2,
  489. },
  490. {
  491. repo: leaksRepo,
  492. description: "toml whitelist regex",
  493. configPath: path.Join(configsDir, "regex"),
  494. numLeaks: 0,
  495. },
  496. {
  497. repo: leaksRepo,
  498. description: "toml whitelist file",
  499. configPath: path.Join(configsDir, "file"),
  500. numLeaks: 0,
  501. },
  502. {
  503. repo: leaksRepo,
  504. description: "toml whitelist commit",
  505. configPath: path.Join(configsDir, "commit"),
  506. numLeaks: 1,
  507. },
  508. {
  509. repo: leaksRepo,
  510. description: "audit whitelist repo",
  511. numLeaks: 0,
  512. whiteListRepos: []*regexp.Regexp{
  513. regexp.MustCompile("gronit"),
  514. },
  515. },
  516. {
  517. repo: leaksRepo,
  518. description: "toml whitelist repo",
  519. numLeaks: 0,
  520. configPath: path.Join(configsDir, "repo"),
  521. },
  522. {
  523. repo: leaksRepo,
  524. description: "leaks present with entropy",
  525. testOpts: Options{
  526. Entropy: 4.7,
  527. },
  528. numLeaks: 6,
  529. },
  530. {
  531. repo: leaksRepo,
  532. description: "Audit until specific commit",
  533. numLeaks: 2,
  534. testOpts: Options{
  535. Commit: "f6839959b7bbdcd23008f1fb16f797f35bcd3a0c",
  536. },
  537. },
  538. {
  539. repo: leaksRepo,
  540. description: "commit depth = 1, one leak",
  541. numLeaks: 1,
  542. testOpts: Options{
  543. Depth: 1,
  544. },
  545. },
  546. {
  547. repo: leaksRepo,
  548. description: "commit depth = 2, two leaks",
  549. numLeaks: 2,
  550. testOpts: Options{
  551. Depth: 2,
  552. },
  553. },
  554. {
  555. repo: leaksRepo,
  556. description: "toml entropy range",
  557. numLeaks: 284,
  558. configPath: path.Join(configsDir, "entropy"),
  559. },
  560. {
  561. repo: leaksRepo,
  562. description: "toml bad entropy range",
  563. numLeaks: 0,
  564. configPath: path.Join(configsDir, "badEntropy"),
  565. expectedErrMsg: "entropy range must be ascending",
  566. },
  567. {
  568. repo: leaksRepo,
  569. description: "toml bad entropy2 range",
  570. numLeaks: 0,
  571. configPath: path.Join(configsDir, "badEntropy2"),
  572. expectedErrMsg: "invalid entropy ranges, must be within 0.0-8.0",
  573. },
  574. }
  575. whiteListCommits = make(map[string]bool)
  576. g := goblin.Goblin(t)
  577. for _, test := range tests {
  578. g.Describe("TestAuditRepo", func() {
  579. g.It(test.description, func() {
  580. opts = test.testOpts
  581. // settin da globs
  582. if test.whiteListFiles != nil {
  583. whiteListFiles = test.whiteListFiles
  584. } else {
  585. whiteListFiles = nil
  586. }
  587. if test.whiteListCommits != nil {
  588. whiteListCommits = test.whiteListCommits
  589. } else {
  590. whiteListCommits = nil
  591. }
  592. if test.whiteListRegexes != nil {
  593. whiteListRegexes = test.whiteListRegexes
  594. } else {
  595. whiteListRegexes = nil
  596. }
  597. if test.whiteListRepos != nil {
  598. whiteListRepos = test.whiteListRepos
  599. } else {
  600. whiteListRepos = nil
  601. }
  602. skip := false
  603. // config paths
  604. if test.configPath != "" {
  605. os.Setenv("GITLEAKS_CONFIG", test.configPath)
  606. err := loadToml()
  607. if err != nil {
  608. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  609. skip = true
  610. }
  611. }
  612. if !skip {
  613. leaks, err = auditGitRepo(test.repo)
  614. if opts.Redact {
  615. g.Assert(leaks[0].Offender).Equal("REDACTED")
  616. }
  617. g.Assert(len(leaks)).Equal(test.numLeaks)
  618. }
  619. })
  620. })
  621. }
  622. }
  623. func TestOptionGuard(t *testing.T) {
  624. var tests = []struct {
  625. testOpts Options
  626. githubToken bool
  627. description string
  628. expectedErrMsg string
  629. expectedErrMsgFuzzy string
  630. }{
  631. {
  632. testOpts: Options{},
  633. description: "default no opts",
  634. expectedErrMsg: "",
  635. },
  636. {
  637. testOpts: Options{
  638. GithubUser: "fakeUser",
  639. GithubOrg: "fakeOrg",
  640. },
  641. description: "double owner",
  642. expectedErrMsg: "github user and organization set",
  643. },
  644. {
  645. testOpts: Options{
  646. GithubOrg: "fakeOrg",
  647. OwnerPath: "/dev/null",
  648. },
  649. description: "local and remote target",
  650. expectedErrMsg: "github organization set and local owner path",
  651. },
  652. {
  653. testOpts: Options{
  654. GithubUser: "fakeUser",
  655. OwnerPath: "/dev/null",
  656. },
  657. description: "local and remote target",
  658. expectedErrMsg: "github user set and local owner path",
  659. },
  660. {
  661. testOpts: Options{
  662. GithubUser: "fakeUser",
  663. SingleSearch: "*/./....",
  664. },
  665. description: "single search invalid regex gaurd",
  666. expectedErrMsgFuzzy: "unable to compile regex: */./...., ",
  667. },
  668. {
  669. testOpts: Options{
  670. GithubUser: "fakeUser",
  671. SingleSearch: "mystring",
  672. },
  673. description: "single search regex gaurd",
  674. expectedErrMsg: "",
  675. },
  676. {
  677. testOpts: Options{
  678. GithubOrg: "fakeOrg",
  679. Entropy: 9,
  680. },
  681. description: "Invalid entropy level guard",
  682. expectedErrMsg: "The maximum level of entropy is 8",
  683. },
  684. }
  685. g := goblin.Goblin(t)
  686. for _, test := range tests {
  687. g.Describe("Test Option Gaurd", func() {
  688. g.It(test.description, func() {
  689. os.Clearenv()
  690. opts = test.testOpts
  691. if test.githubToken {
  692. os.Setenv("GITHUB_TOKEN", "fakeToken")
  693. }
  694. err := optsGuard()
  695. if err != nil {
  696. if test.expectedErrMsgFuzzy != "" {
  697. g.Assert(strings.Contains(err.Error(), test.expectedErrMsgFuzzy)).Equal(true)
  698. } else {
  699. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  700. }
  701. } else {
  702. g.Assert("").Equal(test.expectedErrMsg)
  703. }
  704. })
  705. })
  706. }
  707. }
  708. func TestLoadToml(t *testing.T) {
  709. tmpDir, _ := ioutil.TempDir("", "gitleaksTestConfigDir")
  710. defer os.RemoveAll(tmpDir)
  711. err := ioutil.WriteFile(path.Join(tmpDir, "gitleaksConfig"), []byte(defaultConfig), 0644)
  712. if err != nil {
  713. panic(err)
  714. }
  715. configPath := path.Join(tmpDir, "gitleaksConfig")
  716. noConfigPath := path.Join(tmpDir, "gitleaksConfigNope")
  717. var tests = []struct {
  718. testOpts Options
  719. description string
  720. configPath string
  721. expectedErrMsg string
  722. singleSearch bool
  723. }{
  724. {
  725. testOpts: Options{
  726. ConfigPath: configPath,
  727. },
  728. description: "path to config",
  729. },
  730. {
  731. testOpts: Options{},
  732. description: "env var path to no config",
  733. singleSearch: true,
  734. },
  735. {
  736. testOpts: Options{
  737. ConfigPath: noConfigPath,
  738. },
  739. description: "no path to config",
  740. expectedErrMsg: fmt.Sprintf("no gitleaks config at %s", noConfigPath),
  741. },
  742. {
  743. testOpts: Options{},
  744. description: "env var path to config",
  745. configPath: configPath,
  746. expectedErrMsg: "",
  747. },
  748. {
  749. testOpts: Options{},
  750. description: "env var path to no config",
  751. configPath: noConfigPath,
  752. expectedErrMsg: fmt.Sprintf("problem loading config: open %s: no such file or directory", noConfigPath),
  753. },
  754. }
  755. g := goblin.Goblin(t)
  756. for _, test := range tests {
  757. g.Describe("TestLoadToml", func() {
  758. g.It(test.description, func() {
  759. opts = test.testOpts
  760. if test.singleSearch {
  761. singleSearchRegex = regexp.MustCompile("test")
  762. } else {
  763. singleSearchRegex = nil
  764. }
  765. if test.configPath != "" {
  766. os.Setenv("GITLEAKS_CONFIG", test.configPath)
  767. } else {
  768. os.Clearenv()
  769. }
  770. err := loadToml()
  771. if err != nil {
  772. g.Assert(err.Error()).Equal(test.expectedErrMsg)
  773. } else {
  774. g.Assert("").Equal(test.expectedErrMsg)
  775. }
  776. })
  777. })
  778. }
  779. }