2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-08-13 09:39:45 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-02 10:23:25 +00:00
|
|
|
package identity
|
2018-08-13 09:39:45 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-12-17 15:09:52 +00:00
|
|
|
"fmt"
|
2018-08-13 09:39:45 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-01-02 17:39:17 +00:00
|
|
|
|
|
|
|
"storj.io/storj/internal/testcontext"
|
2018-08-13 09:39:45 +01:00
|
|
|
)
|
|
|
|
|
2018-08-23 15:08:26 +01:00
|
|
|
func TestNewCA(t *testing.T) {
|
2018-12-17 15:09:52 +00:00
|
|
|
const expectedDifficulty = 4
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2018-11-01 15:48:43 +00:00
|
|
|
ca, err := NewCA(context.Background(), NewCAOptions{
|
|
|
|
Difficulty: expectedDifficulty,
|
|
|
|
Concurrency: 5,
|
|
|
|
})
|
2018-08-13 09:39:45 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, ca)
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
actualDifficulty, err := ca.ID.Difficulty()
|
|
|
|
assert.NoError(t, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
assert.True(t, actualDifficulty >= expectedDifficulty)
|
|
|
|
}
|
|
|
|
|
2018-08-27 23:23:48 +01:00
|
|
|
func TestFullCertificateAuthority_NewIdentity(t *testing.T) {
|
2019-01-02 17:39:17 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
ca, err := NewCA(ctx, NewCAOptions{
|
2018-12-17 15:09:52 +00:00
|
|
|
Difficulty: 12,
|
|
|
|
Concurrency: 4,
|
|
|
|
})
|
2019-01-02 17:39:17 +00:00
|
|
|
if !assert.NoError(t, err) || !assert.NotNil(t, ca) {
|
2018-12-17 15:09:52 +00:00
|
|
|
t.Fatal(err)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
2018-08-27 23:23:48 +01:00
|
|
|
|
|
|
|
fi, err := ca.NewIdentity()
|
2019-01-02 17:39:17 +00:00
|
|
|
if !assert.NoError(t, err) || !assert.NotNil(t, fi) {
|
2018-12-17 15:09:52 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-08-27 23:23:48 +01:00
|
|
|
|
|
|
|
assert.Equal(t, ca.Cert, fi.CA)
|
|
|
|
assert.Equal(t, ca.ID, fi.ID)
|
|
|
|
assert.NotEqual(t, ca.Key, fi.Key)
|
|
|
|
assert.NotEqual(t, ca.Cert, fi.Leaf)
|
|
|
|
|
|
|
|
err = fi.Leaf.CheckSignatureFrom(ca.Cert)
|
|
|
|
assert.NoError(t, err)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
func TestFullCertificateAuthority_Sign(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
caOpts := NewCAOptions{
|
|
|
|
Difficulty: 12,
|
|
|
|
Concurrency: 4,
|
|
|
|
}
|
|
|
|
|
|
|
|
ca, err := NewCA(ctx, caOpts)
|
|
|
|
if !assert.NoError(t, err) || !assert.NotNil(t, ca) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
toSign, err := NewCA(ctx, caOpts)
|
|
|
|
if !assert.NoError(t, err) || !assert.NotNil(t, toSign) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
signed, err := ca.Sign(toSign.Cert)
|
|
|
|
if !assert.NoError(t, err) || !assert.NotNil(t, signed) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, toSign.Cert.RawTBSCertificate, signed.RawTBSCertificate)
|
|
|
|
assert.NotEqual(t, toSign.Cert.Signature, signed.Signature)
|
|
|
|
assert.NotEqual(t, toSign.Cert.Raw, signed.Raw)
|
|
|
|
|
|
|
|
err = signed.CheckSignatureFrom(ca.Cert)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2018-12-18 11:55:55 +00:00
|
|
|
func TestFullCAConfig_Save(t *testing.T) {
|
|
|
|
// TODO(bryanchriswhite): test with both
|
|
|
|
// TODO(bryanchriswhite): test with only cert path
|
|
|
|
// TODO(bryanchriswhite): test with only key path
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
2018-12-17 15:09:52 +00:00
|
|
|
func BenchmarkNewCA(b *testing.B) {
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, difficulty := range []uint16{8, 12} {
|
|
|
|
for _, concurrency := range []uint{1, 2, 5, 10} {
|
|
|
|
test := fmt.Sprintf("%d/%d", difficulty, concurrency)
|
|
|
|
b.Run(test, func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, _ = NewCA(ctx, NewCAOptions{
|
|
|
|
Difficulty: difficulty,
|
|
|
|
Concurrency: concurrency,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
}
|