| 1 | package xml_test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/tomwright/dasel/v3/parsing" |
| 9 | "github.com/tomwright/dasel/v3/parsing/json" |
| 10 | "github.com/tomwright/dasel/v3/parsing/xml" |
| 11 | ) |
| 12 | |
| 13 | func TestXmlReader_Read(t *testing.T) { |
| 14 | t.Run("nested xml elements", func(t *testing.T) { |
| 15 | r, err := xml.XML.NewReader(parsing.DefaultReaderOptions()) |
| 16 | if err != nil { |
| 17 | t.Fatalf("Unexpected error: %s", err) |
| 18 | } |
| 19 | |
| 20 | w, err := json.JSON.NewWriter(parsing.DefaultWriterOptions()) |
| 21 | if err != nil { |
| 22 | t.Fatalf("Unexpected error: %s", err) |
| 23 | } |
| 24 | |
| 25 | data, err := r.Read([]byte(`<Document> |
| 26 | <Sender>Ivanov</Sender> |
| 27 | <In_N_Document> |
| 28 | <N_Document>1024</N_Document> |
| 29 | <Date_Reg>2024-06-21T15:07:29.0451517+03:00</Date_Reg> |
| 30 | </In_N_Document> |
| 31 | <Out_N_Document> |
| 32 | <N_Document>2043</N_Document> |
| 33 | <Date_Reg>2024-05-01T00:00:00</Date_Reg> |
| 34 | </Out_N_Document> |
| 35 | <Content>Skzzkz</Content> |
| 36 | <DSP>true</DSP> |
| 37 | </Document> |
| 38 | `)) |
| 39 | if err != nil { |
| 40 | t.Fatalf("Unexpected error: %s", err) |
| 41 | } |
| 42 | |
| 43 | jsonBytes, err := w.Write(data) |
| 44 | if err != nil { |
| 45 | t.Fatalf("Unexpected error: %s", err) |
| 46 | } |
| 47 | |
| 48 | expected := `{ |
| 49 | "Document": { |
| 50 | "Sender": "Ivanov", |
| 51 | "In_N_Document": { |
| 52 | "N_Document": "1024", |
| 53 | "Date_Reg": "2024-06-21T15:07:29.0451517+03:00" |
| 54 | }, |
| 55 | "Out_N_Document": { |
| 56 | "N_Document": "2043", |
| 57 | "Date_Reg": "2024-05-01T00:00:00" |
| 58 | }, |
| 59 | "Content": "Skzzkz", |
| 60 | "DSP": "true" |
| 61 | } |
| 62 | } |
| 63 | ` |
| 64 | if string(jsonBytes) != expected { |
| 65 | t.Fatalf("Expected:\n%s\nGot:\n%s", expected, string(jsonBytes)) |
| 66 | } |
| 67 | }) |
| 68 | |
| 69 | t.Run("nested xml elements with processing instruction", func(t *testing.T) { |
| 70 | r, err := xml.XML.NewReader(parsing.DefaultReaderOptions()) |
| 71 | if err != nil { |
| 72 | t.Fatalf("Unexpected error: %s", err) |
| 73 | } |
| 74 | |
| 75 | w, err := json.JSON.NewWriter(parsing.DefaultWriterOptions()) |
| 76 | if err != nil { |
| 77 | t.Fatalf("Unexpected error: %s", err) |
| 78 | } |
| 79 | |
| 80 | data, err := r.Read([]byte(`<?xml version="1.0" encoding="utf-8" standalone="yes"?> |
| 81 | <Document> |
| 82 | <Sender>Ivanov</Sender> |
| 83 | <In_N_Document> |
| 84 | <N_Document>1024</N_Document> |
| 85 | <Date_Reg>2024-06-21T15:07:29.0451517+03:00</Date_Reg> |
| 86 | </In_N_Document> |
| 87 | <Out_N_Document> |
| 88 | <N_Document>2043</N_Document> |
| 89 | <Date_Reg>2024-05-01T00:00:00</Date_Reg> |
| 90 | </Out_N_Document> |
| 91 | <Content>Skzzkz</Content> |
| 92 | <DSP>true</DSP> |
| 93 | </Document> |
| 94 | `)) |
| 95 | if err != nil { |
| 96 | t.Fatalf("Unexpected error: %s", err) |
| 97 | } |
| 98 | |
| 99 | jsonBytes, err := w.Write(data) |
| 100 | if err != nil { |
| 101 | t.Fatalf("Unexpected error: %s", err) |
| 102 | } |
| 103 | |
| 104 | expected := `{ |
| 105 | "Document": { |
| 106 | "Sender": "Ivanov", |
| 107 | "In_N_Document": { |
| 108 | "N_Document": "1024", |
| 109 | "Date_Reg": "2024-06-21T15:07:29.0451517+03:00" |
| 110 | }, |
| 111 | "Out_N_Document": { |
| 112 | "N_Document": "2043", |
| 113 | "Date_Reg": "2024-05-01T00:00:00" |
| 114 | }, |
| 115 | "Content": "Skzzkz", |
| 116 | "DSP": "true" |
| 117 | } |
| 118 | } |
| 119 | ` |
| 120 | if string(jsonBytes) != expected { |
| 121 | t.Fatalf("Expected:\n%s\nGot:\n%s", expected, string(jsonBytes)) |
| 122 | } |
| 123 | }) |
| 124 | |
| 125 | t.Run("cdata tag", func(t *testing.T) { |
| 126 | r, err := xml.XML.NewReader(parsing.DefaultReaderOptions()) |
| 127 | if err != nil { |
| 128 | t.Fatalf("Unexpected error: %s", err) |
| 129 | } |
| 130 | |
| 131 | data, err := r.Read([]byte(`<foo> |
| 132 | <![CDATA[<bar>baz</bar>]]> |
| 133 | </foo> |
| 134 | `)) |
| 135 | if err != nil { |
| 136 | t.Fatalf("Unexpected error: %s", err) |
| 137 | } |
| 138 | |
| 139 | x, err := data.GetMapKey("foo") |
| 140 | if err != nil { |
| 141 | t.Fatalf("Unexpected error: %s", err) |
| 142 | } |
| 143 | got, err := x.StringValue() |
| 144 | if err != nil { |
| 145 | t.Fatalf("Unexpected error: %s", err) |
| 146 | } |
| 147 | |
| 148 | exp := "<bar>baz</bar>" |
| 149 | if exp != got { |
| 150 | t.Fatalf("Expected value %q but got %q", exp, got) |
| 151 | } |
| 152 | }) |
| 153 | |
| 154 | t.Run("empty cdata tag", func(t *testing.T) { |
| 155 | r, err := xml.XML.NewReader(parsing.DefaultReaderOptions()) |
| 156 | if err != nil { |
| 157 | t.Fatalf("Unexpected error: %s", err) |
| 158 | } |
| 159 | |
| 160 | data, err := r.Read([]byte(`<foo> |
| 161 | <![CDATA[]]> |
| 162 | </foo> |
| 163 | `)) |
| 164 | if err != nil { |
| 165 | t.Fatalf("Unexpected error: %s", err) |
| 166 | } |
| 167 | |
| 168 | x, err := data.GetMapKey("foo") |
| 169 | if err != nil { |
| 170 | t.Fatalf("Unexpected error: %s", err) |
| 171 | } |
| 172 | got, err := x.StringValue() |
| 173 | if err != nil { |
| 174 | t.Fatalf("Unexpected error: %s", err) |
| 175 | } |
| 176 | |
| 177 | exp := "" |
| 178 | if exp != got { |
| 179 | t.Fatalf("Expected value %q but got %q", exp, got) |
| 180 | } |
| 181 | }) |
| 182 | } |
| 183 | |
| 184 | func TestXMLReader_DepthLimit(t *testing.T) { |
| 185 | reader, err := xml.XML.NewReader(parsing.DefaultReaderOptions()) |
| 186 | if err != nil { |
| 187 | t.Fatal(err) |
| 188 | } |
| 189 | |
| 190 | t.Run("deeply nested elements exceeding limit return error", func(t *testing.T) { |
| 191 | // 10_001 levels — one past the 10_000 limit. Each open tag is 3 bytes (<a>), |
| 192 | // so total ~60 KB, well under the 10 MB size cap. |
| 193 | const depth = 10_001 |
| 194 | open := strings.Repeat("<a>", depth) |
| 195 | close := strings.Repeat("</a>", depth) |
| 196 | _, err := reader.Read([]byte(open + close)) |
| 197 | if !errors.Is(err, xml.ErrXMLMaxDepthExceeded) { |
| 198 | t.Fatalf("expected ErrXMLMaxDepthExceeded, got %v", err) |
| 199 | } |
| 200 | }) |
| 201 | |
| 202 | t.Run("normal nesting within limit succeeds", func(t *testing.T) { |
| 203 | data := []byte(`<root><a><b><c>hello</c></b></a></root>`) |
| 204 | _, err := reader.Read(data) |
| 205 | if err != nil { |
| 206 | t.Fatalf("unexpected error: %v", err) |
| 207 | } |
| 208 | }) |
| 209 | } |