Exemple #1
0
def test_remove_distributor_entity(swidtag, filename):
    assert swidtag.entity_set.count() == 2
    with open('tests/test_tags/strongswan.full.swidtag.singleentity') as f:
        xml = f.read()
        tag, replaced = utils.process_swid_tag(xml, allow_tag_update=True)
        assert replaced is True
        assert tag.entity_set.count() == 1
Exemple #2
0
def test_remove_publisher_entity(swidtag, filename):
    assert swidtag.entity_set.count() == 2
    with open('tests/test_tags/strongswan.full.swidtag.singleentity') as f:
        xml = f.read()
        tag, replaced = utils.process_swid_tag(xml, allow_tag_update=True)
        assert replaced is True
        assert tag.entity_set.count() == 1
Exemple #3
0
def test_changed_software_entity_name(swidtag, filename):
    new_xml = swidtag.swid_xml.replace(
        'SoftwareIdentity name="strongswan"',
        'SoftwareIdentity name="strongswan123"')
    tag, replaced = utils.process_swid_tag(new_xml, allow_tag_update=True)
    assert replaced is True
    assert Tag.objects.count() == 1
Exemple #4
0
def test_invalid_tags(filename):
    with open('tests/test_tags/invalid_tags/%s' % filename) as f:
        xml = f.read()
        # an invalid tag should raise an ValueError
        with pytest.raises(ValueError):
            tag, replaced = utils.process_swid_tag(xml)
        assert len(Tag.objects.all()) == 0
        assert len(Entity.objects.all()) == 0
Exemple #5
0
def test_invalid_tags(filename):
    with open('tests/test_tags/invalid_tags/%s' % filename) as f:
        xml = f.read()
        # an invalid tag should raise an ValueError
        with pytest.raises(ValueError):
            tag, replaced = utils.process_swid_tag(xml)
        assert len(Tag.objects.all()) == 0
        assert len(Entity.objects.all()) == 0
Exemple #6
0
def test_tag_replace_files(swidtag, filename):
    assert swidtag.files.count() == 7

    with open('tests/test_tags/strongswan.full.swidtag.replacement') as f:
        xml = f.read()
        tag, replaced = utils.process_swid_tag(xml, allow_tag_update=True)

    assert replaced is True
    assert tag.files.count() == 3
Exemple #7
0
def test_tag_replace_files(swidtag, filename):
    assert swidtag.files.count() == 7

    with open('tests/test_tags/strongswan.full.swidtag.replacement') as f:
        xml = f.read()
        tag, replaced = utils.process_swid_tag(xml, allow_tag_update=True)

    assert replaced is True
    assert tag.files.count() == 3
Exemple #8
0
def test_tag_add_entities(swidtag, filename):
    assert swidtag.entityrole_set.count() == 2
    old_software_id = swidtag.software_id
    with open('tests/test_tags/strongswan.full.swidtag.replacement') as f:
        xml = f.read()
        tag, replaced = utils.process_swid_tag(xml, allow_tag_update=True)

    assert tag.software_id == old_software_id
    assert replaced is True
    assert tag.entity_set.count() == 4
Exemple #9
0
def test_tag_add_entities(swidtag, filename):
    assert swidtag.entityrole_set.count() == 2
    old_software_id = swidtag.software_id
    with open('tests/test_tags/strongswan.full.swidtag.replacement') as f:
        xml = f.read()
        tag, replaced = utils.process_swid_tag(xml, allow_tag_update=True)

    assert tag.software_id == old_software_id
    assert replaced is True
    assert tag.entity_set.count() == 4
Exemple #10
0
def test_change_tagcreator_entity_regid(swidtag, filename):
    """
    Changing a tagCreator entity creates a new tag, since a tag is uniquely identified
    by the regid (of the tagCreator entity) and the unique_id of the tag itself.
    """
    new_xml = swidtag.swid_xml.replace('name="strongSwan" regid="strongswan.org"',
                                       'name="strongSwan" regid="strongswan.net"')
    tag, replaced = utils.process_swid_tag(new_xml, allow_tag_update=True)
    assert Tag.objects.count() == 2
    assert 'strongswan.net' in tag.software_id
    assert replaced is False
Exemple #11
0
def test_change_tagcreator_entity_regid(swidtag, filename):
    """
    Changing a tagcreator entity creates a new tag, since a tag is uniquely identified
    by the regid (of the tagcreator entity) and the unique_id of the tag itself.
    """
    new_xml = swidtag.swid_xml.replace(
        'name="strongSwan" regid="regid.2004-03.org.strongswan"',
        'name="strongSwan" regid="regid.2005-03.org.strongswan"')
    tag, replaced = utils.process_swid_tag(new_xml, allow_tag_update=True)
    assert Tag.objects.count() == 2
    assert 'regid.2005-03.org.strongswan' in tag.software_id
    assert replaced is False
Exemple #12
0
def swidtag(request, transactional_db):
    """
    Create and return a apps.swid.models.Tag instance based on the specified file.

    This requires the test using this fixture to be parametrized with a
    'filename' argument that specifies the filename of the SWID tag test file
    inside `tests/test_tags/`.

    """
    filename = request.getfuncargvalue('filename')
    with open('tests/test_tags/%s' % filename, 'r') as f:
        tag_xml = f.read()
        return utils.process_swid_tag(tag_xml, allow_tag_update=True)[0]
Exemple #13
0
def swidtag(request, transactional_db):
    """
    Create and return a apps.swid.models.Tag instance based on the specified file.

    This requires the test using this fixture to be parametrized with a
    'filename' argument that specifies the filename of the SWID tag test file
    inside `tests/test_tags/`.

    """
    filename = request.getfuncargvalue('filename')
    with open('tests/test_tags/%s' % filename, 'r') as f:
        tag_xml = f.read()
        return utils.process_swid_tag(tag_xml, allow_tag_update=True)[0]
Exemple #14
0
def test_swid_measurement_diff(api_client, session, swidtag, filename):
    software_ids = [
        'strongswan.org__debian_7.4-x86_64-cowsay-3.03+dfsg1-4',
        'strongswan.org__debian_7.4-x86_64-strongswan-4.5.2-1.5+deb7u3'
    ]
    data = {'data': software_ids}

    # make call to get diff
    response = api_client.post(reverse('session-swid-measurement', args=[session.id]), data, format='json')
    assert response.status_code == status.HTTP_412_PRECONDITION_FAILED
    assert len(response.data) == 1
    assert software_ids[1] not in response.data
    assert software_ids[0] in response.data
    assert session.tag_set.count() == 0

    # insert missing tag into db
    with open('tests/test_tags/cowsay.short.swidtag') as file:
        utils.process_swid_tag(file.read())

    # make call again
    response = api_client.post(reverse('session-swid-measurement', args=[session.id]), data, format='json')
    assert response.status_code == status.HTTP_200_OK
    assert len(response.data) == 0
    assert session.tag_set.count() == 2
Exemple #15
0
def test_change_entity_name(swidtag, filename):
    entity_to_update = Entity.objects.get(regid='strongswan.org')
    old_entity_name = 'strongSwan'
    new_entity_name = 'strongSwan Project'

    assert Entity.objects.count() == 2
    assert swidtag.entity_set.count() == 2
    assert entity_to_update.name == old_entity_name

    new_xml = swidtag.swid_xml.replace('name="%s"' % old_entity_name, 'name="%s"' % new_entity_name)
    tag, replaced = utils.process_swid_tag(new_xml, allow_tag_update=False)
    entity_to_update = Entity.objects.get(regid='strongswan.org')

    assert Entity.objects.count() == 2
    assert tag.entity_set.count() == 2
    assert entity_to_update.name == new_entity_name
Exemple #16
0
def test_change_entity_name(swidtag, filename):
    entity_to_update = Entity.objects.get(regid='regid.2004-03.org.strongswan')
    old_entity_name = 'strongSwan'
    new_entity_name = 'strongSwan Project'

    assert Entity.objects.count() == 2
    assert swidtag.entity_set.count() == 2
    assert entity_to_update.name == old_entity_name

    new_xml = swidtag.swid_xml.replace('name="%s"' % old_entity_name,
                                       'name="%s"' % new_entity_name)
    tag, replaced = utils.process_swid_tag(new_xml, allow_tag_update=False)
    entity_to_update = Entity.objects.get(regid='regid.2004-03.org.strongswan')

    assert Entity.objects.count() == 2
    assert tag.entity_set.count() == 2
    assert entity_to_update.name == new_entity_name
Exemple #17
0
    def handle(self, *args, **kwargs):
        if len(args) != 1:
            raise CommandError('Usage: ./manage.py importswid <filename>')

        filename = args[0]

        if not os.path.isfile(filename):
            raise CommandError('No such file: ' + filename)

        encoding = self.stdout.encoding or 'ascii'
        with open(filename, 'r') as f:
            for line in f:
                tag_xml = line.strip().decode('utf8')
                tag, replaced = utils.process_swid_tag(tag_xml, allow_tag_update=True)
                if replaced:
                    self.stdout.write('Replaced {0}'.format(tag).encode(encoding, 'replace'))
                else:
                    self.stdout.write('Added {0}'.format(tag).encode(encoding, 'replace'))
Exemple #18
0
    def handle(self, *args, **kwargs):
        if len(args) != 1:
            raise CommandError('Usage: ./manage.py importswid <filename>')

        filename = args[0]

        if not os.path.isfile(filename):
            raise CommandError('No such file: ' + filename)

        # Publish SWID tags on XMPP-Grid?
        xmpp_connected = False
        if USE_XMPP:
            # Initialize XMPP client
            xmpp = XmppGridClient(XMPP_GRID['jid'], XMPP_GRID['password'],
                                  XMPP_GRID['pubsub_server'])
            xmpp.ca_certs = XMPP_GRID['cacert']
            xmpp.certfile = XMPP_GRID['certfile']
            xmpp.keyfile = XMPP_GRID['keyfile']
            xmpp.use_ipv6 = XMPP_GRID['use_ipv6']

            # Connect to the XMPP server and start processing XMPP stanzas.
            if xmpp.connect():
                xmpp.process()
                xmpp_connected = True
            else:
                self.stdout.write('Unable to connect to XMPP-Grid server.')

        encoding = self.stdout.encoding or 'ascii'
        with open(filename, 'r') as f:
            for line in f:
                tag_xml = line.strip().decode('utf8')
                tag, replaced = utils.process_swid_tag(tag_xml,
                                                       allow_tag_update=True)
                if replaced:
                    self.stdout.write('Replaced {0}'.format(tag).encode(
                        encoding, 'replace'))
                else:
                    self.stdout.write('Added {0}'.format(tag).encode(
                        encoding, 'replace'))
                if xmpp_connected:
                    xmpp.publish(XMPP_GRID['node_swidtags'], tag.software_id,
                                 tag.json())
        if xmpp_connected:
            xmpp.disconnect()
Exemple #19
0
def test_change_duplicate_regid_entity_name(swidtag, filename):
    """
    Changing the name of an entity (with a role other than tagCreator) will create
    a new entity, since an entity is uniquely identified by its name and regid.

    """

    assert Entity.objects.count() == 1
    assert swidtag.entity_set.count() == 2
    new_xml = swidtag.swid_xml.replace('name="strongSwan"', 'name="strongswan123"')
    tag, replaced = utils.process_swid_tag(new_xml, allow_tag_update=True)
    assert tag.entity_set.count() == 2
    assert Entity.objects.count() == 1
    assert Tag.objects.count() == 1
    assert replaced is True

    # new entities should be wired up correctly
    test_entities = ['HSR', 'HSR']
    real_entities = tag.entity_set.values_list('name', flat=True)
    assert sorted(test_entities) == sorted(real_entities)
Exemple #20
0
def test_change_duplicate_regid_entity_name(swidtag, filename):
    """
    Changing the name of an entity (with a role other than tagcreator) will create a new entity, since an
    entity is uniquely identified by its name and regid.

    """

    assert Entity.objects.count() == 1
    assert swidtag.entity_set.count() == 2
    new_xml = swidtag.swid_xml.replace('name="strongSwan"',
                                       'name="strongswan123"')
    tag, replaced = utils.process_swid_tag(new_xml, allow_tag_update=True)
    assert tag.entity_set.count() == 2
    assert Entity.objects.count() == 1
    assert Tag.objects.count() == 1
    assert replaced is True

    # new entities should be wired up correctly
    test_entities = ['HSR', 'HSR']
    real_entities = tag.entity_set.values_list('name', flat=True)
    assert sorted(test_entities) == sorted(real_entities)
Exemple #21
0
    def handle(self, *args, **kwargs):
        if len(args) != 1:
            raise CommandError('Usage: ./manage.py importswid <filename>')

        filename = args[0]

        if not os.path.isfile(filename):
            raise CommandError('No such file: ' + filename)

        # Publish SWID tags on XMPP-Grid?
        xmpp_connected = False
        if USE_XMPP:
            # Initialize XMPP client
            xmpp = XmppGridClient(XMPP_GRID['jid'], XMPP_GRID['password'],
                                  XMPP_GRID['pubsub_server'])
            xmpp.ca_certs = XMPP_GRID['cacert']
            xmpp.certfile = XMPP_GRID['certfile']
            xmpp.keyfile = XMPP_GRID['keyfile']
            xmpp.use_ipv6 = XMPP_GRID['use_ipv6']

            # Connect to the XMPP server and start processing XMPP stanzas.
            if xmpp.connect():
                xmpp.process()
                xmpp_connected = True
            else:
                self.stdout.write('Unable to connect to XMPP-Grid server.')

        encoding = self.stdout.encoding or 'ascii'
        with open(filename, 'r') as f:
            for line in f:
                tag_xml = line.strip().decode('utf8')
                tag, replaced = utils.process_swid_tag(tag_xml, allow_tag_update=True)
                if replaced:
                    self.stdout.write('Replaced {0}'.format(tag).encode(encoding, 'replace'))
                else:
                    self.stdout.write('Added {0}'.format(tag).encode(encoding, 'replace'))
                if xmpp_connected:
                    xmpp.publish(XMPP_GRID['node_swidtags'], tag.software_id, tag.json())
        if xmpp_connected:
            xmpp.disconnect()
Exemple #22
0
# TRUNCATE SWID TABLES

Tag.files.through.objects.all().delete()
Tag.sessions.through.objects.all().delete()
Tag.objects.all().delete()

EntityRole.objects.all().delete()
Entity.objects.all().delete()

# IMPORT SWID TAGS

filename = "tests/test_tags/ubuntu_full_swid.txt"
with open(filename, 'r') as f:
    for line in f:
        tag_xml = line.strip().decode('utf8')
        tag, replaced = utils.process_swid_tag(tag_xml)
        if replaced:
            print('Replaced {0}'.format(tag))
        else:
            print('Added {0}'.format(tag))

# WIRE UP SOME SESSIONS WITH TAGS

nexus_prime = Device.objects.get(pk=2)
lenovo_twist = Device.objects.get(pk=1)
sessions_prime = nexus_prime.sessions.order_by('-time')[:5]
sessions_twist = lenovo_twist.sessions.order_by('-time')[:5]

tag_sets = [
    Tag.objects.filter(id__in=range(100)),
    Tag.objects.filter(id__in=range(45)),
Exemple #23
0
def test_changed_software_entity_name(swidtag, filename):
    new_xml = swidtag.swid_xml.replace('SoftwareIdentity name="strongswan"',
                                       'SoftwareIdentity name="strongswan123"')
    tag, replaced = utils.process_swid_tag(new_xml, allow_tag_update=True)
    assert replaced is True
    assert Tag.objects.count() == 1
Tag.files.through.objects.all().delete()
Tag.sessions.through.objects.all().delete()
Tag.objects.all().delete()

EntityRole.objects.all().delete()
Entity.objects.all().delete()


# IMPORT SWID TAGS

filename = "tests/test_tags/ubuntu_full_swid.txt"
with open(filename, 'r') as f:
    for line in f:
        tag_xml = line.strip().decode('utf8')
        tag, replaced = utils.process_swid_tag(tag_xml)
        if replaced:
            print('Replaced {0}'.format(tag))
        else:
            print('Added {0}'.format(tag))


# WIRE UP SOME SESSIONS WITH TAGS

nexus_prime = Device.objects.get(pk=2)
lenovo_twist = Device.objects.get(pk=1)
sessions_prime = nexus_prime.sessions.order_by('-time')[:5]
sessions_twist = lenovo_twist.sessions.order_by('-time')[:5]

tag_sets = [
    Tag.objects.filter(id__in=range(100)),