gitleaks_test.go 19 KB

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