aboutsummaryrefslogtreecommitdiff
path: root/rpkid/xml-parse-test.py
blob: bde7d167ec099cff3750c68e7b0149f2fd75ce58 (plain) (blame)
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
# $Id$

# Copyright (C) 2007--2008  American Registry for Internet Numbers ("ARIN")
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND ARIN DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS.  IN NO EVENT SHALL ARIN BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

import glob, rpki.up_down, rpki.left_right, rpki.relaxng, xml.sax, lxml.etree, lxml.sax, POW, POW.pkix

verbose = True

def test(fileglob, rng, sax_handler, encoding, tester=None):
  files = glob.glob(fileglob)
  files.sort()
  for f in files:
    print "\n<!--", f, "-->"
    handler = sax_handler()
    elt_in = lxml.etree.parse(f).getroot()
    rng.assertValid(elt_in)
    lxml.sax.saxify(elt_in, handler)
    elt_out = handler.result.toXML()
    rng.assertValid(elt_out)
    if (tester):
      tester(elt_in, elt_out, handler.result)
    print lxml.etree.tostring(elt_out, pretty_print=True, encoding=encoding, xml_declaration=True)

def pprint_cert(cert):
  print cert.get_POW().pprint()

def ud_tester(elt_in, elt_out, msg):
  assert isinstance(msg, rpki.up_down.message_pdu)
  if verbose:
    if isinstance(msg.payload, rpki.up_down.list_response_pdu):
      for c in msg.payload.classes:
        for i in range(len(c.certs)):
          print "[Certificate #%d]" % i
          pprint_cert(c.certs[i].cert)
        print "[Issuer]"
        pprint_cert(c.issuer)

def lr_tester(elt_in, elt_out, msg):
  assert isinstance(msg, rpki.left_right.msg)
  if verbose:
    for bsc in [x for x in msg if isinstance(x, rpki.left_right.bsc_elt)]:
      for cert in bsc.signing_cert:
        pprint_cert(cert)

test(fileglob="up-down-protocol-samples/*.xml",
     rng=rpki.relaxng.up_down,
     sax_handler=rpki.up_down.sax_handler,
     encoding="utf-8",
     tester=ud_tester)

test(fileglob="left-right-protocol-samples/*.xml",
     rng=rpki.relaxng.left_right,
     sax_handler=rpki.left_right.sax_handler,
     encoding="us-ascii",
     tester=lr_tester)