Skip to content
Snippets Groups Projects
Commit 60cf841a authored by Spencer Brown's avatar Spencer Brown
Browse files

Fix implementation of nick list lookup for user map

parent e6e893f2
No related branches found
No related tags found
No related merge requests found
File mode changed from 100644 to 100755
......@@ -9,6 +9,7 @@ package globals
import (
pb "gitlab.com/privategrity/comms/mixmessages"
"gitlab.com/privategrity/crypto/cyclic"
"github.com/spf13/jwalterweatherman"
)
// Globally initiated UserRegistry
......@@ -78,6 +79,7 @@ func (u *User) DeepCopy() *User {
nu.Id = u.Id
nu.Address = u.Address
nu.Nick = u.Nick
nu.Transmission = *u.Transmission.DeepCopy()
......@@ -93,7 +95,7 @@ func (u *User) DeepCopy() *User {
// NewUser creates a new User object with default fields and given address.
func (m *UserMap) NewUser(address string) *User {
idCounter++
return &User{Id: idCounter - 1, Address: address,
return &User{Id: idCounter - 1, Address: address, Nick: "",
// TODO: each user should have unique base and secret keys
Transmission: ForwardKey{BaseKey: cyclic.NewIntFromString(
"c1248f42f8127999e07c657896a26b56fd9a499c6199e1265053132451128f52", 16),
......@@ -135,6 +137,7 @@ func (m *UserMap) CountUsers() int {
}
func (m *UserMap) GetNickList() (ids []uint64, nicks []string) {
userCount := m.CountUsers()
nicks = make([]string, 0, userCount)
......@@ -143,6 +146,8 @@ func (m *UserMap) GetNickList() (ids []uint64, nicks []string) {
if user != nil {
nicks = append(nicks, user.Nick)
ids = append(ids, user.Id)
} else {
jwalterweatherman.FATAL.Panicf("A user was nil.")
}
}
......
......@@ -136,6 +136,7 @@ func (m *UserDatabase) UpsertUser(user *User) {
// On conflict, update the user's fields
OnConflict("(id) DO UPDATE").
Set("address = EXCLUDED.address," +
"nick = EXCLUDED.nick," +
"transmission_base_key = EXCLUDED.transmission_base_key," +
"transmission_recursive_key = EXCLUDED.transmission_recursive_key," +
"reception_base_key = EXCLUDED.reception_base_key," +
......@@ -144,8 +145,7 @@ func (m *UserDatabase) UpsertUser(user *User) {
// Otherwise, insert the new user
Insert()
if err != nil {
jww.FATAL.Printf("Unable to upsert user %d!", user.Id)
panic(err)
jww.FATAL.Panicf("Unable to upsert user %d! %s", user.Id, err.Error())
}
}
......@@ -153,8 +153,7 @@ func (m *UserDatabase) UpsertUser(user *User) {
func (m *UserDatabase) CountUsers() int {
count, err := m.db.Model(&UserDB{}).Count()
if err != nil {
jww.FATAL.Println("Unable to count users!")
panic(err)
jww.FATAL.Panicf("Unable to count users! %s", err.Error())
}
return count
}
......@@ -167,8 +166,7 @@ func (m *UserDatabase) GetNickList() (ids []uint64, nicks []string) {
err := m.db.Model(&model).Column("id", "nick").Select(&ids, &nicks)
if err != nil {
jww.FATAL.Println("Unable to get contact list!")
panic(err)
jww.FATAL.Panicf("Unable to get contact list! %s", err.Error())
}
return ids, nicks
......
......@@ -15,6 +15,7 @@ import (
func TestUserRegistry(t *testing.T) {
testUser := Users.NewUser("Someplace")
testUser.Nick = "Me"
numUsers := Users.CountUsers()
Users.DeleteUser(testUser.Id)
Users.UpsertUser(testUser)
......@@ -25,13 +26,17 @@ func TestUserRegistry(t *testing.T) {
}
getUser.Transmission.RecursiveKey.SetInt64(5)
getUser.Nick = "Michael"
Users.UpsertUser(getUser)
getUser2, _ := Users.GetUser(testUser.Id)
if getUser2.Transmission.RecursiveKey.Int64() != 5 {
t.Errorf("UpsertUser: User did not save!")
if getUser2.Transmission.RecursiveKey.Int64() != 5 || getUser2.
Nick != "Michael" {
t.Errorf("UpsertUser: User did not save! Got: %v, %v; expected: %v, " +
"%v", getUser2.Transmission.RecursiveKey.Int64(), getUser2.Nick,
5, "Michael")
}
Users.DeleteUser(testUser.Id)
......
......@@ -22,8 +22,8 @@ func (s ServerImpl) RequestContactList(inputMsg *pb.
idList, nickList := globals.Users.GetNickList()
for i := 0; i < userCount; i++ {
contactList.Contacts[i].Nick = nickList[i]
contactList.Contacts[i].UserID = idList[i]
contactList.Contacts[i] = &pb.Contact{Nick: nickList[i],
UserID: idList[i]}
}
return &contactList
......
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2018 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package io
import (
"testing"
"gitlab.com/privategrity/server/globals"
pb "gitlab.com/privategrity/comms/mixmessages"
"gitlab.com/privategrity/comms/mixclient"
)
// MARK
func TestRequestContactList(t *testing.T) {
// empty user registry from previous tests so we can make sure that our
// user's first in the map
globals.Users.DeleteUser(1)
user := globals.Users.NewUser("test") // Create user
user.Nick = "Michael"
globals.Users.UpsertUser(user) // Insert user into registry
user = globals.Users.NewUser("test")
user.Nick = "Me"
globals.Users.UpsertUser(user)
// Currently we just return all the nicks
contacts, err := mixclient.RequestContactList(NextServer, &pb.ContactPoll{})
if err != nil {
t.Errorf("RequestContactList() returned an error: %v", err.Error())
}
expectedNicks := []string{"Michael", "Me"}
for i := 0; i < len(expectedNicks); i++ {
if contacts.Contacts[i] == nil {
t.Errorf("Got a nil nick at index %v.", i)
} else if expectedNicks[i] != contacts.Contacts[i].Nick {
t.Errorf("Got an unexpected nick at index %v. Got: %v, " +
"expected: %v", i, contacts.Contacts[i].Nick, expectedNicks[i])
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment