Skip to content

Commit

Permalink
remove ExtensionObjectTypeID service hack
Browse files Browse the repository at this point in the history
  • Loading branch information
deosjr committed Dec 6, 2023
1 parent b5f8646 commit 2c74fde
Show file tree
Hide file tree
Showing 30 changed files with 62 additions and 58 deletions.
19 changes: 18 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ func (c *Client) ActivateSession(ctx context.Context, s *Session) error {
},
ClientSoftwareCertificates: nil,
LocaleIDs: s.cfg.LocaleIDs,
UserIdentityToken: ua.NewExtensionObject(s.cfg.UserIdentityToken),
UserIdentityToken: ua.NewExtensionObject(s.cfg.UserIdentityToken, extensionObjectTypeID(s.cfg.UserIdentityToken)),
UserTokenSignature: s.cfg.UserTokenSignature,
}
return c.SecureChannel().SendRequest(ctx, req, s.resp.AuthenticationToken, func(v interface{}) error {
Expand All @@ -895,6 +895,23 @@ func (c *Client) ActivateSession(ctx context.Context, s *Session) error {
})
}

func extensionObjectTypeID(v interface{}) *ua.ExpandedNodeID {
switch v.(type) {
case *ua.AnonymousIdentityToken:
return ua.NewFourByteExpandedNodeID(0, id.AnonymousIdentityToken_Encoding_DefaultBinary)
case *ua.UserNameIdentityToken:
return ua.NewFourByteExpandedNodeID(0, id.UserNameIdentityToken_Encoding_DefaultBinary)
case *ua.X509IdentityToken:
return ua.NewFourByteExpandedNodeID(0, id.X509IdentityToken_Encoding_DefaultBinary)
case *ua.IssuedIdentityToken:
return ua.NewFourByteExpandedNodeID(0, id.IssuedIdentityToken_Encoding_DefaultBinary)
case *ua.ServerStatusDataType:
return ua.NewFourByteExpandedNodeID(0, id.ServerStatusDataType_Encoding_DefaultBinary)
default:
return ua.NewTwoByteExpandedNodeID(0)
}
}

// CloseSession closes the current session.
//
// See Part 4, 5.6.4
Expand Down
4 changes: 3 additions & 1 deletion ua/activate_session_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package ua

import (
"testing"

"github.com/gopcua/opcua/id"
)

func TestActivateSessionRequest(t *testing.T) {
Expand All @@ -17,7 +19,7 @@ func TestActivateSessionRequest(t *testing.T) {
ClientSignature: &SignatureData{},
ClientSoftwareCertificates: nil,
LocaleIDs: nil,
UserIdentityToken: NewExtensionObject(&AnonymousIdentityToken{PolicyID: "anonymous"}),
UserIdentityToken: NewExtensionObject(&AnonymousIdentityToken{PolicyID: "anonymous"}, NewFourByteExpandedNodeID(0, id.AnonymousIdentityToken_Encoding_DefaultBinary)),
UserTokenSignature: &SignatureData{},
},
Bytes: flatten(
Expand Down
2 changes: 1 addition & 1 deletion ua/cancel_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestCancelRequest(t *testing.T) {
}),
Timestamp: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC),
RequestHandle: 1,
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
RequestHandle: 1,
},
Expand Down
2 changes: 1 addition & 1 deletion ua/cancel_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestCancelResponse(t *testing.T) {
RequestHandle: 1,
ServiceDiagnostics: &DiagnosticInfo{},
StringTable: []string{},
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
CancelCount: 1,
},
Expand Down
2 changes: 1 addition & 1 deletion ua/close_secure_channel_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestCloseSecureChannelRequest(t *testing.T) {
}),
Timestamp: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC),
RequestHandle: 1,
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
},
Bytes: []byte{
Expand Down
2 changes: 1 addition & 1 deletion ua/close_secure_channel_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestCloseSecureChannelResponse(t *testing.T) {
RequestHandle: 1,
ServiceDiagnostics: &DiagnosticInfo{},
StringTable: []string{},
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
},
Bytes: []byte{
Expand Down
2 changes: 1 addition & 1 deletion ua/close_session_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestCloseSessionRequest(t *testing.T) {
}),
Timestamp: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC),
RequestHandle: 1,
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
DeleteSubscriptions: true,
},
Expand Down
2 changes: 1 addition & 1 deletion ua/close_session_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestCloseSessionResponse(t *testing.T) {
RequestHandle: 1,
ServiceDiagnostics: &DiagnosticInfo{},
StringTable: []string{},
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
},
Bytes: []byte{
Expand Down
2 changes: 1 addition & 1 deletion ua/create_session_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestCreateSessionRequest(t *testing.T) {
}),
Timestamp: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC),
RequestHandle: 1,
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
ClientDescription: &ApplicationDescription{
ApplicationURI: "app-uri",
Expand Down
2 changes: 1 addition & 1 deletion ua/create_session_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestCreateSessionResponse(t *testing.T) {
RequestHandle: 1,
ServiceDiagnostics: &DiagnosticInfo{},
StringTable: []string{},
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
SessionID: NewNumericNodeID(0, 1),
AuthenticationToken: NewByteStringNodeID(0, []byte{
Expand Down
2 changes: 1 addition & 1 deletion ua/create_subscription_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestCreateSubscriptionResponse(t *testing.T) {
RequestHandle: 1,
ServiceDiagnostics: &DiagnosticInfo{},
StringTable: []string{},
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
SubscriptionID: 1,
RevisedPublishingInterval: 1000,
Expand Down
23 changes: 2 additions & 21 deletions ua/extension_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"reflect"

"github.com/gopcua/opcua/debug"
"github.com/gopcua/opcua/id"
)

// eotypes contains all known extension objects.
Expand Down Expand Up @@ -66,9 +65,9 @@ type ExtensionObject struct {
Value interface{}
}

func NewExtensionObject(value interface{}) *ExtensionObject {
func NewExtensionObject(value interface{}, typeID *ExpandedNodeID) *ExtensionObject {
e := &ExtensionObject{
TypeID: ExtensionObjectTypeID(value),
TypeID: typeID,
Value: value,
}
e.UpdateMask()
Expand Down Expand Up @@ -146,21 +145,3 @@ func (e *ExtensionObject) UpdateMask() {
e.EncodingMask = ExtensionObjectBinary
}
}

// TODO, I believe this was only used by service hacks?
func ExtensionObjectTypeID(v interface{}) *ExpandedNodeID {
switch v.(type) {
case *AnonymousIdentityToken:
return NewFourByteExpandedNodeID(0, id.AnonymousIdentityToken_Encoding_DefaultBinary)
case *UserNameIdentityToken:
return NewFourByteExpandedNodeID(0, id.UserNameIdentityToken_Encoding_DefaultBinary)
case *X509IdentityToken:
return NewFourByteExpandedNodeID(0, id.X509IdentityToken_Encoding_DefaultBinary)
case *IssuedIdentityToken:
return NewFourByteExpandedNodeID(0, id.IssuedIdentityToken_Encoding_DefaultBinary)
case *ServerStatusDataType:
return NewFourByteExpandedNodeID(0, id.ServerStatusDataType_Encoding_DefaultBinary)
default:
return NewTwoByteExpandedNodeID(0)
}
}
4 changes: 3 additions & 1 deletion ua/extension_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ package ua

import (
"testing"

"github.com/gopcua/opcua/id"
)

func TestExtensionObject(t *testing.T) {
cases := []CodecTestCase{
{
Name: "anonymous-user-identity-token",
Struct: NewExtensionObject(&AnonymousIdentityToken{PolicyID: "anonymous"}),
Struct: NewExtensionObject(&AnonymousIdentityToken{PolicyID: "anonymous"}, NewFourByteExpandedNodeID(0, id.AnonymousIdentityToken_Encoding_DefaultBinary)),
Bytes: []byte{
// TypeID
0x01, 0x00, 0x41, 0x01,
Expand Down
4 changes: 2 additions & 2 deletions ua/find_servers_on_network_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestFindServersOnNetworkRequest(t *testing.T) {
}),
Timestamp: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC),
RequestHandle: 1,
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
StartingRecordID: 1000,
MaxRecordsToReturn: 0,
Expand All @@ -34,7 +34,7 @@ func TestFindServersOnNetworkRequest(t *testing.T) {
// 0xa6, 0x43, 0xf8, 0x77, 0x7b, 0xc6, 0x2f, 0xc8,
// }),
// time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC),
// 1, 0, 0, "", NewExtensionObject(nil),
// 1, 0, 0, "", NewExtensionObject(nil,nil),
// ),
// 1000,
// 0,
Expand Down
4 changes: 2 additions & 2 deletions ua/find_servers_on_network_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestFindServersOnNetworkResponse(t *testing.T) {
RequestHandle: 1,
ServiceDiagnostics: &DiagnosticInfo{},
StringTable: []string{},
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
LastCounterResetTime: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC),
Servers: []*ServerOnNetwork{
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestFindServersOnNetworkResponse(t *testing.T) {
RequestHandle: 1,
ServiceDiagnostics: &DiagnosticInfo{},
StringTable: []string{},
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
LastCounterResetTime: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC),
Servers: []*ServerOnNetwork{
Expand Down
2 changes: 1 addition & 1 deletion ua/find_servers_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestFindServersRequest(t *testing.T) {
}),
Timestamp: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC),
RequestHandle: 1,
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
EndpointURL: "opc.tcp://wow.its.easy:11111/UA/Server",
},
Expand Down
2 changes: 1 addition & 1 deletion ua/find_servers_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestFindServersResponse(t *testing.T) {
RequestHandle: 1,
ServiceDiagnostics: &DiagnosticInfo{},
StringTable: []string{},
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
Servers: []*ApplicationDescription{
{
Expand Down
2 changes: 1 addition & 1 deletion ua/get_endpoints_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestGetEndpointsRequest(t *testing.T) {
}),
Timestamp: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC),
RequestHandle: 1,
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
EndpointURL: "opc.tcp://wow.its.easy:11111/UA/Server",
},
Expand Down
2 changes: 1 addition & 1 deletion ua/get_endpoints_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestGetEndpointsResponse(t *testing.T) {
RequestHandle: 1,
ServiceDiagnostics: &DiagnosticInfo{},
StringTable: []string{},
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
Endpoints: []*EndpointDescription{
{
Expand Down
2 changes: 1 addition & 1 deletion ua/open_secure_channel_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestOpenSecureChannelRequest(t *testing.T) {
}),
Timestamp: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC),
RequestHandle: 1,
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
ClientProtocolVersion: 0,
RequestType: SecurityTokenRequestTypeIssue,
Expand Down
2 changes: 1 addition & 1 deletion ua/open_secure_channel_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestOpenSecureChannelResponse(t *testing.T) {
RequestHandle: 1,
ServiceDiagnostics: &DiagnosticInfo{},
StringTable: []string{},
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
ServerProtocolVersion: 0,
SecurityToken: &ChannelSecurityToken{
Expand Down
2 changes: 1 addition & 1 deletion ua/read_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestReadRequest(t *testing.T) {
}),
Timestamp: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC),
RequestHandle: 1,
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
MaxAge: 0,
TimestampsToReturn: TimestampsToReturnBoth,
Expand Down
2 changes: 1 addition & 1 deletion ua/read_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestReadResponse(t *testing.T) {
RequestHandle: 1,
ServiceDiagnostics: &DiagnosticInfo{},
StringTable: []string{},
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
Results: []*DataValue{
{
Expand Down
2 changes: 1 addition & 1 deletion ua/request_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func NewNullRequestHeader() *RequestHeader {
return &RequestHeader{
AuthenticationToken: NewTwoByteNodeID(0),
Timestamp: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
}
}

Expand Down
2 changes: 1 addition & 1 deletion ua/response_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func NewNullResponseHeader() *ResponseHeader {
return &ResponseHeader{
Timestamp: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
ServiceDiagnostics: &DiagnosticInfo{},
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
}
}

Expand Down
5 changes: 3 additions & 2 deletions ua/variant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/gopcua/opcua/errors"
"github.com/gopcua/opcua/id"

"github.com/pascaldekloe/goe/verify"
)
Expand Down Expand Up @@ -259,7 +260,7 @@ func TestVariant(t *testing.T) {
{
Name: "ExtensionObject",
Struct: MustVariant(NewExtensionObject(
&AnonymousIdentityToken{PolicyID: "anonymous"},
&AnonymousIdentityToken{PolicyID: "anonymous"}, NewFourByteExpandedNodeID(0, id.AnonymousIdentityToken_Encoding_DefaultBinary),
)),
Bytes: []byte{
// variant encoding mask
Expand Down Expand Up @@ -291,7 +292,7 @@ func TestVariant(t *testing.T) {
},
SecondsTillShutdown: 0,
ShutdownReason: NewLocalizedText(""),
},
}, NewFourByteExpandedNodeID(0, id.ServerStatusDataType_Encoding_DefaultBinary),
)),
Bytes: []byte{
// variant encoding mask
Expand Down
4 changes: 2 additions & 2 deletions ua/write_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestWriteRequest(t *testing.T) {
}),
Timestamp: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC),
RequestHandle: 1,
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
NodesToWrite: []*WriteValue{
{
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestWriteRequest(t *testing.T) {
}),
Timestamp: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC),
RequestHandle: 1,
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
NodesToWrite: []*WriteValue{
{
Expand Down
4 changes: 2 additions & 2 deletions ua/write_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestWriteResponse(t *testing.T) {
RequestHandle: 1,
ServiceDiagnostics: &DiagnosticInfo{},
StringTable: []string{},
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
Results: []StatusCode{StatusOK},
},
Expand Down Expand Up @@ -51,7 +51,7 @@ func TestWriteResponse(t *testing.T) {
RequestHandle: 1,
ServiceDiagnostics: &DiagnosticInfo{},
StringTable: []string{},
AdditionalHeader: NewExtensionObject(nil),
AdditionalHeader: NewExtensionObject(nil, NewTwoByteExpandedNodeID(0)),
},
Results: []StatusCode{StatusOK, StatusBadUserAccessDenied},
},
Expand Down
Loading

0 comments on commit 2c74fde

Please sign in to comment.