Example #1
0
    def load_children(self):
        """
        Load the subelements from the xml_element in its correspondent classes.

        :returns: List of child objects.
        :rtype: list
        :raises CardinalityException: If there is more than one Version child.
        :raises CardinalityException: If there is no Group and no Rule child.
        """

        # Containers
        children = list()
        statuses = list()
        version = None
        titles = list()
        descriptions = list()
        platforms = list()
        groups = list()
        rules = list()

        # Element load
        for element in self.xml_element:
            uri, tag = Element.get_namespace_and_tag(element.tag)
            if tag == 'version':
                if version is None:
                    version = Version(element)
                else:
                    error_msg = 'version element found more than once'
                    raise CardinalityException(error_msg)
            elif tag == 'status':
                statuses.append(Status(element))
            elif tag == 'title':
                titles.append(Title(element))
            elif tag == 'description':
                descriptions.append(Description(element))
            elif tag == 'platform':
                platforms.append(Platform(element))
            elif tag == 'Group':
                groups.append(Group(element))
            elif tag == 'Rule':
                rules.append(Rule(element))

        # Element validation
        if len(groups) <= 0 and len(rules) <= 0:
            error_msg = 'a group must contain at least a group or a rule'
            raise CardinalityException(error_msg)

        # List construction
        children.extend(statuses)
        if version is not None:
            children.append(version)
        children.extend(titles)
        children.extend(descriptions)
        children.extend(platforms)
        children.extend(groups)
        children.extend(rules)

        return children
Example #2
0
    def test_print_object(self):
        """
        Tests the string representation of an Element object
        """

        xml_element = self.load_example_element()

        xccdf_element = Element(xml_element)

        uri, tag = Element.get_namespace_and_tag(xml_element.tag)

        string_element = '<{namespace}>{tag}'.format(namespace=uri, tag=tag)

        self.assertEqual(str(xccdf_element), string_element,
                         'String representation does not match')
Example #3
0
    def test_print_object(self):
        """
        Tests the string representation of an Element object
        """

        xml_element = self.load_example_element()

        xccdf_element = Element(xml_element)

        uri, tag = Element.get_namespace_and_tag(xml_element.tag)

        string_element = '<{namespace}>{tag}'.format(namespace=uri, tag=tag)

        self.assertEqual(str(xccdf_element), string_element,
                         'String representation does not match')
Example #4
0
    def load_children(self):
        """
        Load the subelements from the xml_element in its correspondent classes.

        :returns: List of child objects.
        :rtype: list
        :raises CardinalityException: If there is more than one Version child.
        :raises CardinalityException: If there is no Version child.
        :raises CardinalityException: If there is no Profile element.
        """
        # Containers
        children = list()
        statuses = list()
        version = None
        profiles = list()

        # Element load
        for element in self.xml_element:
            uri, tag = Element.get_namespace_and_tag(element.tag)
            if tag == 'version':
                if version is None:
                    version = TailoringVersion(element)
                else:
                    error_msg = 'version element found more than once'
                    raise CardinalityException(error_msg)
            elif tag == 'status':
                statuses.append(Status(element))
            elif tag == 'Profile':
                profiles.append(Profile(element))

        # Element validation
        if version is None:
            error_msg = 'version element is required'
            raise CardinalityException(error_msg)
        if len(profiles) <= 0:
            error_msg = 'Profile element is required at least once'
            raise CardinalityException(error_msg)

        # List construction
        children.extend(statuses)
        if version is not None:
            children.append(version)
        children.extend(profiles)

        return children
Example #5
0
    def load_children(self):
        """
        Load the subelements from the xml_element in its correspondent classes.

        :returns: List of child objects.
        :rtype: list
        :raises CardinalityException: If there is more than one Version child.
        :raises CardinalityException: If there is no Version child.
        :raises CardinalityException: If there is no Profile element.
        """
        # Containers
        children = list()
        statuses = list()
        version = None
        profiles = list()

        # Element load
        for element in self.xml_element:
            uri, tag = Element.get_namespace_and_tag(element.tag)
            if tag == 'version':
                if version is None:
                    version = TailoringVersion(element)
                else:
                    error_msg = 'version element found more than once'
                    raise CardinalityException(error_msg)
            elif tag == 'status':
                statuses.append(Status(element))
            elif tag == 'Profile':
                profiles.append(Profile(element))

        # Element validation
        if version is None:
            error_msg = 'version element is required'
            raise CardinalityException(error_msg)
        if len(profiles) <= 0:
            error_msg = 'Profile element is required at least once'
            raise CardinalityException(error_msg)

        # List construction
        children.extend(statuses)
        if version is not None:
            children.append(version)
        children.extend(profiles)

        return children
Example #6
0
    def test_init_all_ok_with_element(self):
        """
        Tests the class constructor with an xml_element
        """
        xml_element = self.load_example_element()

        xccdf_element = Element(xml_element)

        uri, tag = Element.get_namespace_and_tag(xml_element.tag)

        self.assertEqual(xccdf_element.namespace, uri,
                         'Namespace does not match')
        self.assertEqual(xccdf_element.name, tag, 'Tag name does not match')

        # Attributes asserting
        for attr, value in iter(xml_element.attrib.items()):
            self.assertEqual(
                getattr(xccdf_element, attr), value,
                '{attr} attribute value does not match'.format(attr=attr))
Example #7
0
    def load_example_element(self):
        """
        Helper method to load an XML element
        """

        xml_path = os.path.abspath(os.path.dirname(__file__))
        xml_file = io.open(
            os.path.join(xml_path, 'examples', 'example_xccdf.xml'))

        xml_string = xml_file.read()
        xml_file.close()

        element_tree = etree.fromstring(xml_string.encode('utf-8'))

        for element in element_tree:
            uri, tag = Element.get_namespace_and_tag(element.tag)

            if tag == 'Group':
                return element
Example #8
0
    def test_init_all_ok_with_element(self):
        """
        Tests the class constructor with an xml_element
        """
        xml_element = self.load_example_element()

        xccdf_element = Element(xml_element)

        uri, tag = Element.get_namespace_and_tag(xml_element.tag)

        self.assertEqual(xccdf_element.namespace, uri,
                         'Namespace does not match')
        self.assertEqual(xccdf_element.name, tag,
                         'Tag name does not match')

        # Attributes asserting
        for attr, value in iter(xml_element.attrib.items()):
            self.assertEqual(getattr(xccdf_element, attr), value,
                             '{attr} attribute value does not match'.format(
                                 attr=attr))
Example #9
0
    def load_example_element(self):
        """
        Helper method to load an XML element
        """

        xml_path = os.path.abspath(os.path.dirname(__file__))
        xml_file = io.open(os.path.join(
            xml_path,
            'examples',
            'example_xccdf.xml'))

        xml_string = xml_file.read()
        xml_file.close()

        element_tree = etree.fromstring(xml_string.encode('utf-8'))

        for element in element_tree:
            uri, tag = Element.get_namespace_and_tag(element.tag)

            if tag == 'Group':
                return element