Skip to content

Commit

Permalink
PAM service tests (#22)
Browse files Browse the repository at this point in the history
Package tests for `internal/services/pam`.

UDENG-1172
  • Loading branch information
denisonbarbosa committed Sep 5, 2023
2 parents a28f5e0 + 5de6947 commit 729fb0c
Show file tree
Hide file tree
Showing 18 changed files with 681 additions and 8 deletions.
595 changes: 595 additions & 0 deletions internal/services/pam/pam_test.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions internal/services/pam/testdata/TestAvailableBrokers/golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- id: local_ID
name: local
brandicon: ""
- id: BrokerMock_ID
name: BrokerMock
brandicon: mock_icon.png
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- id: mode1
label: Mode 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- id: mode1
label: Mode 1
- id: mode2
label: Mode 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FIRST CALL:
access: denied
data: {"mock_answer": "denied by time out"}
err: <nil>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FIRST CALL:
access: allowed
data: {}
err: <nil>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FIRST CALL:
access:
data:
err: rpc error: code = Unknown desc = can't check authorization: Broker "BrokerMock": IsAuthorized errored out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FIRST CALL:
access:
data:
err: rpc error: code = Unknown desc = can't check authorization: invalid access authorization key: invalid
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FIRST CALL:
access:
data:
err: rpc error: code = Unknown desc = can't check authorization: invalid user information (not json formatted): invalid
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FIRST CALL:
access: allowed
data: {"mock_answer": "authentication allowed by timeout"}
err: <nil>
SECOND CALL:
access:
data:
err: rpc error: code = Unknown desc = can't check authorization: Broker "BrokerMock": IsAuthorized already running for session "TestIsAuthorized/Error_when_calling_second_time_without_cancelling_separator_IA_second_call-session_id"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FIRST CALL:
access:
data:
err: rpc error: code = Unknown desc = can't check authorization: no session ID provided
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FIRST CALL:
access:
data:
err: rpc error: code = Unknown desc = can't check authorization: no broker found for session "no broker"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FIRST CALL:
access: allowed
data: {"mock_answer": "authentication allowed by default"}
err: <nil>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FIRST CALL:
access:
data:
err: rpc error: code = Canceled desc = context canceled
SECOND CALL:
access: allowed
data: {"mock_answer": "authentication allowed by timeout"}
err: <nil>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: optional-entry
label: ""
button: ""
wait: ""
entry: ""
content: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: required-entry
label: ""
button: ""
wait: ""
entry: entry_type
content: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ID: BROKER_ID-TestSelectBroker/Successfully_select_a_broker_and_creates_the_session_separator_success-session_id
Encryption Key: BrokerMock_key
20 changes: 12 additions & 8 deletions internal/testutils/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const (
objectPathFmt = "/com/ubuntu/authd/%s"
interfaceFmt = "com.ubuntu.authd.%s"

// IDPrefix is the value used to trim the sessionID in the broker mock.
IDPrefix = "_separator_"
// IDSeparator is the value used to append values to the sessionID in the broker mock.
IDSeparator = "_separator_"
)

var brokerConfigTemplate = `name = %s
Expand Down Expand Up @@ -108,10 +108,11 @@ func writeConfig(cfgDir, name string) (string, error) {

// NewSession returns default values to be used in tests or an error if requested.
func (b *BrokerBusMock) NewSession(username, lang string) (sessionID, encryptionKey string, dbusErr *dbus.Error) {
if username == "NS_error" {
parsedUsername := parseSessionID(username)
if parsedUsername == "NS_error" {
return "", "", dbus.MakeFailedError(fmt.Errorf("Broker %q: NewSession errored out", b.name))
}
if username == "NS_no_id" {
if parsedUsername == "NS_no_id" {
return "", username + "_key", nil
}
return fmt.Sprintf("%s-session_id", username), b.name + "_key", nil
Expand Down Expand Up @@ -281,10 +282,13 @@ func (b *BrokerBusMock) CancelIsAuthorized(sessionID string) (dbusErr *dbus.Erro
}

// parseSessionID is wrapper around the sessionID to remove some values appended during the tests.
//
// The sessionID can have multiple values appended to differentiate between subtests and avoid concurrency conflicts,
// and only the last value (i.e. "..._separator_ID-session_id") will be considered.
func parseSessionID(sessionID string) string {
// We need to prefix the sessionID with the test name in some tests, so we have to consider this here in the broker.
if _, after, found := strings.Cut(sessionID, IDPrefix); found {
sessionID = after
cut := strings.Split(sessionID, IDSeparator)
if len(cut) == 0 {
return ""
}
return strings.TrimSuffix(sessionID, "-session_id")
return strings.TrimSuffix(cut[len(cut)-1], "-session_id")
}

0 comments on commit 729fb0c

Please sign in to comment.