| 1 | package initransformer |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/DefangLabs/secret-detector/pkg/transformers/tests" |
| 7 | ) |
| 8 | |
| 9 | var testCases = []tests.TestCase{ |
| 10 | {"empty input", "", nil}, |
| 11 | {"illegal structure", "this is not an ini file", nil}, |
| 12 | {"keys in default section", |
| 13 | ` |
| 14 | key1=value1 |
| 15 | key2 = value2 |
| 16 | key3: value3`, |
| 17 | map[string]string{"key1": "value1", "key2": "value2", "key3": "value3"}}, |
| 18 | {"keys in a section", |
| 19 | ` |
| 20 | [section1] |
| 21 | key1=value1 |
| 22 | key2 = value2 |
| 23 | key3: value3`, |
| 24 | map[string]string{"section1.key1": "value1", "section1.key2": "value2", "section1.key3": "value3"}}, |
| 25 | {"keys with different types", |
| 26 | ` |
| 27 | [empty values] |
| 28 | key1= |
| 29 | key2 = |
| 30 | key3: |
| 31 | |
| 32 | [numbers] |
| 33 | int1 = 1 |
| 34 | int2=-2 |
| 35 | float1: 1.234 |
| 36 | float2 = 2.30 |
| 37 | |
| 38 | [bool] |
| 39 | bool0: false |
| 40 | bool1 = true`, |
| 41 | map[string]string{ |
| 42 | "empty values.key1": "", |
| 43 | "empty values.key2": "", |
| 44 | "empty values.key3": "", |
| 45 | "numbers.int1": "1", |
| 46 | "numbers.int2": "-2", |
| 47 | "numbers.float1": "1.234", |
| 48 | "numbers.float2": "2.30", |
| 49 | "bool.bool0": "false", |
| 50 | "bool.bool1": "true"}}, |
| 51 | {"keys with special characters", |
| 52 | ` |
| 53 | [ s p a c e s ] |
| 54 | are allowed = yes they are |
| 55 | [$pe.ci@l-#&*?] |
| 56 | can we? = yes, we can! |
| 57 | what/about\slashes=y/e\s |
| 58 | d.o.t.s = ... |
| 59 | ["quotes"] |
| 60 | "double" = "ignored in key & value" |
| 61 | 'single' = 'ignored for value'`, |
| 62 | map[string]string{ |
| 63 | " s p a c e s .are allowed": "yes they are", |
| 64 | "$pe.ci@l-#&*?.can we?": "yes, we can!", |
| 65 | `$pe.ci@l-#&*?.what/about\slashes`: `y/e\s`, |
| 66 | `$pe.ci@l-#&*?.d.o.t.s`: `...`, |
| 67 | `"quotes".double`: "ignored in key & value", |
| 68 | `"quotes".'single'`: "ignored for value"}}, |
| 69 | {"comments are ignored", |
| 70 | ` |
| 71 | # comment for section1 |
| 72 | [section1] |
| 73 | key1=value1 ; inline comment for key1 |
| 74 | ; comment for section2 |
| 75 | [section2] |
| 76 | key1=value1 # inline comment for key1 |
| 77 | # comment for key2 |
| 78 | key2=value2`, |
| 79 | map[string]string{"section1.key1": "value1", "section2.key1": "value1", "section2.key2": "value2"}}, |
| 80 | {"nested keys and sections", |
| 81 | ` |
| 82 | [section1] |
| 83 | key1 = |
| 84 | A = 1/1.A |
| 85 | B = 1/1.B |
| 86 | [section1.sub1] |
| 87 | A = 1.1/A |
| 88 | B = |
| 89 | a = 1.1/B.a |
| 90 | b = 1.1/B.b |
| 91 | `, |
| 92 | map[string]string{ |
| 93 | "section1.key1.A": "1/1.A", |
| 94 | "section1.key1.B": "1/1.B", |
| 95 | "section1.sub1.A": "1.1/A", |
| 96 | "section1.sub1.B.a": "1.1/B.a", |
| 97 | "section1.sub1.B.b": "1.1/B.b", |
| 98 | }}, |
| 99 | {"multiline values", |
| 100 | ` |
| 101 | multi = ln 1 |
| 102 | ln 2 |
| 103 | ln 3 |
| 104 | |
| 105 | [section] |
| 106 | multi = |
| 107 | this is a |
| 108 | long text |
| 109 | `, |
| 110 | map[string]string{"multi": "ln 1\n ln 2\n ln 3", "section.multi": "\n this is a\n long text"}}, |
| 111 | {"ini with CR/LF", "key1=val1\r\nkey2:val2\n", map[string]string{"key1": "val1", "key2": "val2"}}, |
| 112 | {"input with one key", "org.opencontainers.image.ref.name=ubuntu", map[string]string{"org.opencontainers.image.ref.name": "ubuntu"}}, |
| 113 | {"base64 is not parsed", "dGhpcyBpcyBhIHRlc3QgZm9yIGhpZ2ggZW50cm9weSBiYXNlNjQgc2VjcmV0IGRldGVjdGlvbg==", map[string]string(nil)}, |
| 114 | } |
| 115 | |
| 116 | var iniTransformer = NewTransformer() |
| 117 | |
| 118 | func TestTransform(t *testing.T) { |
| 119 | tests.TestTransform(t, iniTransformer, testCases) |
| 120 | } |