File: schema_tests.rs

package info (click to toggle)
rust-libxml 0.3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 596 kB
  • sloc: xml: 239; ansic: 45; makefile: 2
file content (177 lines) | stat: -rw-r--r-- 5,402 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//!
//! Test Schema Loading, XML Validating
//!
use libxml::schemas::SchemaParserContext;
use libxml::schemas::SchemaValidationContext;

use libxml::parser::Parser;

static NOTE_SCHEMA: &str = r#"<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="note">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="to" type="xs:string"/>
        <xs:element name="from" type="xs:string"/>
        <xs:element name="heading" type="xs:string"/>
        <xs:element name="body" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
"#;

static STOCK_SCHEMA: &str = r#"<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="stock">
    <xs:complexType>
      <xs:sequence maxOccurs="unbounded">
        <xs:element name="sample">
          <xs:complexType>
            <xs:all>
              <xs:element name="date" type="xs:date"/>
              <xs:element name="price" type="xs:float"/>
            </xs:all>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="ticker" type="xs:string" use="required"/>
      <xs:attribute name="exchange" type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>
</xs:schema>
"#;

static VALID_NOTE_XML: &str = r#"<?xml version="1.0"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
"#;

static INVALID_NOTE_XML: &str = r#"<?xml version="1.0"?>
<note>
  <bad>Tove</bad>
  <another>Jani</another>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
"#;

static INVALID_STOCK_XML: &str = r#"<?xml version="1.0"?>
<stock junkAttribute="foo">
  <sample>
    <date>2014-01-01</date>
    <price>NOT A NUMBER</price>
  </sample>
  <sample>
    <date>2014-01-02</date>
    <price>540.98</price>
  </sample>
  <sample>
    <date>NOT A DATE</date>
    <price>543.93</price>
  </sample>
</stock
"#;


// TODO: This test has revealed SchemaParserContext+SchemaValidationContext are not safe for
//       multi-threaded use in libxml >=2.12, at least not as currently implemented.
//       while it still reliably succeeds single-threaded, new implementation is needed to use
//       these in a parallel setting.
#[test]
fn schema_from_string() {
  let xml = Parser::default()
    .parse_string(VALID_NOTE_XML)
    .expect("Expected to be able to parse XML Document from string");

  let mut xsdparser = SchemaParserContext::from_buffer(NOTE_SCHEMA);
  let xsd = SchemaValidationContext::from_parser(&mut xsdparser);

  if let Err(errors) = xsd {
    for err in &errors {
      eprintln!("{}", err.message.as_ref().unwrap());
    }
    panic!("Failed to parse schema with {} errors", errors.len());
  }

  let mut xsdvalidator = xsd.unwrap();

  // loop over more than one validation to test for leaks in the error handling callback interactions
  for _ in 0..5 {
    if let Err(errors) = xsdvalidator.validate_document(&xml) {
      for err in &errors {
        eprintln!("{}", err.message.as_ref().unwrap());
      }

      panic!("Invalid XML accoding to XSD schema");
    }
  }
}

#[test]
fn schema_from_string_generates_errors() {
  let xml = Parser::default()
    .parse_string(INVALID_NOTE_XML)
    .expect("Expected to be able to parse XML Document from string");

  let mut xsdparser = SchemaParserContext::from_buffer(NOTE_SCHEMA);
  let xsd = SchemaValidationContext::from_parser(&mut xsdparser);

  if let Err(errors) = xsd {
    for err in &errors {
      eprintln!("{}", err.message.as_ref().unwrap());
    }
    panic!("Failed to parse schema with {} errors", errors.len());
  }

  let mut xsdvalidator = xsd.unwrap();
  for _ in 0..5 {
    if let Err(errors) = xsdvalidator.validate_document(&xml) {
      for err in &errors {
        assert_eq!(
          "Element 'bad': This element is not expected. Expected is ( to ).\n",
          err.message.as_ref().unwrap()
        );
      }
    }
  }
}

#[test]
fn schema_from_string_reports_unique_errors() {
  let xml = Parser::default()
    .parse_string(INVALID_STOCK_XML)
    .expect("Expected to be able to parse XML Document from string");
  
  let mut xsdparser = SchemaParserContext::from_buffer(STOCK_SCHEMA);
  let xsd = SchemaValidationContext::from_parser(&mut xsdparser);

  if let Err(errors) = xsd {
    for err in &errors {
      eprintln!("{}", err.message.as_ref().unwrap());
    }

    panic!("Failed to parse schema with {} errors", errors.len());
  }

  let mut xsdvalidator = xsd.unwrap();
  for _ in 0..5 {
    if let Err(errors) = xsdvalidator.validate_document(&xml) {
      assert_eq!(errors.len(), 5);
      let expected_errors = vec![
        "Element 'stock', attribute 'junkAttribute': The attribute 'junkAttribute' is not allowed.\n",
        "Element 'stock': The attribute 'ticker' is required but missing.\n",
        "Element 'stock': The attribute 'exchange' is required but missing.\n",
        "Element 'price': 'NOT A NUMBER' is not a valid value of the atomic type 'xs:float'.\n",
        "Element 'date': 'NOT A DATE' is not a valid value of the atomic type 'xs:date'.\n"
      ];
      for err_msg in expected_errors {
        assert!(errors.iter().any(|err| err.message.as_ref().unwrap() == err_msg), "Expected error message {} was not found", err_msg);
      }
    }
  }
}