Skip to content

Commit

Permalink
transaction: Add BinaryConversationFunc adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
3v1n0 committed Nov 27, 2023
1 parent 6fecdd0 commit df7e464
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app-transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ func (f ConversationFunc) RespondPAM(s Style, msg string) (string, error) {
return f(s, msg)
}

// BinaryConversationFunc is an adapter to allow the use of ordinary functions
// as binary (only) conversation callbacks.
type BinaryConversationFunc func(BinaryPointer) ([]byte, error)

// RespondPAMBinary is a conversation callback adapter.
func (f BinaryConversationFunc) RespondPAMBinary(ptr BinaryPointer) ([]byte, error) {
return f(ptr)
}

// RespondPAM is a dummy conversation callback adapter.
func (f BinaryConversationFunc) RespondPAM(Style, string) (string, error) {
return "", ErrConv
}

// _go_pam_conv_handler is a C wrapper for the conversation callback function.
//
//export _go_pam_conv_handler
Expand Down
42 changes: 42 additions & 0 deletions module-transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pam

import (
"errors"
"fmt"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -902,6 +903,47 @@ func testMockModuleTransaction(t *testing.T, mt *moduleTransaction) {
return data.Data(), err
},
},
"StartConv-Binary-with-ConvFunc": {
expectedValue: []byte{0x01, 0x02, 0x03, 0x05, 0x00, 0x99},
conversationHandler: BinaryConversationFunc(func(ptr BinaryPointer) ([]byte, error) {
bytes, _ := testBinaryDataDecoder(ptr)
expectedBinary := []byte(
"\x00This is a binary data request\xC5\x00\xffYes it is!")
if !reflect.DeepEqual(bytes, expectedBinary) {
return nil, NewTransactionError(ErrConv,
fmt.Errorf("data mismatch %#v vs %#v", bytes, expectedBinary))
}
return testBinaryDataEncoder([]byte{0x01, 0x02, 0x03, 0x05, 0x00, 0x99}), nil
}),
testFunc: func(mock *mockModuleTransaction) (any, error) {
bytes := testBinaryDataEncoder([]byte(
"\x00This is a binary data request\xC5\x00\xffYes it is!"))
data, err := mt.startConvImpl(mock, NewBinaryConvRequestFromBytes(bytes))
if err != nil {
return data, err
}
resp, _ := data.(BinaryConvResponse)
return resp.Decode(testBinaryDataDecoder)
},
},
"StartConv-Binary-with-ConvFunc-error": {
expectedError: ErrConv,
conversationHandler: BinaryConversationFunc(func(ptr BinaryPointer) ([]byte, error) {
return nil, errors.New("got an error")
}),
testFunc: func(mock *mockModuleTransaction) (any, error) {
return mt.startConvImpl(mock, NewBinaryConvRequestFromBytes([]byte{}))
},
},
"StartConv-String-with-ConvBinaryFunc": {
expectedError: ErrConv,
conversationHandler: BinaryConversationFunc(func(ptr BinaryPointer) ([]byte, error) {
return nil, nil
}),
testFunc: func(mock *mockModuleTransaction) (any, error) {
return mt.startConvImpl(mock, NewStringConvRequest(TextInfo, "prompt"))
},
},
}

for name, tc := range tests {
Expand Down

0 comments on commit df7e464

Please sign in to comment.