Add comprehensive tests for XML parser functionality

- Introduced benchmark tests for various XML parsing methods, including parsing from strings, bytes, and readers, to evaluate performance.
- Added unit tests for the parser interface and validation mechanisms, ensuring robust error handling and validation logic.
- Implemented tests for utility functions related to XML processing, enhancing overall test coverage and reliability.
- Created a test suite for the TED domain, validating the structure and functionality of contract notices and related entities.
- Developed a comprehensive test runner script to facilitate automated testing, coverage analysis, and performance benchmarking for the XML parser package.
This commit is contained in:
n.nakhostin
2025-08-12 12:51:24 +03:30
parent 7dc695752b
commit 5322d3cd8e
6 changed files with 2905 additions and 0 deletions
+39
View File
@@ -61,6 +61,13 @@ func (p *GenericParser[T]) ParseBytes(xmlData []byte) (*T, error) {
targetPtr = reflect.New(resultType).Interface()
}
// Check depth limit before parsing if specified
if p.options.MaxDepth > 0 {
if err := p.checkDepthLimit(xmlData); err != nil {
return nil, err
}
}
// Create XML decoder
decoder := xml.NewDecoder(bytes.NewReader(xmlData))
@@ -142,6 +149,38 @@ func (p *GenericParser[T]) Validate(reader io.Reader) error {
return nil
}
// checkDepthLimit validates that XML doesn't exceed the maximum depth limit
func (p *GenericParser[T]) checkDepthLimit(xmlData []byte) error {
decoder := xml.NewDecoder(bytes.NewReader(xmlData))
depth := 0
maxDepthReached := 0
for {
token, err := decoder.Token()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("XML depth validation failed: %w", err)
}
switch token.(type) {
case xml.StartElement:
depth++
if depth > maxDepthReached {
maxDepthReached = depth
}
if depth > p.options.MaxDepth {
return fmt.Errorf("XML depth %d exceeds maximum allowed depth %d", depth, p.options.MaxDepth)
}
case xml.EndElement:
depth--
}
}
return nil
}
// ParseWithResult parses XML and returns detailed result with metadata
func (p *GenericParser[T]) ParseWithResult(reader io.Reader) (*ParseResult[T], error) {
result := &ParseResult[T]{