| 1 | package xmltransformer |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/DefangLabs/secret-detector/pkg/transformers/tests" |
| 7 | ) |
| 8 | |
| 9 | var testCases = []tests.TestCase{ |
| 10 | {"illegal xml - empty input", "", nil}, |
| 11 | {"illegal xml - simple string", "hello world", nil}, |
| 12 | {"illegal xml - double quoted string", `"hello world"`, nil}, |
| 13 | {"illegal xml - single quoted string", `'hello world'`, nil}, |
| 14 | {"illegal xml - missing open bracket", `<root`, nil}, |
| 15 | {"illegal xml - missing close bracket", `root/>`, nil}, |
| 16 | {"illegal xml - missing close tag", `<root>`, nil}, |
| 17 | {"illegal xml - missing tag name", `</>`, nil}, |
| 18 | {"illegal xml - tag mismatch", `<tag></t>`, nil}, |
| 19 | {"illegal xml - wrong brackets", `{tag /}`, nil}, |
| 20 | {"illegal xml - missing value quotes", `<tag attr=val/>`, nil}, |
| 21 | {"illegal xml - unescaped characters", `<tag attr="<"/>`, nil}, |
| 22 | {"illegal xml - attr in close tag", `<tag><tag a="v" />`, nil}, |
| 23 | |
| 24 | {"empty element tag with no data", `<tag/>`, map[string]string{}}, |
| 25 | {"empty element tag with attributes", `<tag a1="v1"/>`, map[string]string{"tag[a1]": "v1"}}, |
| 26 | {"tag with no data", `<tag></tag>`, map[string]string{}}, |
| 27 | {"tag with attributes", `<tag a1="v1"></tag>`, map[string]string{"tag[a1]": "v1"}}, |
| 28 | {"tag with element", `<tag>e</tag>`, map[string]string{"tag": "e"}}, |
| 29 | {"tag with attributes and element", |
| 30 | `<tag a1="v1">e</tag>`, |
| 31 | map[string]string{"tag": "e", "tag[a1]": "v1"}}, |
| 32 | {"nested tags", |
| 33 | ` |
| 34 | <t1 a1="v1"> |
| 35 | e1 |
| 36 | <t2 a2="v2"/> |
| 37 | <t3> |
| 38 | <t4 a4_1="v4_1" a4_2="v4_2"/> |
| 39 | </t3> |
| 40 | </t1>`, |
| 41 | map[string]string{ |
| 42 | "t1": "e1", |
| 43 | "t1[a1]": "v1", |
| 44 | "t1.t2[a2]": "v2", |
| 45 | "t1.t3.t4[a4_1]": "v4_1", |
| 46 | "t1.t3.t4[a4_2]": "v4_2", |
| 47 | }}, |
| 48 | {"ignore comments, directives and instructions", ` |
| 49 | <?xml version="1.0" encoding="UTF-8"?> |
| 50 | <t1> |
| 51 | <!directice [ignore this tag] > |
| 52 | <!-- ignore this comment --> |
| 53 | <t2>e2</t2> |
| 54 | </t1>`, map[string]string{"t1.t2": "e2"}}, |
| 55 | {"ignore whitespace", "<t>\n\n\n\t\te\t\n\n\n</t>", map[string]string{"t": "e"}}, |
| 56 | {"handle CR/LF", "<t>\r\n\te\r\n</t>", map[string]string{"t": "e"}}, |
| 57 | {"escape characters", |
| 58 | `<t a=""v""><&></t>`, |
| 59 | map[string]string{"t[a]": `"v"`, "t": "<&>"}}, |
| 60 | {"split element body", |
| 61 | `<t1>hello<t2/>world</t1>`, |
| 62 | map[string]string{"t1": "hello", "t1 (ln. 2)": "world"}}, |
| 63 | {"duplicate elements", ` |
| 64 | <r> |
| 65 | <t a="v0"/> |
| 66 | <t a="v1"/> |
| 67 | <t a="v2">e2</t> |
| 68 | </r>`, |
| 69 | map[string]string{"r.t[a]": "v0", "r.t~1[a]": "v1", "r.t~2[a]": "v2", "r.t~2": "e2"}}, |
| 70 | {"duplicate attributes", |
| 71 | `<t a="v0" a="v1"/>`, |
| 72 | map[string]string{"t[a]": "v0", "t[a~1]": "v1"}}, |
| 73 | } |
| 74 | |
| 75 | var xmlTransformer = NewTransformer() |
| 76 | |
| 77 | func TestTransform(t *testing.T) { |
| 78 | tests.TestTransform(t, xmlTransformer, testCases) |
| 79 | } |