gitleaks_test.go 18 KB

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