1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
package attestation
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewClient(t *testing.T) {
baseURL := "http://localhost:1337"
type args struct {
tpmAttestationCABaseURL string
options []Option
}
tests := []struct {
name string
args args
assertFunc assert.ValueAssertionFunc
wantErr bool
}{
{
name: "ok/no-options",
args: args{
tpmAttestationCABaseURL: baseURL,
options: nil,
},
assertFunc: func(tt assert.TestingT, i1 any, i2 ...any) bool {
if assert.IsType(t, &Client{}, i1) {
c, _ := i1.(*Client)
if assert.NotNil(t, c) {
if assert.NotNil(t, c.baseURL) {
assert.Equal(t, baseURL, c.baseURL.String())
}
if assert.NotNil(t, c.client) {
assert.Equal(t, 10*time.Second, c.client.Timeout)
}
return true
}
}
return false
},
wantErr: false,
},
{
name: "ok/with-options",
args: args{
tpmAttestationCABaseURL: baseURL,
options: []Option{WithInsecure(), WithRootsFile("testdata/roots.pem")},
},
assertFunc: func(tt assert.TestingT, i1 any, i2 ...any) bool {
if assert.IsType(t, &Client{}, i1) {
c, _ := i1.(*Client)
if assert.NotNil(t, c) {
if assert.NotNil(t, c.baseURL) {
assert.Equal(t, baseURL, c.baseURL.String())
}
if assert.NotNil(t, c.client) {
assert.Equal(t, 10*time.Second, c.client.Timeout)
}
return true
}
}
return false
},
wantErr: false,
},
{
name: "fail/non-existing-roots",
args: args{
tpmAttestationCABaseURL: baseURL,
options: []Option{WithInsecure(), WithRootsFile("testdata/non-existing-roots.pem")},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewClient(tt.args.tpmAttestationCABaseURL, tt.args.options...)
if tt.wantErr {
assert.Error(t, err)
assert.Nil(t, got)
return
}
assert.NoError(t, err)
assert.True(t, tt.assertFunc(t, got))
})
}
}
|