mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-11-09 23:10:08 +00:00
ca3f025011
This adds a parser capable of handling the XDR protocol files. The parsing grammar requirements are detailed in https://www.rfc-editor.org/rfc/rfc4506#section-6.3 Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
92 lines
2.4 KiB
Python
92 lines
2.4 KiB
Python
# SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
from pathlib import Path
|
|
|
|
from rpcgen.ast import (
|
|
XDRSpecification,
|
|
XDRDefinitionConstant,
|
|
XDRDefinitionEnum,
|
|
XDRDefinitionUnion,
|
|
XDRDefinitionStruct,
|
|
XDRDeclarationScalar,
|
|
XDRDeclarationVariableArray,
|
|
XDREnumValue,
|
|
XDREnumBody,
|
|
XDRStructBody,
|
|
XDRUnionCase,
|
|
XDRUnionBody,
|
|
XDRTypeCustom,
|
|
XDRTypeVoid,
|
|
XDRTypeString,
|
|
XDRTypeOpaque,
|
|
)
|
|
from rpcgen.parser import XDRParser
|
|
|
|
|
|
def test_parser():
|
|
p = Path(Path(__file__).parent, "simple.x")
|
|
with p.open("r") as fp:
|
|
parser = XDRParser(fp)
|
|
|
|
got = parser.parse()
|
|
|
|
enum = XDRDefinitionEnum(
|
|
"filekind",
|
|
XDREnumBody(
|
|
[
|
|
XDREnumValue("TEXT", "0"),
|
|
XDREnumValue("DATA", "1"),
|
|
XDREnumValue("EXEC", "2"),
|
|
],
|
|
),
|
|
)
|
|
|
|
union = XDRDefinitionUnion(
|
|
"filetype",
|
|
XDRUnionBody(
|
|
XDRDeclarationScalar(XDRTypeCustom("filekind", enum), "kind"),
|
|
[
|
|
XDRUnionCase("TEXT", XDRDeclarationScalar(XDRTypeVoid(), None)),
|
|
XDRUnionCase(
|
|
"DATA",
|
|
XDRDeclarationVariableArray(
|
|
XDRTypeString(), "creator", "MAXNAMELEN"
|
|
),
|
|
),
|
|
XDRUnionCase(
|
|
"EXEC",
|
|
XDRDeclarationVariableArray(
|
|
XDRTypeString(), "interpretor", "MAXNAMELEN"
|
|
),
|
|
),
|
|
],
|
|
None,
|
|
),
|
|
)
|
|
|
|
struct = XDRDefinitionStruct(
|
|
"file",
|
|
XDRStructBody(
|
|
[
|
|
XDRDeclarationVariableArray(XDRTypeString(), "filename", "MAXNAMELEN"),
|
|
XDRDeclarationScalar(XDRTypeCustom("filetype", union), "type"),
|
|
XDRDeclarationVariableArray(XDRTypeString(), "owner", "MAXUSERNAME"),
|
|
XDRDeclarationVariableArray(XDRTypeOpaque(), "data", "MAXFILELEN"),
|
|
]
|
|
),
|
|
)
|
|
|
|
want = XDRSpecification()
|
|
want.definitions.extend(
|
|
[
|
|
XDRDefinitionConstant("MAXUSERNAME", "32"),
|
|
XDRDefinitionConstant("MAXFILELEN", "65535"),
|
|
XDRDefinitionConstant("MAXNAMELEN", "255"),
|
|
enum,
|
|
union,
|
|
struct,
|
|
]
|
|
)
|
|
|
|
assert str(got) == str(want)
|