Ejemplo n.º 1
0
 def test_xml_resource_get_locations(self):
     resource = XMLResource(self.col_xml_file)
     self.check_url(resource.url, normalize_url(self.col_xml_file))
     locations = resource.get_locations([('ns', 'other.xsd')])
     self.assertEqual(len(locations), 2)
     self.check_url(locations[0][1], os.path.join(self.col_dir,
                                                  'other.xsd'))
     self.check_url(locations[1][1], normalize_url(self.col_xsd_file))
Ejemplo n.º 2
0
 def test_normalize_url_hash_character(self):
     self.check_url(normalize_url('issue #000.xml', 'file:///dir1/dir2/'),
                    'file:///dir1/dir2/issue %23000.xml')
     self.check_url(
         normalize_url('data.xml', 'file:///dir1/dir2/issue 000'),
         'file:///dir1/dir2/issue 000/data.xml')
     self.check_url(normalize_url('data.xml', '/dir1/dir2/issue #000'),
                    '/dir1/dir2/issue %23000/data.xml')
Ejemplo n.º 3
0
    def test_xml_resource_get_namespaces(self):
        with open(self.vh_xml_file) as schema_file:
            resource = XMLResource(schema_file)
            self.assertIsNone(resource.url)
            self.assertEqual(set(resource.get_namespaces().keys()),
                             {'vh', 'xsi'})
            self.assertFalse(schema_file.closed)

        with open(self.vh_xsd_file) as schema_file:
            resource = XMLResource(schema_file)
            self.assertIsNone(resource.url)
            self.assertEqual(set(resource.get_namespaces().keys()),
                             {'xs', 'vh'})
            self.assertFalse(schema_file.closed)

        resource = XMLResource(self.col_xml_file)
        self.assertEqual(resource.url, normalize_url(self.col_xml_file))
        self.assertEqual(set(resource.get_namespaces().keys()), {'col', 'xsi'})

        resource = XMLResource(self.col_xsd_file)
        self.assertEqual(resource.url, normalize_url(self.col_xsd_file))
        self.assertEqual(set(resource.get_namespaces().keys()), {'', 'xs'})

        resource = XMLResource("""<?xml version="1.0" ?>
            <root xmlns="tns1">
                <tns:elem1 xmlns:tns="tns1" xmlns="unknown"/>
            </root>""",
                               lazy=False)
        self.assertEqual(set(resource.get_namespaces().keys()),
                         {'', 'tns', 'default'})
        resource._lazy = True
        self.assertEqual(resource.get_namespaces().keys(), {''})

        resource = XMLResource("""<?xml version="1.0" ?>
            <root xmlns:tns="tns1">
                <tns:elem1 xmlns:tns="tns1" xmlns="unknown"/>
            </root>""",
                               lazy=False)
        self.assertEqual(set(resource.get_namespaces().keys()),
                         {'default', 'tns'})
        self.assertEqual(
            resource.get_namespaces(root_only=True).keys(), {'tns'})
        resource._lazy = True
        self.assertEqual(resource.get_namespaces().keys(), {'tns'})

        resource = XMLResource("""<?xml version="1.0" ?>
            <root xmlns:tns="tns1">
                <tns:elem1 xmlns:tns="tns3" xmlns="unknown"/>
            </root>""",
                               lazy=False)
        self.assertEqual(set(resource.get_namespaces().keys()),
                         {'default', 'tns', 'tns0'})
        resource._lazy = True
        self.assertEqual(resource.get_namespaces().keys(), {'tns'})
Ejemplo n.º 4
0
    def test_xml_resource_get_namespaces(self):
        with open(self.vh_xml_file) as schema_file:
            resource = XMLResource(schema_file)
            self.assertEqual(resource.url, normalize_url(self.vh_xml_file))
            self.assertEqual(set(resource.get_namespaces().keys()), {'vh', 'xsi'})

        with open(self.vh_xsd_file) as schema_file:
            resource = XMLResource(schema_file)
            self.assertEqual(resource.url, normalize_url(self.vh_xsd_file))
            self.assertEqual(set(resource.get_namespaces().keys()), {'xs', 'vh'})

        resource = XMLResource(self.col_xml_file)
        self.assertEqual(resource.url, normalize_url(self.col_xml_file))
        self.assertEqual(set(resource.get_namespaces().keys()), {'col', 'xsi'})

        resource = XMLResource(self.col_xsd_file)
        self.assertEqual(resource.url, normalize_url(self.col_xsd_file))
        self.assertEqual(set(resource.get_namespaces().keys()), {'', 'xs'})
Ejemplo n.º 5
0
 def test_xml_resource_get_locations(self):
     resource = XMLResource(self.col_xml_file)
     self.assertEqual(resource.url, normalize_url(self.col_xml_file))
     locations = resource.get_locations([('ns', 'other.xsd')])
     self.assertEqual(len(locations), 2)
     if not IS_WIN_PLATFORM:
         self.assertEqual(
             locations[0][1].lower(),
             FILE_SCHEME.format(self.col_dir).lower() + '/other.xsd')
Ejemplo n.º 6
0
    def test_xml_resource_get_namespaces(self):
        with open(self.vh_xml_file) as schema_file:
            resource = XMLResource(schema_file)
            self.assertEqual(resource.url, normalize_url(self.vh_xml_file))
            self.assertEqual(set(resource.get_namespaces().keys()),
                             {'vh', 'xsi'})

        with open(self.vh_xsd_file) as schema_file:
            resource = XMLResource(schema_file)
            self.assertEqual(resource.url, normalize_url(self.vh_xsd_file))
            self.assertEqual(set(resource.get_namespaces().keys()),
                             {'xs', 'vh'})

        resource = XMLResource(self.col_xml_file)
        self.assertEqual(resource.url, normalize_url(self.col_xml_file))
        self.assertEqual(set(resource.get_namespaces().keys()), {'col', 'xsi'})

        resource = XMLResource(self.col_xsd_file)
        self.assertEqual(resource.url, normalize_url(self.col_xsd_file))
        self.assertEqual(set(resource.get_namespaces().keys()), {'', 'xs'})
Ejemplo n.º 7
0
    def test_xml_resource_access(self):
        resource = XMLResource(self.vh_xml_file)
        base_url = resource.base_url

        XMLResource(self.vh_xml_file, allow='local')
        XMLResource(self.vh_xml_file,
                    base_url=os.path.dirname(self.vh_xml_file),
                    allow='sandbox')

        with self.assertRaises(XMLSchemaResourceError) as ctx:
            XMLResource(self.vh_xml_file, allow='remote')
        self.assertTrue(
            str(ctx.exception).startswith("block access to local resource"))

        with self.assertRaises(XMLSchemaResourceError) as ctx:
            XMLResource("https://xmlschema.test/vehicles.xsd", allow='local')
        self.assertEqual(
            str(ctx.exception),
            "block access to remote resource https://xmlschema.test/vehicles.xsd"
        )

        with self.assertRaises(XMLSchemaResourceError) as ctx:
            XMLResource("https://xmlschema.test/vehicles.xsd", allow='sandbox')
        self.assertEqual(
            str(ctx.exception),
            "block access to files out of sandbox requires 'base_url' to be set"
        )

        with self.assertRaises(XMLSchemaResourceError) as ctx:
            XMLResource("/tmp/vehicles.xsd", allow='sandbox')
        self.assertEqual(
            str(ctx.exception),
            "block access to files out of sandbox requires 'base_url' to be set",
        )

        source = "/tmp/vehicles.xsd"
        with self.assertRaises(XMLSchemaResourceError) as ctx:
            XMLResource(source, base_url=base_url, allow='sandbox')
        self.assertEqual(
            str(ctx.exception),
            "block access to out of sandbox file {}".format(
                normalize_url(source)),
        )

        with self.assertRaises(TypeError) as ctx:
            XMLResource("https://xmlschema.test/vehicles.xsd", allow=None)
        self.assertEqual(
            str(ctx.exception),
            "invalid type <class 'NoneType'> for the attribute 'allow'")

        with self.assertRaises(ValueError) as ctx:
            XMLResource("https://xmlschema.test/vehicles.xsd", allow='any')
        self.assertEqual(str(ctx.exception),
                         "'allow' attribute: 'any' is not a security mode")
Ejemplo n.º 8
0
    def test_normalize_url_windows(self):
        win_abs_path1 = 'z:\\Dir_1_0\\Dir2-0\\schemas/XSD_1.0/XMLSchema.xsd'
        win_abs_path2 = 'z:\\Dir-1.0\\Dir-2_0\\'
        self.check_url(normalize_url(win_abs_path1), win_abs_path1)

        self.check_url(normalize_url('k:\\Dir3\\schema.xsd', win_abs_path1),
                       'file:///k:\\Dir3\\schema.xsd')
        self.check_url(normalize_url('k:\\Dir3\\schema.xsd', win_abs_path2),
                       'file:///k:\\Dir3\\schema.xsd')
        self.check_url(normalize_url('schema.xsd', win_abs_path2),
                       'file:///z:\\Dir-1.0\\Dir-2_0/schema.xsd')
        self.check_url(normalize_url('xsd1.0/schema.xsd', win_abs_path2),
                       'file:///z:\\Dir-1.0\\Dir-2_0/xsd1.0/schema.xsd')
        self.check_url(normalize_url('file:///\\k:\\Dir A\\schema.xsd'),
                       'file:///k:\\Dir A\\schema.xsd')
Ejemplo n.º 9
0
    def test_normalize_url_slashes(self):
        # Issue #116
        self.assertEqual(
            normalize_url('//anaconda/envs/testenv/lib/python3.6/'
                          'site-packages/xmlschema/validators/schemas/'),
            'file:///anaconda/envs/testenv/lib/python3.6/'
            'site-packages/xmlschema/validators/schemas/')
        self.assertEqual(normalize_url('/root/dir1/schema.xsd'),
                         'file:///root/dir1/schema.xsd')
        self.assertEqual(normalize_url('//root/dir1/schema.xsd'),
                         'file:///root/dir1/schema.xsd')
        self.assertEqual(normalize_url('////root/dir1/schema.xsd'),
                         'file:///root/dir1/schema.xsd')

        self.assertEqual(normalize_url('dir2/schema.xsd', '//root/dir1/'),
                         'file:///root/dir1/dir2/schema.xsd')
        self.assertEqual(normalize_url('dir2/schema.xsd', '//root/dir1'),
                         'file:///root/dir1/dir2/schema.xsd')
        self.assertEqual(normalize_url('dir2/schema.xsd', '////root/dir1'),
                         'file:///root/dir1/dir2/schema.xsd')
Ejemplo n.º 10
0
    def test_normalize_url(self):
        url1 = "https://example.com/xsd/other_schema.xsd"
        self.check_url(
            normalize_url(url1, base_url="/path_my_schema/schema.xsd"), url1)

        parent_dir = os.path.dirname(os.getcwd())
        self.check_url(normalize_url('../dir1/./dir2'),
                       os.path.join(parent_dir, 'dir1/dir2'))
        self.check_url(
            normalize_url('../dir1/./dir2', '/home', keep_relative=True),
            'file:///dir1/dir2')
        self.check_url(normalize_url('../dir1/./dir2', 'file:///home'),
                       'file:///dir1/dir2')

        self.check_url(normalize_url('other.xsd', 'file:///home'),
                       'file:///home/other.xsd')
        self.check_url(normalize_url('other.xsd', 'file:///home/'),
                       'file:///home/other.xsd')
        self.check_url(normalize_url('file:other.xsd', 'file:///home'),
                       'file:///home/other.xsd')

        cwd_url = 'file://{}/'.format(add_leading_slash(os.getcwd()))
        self.check_url(normalize_url('file:other.xsd', keep_relative=True),
                       'file:other.xsd')
        self.check_url(normalize_url('file:other.xsd'), cwd_url + 'other.xsd')
        self.check_url(
            normalize_url('file:other.xsd', 'http://site/base', True),
            'file:other.xsd')
        self.check_url(normalize_url('file:other.xsd', 'http://site/base'),
                       cwd_url + 'other.xsd')

        self.check_url(normalize_url('dummy path.xsd'),
                       cwd_url + 'dummy path.xsd')
        self.check_url(normalize_url('dummy path.xsd', 'http://site/base'),
                       'http://site/base/dummy%20path.xsd')
        self.check_url(normalize_url('dummy path.xsd', 'file://host/home/'),
                       'file://host/home/dummy path.xsd')

        win_abs_path1 = 'z:\\Dir_1_0\\Dir2-0\\schemas/XSD_1.0/XMLSchema.xsd'
        win_abs_path2 = 'z:\\Dir-1.0\\Dir-2_0\\'
        self.check_url(normalize_url(win_abs_path1), win_abs_path1)

        self.check_url(normalize_url('k:\\Dir3\\schema.xsd', win_abs_path1),
                       'file:///k:\\Dir3\\schema.xsd')
        self.check_url(normalize_url('k:\\Dir3\\schema.xsd', win_abs_path2),
                       'file:///k:\\Dir3\\schema.xsd')
        self.check_url(normalize_url('schema.xsd', win_abs_path2),
                       'file:///z:\\Dir-1.0\\Dir-2_0/schema.xsd')
        self.check_url(normalize_url('xsd1.0/schema.xsd', win_abs_path2),
                       'file:///z:\\Dir-1.0\\Dir-2_0/xsd1.0/schema.xsd')
Ejemplo n.º 11
0
    def test_normalize_url(self):
        url1 = "https://example.com/xsd/other_schema.xsd"
        self.check_url(
            normalize_url(url1, base_url="/path_my_schema/schema.xsd"), url1)

        parent_dir = os.path.dirname(os.getcwd())
        self.check_url(normalize_url('../dir1/./dir2'),
                       os.path.join(parent_dir, 'dir1/dir2'))
        self.check_url(
            normalize_url('../dir1/./dir2', '/home', keep_relative=True),
            'file:///dir1/dir2')
        self.check_url(normalize_url('../dir1/./dir2', 'file:///home'),
                       'file:///dir1/dir2')

        self.check_url(normalize_url('other.xsd', 'file:///home'),
                       'file:///home/other.xsd')
        self.check_url(normalize_url('other.xsd', 'file:///home/'),
                       'file:///home/other.xsd')
        self.check_url(normalize_url('file:other.xsd', 'file:///home'),
                       'file:///home/other.xsd')

        cwd_url = 'file://{}/'.format(add_leading_slash(os.getcwd()))
        self.check_url(normalize_url('file:other.xsd', keep_relative=True),
                       'file:other.xsd')
        self.check_url(normalize_url('file:other.xsd'), cwd_url + 'other.xsd')
        self.check_url(
            normalize_url('file:other.xsd', 'http://site/base', True),
            'file:other.xsd')
        self.check_url(normalize_url('file:other.xsd', 'http://site/base'),
                       cwd_url + 'other.xsd')

        self.check_url(normalize_url('dummy path.xsd'),
                       cwd_url + 'dummy path.xsd')
        self.check_url(normalize_url('dummy path.xsd', 'http://site/base'),
                       'http://site/base/dummy%20path.xsd')
        self.check_url(normalize_url('dummy path.xsd', 'file://host/home/'),
                       'file://host/home/dummy path.xsd')

        win_abs_path1 = 'z:\\Dir_1_0\\Dir2-0\\schemas/XSD_1.0/XMLSchema.xsd'
        win_abs_path2 = 'z:\\Dir-1.0\\Dir-2_0\\'
        self.check_url(normalize_url(win_abs_path1), win_abs_path1)

        self.check_url(normalize_url('k:\\Dir3\\schema.xsd', win_abs_path1),
                       'file:///k:\\Dir3\\schema.xsd')
        self.check_url(normalize_url('k:\\Dir3\\schema.xsd', win_abs_path2),
                       'file:///k:\\Dir3\\schema.xsd')
        self.check_url(normalize_url('schema.xsd', win_abs_path2),
                       'file:///z:\\Dir-1.0\\Dir-2_0/schema.xsd')
        self.check_url(normalize_url('xsd1.0/schema.xsd', win_abs_path2),
                       'file:///z:\\Dir-1.0\\Dir-2_0/xsd1.0/schema.xsd')

        # Issue #116
        self.assertEqual(
            normalize_url(
                '//anaconda/envs/testenv/lib/python3.6/site-packages/xmlschema/validators/schemas/'
            ),
            'file:///anaconda/envs/testenv/lib/python3.6/site-packages/xmlschema/validators/schemas/'
        )
        self.assertEqual(normalize_url('/root/dir1/schema.xsd'),
                         'file:///root/dir1/schema.xsd')
        self.assertEqual(normalize_url('//root/dir1/schema.xsd'),
                         'file:///root/dir1/schema.xsd')
        self.assertEqual(normalize_url('////root/dir1/schema.xsd'),
                         'file:///root/dir1/schema.xsd')

        self.assertEqual(normalize_url('dir2/schema.xsd', '//root/dir1/'),
                         'file:///root/dir1/dir2/schema.xsd')
        self.assertEqual(normalize_url('dir2/schema.xsd', '//root/dir1'),
                         'file:///root/dir1/dir2/schema.xsd')
        self.assertEqual(normalize_url('dir2/schema.xsd', '////root/dir1'),
                         'file:///root/dir1/dir2/schema.xsd')
Ejemplo n.º 12
0
    def test_normalize_url(self):
        url1 = "https://example.com/xsd/other_schema.xsd"
        self.check_url(normalize_url(url1, base_url="/path_my_schema/schema.xsd"), url1)

        parent_dir = os.path.dirname(os.getcwd())
        self.check_url(normalize_url('../dir1/./dir2'), os.path.join(parent_dir, 'dir1/dir2'))
        self.check_url(normalize_url('../dir1/./dir2', '/home', keep_relative=True), 'file:///dir1/dir2')
        self.check_url(normalize_url('../dir1/./dir2', 'file:///home'), 'file:///dir1/dir2')

        self.check_url(normalize_url('other.xsd', 'file:///home'), 'file:///home/other.xsd')
        self.check_url(normalize_url('other.xsd', 'file:///home/'), 'file:///home/other.xsd')
        self.check_url(normalize_url('file:other.xsd', 'file:///home'), 'file:///home/other.xsd')

        cwd_url = 'file://{}/'.format(add_leading_slash(os.getcwd()))
        self.check_url(normalize_url('file:other.xsd', keep_relative=True), 'file:other.xsd')
        self.check_url(normalize_url('file:other.xsd'), cwd_url + 'other.xsd')
        self.check_url(normalize_url('file:other.xsd', 'http://site/base', True), 'file:other.xsd')
        self.check_url(normalize_url('file:other.xsd', 'http://site/base'), cwd_url + 'other.xsd')

        self.check_url(normalize_url('dummy path.xsd'), cwd_url + 'dummy path.xsd')
        self.check_url(normalize_url('dummy path.xsd', 'http://site/base'), 'http://site/base/dummy%20path.xsd')
        self.check_url(normalize_url('dummy path.xsd', 'file://host/home/'), 'file://host/home/dummy path.xsd')

        win_abs_path1 = 'z:\\Dir_1_0\\Dir2-0\\schemas/XSD_1.0/XMLSchema.xsd'
        win_abs_path2 = 'z:\\Dir-1.0\\Dir-2_0\\'
        self.check_url(normalize_url(win_abs_path1), win_abs_path1)

        self.check_url(normalize_url('k:\\Dir3\\schema.xsd', win_abs_path1), 'file:///k:\\Dir3\\schema.xsd')
        self.check_url(normalize_url('k:\\Dir3\\schema.xsd', win_abs_path2), 'file:///k:\\Dir3\\schema.xsd')
        self.check_url(normalize_url('schema.xsd', win_abs_path2), 'file:///z:\\Dir-1.0\\Dir-2_0/schema.xsd')
        self.check_url(
            normalize_url('xsd1.0/schema.xsd', win_abs_path2), 'file:///z:\\Dir-1.0\\Dir-2_0/xsd1.0/schema.xsd'
        )
Ejemplo n.º 13
0
 def test_xml_resource_get_locations(self):
     resource = XMLResource(self.col_xml_file)
     self.check_url(resource.url, normalize_url(self.col_xml_file))
     locations = resource.get_locations([('ns', 'other.xsd')])
     self.assertEqual(len(locations), 2)
     self.check_url(locations[0][1], os.path.join(self.col_dir, 'other.xsd'))
Ejemplo n.º 14
0
    def test_normalize_url_posix(self):
        url1 = "https://example.com/xsd/other_schema.xsd"
        self.check_url(
            normalize_url(url1, base_url="/path_my_schema/schema.xsd"), url1)

        parent_dir = os.path.dirname(os.getcwd())
        self.check_url(normalize_url('../dir1/./dir2'),
                       os.path.join(parent_dir, 'dir1/dir2'))
        self.check_url(
            normalize_url('../dir1/./dir2', '/home', keep_relative=True),
            'file:///dir1/dir2')
        self.check_url(normalize_url('../dir1/./dir2', 'file:///home'),
                       'file:///dir1/dir2')

        self.check_url(normalize_url('other.xsd', 'file:///home'),
                       'file:///home/other.xsd')
        self.check_url(normalize_url('other.xsd', 'file:///home/'),
                       'file:///home/other.xsd')
        self.check_url(normalize_url('file:other.xsd', 'file:///home'),
                       'file:///home/other.xsd')

        cwd = os.getcwd()
        cwd_url = 'file://{}/'.format(cwd) if cwd.startswith(
            '/') else 'file:///{}/'.format(cwd)

        self.check_url(normalize_url('file:other.xsd', keep_relative=True),
                       'file:other.xsd')
        self.check_url(normalize_url('file:other.xsd'), cwd_url + 'other.xsd')
        self.check_url(
            normalize_url('file:other.xsd', 'http://site/base', True),
            'file:other.xsd')
        self.check_url(normalize_url('file:other.xsd', 'http://site/base'),
                       cwd_url + 'other.xsd')

        self.check_url(normalize_url('dummy path.xsd'),
                       cwd_url + 'dummy path.xsd')
        self.check_url(normalize_url('dummy path.xsd', 'http://site/base'),
                       'http://site/base/dummy%20path.xsd')
        self.check_url(normalize_url('dummy path.xsd', 'file://host/home/'),
                       'file://host/home/dummy path.xsd')
Ejemplo n.º 15
0
 def test_absolute_path(self):
     url1 = "https://example.com/xsd/other_schema.xsd"
     self.assertTrue(
         xmlschema.normalize_url(
             url1, base_url="/path_my_schema/schema.xsd") == url1)