Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(amt): add execute support for userinitiated #105

Merged
merged 8 commits into from
Oct 30, 2023
106 changes: 92 additions & 14 deletions pkg/amt/userinitiatedconnection/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,59 @@
package userinitiatedconnection

import (
"encoding/json"
"encoding/xml"

"github.com/open-amt-cloud-toolkit/go-wsman-messages/internal/message"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/amt/actions"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/amt/redirection"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/common"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsman"
)

type RequestedState int

const AMT_UserInitiatedConnectionService = "AMT_UserInitiatedConnectionService"

type (
Response struct {
*wsman.Message
XMLName xml.Name `xml:"Envelope"`
Header message.Header `xml:"Header"`
Body Body `xml:"Body"`
}
Body struct {
XMLName xml.Name `xml:"Body"`
User User `xml:"AMT_UserInitiatedConnectionService"`

EnumerateResponse common.EnumerateResponse
}
User struct {
CreationClassName string
ElementName string
EnabledState int
Name string
SystemCreationClassName string
SystemName string
}
)

const (
AllInterfacesDisabled RequestedState = 32768
BIOSInterfaceEnabled RequestedState = 32769
OSInterfaceEnabled RequestedState = 32770
BIOSandOSInterfacesEnabled RequestedState = 32771
)

func (w *Response) JSON() string {
jsonOutput, err := json.Marshal(w.Body)
if err != nil {
return ""
}
return string(jsonOutput)
}

type Service struct {
base message.Base
base message.Base
client wsman.WSManClient
}

func NewUserInitiatedConnectionService(wsmanMessageCreator *message.WSManMessageCreator) Service {
Expand All @@ -32,23 +67,66 @@ func NewUserInitiatedConnectionService(wsmanMessageCreator *message.WSManMessage
}
}

func NewUserInitiatedConnectionServiceWithClient(wsmanMessageCreator *message.WSManMessageCreator, client wsman.WSManClient) Service {
return Service{
base: message.NewBaseWithClient(wsmanMessageCreator, AMT_UserInitiatedConnectionService, client),
client: client,
}
}

// Get retrieves the representation of the instance
func (UserInitiatedConnectionService Service) Get() string {
return UserInitiatedConnectionService.base.Get(nil)
func (UserInitiatedConnectionService Service) Get() (response Response, err error) {

response = Response{
Message: &wsman.Message{
XMLInput: UserInitiatedConnectionService.base.Get(nil),
},
}

// send the message to AMT
err = UserInitiatedConnectionService.base.Execute(response.Message)
if err != nil {
return
}

// put the xml response into the go struct
err = xml.Unmarshal([]byte(response.XMLOutput), &response)
if err != nil {
return
}

return
}

// Enumerates the instances of this class
func (UserInitiatedConnectionService Service) Enumerate() string {
return UserInitiatedConnectionService.base.Enumerate()
func (UserInitiatedConnectionService Service) Enumerate() (response Response, err error) {
response = Response{
Message: &wsman.Message{
XMLInput: UserInitiatedConnectionService.base.Enumerate(),
},
}
// send the message to AMT
err = UserInitiatedConnectionService.base.Execute(response.Message)
if err != nil {
return
}

// put the xml response into the go struct
err = xml.Unmarshal([]byte(response.XMLOutput), &response)
if err != nil {
return
}

return
}

// Pulls instances of this class, following an Enumerate operation
func (UserInitiatedConnectionService Service) Pull(enumerationContext string) string {
return UserInitiatedConnectionService.base.Pull(enumerationContext)
}
// func (UserInitiatedConnectionService Service) Pull(enumerationContext string) string {
// return UserInitiatedConnectionService.base.Pull(enumerationContext)
// }

// RequestStateChange requests that the state of the element be changed to the value specified in the RequestedState parameter . . .
func (UserInitiatedConnectionService Service) RequestStateChange(requestedState RequestedState) string {
return UserInitiatedConnectionService.base.RequestStateChange(actions.RequestStateChange(redirection.AMT_RedirectionService), int(requestedState))
// // RequestStateChange requests that the state of the element be changed to the value specified in the RequestedState parameter . . .
// func (UserInitiatedConnectionService Service) RequestStateChange(requestedState RequestedState) string {
// return UserInitiatedConnectionService.base.RequestStateChange(actions.RequestStateChange(redirection.AMT_RedirectionService), int(requestedState))

}
// }
103 changes: 88 additions & 15 deletions pkg/amt/userinitiatedconnection/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,117 @@
package userinitiatedconnection

import (
"encoding/xml"
"fmt"
"io"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/open-amt-cloud-toolkit/go-wsman-messages/internal/message"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/common"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsmantesting"
)

type MockClient struct {
}

const (
EnvelopeResponse = `<a:Envelope xmlns:a="http://www.w3.org/2003/05/soap-envelope" x-mlns:b="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:c="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:d="http://schemas.xmlsoap.org/ws/2005/02/trust" xmlns:e="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:f="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd" xmlns:g="http://intel.com/wbem/wscim/1/amt-schema/1/AMT_RedirectionService" xmlns:h="http://schemas.dmtf.org/wbem/wscim/1/common" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><a:Header><b:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</b:To><b:RelatesTo>0</b:RelatesTo><b:Action a:mustUnderstand="true">`
GetBody = `<g:AMT_RedirectionService><g:CreationClassName>AMT_RedirectionService</g:CreationClassName><g:ElementName>Intel(r) AMT Redirection Service</g:ElementName><g:Name>Intel(r) AMT Redirection Service</g:Name><g:SystemCreationClassName>CIM_ComputerSystem</g:SystemCreationClassName><g:SystemName>ManagedSystem</g:SystemName></g:AMT_RedirectionService`
)

var currentMessage = ""

func (c *MockClient) Post(msg string) ([]byte, error) {
// read an xml file from disk:
xmlFile, err := os.Open("../../wsmantesting/responses/amt/userinitiatedconnection/" + strings.ToLower(currentMessage) + ".xml")
if err != nil {
fmt.Println("Error opening file:", err)
return nil, err
}
defer xmlFile.Close()
// read file into string
xmlData, err := io.ReadAll(xmlFile)
if err != nil {
fmt.Println("Error reading file:", err)
return nil, err
}
// strip carriage returns and new line characters
xmlData = []byte(strings.ReplaceAll(string(xmlData), "\r\n", ""))

// Simulate a successful response for testing.
return []byte(xmlData), nil
}
func TestAMT_UserInitiatedConnectionService(t *testing.T) {
messageID := 0
resourceUriBase := "http://intel.com/wbem/wscim/1/amt-schema/1/"
wsmanMessageCreator := message.NewWSManMessageCreator(resourceUriBase)
elementUnderTest := NewUserInitiatedConnectionService(wsmanMessageCreator)

client := MockClient{}
elementUnderTest := NewUserInitiatedConnectionServiceWithClient(wsmanMessageCreator, &client)
t.Run("amt_* Tests", func(t *testing.T) {
tests := []struct {
name string
method string
action string
body string
responseFunc func() string
name string
method string
action string
body string
responseFunc func() (Response, error)
expectedResponse interface{}
}{
//GETS
{"should create a valid AMT_UserInitiatedConnectionService Get wsman message", "AMT_UserInitiatedConnectionService", wsmantesting.GET, "", elementUnderTest.Get},
{
"should create a valid AMT_UserInitiatedConnectionService Get wsman message",
"AMT_UserInitiatedConnectionService",
wsmantesting.GET,
"",
func() (Response, error) {
currentMessage = "Get"
currentMessage = "Get"
return elementUnderTest.Get()
},
Body{
XMLName: xml.Name{Space: "http://www.w3.org/2003/05/soap-envelope", Local: "Body"},
User: User{
CreationClassName: "AMT_UserInitiatedConnectionService",
ElementName: "Intel(r) AMT User Initiated Connection Service",
EnabledState: 32771,
Name: "Intel(r) AMT User Initiated Connection Service",
SystemCreationClassName: "CIM_ComputerSystem",
SystemName: "Intel(r) AMT",
},
},
},
//ENUMERATES
{"should create a valid AMT_UserInitiatedConnectionService Enumerate wsman message", "AMT_UserInitiatedConnectionService", wsmantesting.ENUMERATE, wsmantesting.ENUMERATE_BODY, elementUnderTest.Enumerate},
{
"should create a valid AMT_UserInitiatedConnectionService Enumerate wsman message",
"AMT_UserInitiatedConnectionService",
wsmantesting.ENUMERATE,
wsmantesting.ENUMERATE_BODY,
func() (Response, error) {
currentMessage = "Enumerate"
return elementUnderTest.Enumerate()
},
Body{
XMLName: xml.Name{Space: "http://www.w3.org/2003/05/soap-envelope", Local: "Body"},
EnumerateResponse: common.EnumerateResponse{
EnumerationContext: "D3000000-0000-0000-0000-000000000000",
},
},
},
//PULLS
{"should create a valid AMT_UserInitiatedConnectionService Pull wsman message", "AMT_UserInitiatedConnectionService", wsmantesting.PULL, wsmantesting.PULL_BODY, func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
//{"should create a valid AMT_UserInitiatedConnectionService Pull wsman message", "AMT_UserInitiatedConnectionService", wsmantesting.PULL, wsmantesting.PULL_BODY, func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
correctResponse := wsmantesting.ExpectedResponse(messageID, resourceUriBase, test.method, test.action, "", test.body)
expectedXMLInput := wsmantesting.ExpectedResponse(messageID, resourceUriBase, test.method, test.action, "", test.body)
messageID++
response := test.responseFunc()
if response != correctResponse {
assert.Equal(t, correctResponse, response)
}
response, err := test.responseFunc()
assert.NoError(t, err)
assert.Equal(t, expectedXMLInput, response.XMLInput)
assert.Equal(t, test.expectedResponse, response.Body)
})
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<a:Envelope xmlns:a="http://www.w3.org/2003/05/soap-envelope"
xmlns:b="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:c="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
xmlns:d="http://schemas.xmlsoap.org/ws/2005/02/trust"
xmlns:e="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:f="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd"
xmlns:g="http://schemas.xmlsoap.org/ws/2004/09/enumeration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<a:Header>
<b:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</b:To>
<b:RelatesTo>0</b:RelatesTo>
<b:Action a:mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/09/enumeration/EnumerateResponse</b:Action>
<b:MessageID>uuid:00000000-8086-8086-8086-000000000322</b:MessageID>
<c:ResourceURI>http://intel.com/wbem/wscim/1/amt-schema/1/AMT_UserInitiatedConnectionService</c:ResourceURI>
</a:Header>
<a:Body>
<g:EnumerateResponse>
<g:EnumerationContext>D3000000-0000-0000-0000-000000000000</g:EnumerationContext>
</g:EnumerateResponse>
</a:Body>
</a:Envelope>
28 changes: 28 additions & 0 deletions pkg/wsmantesting/responses/amt/userinitiatedconnection/get.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<a:Envelope xmlns:a="http://www.w3.org/2003/05/soap-envelope"
xmlns:b="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:c="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
xmlns:d="http://schemas.xmlsoap.org/ws/2005/02/trust"
xmlns:e="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:f="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd"
xmlns:g="http://intel.com/wbem/wscim/1/amt-schema/1/AMT_UserInitiatedConnectionService"
xmlns:h="http://schemas.dmtf.org/wbem/wscim/1/common"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<a:Header>
<b:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</b:To>
<b:RelatesTo>0</b:RelatesTo>
<b:Action a:mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse</b:Action>
<b:MessageID>uuid:00000000-8086-8086-8086-00000000034F</b:MessageID>
<c:ResourceURI>http://intel.com/wbem/wscim/1/amt-schema/1/AMT_UserInitiatedConnectionService</c:ResourceURI>
</a:Header>
<a:Body>
<g:AMT_UserInitiatedConnectionService>
<g:CreationClassName>AMT_UserInitiatedConnectionService</g:CreationClassName>
<g:ElementName>Intel(r) AMT User Initiated Connection Service</g:ElementName>
<g:EnabledState>32771</g:EnabledState>
<g:Name>Intel(r) AMT User Initiated Connection Service</g:Name>
<g:SystemCreationClassName>CIM_ComputerSystem</g:SystemCreationClassName>
<g:SystemName>Intel(r) AMT</g:SystemName>
</g:AMT_UserInitiatedConnectionService>
</a:Body>
</a:Envelope>
Loading