| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use libxml::schemas::SchemaParserContext; |
| 5 |
use libxml::schemas::SchemaValidationContext; |
| 6 |
|
| 7 |
use libxml::parser::Parser; |
| 8 |
|
| 9 |
static NOTE_SCHEMA: &str = r#"<?xml version="1.0"?> |
| 10 |
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> |
| 11 |
<xs:element name="note"> |
| 12 |
<xs:complexType> |
| 13 |
<xs:sequence> |
| 14 |
<xs:element name="to" type="xs:string"/> |
| 15 |
<xs:element name="from" type="xs:string"/> |
| 16 |
<xs:element name="heading" type="xs:string"/> |
| 17 |
<xs:element name="body" type="xs:string"/> |
| 18 |
</xs:sequence> |
| 19 |
</xs:complexType> |
| 20 |
</xs:element> |
| 21 |
</xs:schema> |
| 22 |
"#; |
| 23 |
|
| 24 |
static STOCK_SCHEMA: &str = r#"<?xml version="1.0"?> |
| 25 |
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> |
| 26 |
<xs:element name="stock"> |
| 27 |
<xs:complexType> |
| 28 |
<xs:sequence maxOccurs="unbounded"> |
| 29 |
<xs:element name="sample"> |
| 30 |
<xs:complexType> |
| 31 |
<xs:all> |
| 32 |
<xs:element name="date" type="xs:date"/> |
| 33 |
<xs:element name="price" type="xs:float"/> |
| 34 |
</xs:all> |
| 35 |
</xs:complexType> |
| 36 |
</xs:element> |
| 37 |
</xs:sequence> |
| 38 |
<xs:attribute name="ticker" type="xs:string" use="required"/> |
| 39 |
<xs:attribute name="exchange" type="xs:string" use="required"/> |
| 40 |
</xs:complexType> |
| 41 |
</xs:element> |
| 42 |
</xs:schema> |
| 43 |
"#; |
| 44 |
|
| 45 |
static VALID_NOTE_XML: &str = r#"<?xml version="1.0"?> |
| 46 |
<note> |
| 47 |
<to>Tove</to> |
| 48 |
<from>Jani</from> |
| 49 |
<heading>Reminder</heading> |
| 50 |
<body>Don't forget me this weekend!</body> |
| 51 |
</note> |
| 52 |
"#; |
| 53 |
|
| 54 |
static INVALID_NOTE_XML: &str = r#"<?xml version="1.0"?> |
| 55 |
<note> |
| 56 |
<bad>Tove</bad> |
| 57 |
<another>Jani</another> |
| 58 |
<heading>Reminder</heading> |
| 59 |
<body>Don't forget me this weekend!</body> |
| 60 |
</note> |
| 61 |
"#; |
| 62 |
|
| 63 |
static INVALID_STOCK_XML: &str = r#"<?xml version="1.0"?> |
| 64 |
<stock junkAttribute="foo"> |
| 65 |
<sample> |
| 66 |
<date>2014-01-01</date> |
| 67 |
<price>NOT A NUMBER</price> |
| 68 |
</sample> |
| 69 |
<sample> |
| 70 |
<date>2014-01-02</date> |
| 71 |
<price>540.98</price> |
| 72 |
</sample> |
| 73 |
<sample> |
| 74 |
<date>NOT A DATE</date> |
| 75 |
<price>543.93</price> |
| 76 |
</sample> |
| 77 |
</stock |
| 78 |
"#; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
#[test] |
| 86 |
fn schema_from_string() { |
| 87 |
let xml = Parser::default() |
| 88 |
.parse_string(VALID_NOTE_XML) |
| 89 |
.expect("Expected to be able to parse XML Document from string"); |
| 90 |
|
| 91 |
let mut xsdparser = SchemaParserContext::from_buffer(NOTE_SCHEMA); |
| 92 |
let xsd = SchemaValidationContext::from_parser(&mut xsdparser); |
| 93 |
|
| 94 |
if let Err(errors) = xsd { |
| 95 |
for err in &errors { |
| 96 |
eprintln!("{}", err.message.as_ref().unwrap()); |
| 97 |
} |
| 98 |
panic!("Failed to parse schema with {} errors", errors.len()); |
| 99 |
} |
| 100 |
|
| 101 |
let mut xsdvalidator = xsd.unwrap(); |
| 102 |
|
| 103 |
|
| 104 |
for _ in 0..5 { |
| 105 |
if let Err(errors) = xsdvalidator.validate_document(&xml) { |
| 106 |
for err in &errors { |
| 107 |
eprintln!("{}", err.message.as_ref().unwrap()); |
| 108 |
} |
| 109 |
|
| 110 |
panic!("Invalid XML accoding to XSD schema"); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
fn schema_from_string_generates_errors() { |
| 117 |
let xml = Parser::default() |
| 118 |
.parse_string(INVALID_NOTE_XML) |
| 119 |
.expect("Expected to be able to parse XML Document from string"); |
| 120 |
|
| 121 |
let mut xsdparser = SchemaParserContext::from_buffer(NOTE_SCHEMA); |
| 122 |
let xsd = SchemaValidationContext::from_parser(&mut xsdparser); |
| 123 |
|
| 124 |
if let Err(errors) = xsd { |
| 125 |
for err in &errors { |
| 126 |
eprintln!("{}", err.message.as_ref().unwrap()); |
| 127 |
} |
| 128 |
panic!("Failed to parse schema with {} errors", errors.len()); |
| 129 |
} |
| 130 |
|
| 131 |
let mut xsdvalidator = xsd.unwrap(); |
| 132 |
for _ in 0..5 { |
| 133 |
if let Err(errors) = xsdvalidator.validate_document(&xml) { |
| 134 |
for err in &errors { |
| 135 |
assert_eq!( |
| 136 |
"Element 'bad': This element is not expected. Expected is ( to ).\n", |
| 137 |
err.message.as_ref().unwrap() |
| 138 |
); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
#[test] |
| 145 |
fn schema_from_string_reports_unique_errors() { |
| 146 |
let xml = Parser::default() |
| 147 |
.parse_string(INVALID_STOCK_XML) |
| 148 |
.expect("Expected to be able to parse XML Document from string"); |
| 149 |
|
| 150 |
let mut xsdparser = SchemaParserContext::from_buffer(STOCK_SCHEMA); |
| 151 |
let xsd = SchemaValidationContext::from_parser(&mut xsdparser); |
| 152 |
|
| 153 |
if let Err(errors) = xsd { |
| 154 |
for err in &errors { |
| 155 |
eprintln!("{}", err.message.as_ref().unwrap()); |
| 156 |
} |
| 157 |
|
| 158 |
panic!("Failed to parse schema with {} errors", errors.len()); |
| 159 |
} |
| 160 |
|
| 161 |
let mut xsdvalidator = xsd.unwrap(); |
| 162 |
for _ in 0..5 { |
| 163 |
if let Err(errors) = xsdvalidator.validate_document(&xml) { |
| 164 |
assert_eq!(errors.len(), 5); |
| 165 |
let expected_errors = vec![ |
| 166 |
"Element 'stock', attribute 'junkAttribute': The attribute 'junkAttribute' is not allowed.\n", |
| 167 |
"Element 'stock': The attribute 'ticker' is required but missing.\n", |
| 168 |
"Element 'stock': The attribute 'exchange' is required but missing.\n", |
| 169 |
"Element 'price': 'NOT A NUMBER' is not a valid value of the atomic type 'xs:float'.\n", |
| 170 |
"Element 'date': 'NOT A DATE' is not a valid value of the atomic type 'xs:date'.\n" |
| 171 |
]; |
| 172 |
for err_msg in expected_errors { |
| 173 |
assert!(errors.iter().any(|err| err.message.as_ref().unwrap() == err_msg), "Expected error message {} was not found", err_msg); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |