|
| 1 | +// Package entropy_test is for testing the entropy package |
| 2 | +// |
| 3 | +// !IMPORTANT! - none of the keys or strings listed in this file are real keys. They |
| 4 | +// are generated purely to test this package and MUST NOT be used |
| 5 | +// anywhere else as actual credentials |
| 6 | +package entropy_test |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/ONSdigital/git-diff-check/entropy" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + highBase64 = [][]byte{ |
| 17 | + []byte(`ZWVTjPQSdhwRgl204Hc51YCsritMIzn8B=/p9UyeX7xu6KkAGqfm3FJ+oObLDNEva`), |
| 18 | + []byte(`hSXAQy9D1J0hkCQy0tKBCxnpcOQCPeM54RFXZLJE`), |
| 19 | + []byte(`secret=ZWVTjPQSdhwRgl204Hc51YCsritMIzn8B=/p9UyeX7xu6KkAGqfm3FJ+oObLDNEva`), |
| 20 | + []byte(`aws:ZWVTjPQSdhwRgl204Hc51YCsritMIzn8B=/p9UyeX7xu6KkAGqfm3FJ+oObLDNEva`), |
| 21 | + } |
| 22 | + highHex = [][]byte{ |
| 23 | + []byte(`b3A0a1FDfe86dcCE945B72`), |
| 24 | + } |
| 25 | +) |
| 26 | + |
| 27 | +func ExampleCalculateShannon() { |
| 28 | + password := []byte("verysecret") |
| 29 | + if entropy.CalculateShannon(password) < entropy.Base64Threshold { |
| 30 | + fmt.Println("Password not complex enough!") |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestCalculateEntropy(t *testing.T) { |
| 35 | + |
| 36 | + t.Log("Check base64 data") |
| 37 | + for _, b := range highBase64 { |
| 38 | + if e := entropy.CalculateShannon(b); e < entropy.Base64Threshold { |
| 39 | + t.Errorf("Got entropy %f, expected > %f", e, entropy.Base64Threshold) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + t.Log("Check hex data") |
| 44 | + for _, b := range highHex { |
| 45 | + if e := entropy.CalculateShannon(b); e < entropy.HexThreshold { |
| 46 | + t.Errorf("Got entropy %f, expected > %f", e, entropy.HexThreshold) |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestCheck(t *testing.T) { |
| 52 | + |
| 53 | + exampleBlock := []byte(`+// CheckPatchLine takes a line from a patch hunk and tests it for naughty patterns |
| 54 | ++func CheckPatchLine(line []byte) (bool, []Warning) { |
| 55 | ++ warnings := []Warning{} |
| 56 | ++ aws := []byte("hSXAQy9D1J0hkCQy0tKBCxnpcOQCPeM54RFXZLJE") |
| 57 | ++ |
| 58 | ++ // Log in with secret: ZWVTjPQSdhwRgl204Hc51YCsritMIzn8B=/p9UyeX7xu6KkAGqfm3FJ+oObLDNEva |
| 59 | ++ |
| 60 | ++ for _, rule := range linePatterns { |
| 61 | ++ if found := rule.Regex.FindAll(line, -1); len(found) > 0 { |
| 62 | ++ for _, f := range found { |
| 63 | ++ // TODO Ignore exclusions |
| 64 | ++ if string(f) != "b3A0a1FDfe86dcCE945B72" { |
| 65 | ++ warnings = append(warnings, Warning{Type: "line", Line: -1}) |
| 66 | ++ }`) |
| 67 | + |
| 68 | + ok, n := entropy.Check(exampleBlock) |
| 69 | + if ok { |
| 70 | + t.Error("Expected 'not ok'") |
| 71 | + } |
| 72 | + if n != 3 { |
| 73 | + t.Errorf("Expected warnings, got %d, expected 3", n) |
| 74 | + } |
| 75 | + |
| 76 | + for _, b := range highBase64 { |
| 77 | + ok, _ := entropy.Check(b) |
| 78 | + if ok { |
| 79 | + t.Error("Expected failed base64 entropy check") |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + for _, b := range highHex { |
| 84 | + ok, _ := entropy.Check(b) |
| 85 | + if ok { |
| 86 | + t.Error("Expected failed hex entropy check") |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments