storj/internal/testplanet/identities.go

66 lines
1.7 KiB
Go
Raw Normal View History

2019-01-24 20:15:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
2018-11-03 12:17:14 +00:00
// See LICENSE for copying information
package testplanet
import (
"errors"
2019-01-30 20:47:21 +00:00
"storj.io/storj/pkg/identity"
2018-11-03 12:17:14 +00:00
)
//go:generate go run gen_identities.go -count 150 -out identities_table.go
// Identities is a pregenerated full identity table.
type Identities struct {
2019-01-30 20:47:21 +00:00
list []*identity.FullIdentity
2018-11-03 12:17:14 +00:00
next int
}
// NewIdentities creates a new table from provided identities.
2019-01-30 20:47:21 +00:00
func NewIdentities(list ...*identity.FullIdentity) *Identities {
2018-11-03 12:17:14 +00:00
return &Identities{
list: list,
next: 0,
}
}
2018-12-17 15:09:52 +00:00
// PregeneratedIdentity returns a pregenerated identity from a list
2019-01-30 20:47:21 +00:00
func PregeneratedIdentity(index int) (*identity.FullIdentity, error) {
2018-12-17 15:09:52 +00:00
if pregeneratedIdentities.next >= len(pregeneratedIdentities.list) {
return nil, errors.New("out of pregenerated identities")
}
return pregeneratedIdentities.list[index], nil
}
// NewPregeneratedIdentities retruns a new table from provided identities.
func NewPregeneratedIdentities() *Identities {
return pregeneratedIdentities.Clone()
}
2018-11-03 12:17:14 +00:00
// Clone creates a shallow clone of the table.
func (identities *Identities) Clone() *Identities {
return NewIdentities(identities.list...)
}
// NewIdentity gets a new identity from the list.
2019-01-30 20:47:21 +00:00
func (identities *Identities) NewIdentity() (*identity.FullIdentity, error) {
2018-11-03 12:17:14 +00:00
if identities.next >= len(identities.list) {
return nil, errors.New("out of pregenerated identities")
}
id := identities.list[identities.next]
identities.next++
return id, nil
}
// mustParsePEM parses pem encoded chain and key strings.
2019-01-30 20:47:21 +00:00
func mustParsePEM(chain, key string) *identity.FullIdentity {
2018-11-03 12:17:14 +00:00
// TODO: add whitelist handling somehow
2019-01-30 20:47:21 +00:00
fi, err := identity.FullIdentityFromPEM([]byte(chain), []byte(key))
2018-11-03 12:17:14 +00:00
if err != nil {
panic(err)
}
return fi
}