diff --git a/api/client.go b/api/client.go index b48605e152836b7b9ef289675b2f928b0f5c3e66..cd4e6f17e07780197da3f87d2cde2b075f0b193d 100644 --- a/api/client.go +++ b/api/client.go @@ -83,6 +83,7 @@ func NewTestClient(s globals.Storage, locA, locB string, ndfJSON *ndf.NetworkDef return newClient(s, locA, locB, ndfJSON, sendFunc) } +// setStorage is a helper to initialize the new session storage func (cl *Client) setStorage(locA, password string) error { // TODO: FIX ME // While the old session is still valid, we are using the LocA storage to initialize the session @@ -91,10 +92,18 @@ func (cl *Client) setStorage(locA, password string) error { var err error io.SessionV2, err = storage.Init(dirname, password) if err != nil { - return errors.Wrap(err, "Login: could not initialize v2 storage") + return errors.Wrapf(err, "could not initialize v2 "+ + "storage at %s", locA) } clientcrypto.SessionV2 = io.SessionV2 cl.sessionV2 = io.SessionV2 + + // FIXME: Client storage must have regstate set + _, err = cl.sessionV2.GetRegState() + if os.IsNotExist(err) { + cl.sessionV2.SetRegState(user.KeyGenComplete) + } + return nil } @@ -181,16 +190,12 @@ func (cl *Client) Login(password string) (*id.ID, error) { } regState, err := cl.sessionV2.GetRegState() - // fixme ! - if err != nil && os.IsNotExist(err) { - io.SessionV2.SetRegState(user.KeyGenComplete) - regState, _ = io.SessionV2.GetRegState() - - } else if err != nil { - return nil, errors.Wrap(err, "Login: Could not login: Could not get regState") + if err != nil { + return nil, errors.Wrap(err, + "Login: Could not login: Could not get regState") } - if regState < user.KeyGenComplete { + if regState <= user.KeyGenComplete { return nil, errors.New("Cannot log a user in which has not " + "completed registration ") } @@ -619,7 +624,7 @@ func (cl *Client) GetSession() user.Session { return cl.session } -// GetSession returns the session object for external access. Access at yourx +// GetSessionV2 returns the session object for external access. Access at yourx // own risk func (cl *Client) GetSessionV2() *storage.Session { return cl.sessionV2 diff --git a/api/client_test.go b/api/client_test.go index 4d0cb78ec139afd1f18a88342129b0ce884201cf..d20eb0eab0383a0c3cba6fb06ed61a712a176fc2 100644 --- a/api/client_test.go +++ b/api/client_test.go @@ -112,13 +112,13 @@ func TestNewClient_Panic(t *testing.T) { // Arbitrary invalid interface var i rsa.PublicKey // Passed into NewTestClient call to cause a panic - NewTestClient(&globals.RamStorage{}, ".ekv-testnewclientpanic", "", def, i, send) + NewTestClient(&globals.RamStorage{}, ".ekv-testnewclientpanic/a", "", def, i, send) t.Errorf("Failed to detect a bad interface passed in") } // Happy path func TestNewClient(t *testing.T) { - _, err := NewTestClient(&globals.RamStorage{}, ".ekv-testnewclient", "", def, t, send) + _, err := NewTestClient(&globals.RamStorage{}, ".ekv-testnewclient/a", "", def, t, send) if err != nil { t.Errorf("Expected happy path, received error: %+v", err) } @@ -152,7 +152,7 @@ func TestParse(t *testing.T) { // Test that registerUserE2E correctly creates keys and adds them to maps func TestRegisterUserE2E(t *testing.T) { - testClient, err := NewClient(&globals.RamStorage{}, ".ekv-testrege2e", "", def) + testClient, err := NewClient(&globals.RamStorage{}, ".ekv-testrege2e/a", "", def) if err != nil { t.Error(err) } @@ -248,7 +248,7 @@ func TestRegisterUserE2E(t *testing.T) { // Test all keys created with registerUserE2E match what is expected func TestRegisterUserE2E_CheckAllKeys(t *testing.T) { - testClient, err := NewClient(&globals.RamStorage{}, ".ekv-testrege2e-allkeys", "", def) + testClient, err := NewClient(&globals.RamStorage{}, ".ekv-testrege2e-allkeys/a", "", def) if err != nil { t.Fatal(err) } @@ -403,7 +403,7 @@ func TestRegisterUserE2E_CheckAllKeys(t *testing.T) { // Test happy path for precannedRegister func TestClient_precannedRegister(t *testing.T) { //Start client - testClient, err := NewClient(&globals.RamStorage{}, ".ekv-testclient-precannedreg", "", def) + testClient, err := NewClient(&globals.RamStorage{}, ".ekv-testclient-precannedreg/a", "", def) if err != nil { t.Error(err) @@ -427,7 +427,7 @@ func TestClient_precannedRegister(t *testing.T) { func TestClient_sendRegistrationMessage(t *testing.T) { //Start client - testClient, err := NewClient(&globals.RamStorage{}, ".ekv-sendregmsg", "", def) + testClient, err := NewClient(&globals.RamStorage{}, ".ekv-sendregmsg/a", "", def) if err != nil { t.Error(err) } @@ -459,7 +459,7 @@ func TestClient_requestNonce(t *testing.T) { privateKeyRSA, _ := rsa.GenerateKey(rng, TestKeySize) publicKeyRSA := rsa.PublicKey{PublicKey: privateKeyRSA.PublicKey} - testClient, err := NewClient(&globals.RamStorage{}, ".ekv-reqnonce", "", def) + testClient, err := NewClient(&globals.RamStorage{}, ".ekv-reqnonce/a", "", def) if err != nil { t.Error(err) } @@ -491,7 +491,7 @@ func TestClient_requestNonce(t *testing.T) { // Test happy path for confirmNonce func TestClient_confirmNonce(t *testing.T) { - testClient, err := NewClient(&globals.RamStorage{}, ".ekv-confirmnonce", "", def) + testClient, err := NewClient(&globals.RamStorage{}, ".ekv-confirmnonce/a", "", def) if err != nil { t.Error(err) } @@ -559,7 +559,7 @@ func getGroups() (*cyclic.Group, *cyclic.Group) { func TestClient_GetSession(t *testing.T) { //Start client - testClient, _ := NewClient(&globals.RamStorage{}, ".ekv-getsession", "", def) + testClient, _ := NewClient(&globals.RamStorage{}, ".ekv-getsession/a", "", def) testClient.session = &user.SessionObj{} @@ -573,7 +573,7 @@ func TestClient_GetSession(t *testing.T) { func TestClient_GetCommManager(t *testing.T) { //Start client - testClient, _ := NewClient(&globals.RamStorage{}, ".ekv-getcommmanager", "", def) + testClient, _ := NewClient(&globals.RamStorage{}, ".ekv-getcommmanager/a", "", def) testClient.receptionManager = &io.ReceptionManager{} @@ -585,8 +585,8 @@ func TestClient_GetCommManager(t *testing.T) { // Test that client.Shutcown clears out all the expected variables and stops the message reciever. func TestClient_LogoutHappyPath(t *testing.T) { //Initialize a client - d := DummyStorage{LocationA: ".ekv-logouthappypath", StoreA: []byte{'a', 'b', 'c'}} - tc, _ := NewClient(&d, ".ekv-logouthappypath", "", def) + d := DummyStorage{LocationA: ".ekv-logouthappypath/a", StoreA: []byte{'a', 'b', 'c'}} + tc, _ := NewClient(&d, ".ekv-logouthappypath/a", "", def) uid := id.NewIdFromString("kk", id.User, t) tc.receptionManager, _ = io.NewReceptionManager(tc.rekeyChan, uid, nil, nil, nil) @@ -596,10 +596,7 @@ func TestClient_LogoutHappyPath(t *testing.T) { t.Errorf("Could not connect: %+v", err) } - tc.sessionV2, err = storage.Init(".ekv-logouthappypath", "password") - io.SessionV2 = tc.sessionV2 - - err = tc.GenerateKeys(nil, "") + err = tc.GenerateKeys(nil, "password") if err != nil { t.Errorf("Could not generate Keys: %+v", err) } @@ -664,8 +661,8 @@ func TestClient_LogoutHappyPath(t *testing.T) { //Test that the client shutdown will timeout when it fails to shutdown func TestClient_LogoutTimeout(t *testing.T) { //Initialize a client - d := DummyStorage{LocationA: ".ekv-logouttimeout", StoreA: []byte{'a', 'b', 'c'}} - tc, _ := NewClient(&d, ".ekv-logouttimeout", "", def) + d := DummyStorage{LocationA: ".ekv-logouttimeout/a", StoreA: []byte{'a', 'b', 'c'}} + tc, _ := NewClient(&d, ".ekv-logouttimeout/a", "", def) uid := id.NewIdFromString("kk", id.User, t) tc.receptionManager, _ = io.NewReceptionManager(tc.rekeyChan, uid, nil, nil, nil) @@ -728,8 +725,8 @@ func TestClient_LogoutTimeout(t *testing.T) { // Test that if we logout we can logback in. func TestClient_LogoutAndLoginAgain(t *testing.T) { //Initialize a client - dummyStorage := &DummyStorage{LocationA: ".ekv-logoutlogin", StoreA: []byte{'a', 'b', 'c'}} - tc, err := NewClient(dummyStorage, ".ekv-logoutlogin", "", def) + dummyStorage := &DummyStorage{LocationA: ".ekv-logoutloginagain/a", StoreA: []byte{'a', 'b', 'c'}} + tc, err := NewClient(dummyStorage, ".ekv-logoutloginagain/a", "", def) if err != nil { t.Errorf("Failed to create new client: %+v", err) } @@ -746,6 +743,7 @@ func TestClient_LogoutAndLoginAgain(t *testing.T) { if err != nil { t.Errorf("Could not generate Keys: %+v", err) } + tc.sessionV2.SetRegState(user.PermissioningComplete) //Start Message receiver callBack := func(err error) { @@ -766,7 +764,7 @@ func TestClient_LogoutAndLoginAgain(t *testing.T) { } //Redefine client with old session files and attempt to login. - tc, err = NewClient(dummyStorage, ".ekv-logoutlogin", "", def) + tc, err = NewClient(dummyStorage, ".ekv-logoutloginagain/a", "", def) if err != nil { t.Errorf("Failed second client initialization: %+v", err) } diff --git a/api/mockserver.go b/api/mockserver.go index 2d363d7f3d333cc4db983f4ec2380fe053668fee..5405c8277631c7bb60c26ca5f99ae16790e970e1 100644 --- a/api/mockserver.go +++ b/api/mockserver.go @@ -96,7 +96,8 @@ func (d *DummyStorage) SetLocation(lA, lB string) error { } func (d *DummyStorage) GetLocation() (string, string) { - //return fmt.Sprintf("%s,%s", d.LocationA, d.LocationB) + fmt.Printf("LOCATION LOCATION LOCATION: %s,%s\n\n", d.LocationA, + d.LocationB) return d.LocationA, d.LocationB } diff --git a/api/mockserver_test.go b/api/mockserver_test.go index e64309f108dc48398b6d13fb5ff81a12dfce3f4b..804401810beab064de8657dcd2588ef4e364a871 100644 --- a/api/mockserver_test.go +++ b/api/mockserver_test.go @@ -12,8 +12,8 @@ import ( jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/io" //clientStorage "gitlab.com/elixxir/client/storage" + "gitlab.com/elixxir/client/storage" "gitlab.com/elixxir/client/user" - //"gitlab.com/elixxir/client/crypto" "gitlab.com/elixxir/comms/gateway" pb "gitlab.com/elixxir/comms/mixmessages" "gitlab.com/elixxir/comms/notificationBot" @@ -81,9 +81,9 @@ func TestClient_StartMessageReceiver_MultipleMessages(t *testing.T) { } testDef.Nodes = def.Nodes - locA := ".ekv-messagereceiver-multiple" + locA := ".ekv-messagereceiver-multiple/a" storage := DummyStorage{LocationA: locA, StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&storage, ".ekv-messagereceiver-multiple", "", testDef) + client, err := NewClient(&storage, ".ekv-messagereceiver-multiple/a", "", testDef) if err != nil { t.Errorf("Failed to initialize dummy client: %s", err.Error()) } @@ -100,7 +100,7 @@ func TestClient_StartMessageReceiver_MultipleMessages(t *testing.T) { t.Errorf("Could not generate Keys: %+v", err) } - io.SessionV2.SetRegState(user.KeyGenComplete) + client.sessionV2.SetRegState(user.KeyGenComplete) // Register with a valid registration code _, err = client.RegisterWithPermissioning(true, ValidRegCode) @@ -144,8 +144,8 @@ func TestClient_StartMessageReceiver_MultipleMessages(t *testing.T) { func TestRegister_ValidPrecannedRegCodeReturnsZeroID(t *testing.T) { // Initialize client with dummy storage - storage := DummyStorage{LocationA: ".ekv-validprecanned0return", StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&storage, ".ekv-validprecanned0return", "", def) + storage := DummyStorage{LocationA: ".ekv-validprecanned0return/a", StoreA: []byte{'a', 'b', 'c'}} + client, err := NewClient(&storage, ".ekv-validprecanned0return/a", "", def) if err != nil { t.Errorf("Failed to initialize dummy client: %s", err.Error()) } @@ -156,7 +156,7 @@ func TestRegister_ValidPrecannedRegCodeReturnsZeroID(t *testing.T) { if err != nil { t.Errorf("Client failed of connect: %+v", err) } - err = client.GenerateKeys(nil, "") + err = client.GenerateKeys(nil, "password") if err != nil { t.Errorf("Could not generate Keys: %+v", err) } @@ -178,8 +178,8 @@ func TestRegister_ValidPrecannedRegCodeReturnsZeroID(t *testing.T) { // Verify that registering with an invalid registration code will fail func TestRegister_InvalidPrecannedRegCodeReturnsError(t *testing.T) { // Initialize client with dummy storage - storage := DummyStorage{LocationA: ".ekv-invalidprecanerr", StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&storage, ".ekv-invalidprecanerr", "", def) + storage := DummyStorage{LocationA: ".ekv-invalidprecanerr/a", StoreA: []byte{'a', 'b', 'c'}} + client, err := NewClient(&storage, ".ekv-invalidprecanerr/a", "", def) if err != nil { t.Errorf("Failed to initialize dummy client: %s", err.Error()) } @@ -190,7 +190,7 @@ func TestRegister_InvalidPrecannedRegCodeReturnsError(t *testing.T) { t.Errorf("Client failed of connect: %+v", err) } //Generate keys s.t. reg status is prepped for registration - err = client.GenerateKeys(nil, "") + err = client.GenerateKeys(nil, "password") if err != nil { t.Errorf("Could not generate Keys: %+v", err) } @@ -208,8 +208,8 @@ func TestRegister_InvalidPrecannedRegCodeReturnsError(t *testing.T) { //Test that not running generateKeys results in an error. Without running the aforementioned function, // the registration state should be invalid and it should not run func TestRegister_InvalidRegState(t *testing.T) { - storage := DummyStorage{LocationA: ".ekv-invalidregstate", StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&storage, ".ekv-invalidregstate", "", def) + dstorage := DummyStorage{LocationA: ".ekv-invalidregstate/a", StoreA: []byte{'a', 'b', 'c'}} + client, err := NewClient(&dstorage, ".ekv-invalidregstate/a", "", def) if err != nil { t.Errorf("Failed to initialize dummy client: %s", err.Error()) } @@ -235,6 +235,7 @@ func TestRegister_InvalidRegState(t *testing.T) { cmixPrivKey, cmixPubKey, err := generateCmixKeys(cmixGrp) client.session = user.NewSession(nil, usr, pubKey, privKey, cmixPubKey, cmixPrivKey, e2ePubKey, e2ePrivKey, salt, cmixGrp, e2eGrp, "") + client.sessionV2, _ = storage.Init(".ekv-invalidregstate", "password") // _, err = client.RegisterWithPermissioning(false, ValidRegCode) @@ -246,8 +247,8 @@ func TestRegister_InvalidRegState(t *testing.T) { func TestRegister_DeletedUserReturnsErr(t *testing.T) { // Initialize client with dummy storage - storage := DummyStorage{LocationA: ".ekv-deleteusererr", StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&storage, ".ekv-deleteusererr", "", def) + storage := DummyStorage{LocationA: ".ekv-deleteusererr/a", StoreA: []byte{'a', 'b', 'c'}} + client, err := NewClient(&storage, ".ekv-deleteusererr/a", "", def) if err != nil { t.Errorf("Failed to initialize dummy client: %s", err.Error()) } @@ -262,7 +263,7 @@ func TestRegister_DeletedUserReturnsErr(t *testing.T) { // ... tempUser, _ := user.Users.GetUser(id.NewIdFromUInt(5, id.User, t)) user.Users.DeleteUser(id.NewIdFromUInt(5, id.User, t)) - err = client.GenerateKeys(nil, "") + err = client.GenerateKeys(nil, "password") if err != nil { t.Errorf("Could not generate Keys: %+v", err) } @@ -281,8 +282,8 @@ func TestRegister_DeletedUserReturnsErr(t *testing.T) { func TestSend(t *testing.T) { // Initialize client with dummy storage - storage := DummyStorage{LocationA: ".ekv-sendtest", StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&storage, ".ekv-sendtest", "", def) + storage := DummyStorage{LocationA: ".ekv-sendtest/a", StoreA: []byte{'a', 'b', 'c'}} + client, err := NewClient(&storage, ".ekv-sendtest/a", "", def) if err != nil { t.Errorf("Failed to initialize dummy client: %s", err.Error()) } @@ -371,8 +372,8 @@ func TestSend(t *testing.T) { func TestLogout(t *testing.T) { // Initialize client with dummy storage - storage := DummyStorage{LocationA: ".ekv-logout", StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&storage, ".ekv-logout", "", def) + storage := DummyStorage{LocationA: ".ekv-logout/a", StoreA: []byte{'a', 'b', 'c'}} + client, err := NewClient(&storage, ".ekv-logout/a", "", def) if err != nil { t.Errorf("Failed to initialize dummy client: %s", err.Error()) } diff --git a/api/notifications_test.go b/api/notifications_test.go index 76f836cb025954d99370ac9e21ed2c054f0ef4c7..c3c72945ad065f35814ac7c905f87a316aa247a1 100644 --- a/api/notifications_test.go +++ b/api/notifications_test.go @@ -61,8 +61,8 @@ jR+QSAa9eEozCngV6LUagC0YYWDZ // Happy path func TestClient_RegisterForNotifications(t *testing.T) { // Initialize client with dummy storage - storage := DummyStorage{LocationA: ".ekv-registernotifications", StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&storage, ".ekv-registernotifications", "", def) + storage := DummyStorage{LocationA: ".ekv-registernotifications/a", StoreA: []byte{'a', 'b', 'c'}} + client, err := NewClient(&storage, ".ekv-registernotifications/a", "", def) if err != nil { t.Errorf("Failed to initialize dummy client: %s", err.Error()) return @@ -96,8 +96,8 @@ func TestClient_RegisterForNotifications(t *testing.T) { // Happy path func TestClient_UnregisterForNotifications(t *testing.T) { // Initialize client with dummy storage - storage := DummyStorage{LocationA: ".ekv-unregisternotifications", StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&storage, ".ekv-unregisternotifications", "", def) + storage := DummyStorage{LocationA: ".ekv-unregisternotifications/a", StoreA: []byte{'a', 'b', 'c'}} + client, err := NewClient(&storage, ".ekv-unregisternotifications/a", "", def) if err != nil { t.Errorf("Failed to initialize dummy client: %s", err.Error()) } diff --git a/api/register.go b/api/register.go index 7af4fea2bbf0c52025427f6391b3f62cd5db5f66..9103b8b0c9c0f196dbfe86c2073f9c557162133a 100644 --- a/api/register.go +++ b/api/register.go @@ -12,7 +12,6 @@ import ( "github.com/pkg/errors" "gitlab.com/elixxir/client/bots" "gitlab.com/elixxir/client/globals" - "gitlab.com/elixxir/client/io" "gitlab.com/elixxir/client/user" "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/hash" @@ -32,7 +31,7 @@ const SaltSize = 32 // Returns an error if registration fails. func (cl *Client) RegisterWithPermissioning(preCan bool, registrationCode string) (*id.ID, error) { //Check the regState is in proper state for registration - regState, err := io.SessionV2.GetRegState() + regState, err := cl.sessionV2.GetRegState() if err != nil { return nil, err } @@ -67,7 +66,7 @@ func (cl *Client) RegisterWithPermissioning(preCan bool, registrationCode string } //update the state - err = io.SessionV2.SetRegState(user.PermissioningComplete) + err = cl.sessionV2.SetRegState(user.PermissioningComplete) if err != nil { return &id.ZeroUser, err } @@ -80,12 +79,12 @@ func (cl *Client) RegisterWithPermissioning(preCan bool, registrationCode string return &id.ZeroUser, err } //update the session with the registration - err = io.SessionV2.SetRegState(user.PermissioningComplete) + err = cl.sessionV2.SetRegState(user.PermissioningComplete) if err != nil { return nil, err } - err = io.SessionV2.SetRegValidationSig(regValidationSignature) + err = cl.sessionV2.SetRegValidationSig(regValidationSignature) if err != nil { return nil, err } @@ -109,7 +108,7 @@ func (cl *Client) RegisterWithPermissioning(preCan bool, registrationCode string // User discovery. Must be called after Register and InitNetwork. // It will fail if the user has already registered with UDB func (cl *Client) RegisterWithUDB(username string, timeout time.Duration) error { - regState, err := io.SessionV2.GetRegState() + regState, err := cl.sessionV2.GetRegState() if err != nil { return err } @@ -141,7 +140,7 @@ func (cl *Client) RegisterWithUDB(username string, timeout time.Duration) error } //set the registration state - err = io.SessionV2.SetRegState(user.UDBComplete) + err = cl.sessionV2.SetRegState(user.UDBComplete) if err != nil { return errors.Wrap(err, "UDB Registration Failed") } @@ -180,7 +179,7 @@ func (cl *Client) RegisterWithNodes() error { UID := session.GetCurrentUser().User usr := session.GetCurrentUser() //Load the registration signature - regSignature, err := io.SessionV2.GetRegValidationSig() + regSignature, err := cl.sessionV2.GetRegValidationSig() if err != nil && !os.IsNotExist(err) { return errors.Errorf("Failed to get registration signature: %v", err) } @@ -208,7 +207,7 @@ func (cl *Client) RegisterWithNodes() error { //update the session with the registration //HACK HACK HACK sesObj := cl.session.(*user.SessionObj) - err = io.SessionV2.SetRegValidationSig(regSignature) + err = cl.sessionV2.SetRegValidationSig(regSignature) if err != nil { return err } @@ -224,8 +223,10 @@ func (cl *Client) RegisterWithNodes() error { var wg sync.WaitGroup errChan := make(chan error, len(cl.ndf.Gateways)) - //Get the registered node keys - registeredNodes := session.GetNodes() + registeredNodes, err := cl.sessionV2.GetNodeKeys() + if err != nil { + return err + } salt := session.GetSalt() @@ -237,10 +238,10 @@ func (cl *Client) RegisterWithNodes() error { localI := i nodeID, err := id.Unmarshal(cl.ndf.Nodes[i].ID) if err != nil { - return err + return nil } //Register with node if the node has not been registered with already - if _, ok := registeredNodes[*nodeID]; !ok { + if _, ok := registeredNodes[nodeID.String()]; !ok { wg.Add(1) newRegistrations = true go func() { diff --git a/api/register_test.go b/api/register_test.go index dad3b6e8bef3fb297deb22328315eda915330b49..5e0c5edddd1f2671bf2e98f0106213724cb5b3e7 100644 --- a/api/register_test.go +++ b/api/register_test.go @@ -6,7 +6,6 @@ package api import ( - "gitlab.com/elixxir/client/globals" "gitlab.com/elixxir/client/io" "gitlab.com/elixxir/client/user" "gitlab.com/xx_network/primitives/id" @@ -16,7 +15,8 @@ import ( //Test that a registered session may be stored & recovered func TestRegistrationGob(t *testing.T) { // Get a Client - testClient, err := NewClient(&globals.RamStorage{}, ".ekv-registergob", "", def) + storage := DummyStorage{LocationA: ".ekv-registergob/a", StoreA: []byte{'a', 'b', 'c'}} + testClient, err := NewClient(&storage, ".ekv-registergob/a", "", def) if err != nil { t.Error(err) } @@ -58,7 +58,8 @@ func TestRegistrationGob(t *testing.T) { //Happy path for a non precen user func TestClient_Register(t *testing.T) { //Make mock client - testClient, err := NewClient(&globals.RamStorage{}, ".ekv-clientregister", "", def) + storage := DummyStorage{LocationA: ".ekv-clientregister/a", StoreA: []byte{'a', 'b', 'c'}} + testClient, err := NewClient(&storage, ".ekv-clientregister/a", "", def) if err != nil { t.Error(err) @@ -75,7 +76,7 @@ func TestClient_Register(t *testing.T) { } // fixme please (and all other places where this call is above RegisterWithPermissioning in tests) - io.SessionV2.SetRegState(user.KeyGenComplete) + testClient.sessionV2.SetRegState(user.KeyGenComplete) // populate a gob in the store _, err = testClient.RegisterWithPermissioning(true, "WTROXJ33") if err != nil { @@ -113,8 +114,8 @@ func VerifyRegisterGobUser(session user.Session, t *testing.T) { // Verify that a valid precanned user can register func TestRegister_ValidRegParams___(t *testing.T) { // Initialize client with dummy storage - storage := DummyStorage{LocationA: "Blah", StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&storage, ".ekv-validregparams", "", def) + storage := DummyStorage{LocationA: ".ekv-validregparams/a", StoreA: []byte{'a', 'b', 'c'}} + client, err := NewClient(&storage, ".ekv-validregparams/a", "", def) if err != nil { t.Errorf("Failed to initialize dummy client: %s", err.Error()) } diff --git a/bindings/client.go b/bindings/client.go index 834bbfd3bd750259edd1313b99005efb69a18c0a..9625981e614043d2c7fc8b6487c3b3f6c514cfe4 100644 --- a/bindings/client.go +++ b/bindings/client.go @@ -13,7 +13,6 @@ import ( "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/api" "gitlab.com/elixxir/client/globals" - clientIo "gitlab.com/elixxir/client/io" "gitlab.com/elixxir/client/parse" "gitlab.com/elixxir/client/user" "gitlab.com/elixxir/crypto/csprng" @@ -257,7 +256,7 @@ func (cl *Client) backoff(backoffCount int) { func (cl *Client) ChangeUsername(un string) error { globals.Log.INFO.Printf("Binding call: ChangeUsername()\n"+ " username: %s", un) - regState, err := clientIo.SessionV2.GetRegState() + regState, err := cl.client.GetSessionV2().GetRegState() if err != nil { return errors.New(fmt.Sprintf("Could not get reg state: %v", err)) } diff --git a/bindings/client_test.go b/bindings/client_test.go index a0fbf4ba73d9b795aa670e53e2d7c987407a5900..f4d8bb1d00770a5c0b935b44a34d5830b95c156e 100644 --- a/bindings/client_test.go +++ b/bindings/client_test.go @@ -83,7 +83,7 @@ func (i *MockRegistration) CheckRegistration(msg *pb.RegisteredNodeCheck) (*pb.R // Setups general testing params and calls test wrapper func TestMain(m *testing.M) { - io.SessionV2, _ = storage.Init(".ekvbindings", "test") + io.SessionV2, _ = storage.Init(".ekvbindings/a", "test") os.Exit(testMainWrapper(m)) } @@ -92,12 +92,12 @@ func TestNewClientNil(t *testing.T) { ndfStr, pubKey := getNDFJSONStr(def, t) - _, err := NewClient(nil, ".ekv-bindings-newclientnil", "", ndfStr, pubKey) + _, err := NewClient(nil, ".ekv-bindings-newclientnil/a", "", ndfStr, pubKey) if err == nil { t.Errorf("NewClient returned nil on invalid (nil, nil) input!") } - _, err = NewClient(nil, ".ekv-bindings-newclientnil2", "", "", "hello") + _, err = NewClient(nil, ".ekv-bindings-newclientnil2/a", "", "", "hello") if err == nil { t.Errorf("NewClient returned nil on invalid (nil, 'hello') input!") } @@ -109,7 +109,7 @@ func TestNewClient(t *testing.T) { ndfStr, pubKey := getNDFJSONStr(def, t) - client, err := NewClient(&d, ".ekv-bindings-testnewclient", "", ndfStr, pubKey) + client, err := NewClient(&d, ".ekv-bindings-testnewclient/a", "", ndfStr, pubKey) if err != nil { t.Errorf("NewClient returned error: %v", err) } else if client == nil { @@ -125,8 +125,8 @@ func TestRegister(t *testing.T) { ndfStr, pubKey := getNDFJSONStr(def, t) - d := DummyStorage{LocationA: ".ekv-bindings-testreg", StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&d, ".ekv-bindings-testreg", "", ndfStr, pubKey) + d := DummyStorage{LocationA: ".ekv-bindings-testreg/a", StoreA: []byte{'a', 'b', 'c'}} + client, err := NewClient(&d, ".ekv-bindings-testreg/a", "", ndfStr, pubKey) if err != nil { t.Errorf("Failed to marshal group JSON: %s", err) } @@ -170,9 +170,9 @@ func TestClient_ChangeUsername_ErrorPath(t *testing.T) { }() ndfStr, pubKey := getNDFJSONStr(def, t) - d := DummyStorage{LocationA: ".ekv-bindings-changeusername-err", StoreA: []byte{'a', 'b', 'c'}} + d := DummyStorage{LocationA: ".ekv-bindings-changeusername-err/a", StoreA: []byte{'a', 'b', 'c'}} - testClient, err := NewClient(&d, ".ekv-bindings-changeusername-err", "", ndfStr, pubKey) + testClient, err := NewClient(&d, ".ekv-bindings-changeusername-err/a", "", ndfStr, pubKey) if err != nil { t.Errorf("Failed to marshal group JSON: %s", err) } @@ -193,9 +193,9 @@ func TestClient_ChangeUsername_ErrorPath(t *testing.T) { func TestClient_ChangeUsername(t *testing.T) { ndfStr, pubKey := getNDFJSONStr(def, t) - d := DummyStorage{LocationA: ".ekv-bindings-changeuser", StoreA: []byte{'a', 'b', 'c'}} + d := DummyStorage{LocationA: ".ekv-bindings-changeuser/a", StoreA: []byte{'a', 'b', 'c'}} - testClient, err := NewClient(&d, ".ekv-bindings-changeuser", "", ndfStr, pubKey) + testClient, err := NewClient(&d, ".ekv-bindings-changeuser/a", "", ndfStr, pubKey) if err != nil { t.Errorf("Failed to marshal group JSON: %s", err) } @@ -205,12 +205,15 @@ func TestClient_ChangeUsername(t *testing.T) { t.Errorf("Could not connect: %+v", err) } - err = testClient.client.GenerateKeys(nil, "") + err = testClient.client.GenerateKeys(nil, "password") if err != nil { t.Errorf("Could not generate Keys: %+v", err) } regRes, err := testClient.RegisterWithPermissioning(false, ValidRegCode) + if err != nil { + t.Errorf("%+v", err) + } if len(regRes) == 0 { t.Errorf("Invalid registration number received: %v", regRes) } @@ -225,9 +228,9 @@ func TestClient_ChangeUsername(t *testing.T) { func TestClient_StorageIsEmpty(t *testing.T) { ndfStr, pubKey := getNDFJSONStr(def, t) - d := DummyStorage{LocationA: ".ekv-bindings-emptystorage", StoreA: []byte{'a', 'b', 'c'}} + d := DummyStorage{LocationA: ".ekv-bindings-emptystorage/a", StoreA: []byte{'a', 'b', 'c'}} - testClient, err := NewClient(&d, ".ekv-bindings-emptystorage", "", ndfStr, pubKey) + testClient, err := NewClient(&d, ".ekv-bindings-emptystorage/a", "", ndfStr, pubKey) if err != nil { t.Errorf("Failed to marshal group JSON: %s", err) } @@ -256,9 +259,9 @@ func TestClient_StorageIsEmpty(t *testing.T) { func TestDeleteUsername_EmptyContactList(t *testing.T) { ndfStr, pubKey := getNDFJSONStr(def, t) - d := DummyStorage{LocationA: ".ekv-bindings-emptycontacts", StoreA: []byte{'a', 'b', 'c'}} + d := DummyStorage{LocationA: ".ekv-bindings-emptycontacts/a", StoreA: []byte{'a', 'b', 'c'}} - testClient, err := NewClient(&d, ".ekv-bindings-emptycontacts", "", ndfStr, pubKey) + testClient, err := NewClient(&d, ".ekv-bindings-emptycontacts/a", "", ndfStr, pubKey) if err != nil { t.Errorf("Failed to marshal group JSON: %s", err) } @@ -290,8 +293,8 @@ func TestDeleteUsername_EmptyContactList(t *testing.T) { func TestClient_GetRegState(t *testing.T) { ndfStr, pubKey := getNDFJSONStr(def, t) - d := DummyStorage{LocationA: ".ekv-bindings-getregstate", StoreA: []byte{'a', 'b', 'c'}} - testClient, err := NewClient(&d, ".ekv-bindings-getregstate", "", ndfStr, pubKey) + d := DummyStorage{LocationA: ".ekv-bindings-getregstate/a", StoreA: []byte{'a', 'b', 'c'}} + testClient, err := NewClient(&d, ".ekv-bindings-getregstate/a", "", ndfStr, pubKey) if err != nil { t.Errorf("Failed to marshal group JSON: %s", err) } @@ -332,8 +335,8 @@ func TestClient_GetRegState(t *testing.T) { func TestClient_Send(t *testing.T) { ndfStr, pubKey := getNDFJSONStr(def, t) - d := DummyStorage{LocationA: ".ekv-bindings-send", StoreA: []byte{'a', 'b', 'c'}} - testClient, err := NewClient(&d, ".ekv-bindings-send", "", ndfStr, pubKey) + d := DummyStorage{LocationA: ".ekv-bindings-send/a", StoreA: []byte{'a', 'b', 'c'}} + testClient, err := NewClient(&d, ".ekv-bindings-send/a", "", ndfStr, pubKey) if err != nil { t.Errorf("Failed to marshal group JSON: %s", err) @@ -416,8 +419,8 @@ func TestLoginLogout(t *testing.T) { ndfStr, pubKey := getNDFJSONStr(def, t) - d := DummyStorage{LocationA: ".ekv-bindings-loginlogout", StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&d, ".ekv-bindings-loginlogout", "", ndfStr, pubKey) + d := DummyStorage{LocationA: ".ekv-bindings-loginlogout/a", StoreA: []byte{'a', 'b', 'c'}} + client, err := NewClient(&d, ".ekv-bindings-loginlogout/a", "", ndfStr, pubKey) if err != nil { t.Errorf("Error starting client: %+v", err) } @@ -467,8 +470,8 @@ func TestListen(t *testing.T) { ndfStr, pubKey := getNDFJSONStr(def, t) - d := DummyStorage{LocationA: ".ekv-testlisten", StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&d, ".ekv-testlisten", "", ndfStr, pubKey) + d := DummyStorage{LocationA: ".ekv-testlisten/a", StoreA: []byte{'a', 'b', 'c'}} + client, err := NewClient(&d, ".ekv-testlisten/a", "", ndfStr, pubKey) // InitNetwork to gateway err = client.InitNetwork() @@ -512,8 +515,8 @@ func TestStopListening(t *testing.T) { ndfStr, pubKey := getNDFJSONStr(def, t) - d := DummyStorage{LocationA: ".ekv-teststoplistening", StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&d, ".ekv-teststoplistening", "", ndfStr, pubKey) + d := DummyStorage{LocationA: ".ekv-teststoplistening/a", StoreA: []byte{'a', 'b', 'c'}} + client, err := NewClient(&d, ".ekv-teststoplistening/a", "", ndfStr, pubKey) if err != nil { t.Errorf("Failed to create client: %+v", err) } diff --git a/crypto/encrypt.go b/crypto/encrypt.go index 8c39105409b7a0e461493b00cfaf846be31925cd..278d142d50ac681aac1a25b262fcbff401c88f78 100644 --- a/crypto/encrypt.go +++ b/crypto/encrypt.go @@ -27,7 +27,7 @@ var SessionV2 *storage.Session func CMIXEncrypt(session user.Session, topology *connect.Circuit, salt []byte, msg *format.Message) (*format.Message, [][]byte) { // Generate the encryption key - nodeKeys, err := SessionV2.GetNodeKeys(topology) + nodeKeys, err := SessionV2.GetNodeKeysFromCircuit(topology) if err != nil { globals.Log.FATAL.Panicf("could not get nodeKeys: %+v", err) } diff --git a/io/send.go b/io/send.go index b9965f8ec8135016219244eeb6a6f65d6cc89b0c..ff3146ae6b6b34d6bcda0770e454768cb445e97f 100644 --- a/io/send.go +++ b/io/send.go @@ -150,7 +150,7 @@ func (rm *ReceptionManager) send(session user.Session, topology *connect.Circuit } // Retrieve the base key for the zeroeth node - nodeKeys, err := SessionV2.GetNodeKeys(topology) + nodeKeys, err := SessionV2.GetNodeKeysFromCircuit(topology) if err != nil { globals.Log.ERROR.Printf("could not get nodeKeys: %+v", err) return err diff --git a/storage/session.go b/storage/session.go index b0b9e2ee23cfcd2e6177fb2c885921a49aad437d..81d1051917ebf621ab1dae57aaa0d55defedf3fb 100644 --- a/storage/session.go +++ b/storage/session.go @@ -70,8 +70,8 @@ func (s *Session) SetLastMessageId(id string) error { return s.kv.Set("LastMessageID", vo) } -// Helper for obtaining NodeKeys map -func (s *Session) getNodeKeys() (map[string]user.NodeKeys, error) { +// GetNodeKeys returns all keys +func (s *Session) GetNodeKeys() (map[string]user.NodeKeys, error) { key := "NodeKeys" var nodeKeys map[string]user.NodeKeys @@ -116,9 +116,10 @@ func (s *Session) getNodeKeys() (map[string]user.NodeKeys, error) { return nodeKeys, err } -// Obtain NodeKeys from the Session -func (s *Session) GetNodeKeys(topology *connect.Circuit) ([]user.NodeKeys, error) { - nodeKeys, err := s.getNodeKeys() +// GetNodeKeysFromCircuit obtains NodeKeys for a given circuit +func (s *Session) GetNodeKeysFromCircuit(topology *connect.Circuit) ( + []user.NodeKeys, error) { + nodeKeys, err := s.GetNodeKeys() if err != nil { return nil, err } @@ -135,7 +136,7 @@ func (s *Session) GetNodeKeys(topology *connect.Circuit) ([]user.NodeKeys, error // Set NodeKeys in the Session func (s *Session) PushNodeKey(id *id.ID, key user.NodeKeys) error { // Obtain NodeKeys map - nodeKeys, err := s.getNodeKeys() + nodeKeys, err := s.GetNodeKeys() if err != nil { return err } diff --git a/storage/session_test.go b/storage/session_test.go index 28b782cbf15e8d0f7184275a1c2ea4e28ea2025b..8ff1a8eeea44c41e35ed5e282edfbf111ecb846f 100644 --- a/storage/session_test.go +++ b/storage/session_test.go @@ -102,7 +102,7 @@ func TestSession_GetPushNodeKeys(t *testing.T) { } circ := connect.NewCircuit([]*id.ID{testId, testId2}) - results, err := s.GetNodeKeys(circ) + results, err := s.GetNodeKeysFromCircuit(circ) if len(results) != 2 { t.Errorf("Returned unexpected number of node keys: %d", len(results)) diff --git a/user/session.go b/user/session.go index e15a35d681b25c5afab0e2d035d757686a0d0525..89e9bb9d02823a1c4cdf92d1ca8a1ac07b321f77 100644 --- a/user/session.go +++ b/user/session.go @@ -57,7 +57,6 @@ type Session interface { UnlockStorage() GetSessionData() ([]byte, error) GetRegistrationValidationSignature() []byte - GetNodes() map[id.ID]int AppendGarbledMessage(messages ...*format.Message) PopGarbledMessages() []*format.Message GetSalt() []byte @@ -345,16 +344,6 @@ func (s *SessionObj) SetLastMessageID(id string) { s.UnlockStorage() } -func (s *SessionObj) GetNodes() map[id.ID]int { - s.LockStorage() - defer s.UnlockStorage() - nodes := make(map[id.ID]int, 0) - for node := range s.NodeKeys { - nodes[node] = 1 - } - return nodes -} - func (s *SessionObj) GetSalt() []byte { s.LockStorage() defer s.UnlockStorage()