From 7ea44658ebbc28b44d53372502137ecd60caa975 Mon Sep 17 00:00:00 2001 From: Thomas Hipp Date: Tue, 4 Jul 2017 10:16:07 +0200 Subject: [PATCH] Add support for device RNG Add support for device RNG (random number generator), and add test code. Signed-off-by: Thomas Hipp --- domain.go | 34 ++++++++++++++++++++ domain_test.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/domain.go b/domain.go index f9e3a80..b9b0f77 100644 --- a/domain.go +++ b/domain.go @@ -184,6 +184,8 @@ type DomainInterfaceSource struct { Path string `xml:"path,attr,omitempty"` Mode string `xml:"mode,attr,omitempty"` Port uint `xml:"port,attr,omitempty"` + Service string `xml:"service,attr,omitempty"` + Host string `xml:"host,attr,omitempty"` } type DomainInterfaceTarget struct { @@ -384,6 +386,25 @@ type DomainSound struct { Address *DomainAddress `xml:"address"` } +type DomainRNGRate struct { + Bytes uint `xml:"bytes,attr"` + Period uint `xml:"period,attr,omitempty"` +} + +type DomainRNGBackend struct { + Device string `xml:",chardata"` + Model string `xml:"model,attr"` + Type string `xml:"type,attr,omitempty"` + Sources []DomainInterfaceSource `xml:"source"` +} + +type DomainRNG struct { + XMLName xml.Name `xml:"rng"` + Model string `xml:"model,attr"` + Rate *DomainRNGRate `xml:"rate"` + Backend *DomainRNGBackend `xml:"backend"` +} + type DomainDeviceList struct { Emulator string `xml:"emulator,omitempty"` Controllers []DomainController `xml:"controller"` @@ -398,6 +419,7 @@ type DomainDeviceList struct { Channels []DomainChannel `xml:"channel"` MemBalloon *DomainMemBalloon `xml:"memballoon"` Sounds []DomainSound `xml:"sound"` + RNGs []DomainRNG `xml:"rng"` } type DomainMemory struct { @@ -734,3 +756,15 @@ func (d *DomainSound) Marshal() (string, error) { } return string(doc), nil } + +func (d *DomainRNG) Unmarshal(doc string) error { + return xml.Unmarshal([]byte(doc), d) +} + +func (d *DomainRNG) Marshal() (string, error) { + doc, err := xml.MarshalIndent(d, "", " ") + if err != nil { + return "", err + } + return string(doc), nil +} diff --git a/domain_test.go b/domain_test.go index 0ce908b..47e5e26 100644 --- a/domain_test.go +++ b/domain_test.go @@ -333,6 +333,30 @@ var domainTestData = []struct { }, }, }, + RNGs: []DomainRNG{ + DomainRNG{ + Model: "virtio", + Rate: &DomainRNGRate{ + Period: 2000, + Bytes: 1234, + }, + Backend: &DomainRNGBackend{ + Model: "egd", + Type: "udp", + Sources: []DomainInterfaceSource{ + DomainInterfaceSource{ + Mode: "bind", + Service: "1234", + }, + DomainInterfaceSource{ + Mode: "connect", + Host: "1.2.3.4", + Service: "1234", + }, + }, + }, + }, + }, }, }, Expected: []string{ @@ -368,6 +392,13 @@ var domainTestData = []struct { ` `, `
`, ` `, + ` `, + ` `, + ` `, + ` `, + ` `, + ` `, + ` `, ` `, ``, }, @@ -1334,6 +1365,60 @@ var domainTestData = []struct { ``, }, }, + { + Object: &DomainRNG{ + Model: "virtio", + Rate: &DomainRNGRate{ + Period: 2000, + Bytes: 1234, + }, + Backend: &DomainRNGBackend{ + Device: "/dev/random", + Model: "random", + }, + }, + + Expected: []string{ + ``, + ` `, + ` /dev/random`, + ``, + }, + }, + { + Object: &DomainRNG{ + Model: "virtio", + Rate: &DomainRNGRate{ + Period: 2000, + Bytes: 1234, + }, + Backend: &DomainRNGBackend{ + Model: "egd", + Type: "udp", + Sources: []DomainInterfaceSource{ + DomainInterfaceSource{ + Mode: "bind", + Service: "1234", + }, + DomainInterfaceSource{ + Mode: "connect", + Host: "1.2.3.4", + Service: "1234", + }, + }, + }, + }, + + Expected: []string{ + ``, + ` `, + ` `, + ` `, + ` `, + ` `, + ``, + }, + }, } func TestDomain(t *testing.T) {