From df7e46475c90392ccf573f07254e4ada26f61822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Thu, 26 Oct 2023 00:56:49 +0200 Subject: [PATCH] transaction: Add BinaryConversationFunc adapter --- app-transaction.go | 14 +++++++++++++ module-transaction_test.go | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/app-transaction.go b/app-transaction.go index 8bd709e7..a96f4cda 100644 --- a/app-transaction.go +++ b/app-transaction.go @@ -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 diff --git a/module-transaction_test.go b/module-transaction_test.go index f0c06a5c..3b424af4 100644 --- a/module-transaction_test.go +++ b/module-transaction_test.go @@ -3,6 +3,7 @@ package pam import ( "errors" + "fmt" "reflect" "strings" "testing" @@ -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 {