Skip to content
This repository has been archived by the owner on Jan 7, 2022. It is now read-only.

Commit

Permalink
Add support for device RNG
Browse files Browse the repository at this point in the history
Add support for device RNG (random number generator), and add test code.

Signed-off-by: Thomas Hipp <thipp@suse.de>
  • Loading branch information
Thomas Hipp authored and berrange committed Jul 5, 2017
1 parent 1688e3b commit 7ea4465
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
34 changes: 34 additions & 0 deletions domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"`
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
85 changes: 85 additions & 0 deletions domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -368,6 +392,13 @@ var domainTestData = []struct {
` <codec type="duplex"></codec>`,
` <address type="pci" domain="0" bus="0" slot="8" function="0"></address>`,
` </sound>`,
` <rng model="virtio">`,
` <rate bytes="1234" period="2000"></rate>`,
` <backend model="egd" type="udp">`,
` <source mode="bind" service="1234"></source>`,
` <source mode="connect" service="1234" host="1.2.3.4"></source>`,
` </backend>`,
` </rng>`,
` </devices>`,
`</domain>`,
},
Expand Down Expand Up @@ -1334,6 +1365,60 @@ var domainTestData = []struct {
`</sound>`,
},
},
{
Object: &DomainRNG{
Model: "virtio",
Rate: &DomainRNGRate{
Period: 2000,
Bytes: 1234,
},
Backend: &DomainRNGBackend{
Device: "/dev/random",
Model: "random",
},
},

Expected: []string{
`<rng model="virtio">`,
` <rate bytes="1234" period="2000"></rate>`,
` <backend model="random">/dev/random</backend>`,
`</rng>`,
},
},
{
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{
`<rng model="virtio">`,
` <rate bytes="1234" period="2000"></rate>`,
` <backend model="egd" type="udp">`,
` <source mode="bind" service="1234"></source>`,
` <source mode="connect" service="1234" host="1.2.3.4"></source>`,
` </backend>`,
`</rng>`,
},
},
}

func TestDomain(t *testing.T) {
Expand Down

0 comments on commit 7ea4465

Please sign in to comment.