diff --git a/cmd/ud.go b/cmd/ud.go index afd4428e6dbd5e86033c1c6cd56576fcda057e8a..4fa083d32cdd3641f2c333fd63a2e47cf36e1af2 100644 --- a/cmd/ud.go +++ b/cmd/ud.go @@ -243,10 +243,10 @@ func init() { } func printContact(c contact.Contact) { - jww.DEBUG.Printf("Printing client: %+v", c) + jww.DEBUG.Printf("Printing contact: %+v", c) cBytes := c.Marshal() if len(cBytes) == 0 { - jww.ERROR.Print("Marshaled client has a size of 0.") + jww.ERROR.Print("Marshaled contact has a size of 0.") } else { jww.DEBUG.Printf("Printing marshaled contact of size %d.", len(cBytes)) } diff --git a/ud/search.go b/ud/search.go index 8c95c08662cdbb574e11c7291a95487ec582af24..86a19551b5af2c7bc95c0736845263e91d17da0e 100644 --- a/ud/search.go +++ b/ud/search.go @@ -74,7 +74,7 @@ func (m *Manager) searchResponseHandler(factMap map[string]fact.Fact, } //return an error if no facts are found - if len(searchResponse.Contacts)==0{ + if len(searchResponse.Contacts) == 0 { go callback(nil, errors.New("No contacts found in search")) } @@ -119,12 +119,15 @@ func (m *Manager) parseContacts(response []*Contact, if err != nil { return nil, errors.Errorf("failed to parse Contact user ID: %+v", err) } - + var facts []fact.Fact + if c.Username != "" { + facts = []fact.Fact{{c.Username, fact.Username}} + } // Create new Contact contacts[i] = contact.Contact{ ID: uid, DhPubKey: m.grp.NewIntFromBytes(c.PubKey), - Facts: []fact.Fact{}, + Facts: facts, } // Assign each Fact with a matching hash to the Contact diff --git a/ud/search_test.go b/ud/search_test.go index 072862a0557ff034ecc8fc9fb9baea7180f3455d..c333cf08c5beede7a0ea8389b3adcbb2b4e18544 100644 --- a/ud/search_test.go +++ b/ud/search_test.go @@ -448,6 +448,46 @@ func TestManager_parseContacts(t *testing.T) { } } +func TestManager_parseContacts_username(t *testing.T) { + m := &Manager{grp: cyclic.NewGroup(large.NewInt(107), large.NewInt(2))} + + // Generate fact list + var factList fact.FactList + for i := 0; i < 10; i++ { + factList = append(factList, fact.Fact{ + Fact: fmt.Sprintf("fact %d", i), + T: fact.FactType(rand.Intn(4)), + }) + } + factHashes, factMap := hashFactList(factList) + + var contacts []*Contact + var expectedContacts []contact.Contact + for i, hash := range factHashes { + contacts = append(contacts, &Contact{ + UserID: id.NewIdFromString("user", id.User, t).Marshal(), + Username: "zezima", + PubKey: []byte{byte(i + 1)}, + TrigFacts: []*HashFact{hash}, + }) + expectedContacts = append(expectedContacts, contact.Contact{ + ID: id.NewIdFromString("user", id.User, t), + DhPubKey: m.grp.NewIntFromBytes([]byte{byte(i + 1)}), + Facts: fact.FactList{{"zezima", fact.Username}, factMap[string(hash.Hash)]}, + }) + } + + testContacts, err := m.parseContacts(contacts, factMap) + if err != nil { + t.Errorf("parseContacts() returned an error: %+v", err) + } + + if !reflect.DeepEqual(expectedContacts, testContacts) { + t.Errorf("parseContacts() did not return the expected contacts."+ + "\nexpected: %+v\nreceived: %+v", expectedContacts, testContacts) + } +} + // Error path: provided contact IDs are malformed and cannot be unmarshaled. func TestManager_parseContacts_IdUnmarshalError(t *testing.T) { m := &Manager{grp: cyclic.NewGroup(large.NewInt(107), large.NewInt(2))}