diff --git a/ud/search.go b/ud/search.go index bfb754ec8f16fd65e260fc416cb3a2a124d4d0f7..86a19551b5af2c7bc95c0736845263e91d17da0e 100644 --- a/ud/search.go +++ b/ud/search.go @@ -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{{c.Username, fact.Username}}, + 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))}