Debian Codegraph @

rdfloader/xmlreader/xmlreader_test.go

raw symbols SCIP
1 package rdfloader
2
3 import (
4 "bufio"
5 "fmt"
6 "io/ioutil"
7 "math/rand"
8 "os"
9 "reflect"
10 "testing"
11 "time"
12 )
13
14 const SampleRDF = `
15 <rdf:RDF
16 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
17 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
18 xmlns:custom="http://www.example.com/sample#">
19 <rdf:Description rdf:about="https://www.other_domain/another_sample">
20 <custom:Title>First Tag</custom:Title>
21 <custom:Content>Some
22 Multiline
23 Content
24 </custom:Content>
25 <custom:BlankTag></custom:BlankTag>
26 <custom:END custom:value="https://www.end.com/end_tag" />
27 </rdf:Description>
28 </rdf:RDF>
29 `
30
31 type TestFile struct {
32 name string
33 }
34
35 func InitTestFile(content string) (TestFile, error) {
36 // creates a temporary file with given content in the current directory
37 // and returns the pointer to testfile struct.
38
39 file := TestFile{}
40 rand.Seed(time.Now().Unix()) // seed to ensure different pseudo-random number.
41 file.name = fmt.Sprintf("sample_file_for_test%v.rdf", rand.Int())
42
43 // writing the content to file and returning the err.
44 err := ioutil.WriteFile(file.name, []byte(content), 777)
45 return file, err
46 }
47
48 func (file *TestFile) Delete() {
49 // delete the temporary file
50 err := os.Remove(file.name)
51 if err != nil {
52 panic("couldn't delete test file " + file.name) // didnt' expect any error
53 }
54 }
55
56 func TestXMLReaderFromFileObject(t *testing.T) {
57 testFile, err := InitTestFile(SampleRDF)
58 if err != nil {
59 t.Errorf(err.Error())
60 return
61 }
62 defer testFile.Delete()
63
64 // nothing much to test here.
65 // just creating a xmlReader.
66 fh, err := os.Open(testFile.name)
67 if err != nil {
68 panic("cannot open test file")
69 }
70 defer fh.Close()
71 XMLReaderFromFileObject(bufio.NewReader(fh))
72 }
73
74 func TestXMLReaderFromFilePath(t *testing.T) {
75 // testing if the program raises error when the given file is not present.
76 randomFileName := "@#$%^&*()_+"
77 _, err := XMLReaderFromFilePath(randomFileName)
78 if err == nil {
79 t.Errorf("Expected function to raise error. Why is there a file named %v?", randomFileName)
80 }
81
82 // xmlreader when the file exists.
83 testFile, err := InitTestFile(SampleRDF)
84 if err != nil {
85 t.Errorf(err.Error())
86 return
87 }
88 defer testFile.Delete()
89
90 xmlReader, err := XMLReaderFromFilePath(testFile.name)
91 defer xmlReader.CloseFileObj()
92 if err != nil {
93 t.Errorf(err.Error())
94 }
95 }
96
97 func TestXMLReader_Read(t *testing.T) {
98 xmlReader := xmlreaderFromString(SampleRDF)
99 root, err := xmlReader.Read()
100 if err != nil {
101 t.Errorf("unexpected error on a valid rdf file. %v", err)
102 }
103 // for the given SampleRDF, the root block has tags as children.
104 // Value attribute of the root tag must be empty.
105 if root.Value != "" {
106 t.Errorf("expected root block value to be empty. Found %v", root.Value)
107 }
108 // root has only one child
109 if nChildren := len(root.Children); nChildren != 1 {
110 t.Errorf("expected root tag to have %v children, found %v children", 1, nChildren)
111 }
112 // root has 3 attributes
113 if lenAttr := len(root.OpeningTag.Attrs); lenAttr != 3 {
114 t.Errorf("expected root attribute to have %v attributes, found %v attributes", 3, lenAttr)
115 }
116
117 // TestCase 2: root block followed by any tag or chars must raise an error
118 xmlReader = xmlreaderFromString(SampleRDF + "\n<tag/>")
119 _, err = xmlReader.Read()
120 if err == nil {
121 t.Error("expected Read() to raise an error")
122 }
123 }
124
125 func TestXMLReader_readAttribute(t *testing.T) {
126 // the readAttribute assumes that the file pointer points to the name of the attribute.
127
128 // string of rdf tag where the tag is having children.
129 // for example: <tag attributes> <child> </child> </tag>
130 tagIncompleteString := `xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
131 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
132 xmlns:custom="http://www.example.com/sample#">
133 ... other tags ...
134 `
135 xmlReader := xmlreaderFromString(tagIncompleteString)
136 attr, err := xmlReader.readAttribute()
137 if err != nil {
138 t.Errorf("unexpected error, %v", err)
139 }
140
141 // attribute has 3 properties: Name, SchemaName and Value.
142 if attr.Name != "rdf" {
143 t.Errorf("Wrong attribute name. Found %v, Expected %v", attr.Name, "rdf")
144 }
145 if attr.SchemaName != "xmlns" {
146 t.Errorf("Wrong attribute schemaname. Found %v, Expected %v", attr.SchemaName, "xmlns")
147 }
148 if attr.Value != "http://www.w3.org/1999/02/22-rdf-syntax-ns#" {
149 t.Errorf(
150 "Wrong attribute value. Found %v, Expected %v",
151 attr.Value,
152 "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
153 )
154 }
155 }
156
157 func TestXMLReader_readBlock(t *testing.T) {
158 // TestCase 1: prolog with only one block
159 testString := `<? xml version="1.0" ?>
160 <rdf:RDF />`
161 xmlReader := xmlreaderFromString(testString)
162 block, err := xmlReader.readBlock()
163 if err != nil {
164 t.Errorf("unexpected error: %v", err)
165 }
166 if block.OpeningTag.SchemaName != "rdf" || block.OpeningTag.Name != "RDF" {
167 t.Errorf("expected <rdf:RDF>, found <%v:%v>", block.OpeningTag.SchemaName, block.OpeningTag.Name)
168 }
169
170 // TestCase 2: block with invalid prolog
171 testString = `<? xml version="1.0" >`
172 xmlReader = xmlreaderFromString(testString)
173 _, err = xmlReader.readBlock()
174 if err == nil {
175 t.Error("expected an error reporting invalid prolog")
176 }
177
178 // TestCase 3: complete block without a separate closing tag.
179 testString = `<rdf:RDF/>`
180 xmlReader = xmlreaderFromString(testString)
181 block, _ = xmlReader.readBlock()
182 if len(block.Children) != 0 {
183 t.Errorf("expected block to have no children. Found %v children", len(block.Children))
184 }
185 if block.OpeningTag.SchemaName != "rdf" || block.OpeningTag.Name != "RDF" {
186 t.Errorf("expected block opening tag to be <rdf:RDF>, found: <%v:%v>", block.OpeningTag.SchemaName, block.OpeningTag.Name)
187 }
188
189 // TestCase 4: different opening and closing tag
190 testString = `<rdf:RDF> </rdf:rdf>`
191 xmlReader = xmlreaderFromString(testString)
192 _, err = xmlReader.readBlock()
193 if err == nil {
194 t.Error("should've raised an error reporting different opening and closing tags")
195 }
196
197 // TestCase 5: valid case: block with single attribute
198 xmlReader = xmlreaderFromString(SampleRDF)
199 _, err = xmlReader.readBlock()
200 if err != nil {
201 t.Errorf("unexpected error: %v", err)
202 }
203
204 // TestCase 6: invalid case: block with invalid cdata ( cdata without end tag )
205 testString = `
206 <spdx:extractedText>
207 <![CDATA[License by Nomos.
208 </spdx:extractedText>`
209 xmlReader = xmlreaderFromString(testString)
210 _, err = xmlReader.readBlock()
211 if err == nil {
212 t.Error("expected an error saying eof reading cdata end tag")
213 }
214
215 // TestCase 7: valid case: block with valid cdata.
216 testString = `
217 <spdx:extractedText>
218 <![CDATA[License by Nomos.]]>
219 </spdx:extractedText>`
220 xmlReader = xmlreaderFromString(testString)
221 block, err = xmlReader.readBlock()
222 if err != nil {
223 t.Errorf("unexpected error: %v", err)
224 }
225 expectedBlock := Block{
226 OpeningTag: Tag{
227 SchemaName: "spdx",
228 Name: "extractedText",
229 Attrs: nil,
230 },
231 Value: "<![CDATA[License by Nomos.]]>",
232 Children: nil,
233 }
234 if !reflect.DeepEqual(block, expectedBlock) {
235 t.Errorf("mismatching output. \nExpected: %+v. \nFound: %+v", expectedBlock, block)
236 }
237 }
238
239 func TestXMLReader_readClosingTag(t *testing.T) {
240 // TestCase 1: empty input, should raise an error
241 testString := ""
242 xmlReader := xmlreaderFromString(testString)
243 _, err := xmlReader.readClosingTag()
244 if err == nil {
245 t.Errorf("expected an error, found nil")
246 }
247
248 // TestCase 2: valid input with name and schemaName
249 xmlReader = xmlreaderFromString("</rdf:RDF>")
250 closingTag, err := xmlReader.readClosingTag()
251 if err != nil {
252 t.Errorf("unexpected errof: %v", err)
253 }
254 if closingTag.SchemaName != "rdf" || closingTag.Name != "RDF" {
255 t.Errorf("invalid closing tag. Expected </rdf:RDF> found </%v:%v>", closingTag.SchemaName, closingTag.Name)
256 }
257
258 // TestCase 2: valid input without schemaName
259 testString = `</tag>`
260 xmlReader = xmlreaderFromString(testString)
261 closingTag, err = xmlReader.readClosingTag()
262 if err != nil {
263 t.Errorf("unexpected errof: %v", err)
264 }
265 if closingTag.SchemaName != "" {
266 t.Errorf("invalid closing tag. Expected </tag> found </%v:%v>", closingTag.SchemaName, closingTag.Name)
267 }
268 if closingTag.Name != "tag" {
269 t.Errorf("invalid closing tag. Expected </tag> found </%v>", closingTag.Name)
270 }
271
272 // TestCase 3: invalid input with space before colon
273 testString = "</rdf :RDF>"
274 xmlReader = xmlreaderFromString(testString)
275 _, err = xmlReader.readClosingTag()
276 if err == nil {
277 t.Errorf("reading invalid closing tag didn't raise any error")
278 }
279
280 // TestCase 4: invalid input with WHITESPACE after colon
281 testString = `</rdf:
282 RDF>`
283 xmlReader = xmlreaderFromString(testString)
284 _, err = xmlReader.readClosingTag()
285 if err == nil {
286 t.Errorf("reading invalid closing tag didn't raise any error")
287 }
288
289 // TestCase 5: invalid input with stray characters before the closing tag end.
290 testString = `</rdf:RDF stray-chars>`
291 xmlReader = xmlreaderFromString(testString)
292 _, err = xmlReader.readClosingTag()
293 if err == nil {
294 t.Errorf("reading invalid closing tag didn't raise any error")
295 }
296
297 // TestCase 6: invalid input which doesn't start with a </
298 testString = `<rdf:RDF>`
299 xmlReader = xmlreaderFromString(testString)
300 _, err = xmlReader.readClosingTag()
301 if err == nil {
302 t.Errorf("reading invalid closing tag didn't raise any error")
303 }
304 }
305
306 func TestXMLReader_readColonPair(t *testing.T) {
307
308 // Testcase 1: Valid input.
309 testString := `rdf:RDF>`
310 xmlReader := xmlreaderFromString(testString)
311 pair, colonFound, err := xmlReader.readColonPair(1 << '>')
312 if err != nil {
313 t.Errorf("unexpected error: %v", err)
314 }
315 if colonFound != true {
316 t.Error("input had colon. program didn't find any colon")
317 }
318 if pair.First != "rdf" || pair.Second != "RDF" {
319 t.Errorf("wrong pair found. Expected: rdf:RDF, found %v:%v", pair.First, pair.Second)
320 }
321 // after reading the pair, next character should be >
322 nextRune, err := xmlReader.readARune()
323 if err != nil {
324 t.Errorf("unexpected error reading next character after reading attribute. Error: %v", err)
325 }
326 if nextRune != '>' {
327 t.Errorf("next rune to be read should be >, found %v", nextRune)
328 }
329
330 // TestCase 2: valid input without colon:
331 testString = `tag>`
332 xmlReader = xmlreaderFromString(testString)
333 pair, colonFound, err = xmlReader.readColonPair(1 << '>')
334 if err != nil {
335 t.Errorf("unexpected error: %v", err)
336 }
337 if colonFound == true {
338 t.Error("function found a colon when it doesn't exist in the input")
339 }
340 if pair.First != "tag" && pair.Second != "" {
341 t.Errorf("expected pair: ('%v', '%v'), found pair: ('%v', '%v')", "tag", "", pair.First, pair.Second)
342 }
343
344 // TestCase 3: valid tag, invalid delimiter
345 testString = `rdf:RDF>`
346 xmlReader = xmlreaderFromString(testString)
347 _, _, err = xmlReader.readColonPair(1 << ' ')
348 if err == nil {
349 t.Errorf("expected program to raise EOF error. error raised: %v", err)
350 }
351 }
352
353 func TestXMLReader_readOpeningTag(t *testing.T) {
354 // TestCase 1: empty input which doesn't have any opening tag.
355 testString := ""
356 xmlReader := xmlreaderFromString(testString)
357 _, err := xmlReader.readClosingTag()
358 if err == nil {
359 t.Errorf("expected an EOF error, found nil")
360 }
361
362 // TestCase 2: invalid input which doesn't start with <
363 testString = "extra chars <rdf:RDF>"
364 xmlReader = xmlreaderFromString(testString)
365 _, _, _, err = xmlReader.readOpeningTag()
366 if err == nil {
367 t.Errorf("expected an error, found nil")
368 }
369
370 // TestCase 3: invalid input which doesn't have < tag
371 testString = "extra chars"
372 xmlReader = xmlreaderFromString(testString)
373 _, _, _, err = xmlReader.readOpeningTag()
374 if err == nil {
375 t.Errorf("expected an EOF error, found nil")
376 }
377
378 // TestCase 4: invalid input : closing tag.
379 testString = "</tag>"
380 xmlReader = xmlreaderFromString(testString)
381 _, _, _, err = xmlReader.readOpeningTag()
382 if err == nil {
383 t.Error("expected an error, found nil")
384 }
385
386 // TestCase 5: valid prolog as an opening tag.
387 testString = `<? xml version="1.0" ?>`
388 xmlReader = xmlreaderFromString(testString)
389 _, isProlog, _, err := xmlReader.readOpeningTag()
390 if err != nil {
391 t.Errorf("unexpected error: %v", err)
392 }
393 if !isProlog {
394 t.Error("input has a prolog, program didn't found any")
395 }
396
397 // TestCase 6: invalid prolog as an opening tag.
398 testString = `<? xml version="1.0" >`
399 xmlReader = xmlreaderFromString(testString)
400 _, isProlog, _, err = xmlReader.readOpeningTag()
401 if err == nil {
402 t.Error("expected an EOF error")
403 }
404 if !isProlog {
405 t.Errorf("program didn't identify prolog tag")
406 }
407
408 // TestCase 7: input tag with only name and schema name.
409 testString = `<rdf:RDF
410 >`
411 xmlReader = xmlreaderFromString(testString)
412 openingTag, isProlog, blockComplete, err := xmlReader.readOpeningTag()
413 if err != nil {
414 t.Errorf("unexpected error: %v", err)
415 }
416 if isProlog {
417 t.Error("program incorrectly identified opening tag as a prolog")
418 }
419 if blockComplete {
420 t.Error("block was incorrectly tagged as complete")
421 }
422 if openingTag.SchemaName != "rdf" || openingTag.Name != "RDF" {
423 t.Errorf("Expected tag: <rdf:RDF>, found: <%v:%v>", openingTag.SchemaName, openingTag.Name)
424 }
425 // there shouldn't be any attribute in an empty tag.
426 if len(openingTag.Attrs) != 0 {
427 t.Errorf("found unwanted attributes in an empty tag")
428 }
429
430 // TestCase 8: opening tag of type <rdf:RDF /> where block completed.
431 testString = `< rdf:RDF
432 />`
433 xmlReader = xmlreaderFromString(testString)
434 openingTag, isProlog, blockComplete, err = xmlReader.readOpeningTag()
435 if err != nil {
436 t.Errorf("unexpected error: %v", err)
437 }
438 if isProlog {
439 t.Error("program incorrectly identified opening tag as a prolog")
440 }
441 if !blockComplete {
442 t.Error("block didn't identify block as complete")
443 }
444 if openingTag.SchemaName != "rdf" || openingTag.Name != "RDF" {
445 t.Errorf("Expected tag: <rdf:RDF>, found: <%v:%v>", openingTag.SchemaName, openingTag.Name)
446 }
447 // there shouldn't be any attributes in an empty tag.
448 if len(openingTag.Attrs) != 0 {
449 t.Error("found unwanted attributes in an empty tag")
450 }
451
452 // TestCase 9: opening tag with incomplete attribute definition.
453 testString = `<rdf:RDF xmlns:rdf=>`
454 xmlReader = xmlreaderFromString(testString)
455 _, _, _, err = xmlReader.readOpeningTag()
456 if err == nil {
457 t.Error("input tag with incomplete attribute didn't raise any error")
458 }
459
460 // TestCase 10: valid input: opening tag with a single attribute.
461 testString = `< rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" />`
462 xmlReader = xmlreaderFromString(testString)
463 openingTag, _, blockComplete, err = xmlReader.readOpeningTag()
464 if !blockComplete {
465 t.Error("a complete block wasn't tagged as complete")
466 }
467 if err != nil {
468 t.Errorf("unexpected error: %v", err)
469 }
470 if nAttrs := len(openingTag.Attrs); nAttrs != 1 {
471 t.Errorf("expected only 1 attribute, found %v attributes.", nAttrs)
472 }
473 }
474
475 func TestXMLReader_readCDATA(t *testing.T) {
476 CDATA_OPENING := "<![CDATA["
477 data := "random data"
478 CDATA_CLOSING := "]]>"
479
480 // TestCase 1: reading in an empty file must raise an error
481 fileContent := ""
482 xmlReader := xmlreaderFromString(fileContent)
483 _, err := xmlReader.readCDATA()
484 if err == nil {
485 t.Errorf("expected an eof reading from an empty file")
486 }
487
488 // TestCase 2: file-pointer doesn't start with a CDATA Tag:
489 // Must raise an error
490 fileContent = "<rdf:RDF> </rdf:RDF>"
491 xmlReader = xmlreaderFromString(fileContent)
492 _, err = xmlReader.readCDATA()
493 if err == nil {
494 t.Errorf("expected an error saying not a valid cdata tag")
495 }
496
497 // TestCase 3: similar to TC2 but the file has a CDATA Tag which is prefixed by blank chars.
498 // Must raise an error. as the function expects blank chars to be stripped before input
499 fileContent = " " + CDATA_OPENING + data + CDATA_CLOSING
500 xmlReader = xmlreaderFromString(fileContent)
501 _, err = xmlReader.readCDATA()
502 if err == nil {
503 t.Errorf("expected an error saying not a valid cdata tag")
504 }
505
506 // TestCase 4: CDATA tag without closing must raise an error
507 fileContent = CDATA_OPENING + data
508 xmlReader = xmlreaderFromString(fileContent)
509 _, err = xmlReader.readCDATA()
510 if err == nil {
511 t.Errorf("expected an error saying eof reading closing tag")
512 }
513
514 // TestCase 5: Valid CDATA Tag
515 fileContent = CDATA_OPENING + data + CDATA_CLOSING + " some other content.... "
516 xmlReader = xmlreaderFromString(fileContent)
517 output, err := xmlReader.readCDATA()
518 if err != nil {
519 t.Errorf("unexpected error: %v", err)
520 }
521 expectedOutput := CDATA_OPENING + data + CDATA_CLOSING
522 if output != expectedOutput {
523 t.Errorf("expected: %s as the output, found: %s", fileContent, output)
524 }
525 }