Skip to content
Snippets Groups Projects
Commit 48acc86b authored by Tigran Avakyan's avatar Tigran Avakyan
Browse files

Moved conf package outside of server and wrote soem tests for db

parent ac619811
Branches
Tags
No related merge requests found
File moved
File moved
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2019 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package conf
import "github.com/pkg/errors"
type DB interface {
GetDBName() string
GetUserName() string
GetPassword() string // TODO: maybe this should be a secure string via memguard
GetAddresses() []string
}
type dbImpl struct {
dbName string
userName string
password string
addresses []string
}
// NewDB returns an interface to a DB if all inputs are valid,
// otherwise it returns an error specifying which input was invalid.
func NewDB(dbName, userName, password string, addresses []string) (DB, error) {
// If input fields are not valid return an error.
if !isDBNameValid(dbName) {
return nil, errors.Errorf("NewDB failed with dbName %s", dbName)
}
if !isUserNameValid(userName) {
return nil, errors.Errorf("NewDB failed with userName %s", userName)
}
if !isPasswordValid(password) {
return nil, errors.Errorf("NewDB failed with password %s", password)
}
if addresses == nil {
return nil, errors.Errorf("NewDB failed with addresses nil")
}
for _, address := range addresses {
if !isAddressValid(address) {
return nil, errors.Errorf("NewDB failed with addresses %s", address)
}
}
// Otherwise return the interface to the object with no error
return dbImpl{
dbName: dbName,
userName: userName,
password: password,
addresses: addresses,
}, nil
}
// GetDBName returns the stored database schema name
func (db dbImpl) GetDBName() string {
return db.dbName
}
// GetAddresses
func (db dbImpl) GetAddresses() []string {
return db.addresses
}
// GetUserName returns the stored user name for db login
func (db dbImpl) GetUserName() string {
return db.userName
}
// GetPassword returns the stored password for db login
func (db dbImpl) GetPassword() string {
return db.password
}
// isDBNameValid returns true for any string
// TODO: Function should return true for any string
// which is a valid database name based on db impl.
func isDBNameValid(dbName string) bool {
return true
}
// isUserNameValid returns true for any string
// TODO: Function should return true for any
// alphanumeric which doesn't begin with a number
func isUserNameValid(userName string) bool {
return true
}
// isPasswordValid returns true for any string
// TODO: Change password to be a secure memguard type
// and modify this function to handle that accordingly
func isPasswordValid(password string) bool {
return true
}
// isAddressValid returns true for any string
// TODO: Function should check if format matches
// <ip_address>:<port> or some eq. representation.
func isAddressValid(address string) bool {
return true
}
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2019 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package conf
import "testing"
const ValidDBName = "ValidDBName123"
const ValidUserName = "ValidUserName123"
const ValidPassword = "Z8X:6d*n$9A)YQr5"
var ValidAddresses = []string{"127.0.0.1:5000", "127.0.0.1:5001"}
// NewDB should return an error on empty or non-alpha db name
func TestNewDB_ReturnsErrorOnInvalidDBName(t *testing.T) {
invalidDBNames := []string{"", "#@#$#@"}
userName := ValidUserName
password := ValidPassword
addresses := ValidAddresses
for _, invalidDBName := range invalidDBNames {
_, err := NewDB(invalidDBName, userName, password, addresses)
if err == nil {
t.Errorf("NewDB did not return an error for dbName %s", invalidDBName)
}
}
}
// NewDB should return an error on empty or non-alpha username
func TestNewDB_ReturnsErrorOnInvalidUserName(t *testing.T) {
invalidUserNames := []string{"", "#@#$#@", "0123"}
dbName := ValidDBName
password := ValidPassword
addresses := ValidAddresses
for _, invalidUserName := range invalidUserNames {
_, err := NewDB(dbName, invalidUserName, password, addresses)
if err == nil {
t.Errorf("NewDB did not return an error for username %s", invalidUserName)
}
}
}
// NewDB should return an error on a long password
func TestNewDB_ReturnsErrorOnInvalidPassword(t *testing.T) {
invalidPasswords := []string{`
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
àbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbcàbc
`}
dbName := ValidDBName
userName := ValidUserName
addresses := ValidAddresses
for _, invalidPassword := range invalidPasswords {
_, err := NewDB(dbName, userName, invalidPassword, addresses)
if err == nil {
t.Errorf("NewDB did not return an error for password %s", invalidPassword)
}
}
}
// NewDB should return an error on empty or non-alpha list of addresses
func TestNewDB_ReturnsErrorOnInvalidAddress(t *testing.T) {
invalidAddressesList := [][]string{
{""},
{"#@#$#@"},
{"0123"},
}
dbName := ValidDBName
userName := ValidUserName
password := ValidPassword
for _, invalidAddresses := range invalidAddressesList {
_, err := NewDB(dbName, userName, password, invalidAddresses)
if err == nil {
t.Errorf("NewDB did not return an error for addresses %s", invalidAddresses)
}
}
}
// GetDBName should match expected value when created with valid inputs
func TestGetDBName_ReturnsExpectedValidValue(t *testing.T) {
db := createValidDB(t)
expectedDbName := ValidDBName
if db.GetDBName() != expectedDbName {
t.Errorf("GetDBName() did not return expected value of %s", expectedDbName)
}
}
// GetUserName should match expected value when created with valid inputs
func TestGetUserName_ReturnsExpectedValidValue(t *testing.T) {
db := createValidDB(t)
expectedUserName := ValidUserName
if db.GetUserName() != expectedUserName {
t.Errorf("GetUserName() did not return expected value of %s", expectedUserName)
}
}
// GetPassword should match expected value when created with valid inputs
func TestGetPassword_ReturnsExpectedValidValue(t *testing.T) {
db := createValidDB(t)
expectedPassword := ValidPassword
if db.GetPassword() != expectedPassword {
t.Errorf("GetPassword() did not return expected value of %s", expectedPassword)
}
}
// GetAddresses should match expected values when created with valid inputs
func TestGetAddresses_ReturnsExpectedValidValue(t *testing.T) {
db := createValidDB(t)
expectedAddresses := ValidAddresses
addresses := db.GetAddresses()
for i, address := range addresses {
if address != expectedAddresses[i] {
t.Errorf("GetAddresses() did not return expected value of %s on %d",
expectedAddresses, i)
}
}
}
// createValidDB is a helper test function
// which creates and returns a valid DB instance
func createValidDB(t *testing.T) DB {
dbName := ValidDBName
userName := ValidUserName
password := ValidPassword
addresses := ValidAddresses
db, err := NewDB(dbName, userName, password, addresses)
if err != nil {
t.Error("NewDB received invalid inputs")
}
return db
}
......@@ -6,7 +6,12 @@
package conf
import "gitlab.com/elixxir/crypto/cyclic"
import (
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/elixxir/crypto/large"
"strings"
)
type Groups interface {
GetCMix() *cyclic.Group
......@@ -33,6 +38,34 @@ func (grps groupsImpl) GetE2E() *cyclic.Group {
return grps.e2e
}
func toGroup(strings map[string]string) *cyclic.Group {
return nil
// toGroup takes a group represented by a map of string to string
// and uses the prime, small prime and generator to created
// and returns a a cyclic group object.
func toGroup(grp map[string]string) *cyclic.Group {
pStr, pOk := grp["prime"]
qStr, qOk := grp["smallprime"]
gStr, gOk := grp["generator"]
if !gOk || !qOk || !pOk {
jww.FATAL.Panicf("Invalid Group Config "+
"(prime: %v, smallPrime: %v, generator: %v",
pOk, qOk, gOk)
}
// TODO: Is there any error checking we should do here? If so, what?
p := toLargeInt(strings.ReplaceAll(pStr, " ", ""))
q := toLargeInt(strings.ReplaceAll(qStr, " ", ""))
g := toLargeInt(strings.ReplaceAll(gStr, " ", ""))
return cyclic.NewGroup(p, g, q)
}
// toLargeInt takes in a string representation of a large int.
// If the first 2 bytes are '0x' it parses a base 16 number,
// otherwise it parses a base 10 and returns the result.
func toLargeInt(str string) *large.Int {
if len(str) > 2 && "0x" == str[:2] {
return large.NewIntFromString(str[2:], 16)
}
return large.NewIntFromString(str, 10)
}
File moved
......@@ -22,14 +22,15 @@ type paramsImpl struct {
reg Reg
}
func NewParams(db DB, groups Groups, path Path, context Context, reg Reg) Params {
func NewParams(db DB, groups Groups, path Path, context Context, reg Reg) (Params, error) {
return paramsImpl{
db: db,
groups: groups,
path: path,
context: context,
reg: reg,
}
}, nil
}
func (params paramsImpl) GetDB() DB {
......
......@@ -8,6 +8,6 @@ package conf
import "testing"
func TestParams_New(t *testing.T) {
func TestNewParams_ErrorOnEmptyDB(t *testing.T) {
//params, err := NewParams(nil, nil, nil, nil,nil)
}
File moved
File moved
File moved
File moved
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2019 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package conf
type DB interface {
GetDBName() string
GetAddresses() []string
GetUsername() string
GetPassword() string
}
type dbImpl struct {
addresses []string
username string
password string // TODO: maybe this should be a secure string via memguard
dbName string
}
func NewDB(addresses []string, username, password, dbName string) DB {
return dbImpl{
addresses: addresses,
username: username,
password: password,
dbName: dbName,
}
}
func (db dbImpl) GetDBName() string {
return db.dbName
}
func (db dbImpl) GetAddresses() []string {
return db.addresses
}
func (db dbImpl) GetUsername() string {
return db.username
}
func (db dbImpl) GetPassword() string {
return db.password
}
......@@ -9,8 +9,8 @@ import (
"gitlab.com/elixxir/crypto/large"
"gitlab.com/elixxir/crypto/signature"
"gitlab.com/elixxir/primitives/id"
"gitlab.com/elixxir/server/conf"
"gitlab.com/elixxir/server/globals"
"gitlab.com/elixxir/server/server/conf"
"gitlab.com/elixxir/server/server/round"
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment