mirror of
https://github.com/gravitational/teleport.git
synced 2026-09-21 14:35:22 +08:00
All tests in lib/auth that do not modify global variables (modules, insecure dev mode) or make use of t.Setenv directly are now run in parallel.
78 lines
4.2 KiB
Go
78 lines
4.2 KiB
Go
/*
|
|
* Teleport
|
|
* Copyright (C) 2023 Gravitational, Inc.
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package auth_test
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
apievents "github.com/gravitational/teleport/api/types/events"
|
|
"github.com/gravitational/teleport/lib/auth"
|
|
"github.com/gravitational/teleport/lib/auth/authclient"
|
|
"github.com/gravitational/teleport/lib/events/eventstest"
|
|
)
|
|
|
|
func TestServerAuthenticateUserUserAgentTrim(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
emitter := &eventstest.MockRecorderEmitter{}
|
|
r := authclient.AuthenticateUserRequest{
|
|
ClientMetadata: &authclient.ForwardedClientMetadata{
|
|
UserAgent: strings.Repeat("A", auth.MaxUserAgentLen+1),
|
|
},
|
|
}
|
|
// Ignoring the error here because we really just care that the event was logged.
|
|
srv := &auth.Server{}
|
|
srv.SetEmitter(emitter)
|
|
srv.AuthenticateUserLogin(ctx, r)
|
|
event := emitter.LastEvent()
|
|
loginEvent, ok := event.(*apievents.UserLogin)
|
|
require.True(t, ok)
|
|
require.LessOrEqual(t, len(loginEvent.UserAgent), auth.MaxUserAgentLen)
|
|
}
|
|
|
|
func Test_trimUserAgent(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []struct {
|
|
name string
|
|
inputUserAgent string
|
|
wantUserAgent string
|
|
}{
|
|
{
|
|
name: "short",
|
|
inputUserAgent: "foo/1.0",
|
|
wantUserAgent: "foo/1.0",
|
|
},
|
|
{
|
|
name: "trimmed",
|
|
inputUserAgent: strings.Repeat("foo/1.0 ", 500),
|
|
wantUserAgent: "foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1.0 foo/1...",
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
require.Equal(t, test.wantUserAgent, auth.TrimUserAgent(test.inputUserAgent))
|
|
})
|
|
}
|
|
}
|