Exemplo n.º 1
0
 def assert_specifications(
         self, filenames: List[str],
         specifications: Dict[str, Specification]) -> None:
     parser = Parser()
     for filename in filenames:
         parser.parse(filename)
     self.assertEqual(parser.specifications(), specifications, filenames)
Exemplo n.º 2
0
 def test_tls(self) -> None:
     parser = Parser()
     for f in [
             "tls_alert.rflx", "tls_handshake.rflx", "tls_heartbeat.rflx",
             "tls_record.rflx"
     ]:
         parser.parse(f"{self.specdir}/{f}")
Exemplo n.º 3
0
def parse(files: List) -> Tuple[List, List]:
    parser = Parser()

    for f in files:
        if not Path(f).is_file():
            raise Error(f'file not found: "{f}"')

        print(f"Parsing {f}... ", end="", flush=True)
        parser.parse(f)
        print("OK")

    return (parser.messages, parser.refinements)
Exemplo n.º 4
0
 def test_valid_first() -> None:
     parser = Parser()
     parser.parse_string("""
         package Foo is
             type Element is mod 2**8;
             type Bar is
                 message
                     F1 : Element then F2 with First => F1'First;
                     F2 : Element;
                 end message;
         end Foo;
         """)
Exemplo n.º 5
0
 def test_valid_length_reference() -> None:
     parser = Parser()
     parser.parse_string("""
         package Foo is
             type Element is mod 2**8;
             type Bar is
                 message
                     F1 : Element then F2 with Length => F1;
                     F2 : Payload;
                 end message;
         end Foo;
         """)
Exemplo n.º 6
0
 def test_valid_use_message_first_last() -> None:
     parser = Parser()
     parser.parse_string("""
         package Foo is
            type Finished is
               message
                  null
                     then Verify_Data
                        with Length => Message'Last - Message'First + 1;
                  Verify_Data : Payload;
               end message;
         end Foo;
         """)
Exemplo n.º 7
0
 def test_valid_length_from_message_last() -> None:
     parser = Parser()
     parser.parse_string("""
         package Foo is
             type Element is mod 2**8;
             type Bar is
                 message
                     F1 : Element;
                     F2 : Element
                         then null
                             with Length => Message'Last - F1'Last;
                 end message;
         end Foo;
         """)
Exemplo n.º 8
0
    def assert_integration(self, basenames: List[str]) -> None:
        parser = Parser()
        for basename in basenames:
            parser.parse(f"{self.specdir}/{basename}.rflx")

        generator = Generator("RFLX.", reproducible=True)
        generator.generate(parser.messages, parser.refinements)

        for unit in generator.units.values():
            with open(f"{self.testdir}/{unit.name}.ads", "r") as f:
                self.assertEqual(unit.specification, f.read())
            if unit.body:
                with open(f"{self.testdir}/{unit.name}.adb", "r") as f:
                    self.assertEqual(unit.body, f.read())
Exemplo n.º 9
0
    def assert_dissector(self, basenames: List[str]) -> None:
        parser = Parser()
        for basename in basenames:
            parser.parse(f'{self.specdir}/{basename}.rflx')

        generator = Generator()
        generator.generate_dissector(parser.pdus, parser.refinements)

        for unit in generator.units():
            basename = unit.package.name.lower().replace('.', '-')
            with open(f'{self.testdir}/{basename}.ads', 'r') as f:
                self.assertEqual(unit.specification(), f.read())
            if unit.definition().strip():
                with open(f'{self.testdir}/{basename}.adb', 'r') as f:
                    self.assertEqual(unit.definition(), f.read())
Exemplo n.º 10
0
    def __init__(self, files: List[str]) -> None:
        parser = Parser()
        self.__packages: Dict[str, Package] = {}

        for f in files:
            if not Path(f).is_file():
                raise FileNotFoundError(f'file not found: "{f}"')
            parser.parse(Path(f))
        model = parser.create_model()
        packages = set(str(m.package) for m in model.messages)
        for p in packages:
            self.__packages[p] = Package(p)
            for m in [x for x in model.messages if str(x.package) == p]:
                self.__packages[p][str(m.name)] = MessageValue(
                    m, model.refinements)
Exemplo n.º 11
0
 def test_message_with_two_length_fields() -> None:
     parser = Parser()
     parser.parse_string("""
            package Test is
               type Length is mod 2**8;
               type Packet is
                  message
                     Length_1 : Length;
                     Length_2 : Length
                        then Payload
                           with Length => Length_1 + Length_2;
                     Payload : Payload;
                  end message;
            end Test;
         """)
Exemplo n.º 12
0
 def test_exclusive_with_length_valid() -> None:
     parser = Parser()
     parser.parse_string("""
         package Foo is
             type Element is range 0..2**32-1 with Size => 32;
             type Bar is
                 message
                     F1 : Element
                         then null
                             if F1'Length = 32 and F1 < 50,
                         then F2
                             if F1'Length = 32 and F1 > 100;
                     F2 : Element;
                 end message;
         end Foo;
         """)
Exemplo n.º 13
0
 def test_exclusive_enum_valid() -> None:
     parser = Parser()
     parser.parse_string("""
         package Foo is
             type Kind is (Val1 => 1, Val2 => 2) with Size => 8;
             type Element is range 0..2**32-1 with Size => 32;
             type Bar is
                 message
                     F1 : Kind
                         then null
                             if F1 = Val1,
                         then F2
                             if F1 = Val2;
                     F2 : Element;
                 end message;
         end Foo;
         """)
Exemplo n.º 14
0
 def test_tlv_valid_enum() -> None:
     parser = Parser()
     parser.parse_string("""
         package Foo is
             type Tag is (T1, T2, T3) with Size => 8;
             type Length is range 0 .. 2**14 with Size => 16;
             type Bar is
                 message
                     L : Length;
                     T : Tag
                         then V
                             with Length => L
                             if T /= T2 and L <= 2**13;
                     V : Payload;
                 end message;
         end Foo;
         """)
Exemplo n.º 15
0
def main(argv: Sequence[str]) -> int:
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument("directory",
                            metavar="DIRECTORY",
                            help="output directory",
                            type=pathlib.Path)
    args = arg_parser.parse_args(argv[1:])

    parser = Parser()
    for f in SPECIFICATION_FILES:
        parser.parse(pathlib.Path(f))

    generator = Generator("RFLX", reproducible=True)
    generator.generate(parser.create_model())
    for model in MODELS:
        generator.generate(model)
    generator.write_units(args.directory)
    generator.write_library_files(args.directory)
    generator.write_top_level_package(args.directory)

    return 0
Exemplo n.º 16
0
def parse(files: List) -> Model:
    parser = Parser()

    error = RecordFluxError()
    for f in files:
        if not Path(f).is_file():
            error.append(f'file not found: "{f}"', Subsystem.CLI,
                         Severity.ERROR)
            continue
        try:
            parser.parse(Path(f))
        except RecordFluxError as e:
            error.extend(e)

    try:
        model = parser.create_model()
    except RecordFluxError as e:
        error.extend(e)

    error.propagate()
    return model
Exemplo n.º 17
0
 def assert_parse_exception_string(self, string: str, regex: str) -> None:
     with self.assertRaisesRegex(ModelError, regex):
         Parser().parse_string(string)
Exemplo n.º 18
0
 def assert_messages_string(self, string: str,
                            messages: List[Message]) -> None:
     parser = Parser()
     parser.parse_string(string)
     self.assert_messages(parser.messages, messages)
Exemplo n.º 19
0
 def assert_parser_error_string(self, string: str, regex: str) -> None:
     with self.assertRaisesRegex(ParserError, regex):
         Parser().parse_string(string)
Exemplo n.º 20
0
 def assert_parse_exception_string(self, string: str, regex: str) -> None:
     with self.assertRaisesRegex(ParseFatalException, regex):
         Parser().parse_string(string)
Exemplo n.º 21
0
 def assert_parser_error(self, filenames: List[str], regex: str) -> None:
     with self.assertRaisesRegex(ParserError, regex):
         parser = Parser()
         for filename in filenames:
             parser.parse(filename)
Exemplo n.º 22
0
 def assert_pdus(self, filenames: List[str], pdus: List[PDU]) -> None:
     parser = Parser()
     for filename in filenames:
         parser.parse(filename)
     self.assertEqual(parser.pdus, pdus, filenames)
Exemplo n.º 23
0
 def assert_specifications_string(
         self, string: str, specifications: Dict[str,
                                                 Specification]) -> None:
     parser = Parser()
     parser.parse_string(string)
     self.assertEqual(parser.specifications(), specifications)
Exemplo n.º 24
0
 def assert_messages_files(self, filenames: List[str],
                           messages: List[Message]) -> None:
     parser = Parser()
     for filename in filenames:
         parser.parse(filename)
     self.assert_messages(parser.messages, messages)
Exemplo n.º 25
0
 def assert_refinements_string(self, string: str,
                               refinements: List[Refinement]) -> None:
     parser = Parser()
     parser.parse_string(string)
     self.assertEqual(parser.refinements, refinements)