| 1 | package xml_test |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/tomwright/dasel/v3/parsing" |
| 8 | daselxml "github.com/tomwright/dasel/v3/parsing/xml" |
| 9 | ) |
| 10 | |
| 11 | // newTestReaderWriter creates a reader and writer for round-trip testing. |
| 12 | func newTestReaderWriter(t *testing.T) (parsing.Reader, parsing.Writer) { |
| 13 | t.Helper() |
| 14 | r, err := daselxml.XML.NewReader(parsing.DefaultReaderOptions()) |
| 15 | if err != nil { |
| 16 | t.Fatalf("Unexpected error creating reader: %s", err) |
| 17 | } |
| 18 | w, err := daselxml.XML.NewWriter(parsing.DefaultWriterOptions()) |
| 19 | if err != nil { |
| 20 | t.Fatalf("Unexpected error creating writer: %s", err) |
| 21 | } |
| 22 | return r, w |
| 23 | } |
| 24 | |
| 25 | // assertRoundTrip performs a read-write round-trip and verifies expected strings are present. |
| 26 | func assertRoundTrip(t *testing.T, r parsing.Reader, w parsing.Writer, input string, expected []string) { |
| 27 | t.Helper() |
| 28 | data, err := r.Read([]byte(input)) |
| 29 | if err != nil { |
| 30 | t.Fatalf("Unexpected error reading XML: %s", err) |
| 31 | } |
| 32 | |
| 33 | output, err := w.Write(data) |
| 34 | if err != nil { |
| 35 | t.Fatalf("Unexpected error writing XML: %s", err) |
| 36 | } |
| 37 | |
| 38 | outputStr := string(output) |
| 39 | for _, exp := range expected { |
| 40 | if !strings.Contains(outputStr, exp) { |
| 41 | t.Errorf("Expected output to contain %q, got:\n%s", exp, outputStr) |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // assertOrderedContains verifies that the given strings appear in the specified order in output. |
| 47 | func assertOrderedContains(t *testing.T, output string, ordered []string) { |
| 48 | t.Helper() |
| 49 | for i := 1; i < len(ordered); i++ { |
| 50 | prevIdx := strings.Index(output, ordered[i-1]) |
| 51 | currIdx := strings.Index(output, ordered[i]) |
| 52 | if prevIdx < 0 { |
| 53 | t.Errorf("Expected %q to be present in output, got:\n%s", ordered[i-1], output) |
| 54 | continue |
| 55 | } |
| 56 | if currIdx < 0 { |
| 57 | t.Errorf("Expected %q to be present in output, got:\n%s", ordered[i], output) |
| 58 | continue |
| 59 | } |
| 60 | if prevIdx >= currIdx { |
| 61 | t.Errorf("Expected %q before %q in output, got:\n%s", ordered[i-1], ordered[i], output) |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // TestXmlReader_StructuredModeWithComments tests comment preservation in structured mode |
| 67 | func TestXmlReader_StructuredModeWithComments(t *testing.T) { |
| 68 | t.Run("basic_comment_structured", func(t *testing.T) { |
| 69 | options := parsing.DefaultReaderOptions() |
| 70 | options.Ext = map[string]string{"xml-mode": "structured"} |
| 71 | r, err := daselxml.XML.NewReader(options) |
| 72 | if err != nil { |
| 73 | t.Fatalf("Unexpected error: %s", err) |
| 74 | } |
| 75 | |
| 76 | input := `<!--comment--><root><child>text</child></root>` |
| 77 | data, err := r.Read([]byte(input)) |
| 78 | if err != nil { |
| 79 | t.Fatalf("Unexpected error: %s", err) |
| 80 | } |
| 81 | |
| 82 | childrenNode, err := data.GetMapKey("children") |
| 83 | if err != nil { |
| 84 | t.Fatalf("Expected 'children' key in structured mode: %s", err) |
| 85 | } |
| 86 | |
| 87 | childrenLen, err := childrenNode.SliceLen() |
| 88 | if err != nil { |
| 89 | t.Fatalf("Expected children to be a slice: %s", err) |
| 90 | } |
| 91 | |
| 92 | if childrenLen == 0 { |
| 93 | t.Fatalf("Expected at least one child element") |
| 94 | } |
| 95 | |
| 96 | rootElement, err := childrenNode.GetSliceIndex(0) |
| 97 | if err != nil { |
| 98 | t.Fatalf("Expected to get first child: %s", err) |
| 99 | } |
| 100 | |
| 101 | comments, ok := rootElement.MetadataValue("xml_comments") |
| 102 | if !ok { |
| 103 | t.Errorf("Expected xml_comments metadata to exist") |
| 104 | } |
| 105 | |
| 106 | if comments == nil { |
| 107 | t.Errorf("Expected comments to be preserved in structured mode") |
| 108 | } |
| 109 | }) |
| 110 | } |
| 111 | |
| 112 | // TestXmlRoundTrip_CommentPreservation tests round-trip preservation of XML comments |
| 113 | func TestXmlRoundTrip_CommentPreservation(t *testing.T) { |
| 114 | tests := []struct { |
| 115 | name string |
| 116 | input string |
| 117 | expected []string |
| 118 | }{ |
| 119 | { |
| 120 | name: "single comment", |
| 121 | input: `<!--This is a comment--> |
| 122 | <root> |
| 123 | <child>text</child> |
| 124 | </root> |
| 125 | `, |
| 126 | expected: []string{"<!--This is a comment-->", "<root>", "<child>text</child>"}, |
| 127 | }, |
| 128 | { |
| 129 | name: "multiple comments before root", |
| 130 | input: `<!--First comment--> |
| 131 | <!--Second comment--> |
| 132 | <root> |
| 133 | <child>text</child> |
| 134 | </root> |
| 135 | `, |
| 136 | expected: []string{"<!--First comment-->", "<!--Second comment-->"}, |
| 137 | }, |
| 138 | { |
| 139 | name: "comment before complex child element", |
| 140 | input: `<root><!--Section comment--><section><item>text</item></section></root>`, |
| 141 | expected: []string{"<!--Section comment-->", "<section>"}, |
| 142 | }, |
| 143 | { |
| 144 | name: "processing instruction and comments", |
| 145 | input: `<?xml version="1.0" encoding="UTF-8"?> |
| 146 | <!--Document comment--> |
| 147 | <root> |
| 148 | <child>text</child> |
| 149 | </root> |
| 150 | `, |
| 151 | expected: []string{`<?xml version="1.0" encoding="UTF-8"?>`, "<!--Document comment-->", "<root>"}, |
| 152 | }, |
| 153 | { |
| 154 | name: "nested elements and comments", |
| 155 | input: `<!--Root level comment--> |
| 156 | <Document> |
| 157 | <!--Section comment--> |
| 158 | <Section> |
| 159 | <Item>value1</Item> |
| 160 | <Item>value2</Item> |
| 161 | </Section> |
| 162 | </Document> |
| 163 | `, |
| 164 | expected: []string{"<!--Root level comment-->", "<!--Section comment-->", "<Document>", "<Section>"}, |
| 165 | }, |
| 166 | } |
| 167 | |
| 168 | for _, tt := range tests { |
| 169 | t.Run(tt.name, func(t *testing.T) { |
| 170 | r, w := newTestReaderWriter(t) |
| 171 | assertRoundTrip(t, r, w, tt.input, tt.expected) |
| 172 | }) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // TestXmlRoundTrip_EdgeCases tests edge cases for comment handling |
| 177 | func TestXmlRoundTrip_EdgeCases(t *testing.T) { |
| 178 | tests := []struct { |
| 179 | name string |
| 180 | input string |
| 181 | expected []string |
| 182 | }{ |
| 183 | { |
| 184 | name: "empty comment", |
| 185 | input: `<!----><root><child>text</child></root>`, |
| 186 | expected: []string{"<!---->", "<root>"}, |
| 187 | }, |
| 188 | { |
| 189 | name: "whitespace-only comment", |
| 190 | input: `<!-- --><root><child>text</child></root>`, |
| 191 | expected: []string{"<!-- -->"}, |
| 192 | }, |
| 193 | { |
| 194 | name: "comment between sibling elements", |
| 195 | input: `<root><first>one</first><!--between siblings--><second>two</second></root>`, |
| 196 | expected: []string{"<!--between siblings-->", "<first>one</first>", "<second>two</second>"}, |
| 197 | }, |
| 198 | { |
| 199 | name: "multiple comments between siblings", |
| 200 | input: `<root><a>1</a><!--comment1--><!--comment2--><b>2</b></root>`, |
| 201 | expected: []string{"<!--comment1-->", "<!--comment2-->"}, |
| 202 | }, |
| 203 | { |
| 204 | name: "trailing comment after last child", |
| 205 | input: `<root><child>text</child><!--trailing comment--></root>`, |
| 206 | expected: []string{"<!--trailing comment-->", "<child>text</child>"}, |
| 207 | }, |
| 208 | { |
| 209 | name: "multiple trailing comments", |
| 210 | input: `<root><child>text</child><!--trailing1--><!--trailing2--></root>`, |
| 211 | expected: []string{"<!--trailing1-->", "<!--trailing2-->"}, |
| 212 | }, |
| 213 | { |
| 214 | name: "trailing comment in nested structure", |
| 215 | input: `<root><parent><child>text</child><!--inner trailing--></parent><!--outer trailing--></root>`, |
| 216 | expected: []string{"<!--inner trailing-->", "<!--outer trailing-->"}, |
| 217 | }, |
| 218 | } |
| 219 | |
| 220 | for _, tt := range tests { |
| 221 | t.Run(tt.name, func(t *testing.T) { |
| 222 | r, w := newTestReaderWriter(t) |
| 223 | assertRoundTrip(t, r, w, tt.input, tt.expected) |
| 224 | }) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // TestXmlRoundTrip_SpecialCommentContent tests comments with special but valid content |
| 229 | func TestXmlRoundTrip_SpecialCommentContent(t *testing.T) { |
| 230 | tests := []struct { |
| 231 | name string |
| 232 | input string |
| 233 | expected []string |
| 234 | }{ |
| 235 | { |
| 236 | name: "special characters", |
| 237 | input: `<!--Comment with <special> & "characters" 'here'--><root><child>text</child></root>`, |
| 238 | expected: []string{"<!--Comment with <special>"}, |
| 239 | }, |
| 240 | { |
| 241 | name: "single dash", |
| 242 | input: `<!--Comment with a-single-dash--><root><child>text</child></root>`, |
| 243 | expected: []string{"<!--Comment with a-single-dash-->"}, |
| 244 | }, |
| 245 | } |
| 246 | |
| 247 | for _, tt := range tests { |
| 248 | t.Run(tt.name, func(t *testing.T) { |
| 249 | r, w := newTestReaderWriter(t) |
| 250 | assertRoundTrip(t, r, w, tt.input, tt.expected) |
| 251 | }) |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // TestXmlReader_SecurityLimits tests error handling for security limits |
| 256 | func TestXmlReader_SecurityLimits(t *testing.T) { |
| 257 | t.Run("reject oversized XML input", func(t *testing.T) { |
| 258 | r, err := daselxml.XML.NewReader(parsing.DefaultReaderOptions()) |
| 259 | if err != nil { |
| 260 | t.Fatalf("Unexpected error creating reader: %s", err) |
| 261 | } |
| 262 | |
| 263 | largeContent := strings.Repeat("x", 10_000_001) |
| 264 | input := "<root>" + largeContent + "</root>" |
| 265 | |
| 266 | _, err = r.Read([]byte(input)) |
| 267 | if err == nil { |
| 268 | t.Errorf("Expected error for oversized XML input") |
| 269 | } |
| 270 | if !strings.Contains(err.Error(), "exceeds maximum size") { |
| 271 | t.Errorf("Expected error about maximum size, got: %s", err) |
| 272 | } |
| 273 | }) |
| 274 | |
| 275 | t.Run("reject oversized comment", func(t *testing.T) { |
| 276 | r, err := daselxml.XML.NewReader(parsing.DefaultReaderOptions()) |
| 277 | if err != nil { |
| 278 | t.Fatalf("Unexpected error creating reader: %s", err) |
| 279 | } |
| 280 | |
| 281 | largeComment := strings.Repeat("x", 10_001) |
| 282 | input := "<!--" + largeComment + "--><root><child>text</child></root>" |
| 283 | |
| 284 | _, err = r.Read([]byte(input)) |
| 285 | if err == nil { |
| 286 | t.Errorf("Expected error for oversized comment") |
| 287 | } |
| 288 | if !strings.Contains(err.Error(), "exceeds maximum length") { |
| 289 | t.Errorf("Expected error about maximum length, got: %s", err) |
| 290 | } |
| 291 | }) |
| 292 | |
| 293 | t.Run("reject document with too many comments", func(t *testing.T) { |
| 294 | r, err := daselxml.XML.NewReader(parsing.DefaultReaderOptions()) |
| 295 | if err != nil { |
| 296 | t.Fatalf("Unexpected error creating reader: %s", err) |
| 297 | } |
| 298 | |
| 299 | var builder strings.Builder |
| 300 | builder.WriteString("<root>") |
| 301 | for i := 0; i < 1001; i++ { |
| 302 | builder.WriteString("<!--comment-->") |
| 303 | } |
| 304 | builder.WriteString("<child>text</child></root>") |
| 305 | input := builder.String() |
| 306 | |
| 307 | _, err = r.Read([]byte(input)) |
| 308 | if err == nil { |
| 309 | t.Errorf("Expected error for too many comments") |
| 310 | } |
| 311 | if !strings.Contains(err.Error(), "exceeds maximum comment count") { |
| 312 | t.Errorf("Expected error about maximum comment count, got: %s", err) |
| 313 | } |
| 314 | }) |
| 315 | } |
| 316 | |
| 317 | // TestXmlRoundTrip_ProcessingInstructionReset tests that processing instructions are not duplicated across siblings |
| 318 | func TestXmlRoundTrip_ProcessingInstructionReset(t *testing.T) { |
| 319 | t.Run("processing instructions not duplicated to siblings", func(t *testing.T) { |
| 320 | r, w := newTestReaderWriter(t) |
| 321 | |
| 322 | input := `<?xml version="1.0"?><root><first>one</first><second>two</second></root>` |
| 323 | |
| 324 | data, err := r.Read([]byte(input)) |
| 325 | if err != nil { |
| 326 | t.Fatalf("Unexpected error reading XML: %s", err) |
| 327 | } |
| 328 | |
| 329 | output, err := w.Write(data) |
| 330 | if err != nil { |
| 331 | t.Fatalf("Unexpected error writing XML: %s", err) |
| 332 | } |
| 333 | |
| 334 | outputStr := string(output) |
| 335 | |
| 336 | piCount := strings.Count(outputStr, `<?xml version="1.0"?>`) |
| 337 | if piCount != 1 { |
| 338 | t.Errorf("Expected PI to appear exactly once, but found %d occurrences in:\n%s", piCount, outputStr) |
| 339 | } |
| 340 | |
| 341 | if !strings.Contains(outputStr, "<first>one</first>") { |
| 342 | t.Errorf("Expected first element to be preserved in:\n%s", outputStr) |
| 343 | } |
| 344 | if !strings.Contains(outputStr, "<second>two</second>") { |
| 345 | t.Errorf("Expected second element to be preserved in:\n%s", outputStr) |
| 346 | } |
| 347 | }) |
| 348 | |
| 349 | t.Run("sibling elements do not inherit each others processing instructions", func(t *testing.T) { |
| 350 | options := parsing.DefaultReaderOptions() |
| 351 | options.Ext = map[string]string{"xml-mode": "structured"} |
| 352 | r, err := daselxml.XML.NewReader(options) |
| 353 | if err != nil { |
| 354 | t.Fatalf("Unexpected error creating reader: %s", err) |
| 355 | } |
| 356 | |
| 357 | input := `<root><?target instruction?><first>one</first><second>two</second></root>` |
| 358 | |
| 359 | data, err := r.Read([]byte(input)) |
| 360 | if err != nil { |
| 361 | t.Fatalf("Unexpected error reading XML: %s", err) |
| 362 | } |
| 363 | |
| 364 | children, err := data.GetMapKey("children") |
| 365 | if err != nil { |
| 366 | t.Fatalf("Expected children key: %s", err) |
| 367 | } |
| 368 | |
| 369 | childLen, _ := children.SliceLen() |
| 370 | if childLen < 1 { |
| 371 | t.Fatalf("Expected at least one child") |
| 372 | } |
| 373 | |
| 374 | rootEl, err := children.GetSliceIndex(0) |
| 375 | if err != nil { |
| 376 | t.Fatalf("Expected to get root element: %s", err) |
| 377 | } |
| 378 | |
| 379 | rootChildren, err := rootEl.GetMapKey("children") |
| 380 | if err != nil { |
| 381 | t.Fatalf("Expected root children: %s", err) |
| 382 | } |
| 383 | |
| 384 | rootChildLen, _ := rootChildren.SliceLen() |
| 385 | if rootChildLen < 2 { |
| 386 | t.Fatalf("Expected at least two children in root, got %d", rootChildLen) |
| 387 | } |
| 388 | |
| 389 | firstChild, _ := rootChildren.GetSliceIndex(0) |
| 390 | _, hasPI := firstChild.MetadataValue("xml_processing_instructions") |
| 391 | if !hasPI { |
| 392 | t.Log("First child does not have PI (which is expected behavior after fix)") |
| 393 | } |
| 394 | |
| 395 | secondChild, _ := rootChildren.GetSliceIndex(1) |
| 396 | _, secondHasPI := secondChild.MetadataValue("xml_processing_instructions") |
| 397 | if secondHasPI { |
| 398 | t.Errorf("Second child should NOT have processing instructions (PI was incorrectly duplicated)") |
| 399 | } |
| 400 | }) |
| 401 | } |
| 402 | |
| 403 | // TestXmlRoundTrip_ProcessingInstructions tests round-trip preservation of processing instructions |
| 404 | func TestXmlRoundTrip_ProcessingInstructions(t *testing.T) { |
| 405 | tests := []struct { |
| 406 | name string |
| 407 | input string |
| 408 | expected []string |
| 409 | }{ |
| 410 | { |
| 411 | name: "xml declaration", |
| 412 | input: `<?xml version="1.0" encoding="utf-8" standalone="yes"?> |
| 413 | <root> |
| 414 | <child>text</child> |
| 415 | </root> |
| 416 | `, |
| 417 | expected: []string{`<?xml version="1.0" encoding="utf-8" standalone="yes"?>`}, |
| 418 | }, |
| 419 | { |
| 420 | name: "multiple processing instructions", |
| 421 | input: `<?xml version="1.0" encoding="UTF-8"?> |
| 422 | <?xml-stylesheet type="text/xsl" href="style.xsl"?> |
| 423 | <root> |
| 424 | <child>text</child> |
| 425 | </root> |
| 426 | `, |
| 427 | expected: []string{`<?xml version="1.0" encoding="UTF-8"?>`, `<?xml-stylesheet type="text/xsl" href="style.xsl"?>`}, |
| 428 | }, |
| 429 | } |
| 430 | |
| 431 | for _, tt := range tests { |
| 432 | t.Run(tt.name, func(t *testing.T) { |
| 433 | r, w := newTestReaderWriter(t) |
| 434 | assertRoundTrip(t, r, w, tt.input, tt.expected) |
| 435 | }) |
| 436 | } |
| 437 | } |