@ -2,6 +2,7 @@ package session
import (
"context"
"crypto/x509"
"testing"
"time"
@ -23,8 +24,10 @@ func TestSession_Create(t *testing.T) {
composedOf := testSessionCredentialParams ( t , conn , wrapper , iamRepo )
exp := & timestamp . Timestamp { Timestamp : timestamppb . New ( time . Now ( ) . Add ( time . Hour ) ) }
defaultAddresses := [ ] string { "1.2.3.4" , "a.b.c.d" }
type args struct {
composedOf ComposedOf
addresses [ ] string
opt [ ] Option
}
tests := [ ] struct {
@ -32,6 +35,7 @@ func TestSession_Create(t *testing.T) {
args args
want * Session
wantErr bool
wantAddrErr bool
wantIsErr errors . Code
create bool
wantCreateErr bool
@ -41,6 +45,7 @@ func TestSession_Create(t *testing.T) {
args : args {
composedOf : composedOf ,
opt : [ ] Option { WithExpirationTime ( exp ) } ,
addresses : defaultAddresses ,
} ,
want : & Session {
UserId : composedOf . UserId ,
@ -64,6 +69,7 @@ func TestSession_Create(t *testing.T) {
c . UserId = ""
return c
} ( ) ,
addresses : defaultAddresses ,
} ,
wantErr : true ,
wantIsErr : errors . InvalidParameter ,
@ -76,6 +82,7 @@ func TestSession_Create(t *testing.T) {
c . HostId = ""
return c
} ( ) ,
addresses : defaultAddresses ,
} ,
wantErr : true ,
wantIsErr : errors . InvalidParameter ,
@ -88,6 +95,7 @@ func TestSession_Create(t *testing.T) {
c . TargetId = ""
return c
} ( ) ,
addresses : defaultAddresses ,
} ,
wantErr : true ,
wantIsErr : errors . InvalidParameter ,
@ -100,6 +108,7 @@ func TestSession_Create(t *testing.T) {
c . HostSetId = ""
return c
} ( ) ,
addresses : defaultAddresses ,
} ,
wantErr : true ,
wantIsErr : errors . InvalidParameter ,
@ -112,6 +121,7 @@ func TestSession_Create(t *testing.T) {
c . AuthTokenId = ""
return c
} ( ) ,
addresses : defaultAddresses ,
} ,
wantErr : true ,
wantIsErr : errors . InvalidParameter ,
@ -124,10 +134,34 @@ func TestSession_Create(t *testing.T) {
c . ScopeId = ""
return c
} ( ) ,
addresses : defaultAddresses ,
} ,
wantErr : true ,
wantIsErr : errors . InvalidParameter ,
} ,
{
name : "empty-addresses" ,
args : args {
composedOf : func ( ) ComposedOf {
c := composedOf
return c
} ( ) ,
} ,
want : & Session {
UserId : composedOf . UserId ,
HostId : composedOf . HostId ,
TargetId : composedOf . TargetId ,
HostSetId : composedOf . HostSetId ,
AuthTokenId : composedOf . AuthTokenId ,
ScopeId : composedOf . ScopeId ,
Endpoint : "tcp://127.0.0.1:22" ,
ExpirationTime : composedOf . ExpirationTime ,
ConnectionLimit : composedOf . ConnectionLimit ,
DynamicCredentials : composedOf . DynamicCredentials ,
} ,
wantAddrErr : true ,
wantIsErr : errors . InvalidParameter ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
@ -145,7 +179,12 @@ func TestSession_Create(t *testing.T) {
id , err := db . NewPublicId ( SessionPrefix )
require . NoError ( err )
got . PublicId = id
_ , certBytes , err := newCert ( ctx , wrapper , got . UserId , id , composedOf . ExpirationTime . Timestamp . AsTime ( ) )
_ , certBytes , err := newCert ( ctx , wrapper , got . UserId , id , tt . args . addresses , composedOf . ExpirationTime . Timestamp . AsTime ( ) )
if tt . wantAddrErr {
require . Error ( err )
assert . True ( errors . Match ( errors . T ( tt . wantIsErr ) , err ) )
return
}
require . NoError ( err )
got . Certificate = certBytes
err = db . New ( conn ) . Create ( ctx , got )
@ -155,6 +194,13 @@ func TestSession_Create(t *testing.T) {
} else {
assert . NoError ( err )
}
if len ( tt . args . addresses ) > 0 {
cert , err := x509 . ParseCertificate ( certBytes )
require . NoError ( err )
// Session ID is always encoded, hence the +1
assert . Equal ( len ( tt . args . addresses ) + 1 , len ( cert . DNSNames ) + len ( cert . IPAddresses ) )
}
}
} )
}