コード例 #1
0
def _add_tests():
    import functools
    import itertools

    ofpps = [ofproto_v1_2_parser, ofproto_v1_3_parser]
    for ofpp in ofpps:
        mod = ofpp.__name__.split('.')[-1]
        method_name = 'test_' + mod + '_ofpmatch_compat'

        def _run(self, name, ofpp):
            print('processing %s ...' % name)
            if six.PY3:
                self._test(self, name, ofpp)
            else:
                self._test(name, ofpp)

        print('adding %s ...' % method_name)
        f = functools.partial(_run, name=method_name, ofpp=ofpp)
        test_lib.add_method(Test_Parser_Compat, method_name, f)
コード例 #2
0
def _add_tests():
    _ofp_vers = {
        'of10': ofproto_v1_0.OFP_VERSION,
        'of12': ofproto_v1_2.OFP_VERSION,
        'of13': ofproto_v1_3.OFP_VERSION,
        'of14': ofproto_v1_4.OFP_VERSION,
        'of15': ofproto_v1_5.OFP_VERSION,
    }

    this_dir = os.path.dirname(sys.modules[__name__].__file__)
    ofctl_rest_json_dir = os.path.join(this_dir, 'ofctl_rest_json/')

    for ofp_ver in _ofp_vers:
        # read a json file
        json_path = os.path.join(ofctl_rest_json_dir, ofp_ver + '.json')
        if os.path.exists(json_path):
            _test_cases = json.load(open(json_path))
        else:
            # print("Skip to load test cases for %s" % ofp_ver)
            continue

        # add test
        for test in _test_cases:
            method = test['method']
            path = test['path']
            body = test.get('body', {})

            name = 'test_ofctl_rest_' + method + '_' + ofp_ver + '_' + path
            # print('adding %s ...' % name)
            f = functools.partial(Test_ofctl_rest._test,
                                  name=name,
                                  dp=DummyDatapath(_ofp_vers[ofp_ver]),
                                  method=test['method'],
                                  path=test['path'],
                                  body=body)
            test_lib.add_method(Test_ofctl_rest, name, f)
コード例 #3
0
def _add_tests():
    import os
    import os.path
    import functools

    this_dir = os.path.dirname(sys.modules[__name__].__file__)
    packet_data_dir = os.path.join(this_dir, '../../packet_data')
    json_dir = os.path.join(this_dir, 'json')
    ofvers = [
        'of10',
        'of12',
        'of13',
        'of14',
        'of15',
    ]
    cases = set()
    for ver in ofvers:
        pdir = packet_data_dir + '/' + ver
        jdir = json_dir + '/' + ver
        n_added = 0
        for file in os.listdir(pdir):
            if file.endswith('.packet'):
                truncated = None
            elif '.truncated' in file:
                # contents of .truncated files aren't relevant
                s1, s2 = file.split('.truncated')
                try:
                    truncated = int(s2)
                except ValueError:
                    continue
                file = s1 + '.packet'
            else:
                continue
            wire_msg = open(pdir + '/' + file, 'rb').read()
            if not truncated:
                json_str = open(jdir + '/' + file + '.json', 'r').read()
            else:
                json_str = open(
                    jdir + '/' + file + '.truncated%d.json' % truncated,
                    'r').read()
                wire_msg = wire_msg[:truncated]
            method_name = ('test_' + file).replace('-', '_').replace('.', '_')
            if truncated:
                method_name += '_truncated%d' % truncated

            def _run(self, name, wire_msg, json_str):
                print('processing %s ...' % name)
                if six.PY3:
                    self._test_msg(self, name, wire_msg, json_str)
                else:
                    self._test_msg(name, wire_msg, json_str)

            print('adding %s ...' % method_name)
            f = functools.partial(_run,
                                  name=method_name,
                                  wire_msg=wire_msg,
                                  json_str=json_str)
            test_lib.add_method(Test_Parser, method_name, f)
            cases.add(method_name)
            n_added += 1
        assert n_added > 0
    assert (cases == set(
        unittest.defaultTestLoader.getTestCaseNames(Test_Parser)))
コード例 #4
0
def _add_tests():
    import functools
    import itertools

    class Field(object):
        @classmethod
        def generate_mask(cls):
            return list(cls.generate())[1]

    class Int1(Field):
        @staticmethod
        def generate():
            yield 0
            yield 0xff

    class Int2(Field):
        @staticmethod
        def generate():
            yield 0
            yield 0x1234
            yield 0xffff

    class Int3(Field):
        @staticmethod
        def generate():
            yield 0
            yield 0x123456
            yield 0xffffff

    class Int4(Field):
        @staticmethod
        def generate():
            yield 0
            yield 0x12345678
            yield 0xffffffff

    class Int8(Field):
        @staticmethod
        def generate():
            yield 0
            yield 0x123456789abcdef0
            yield 0xffffffffffffffff

    class Mac(Field):
        @staticmethod
        def generate():
            yield '00:00:00:00:00:00'
            yield 'f2:0b:a4:7d:f8:ea'
            yield 'ff:ff:ff:ff:ff:ff'

    class IPv4(Field):
        @staticmethod
        def generate():
            yield '0.0.0.0'
            yield '192.0.2.1'
            yield '255.255.255.255'

    class IPv6(Field):
        @staticmethod
        def generate():
            yield '::'
            yield 'fe80::f00b:a4ff:fed0:3f70'
            yield 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'

    class B64(Field):
        @staticmethod
        def generate():
            yield 'aG9nZWhvZ2U='
            yield 'ZnVnYWZ1Z2E='

    ofpps = [
        ofproto_v1_2_parser, ofproto_v1_3_parser, ofproto_v1_4_parser,
        ofproto_v1_5_parser
    ]
    common = [
        # OpenFlow Basic
        ('in_port', Int4),
        ('in_phy_port', Int4),
        ('metadata', Int8),
        ('eth_dst', Mac),
        ('eth_src', Mac),
        ('eth_type', Int2),
        ('vlan_vid', Int2),
        ('vlan_pcp', Int1),
        ('ip_dscp', Int1),
        ('ip_ecn', Int1),
        ('ip_proto', Int1),
        ('ipv4_src', IPv4),
        ('ipv4_dst', IPv4),
        ('tcp_src', Int2),
        ('tcp_dst', Int2),
        ('udp_src', Int2),
        ('udp_dst', Int2),
        ('sctp_src', Int2),
        ('sctp_dst', Int2),
        ('icmpv4_type', Int1),
        ('icmpv4_code', Int1),
        ('arp_op', Int2),
        ('arp_spa', IPv4),
        ('arp_tpa', IPv4),
        ('arp_sha', Mac),
        ('arp_tha', Mac),
        ('ipv6_src', IPv6),
        ('ipv6_dst', IPv6),
        ('ipv6_flabel', Int4),
        ('icmpv6_type', Int1),
        ('icmpv6_code', Int1),
        ('ipv6_nd_target', IPv6),
        ('ipv6_nd_sll', Mac),
        ('ipv6_nd_tll', Mac),
        ('mpls_label', Int4),
        ('mpls_tc', Int1),
        # Old ONF Experimenter --> OpenFlow Basic (OF1.4+)
        ('pbb_uca', Int1),
        # ONF Experimenter --> OpenFlow Basic (OF1.5+)
        ('tcp_flags', Int2),
        ('actset_output', Int4),
        # Nicira Experimenter
        ('eth_dst_nxm', Mac),
        ('eth_src_nxm', Mac),
        ('tunnel_id_nxm', Int8),
        ('tun_ipv4_src', IPv4),
        ('tun_ipv4_dst', IPv4),
        ('pkt_mark', Int4),
        ('conj_id', Int4),
        ('tun_ipv6_src', IPv6),
        ('tun_ipv6_dst', IPv6),
        ('_dp_hash', Int4),
        ('reg0', Int4),
        ('reg1', Int4),
        ('reg2', Int4),
        ('reg3', Int4),
        ('reg4', Int4),
        ('reg5', Int4),
        ('reg6', Int4),
        ('reg7', Int4),
        # Common Experimenter
        ('field_100', B64),
    ]
    L = {}
    L[ofproto_v1_2_parser] = common + [
        # OF1.2 doesn't have OXM_OF_PBB_ISID.
        #    OFPXMC_OPENFLOW_BASIC = 0x8000
        #    OXM_OF_PBB_ISID = 37
        #    (OFPXMC_OPENFLOW_BASIC << 7) + OXM_OF_PBB_ISID == 4194341
        ('field_4194341', B64),
    ]
    L[ofproto_v1_3_parser] = common + [
        # OpenFlow Basic (OF1.3+)
        ('mpls_bos', Int1),
        ('pbb_isid', Int3),
        ('tunnel_id', Int8),
        ('ipv6_exthdr', Int2),
    ]
    L[ofproto_v1_4_parser] = L[ofproto_v1_3_parser]
    L[ofproto_v1_5_parser] = L[ofproto_v1_4_parser] + [
        # OpenFlow Basic (OF1.5+)
        ('packet_type', Int4),
    ]

    def flatten_one(l, i):
        if isinstance(i, tuple):
            return l + flatten(i)
        else:
            return l + [i]

    flatten = lambda l: reduce(flatten_one, l, [])

    for ofpp in ofpps:
        for n in range(1, 3):
            for C in itertools.combinations(L[ofpp], n):
                l = [1]
                keys = []
                clss = []
                for (k, cls) in C:
                    l = itertools.product(l, cls.generate())
                    keys.append(k)
                    clss.append(cls)
                l = [flatten(x)[1:] for x in l]
                for domask in [True, False]:
                    for values in l:
                        if domask:
                            values = [(value, cls.generate_mask())
                                      for (cls, value) in zip(clss, values)]
                        d = dict(zip(keys, values))
                        mod = ofpp.__name__.split('.')[-1]
                        method_name = 'test_' + mod
                        if domask:
                            method_name += '_mask'
                        for k in sorted(dict(d).keys()):
                            method_name += '_' + str(k)
                            method_name += '_' + str(d[k])
                        method_name = method_name.replace(':', '_')
                        method_name = method_name.replace('.', '_')
                        method_name = method_name.replace('(', '_')
                        method_name = method_name.replace(')', '_')
                        method_name = method_name.replace(',', '_')
                        method_name = method_name.replace("'", '_')
                        method_name = method_name.replace(' ', '_')

                        def _run(self, name, ofpp, d, domask):
                            print('processing %s ...' % name)
                            if six.PY3:
                                self._test(self, name, ofpp, d, domask)
                            else:
                                self._test(name, ofpp, d, domask)

                        print('adding %s ...' % method_name)
                        f = functools.partial(_run,
                                              name=method_name,
                                              ofpp=ofpp,
                                              d=d,
                                              domask=domask)
                        test_lib.add_method(Test_Parser_OFPMatch, method_name,
                                            f)