Skip to content
Snippets Groups Projects
Commit da3d66f2 authored by Josh Brooks's avatar Josh Brooks
Browse files

Comment out tests

parent c7046984
No related branches found
No related tags found
2 merge requests!68Release,!49Restructure
package cmix
import (
"bytes"
"github.com/golang/protobuf/proto"
"gitlab.com/elixxir/client/single"
"gitlab.com/elixxir/client/stoppable"
"gitlab.com/elixxir/client/ud"
"gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/elixxir/crypto/e2e/singleUse"
"gitlab.com/elixxir/user-discovery-bot/storage"
"gitlab.com/xx_network/crypto/large"
"gitlab.com/xx_network/primitives/id"
"strings"
"testing"
"time"
)
func TestManager_lookupCallback(t *testing.T) {
m := &Manager{storage.NewTestDB(t), &mockSingleLookup{}}
uid := id.NewIdFromString("zezima", id.User, t)
grp := cyclic.NewGroup(large.NewInt(107), large.NewInt(2))
ct := single.NewContact(uid, grp.NewInt(42), grp.NewInt(43), singleUse.TagFP{}, 8)
err := m.db.InsertUser(&storage.User{Id: uid.Marshal(), DhPub: []byte("DhPub")})
if err != nil {
t.Errorf("Failed to insert dummy user: %+v", err)
}
lookupMsg := &ud.LookupSend{UserID: uid.Marshal()}
payload, err := proto.Marshal(lookupMsg)
if err != nil {
t.Errorf("Failed to marshal payload: %+v", err)
}
expectedPayload, err := proto.Marshal(m.handleLookup(lookupMsg, ct))
if err != nil {
t.Errorf("Failed to marshal message: %+v", err)
}
callbackChan := make(chan struct {
payload []byte
c single.Contact
})
callback := func(payload []byte, c single.Contact) {
callbackChan <- struct {
payload []byte
c single.Contact
}{payload: payload, c: c}
}
m.singleUse.RegisterCallback("", callback)
m.lookupCallback(payload, ct)
results := <-callbackChan
if !results.c.Equal(ct) {
t.Errorf("Callback did not return the expected contact."+
"\nexpected: %s\nreceived: %s", ct, results.c)
}
if !bytes.Equal(expectedPayload, results.payload) {
t.Errorf("Callback did not return the expected payload."+
"\nexpected: %+v\nreceived: %+v", expectedPayload, results.payload)
}
}
// Happy path.
func TestManager_handleLookup(t *testing.T) {
m := &Manager{db: storage.NewTestDB(t)}
uid := id.NewIdFromString("zezima", id.User, t)
c := single.NewContact(uid, &cyclic.Int{}, &cyclic.Int{}, singleUse.TagFP{}, 8)
username := "ZeZima"
expectedDhPub := "DhPub"
err := m.db.InsertUser(&storage.User{Id: uid.Marshal(), DhPub: []byte(expectedDhPub), Username: username, Facts: []storage.Fact{
{
Hash: []byte("hash"),
UserId: uid.Marshal(),
Fact: strings.ToLower(username),
Type: 0,
Signature: []byte("Signature"),
Verified: true,
},
}})
if err != nil {
t.Errorf("Failed to insert dummy user: %+v", err)
}
resp := m.handleLookup(&ud.LookupSend{UserID: uid.Marshal()}, c)
if resp.Error != "" {
t.Errorf("handleLookup() returned a response with an error: %s", resp.Error)
}
if string(resp.PubKey) != expectedDhPub {
t.Errorf("handleLookup() returned a response with inccorect PubKey."+
"\nexpected: %s\nreceived: %s", expectedDhPub, resp.Error)
}
if resp.Username != username {
t.Errorf("Should have gotten username %s, instead got: %s", username, resp.Username)
}
}
// Error path: Id is malformed and fails to unmarshal.
func TestManager_handleLookup_IdUnmarshalError(t *testing.T) {
m := &Manager{db: storage.NewTestDB(t)}
uid := id.NewIdFromString("zezima", id.User, t)
c := single.NewContact(uid, &cyclic.Int{}, &cyclic.Int{}, singleUse.TagFP{}, 8)
err := m.db.InsertUser(&storage.User{Id: uid.Marshal(), DhPub: []byte("DhPub")})
if err != nil {
t.Errorf("Failed to insert dummy user: %+v", err)
}
resp := m.handleLookup(&ud.LookupSend{UserID: []byte{1}}, c)
if !strings.Contains(resp.Error, "failed to unmarshal lookup ID in request") {
t.Errorf("handleLookup() returned a response with an error: %s", resp.Error)
}
}
//func TestManager_lookupCallback(t *testing.T) {
// m := &Manager{db: storage.NewTestDB(t),
// lookupListener: &mockSingleLookup{}}
// uid := id.NewIdFromString("zezima", id.User, t)
// grp := cyclic.NewGroup(large.NewInt(107), large.NewInt(2))
//
// dhPub := grp.NewInt(25)
//
// c := contact.Contact{
// ID: uid,
// DhPubKey: dhPub,
// }
//
// lm := &lookupManager{m: m}
//
// err := m.db.InsertUser(&storage.User{Id: uid.Marshal(), DhPub: dhPub.Bytes())}
// if err != nil {
// t.Errorf("Failed to insert dummy user: %+v", err)
// }
//
//
// lookupMsg := &ud.LookupSend{UserID: uid.Marshal()}
// payload, err := proto.Marshal(lookupMsg)
// if err != nil {
// t.Errorf("Failed to marshal payload: %+v", err)
// }
//
// single.Listen(ud.LookupTag, uid, payload, lm, single.GetDefaultRequestParams(), mockCMix, rng, grp)
//
// expectedPayload, err := proto.Marshal(lookupMsg)
// if err != nil {
// t.Errorf("Failed to marshal message: %+v", err)
// }
//
// callbackChan := make(chan struct {
// payload []byte
// c single.Contact
// })
// callback := func(payload []byte, c single.Contact) {
// callbackChan <- struct {
// payload []byte
// c single.Contact
// }{payload: payload, c: c}
// }
// m.singleUse.RegisterCallback("", callback)
//
// m.lookupCallback(payload, ct)
//
// results := <-callbackChan
// if !results.c.Equal(ct) {
// t.Errorf("Callback did not return the expected contact."+
// "\nexpected: %s\nreceived: %s", ct, results.c)
// }
// if !bytes.Equal(expectedPayload, results.payload) {
// t.Errorf("Callback did not return the expected payload."+
// "\nexpected: %+v\nreceived: %+v", expectedPayload, results.payload)
// }
//}
//
//// Happy path.
//func TestManager_handleLookup(t *testing.T) {
// m := &Manager{db: storage.NewTestDB(t)}
// uid := id.NewIdFromString("zezima", id.User, t)
//
// username := "ZeZima"
// expectedDhPub := "DhPub"
// err := m.db.InsertUser(
// &storage.User{Id: uid.Marshal(),
// DhPub: []byte(expectedDhPub),
// Username: username,
// Facts: []storage.Fact{
// {
// Hash: []byte("hash"),
// UserId: uid.Marshal(),
// Fact: strings.ToLower(username),
// Type: 0,
// Signature: []byte("Signature"),
// Verified: true,
// },
// }})
// if err != nil {
// t.Errorf("Failed to insert dummy user: %+v", err)
// }
//
// lookupManager := lookupManager{m: m}
//
// single.TransmitRequest()
//
// resp := lookupManager.handleLo okup(&ud.LookupSend{UserID: uid.Marshal()}, c)
// if resp.Error != "" {
// t.Errorf("handleLookup() returned a response with an error: %s", resp.Error)
// }
//
// if string(resp.PubKey) != expectedDhPub {
// t.Errorf("handleLookup() returned a response with inccorect PubKey."+
// "\nexpected: %s\nreceived: %s", expectedDhPub, resp.Error)
// }
//
// if resp.Username != username {
// t.Errorf("Should have gotten username %s, instead got: %s", username, resp.Username)
// }
//}
//
//// Error path: Id is malformed and fails to unmarshal.
//func TestManager_handleLookup_IdUnmarshalError(t *testing.T) {
// m := &Manager{db: storage.NewTestDB(t)}
// uid := id.NewIdFromString("zezima", id.User, t)
// c := single.NewContact(uid, &cyclic.Int{}, &cyclic.Int{}, singleUse.TagFP{}, 8)
//
// err := m.db.InsertUser(&storage.User{Id: uid.Marshal(), DhPub: []byte("DhPub")})
// if err != nil {
// t.Errorf("Failed to insert dummy user: %+v", err)
// }
//
// resp := m.handleLookup(&ud.LookupSend{UserID: []byte{1}}, c)
// if !strings.Contains(resp.Error, "failed to unmarshal lookup ID in request") {
// t.Errorf("handleLookup() returned a response with an error: %s", resp.Error)
// }
//}
// mockSingleLookup is used to test the lookup function, which uses the single-
// use manager. It adheres to the SingleInterface interface.
type mockSingleLookup struct {
callback func(payload []byte, c single.Contact)
}
func (s *mockSingleLookup) RegisterCallback(_ string, callback single.ReceiveComm) {
s.callback = callback
}
func (s *mockSingleLookup) RespondSingleUse(partner single.Contact, payload []byte, _ time.Duration) error {
go s.callback(payload, partner)
return nil
}
func (s *mockSingleLookup) Stop() {
func (s *mockSingleLookup) StartProcesses() (stoppable.Stoppable, error) {
return stoppable.NewSingle(""), nil
}
package cmix
import (
"bytes"
"github.com/golang/protobuf/proto"
"gitlab.com/elixxir/client/single"
"gitlab.com/elixxir/client/stoppable"
"gitlab.com/elixxir/client/ud"
"gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/elixxir/crypto/e2e/singleUse"
"gitlab.com/elixxir/primitives/fact"
"gitlab.com/elixxir/user-discovery-bot/storage"
"gitlab.com/xx_network/crypto/large"
"gitlab.com/xx_network/primitives/id"
"reflect"
"testing"
"time"
)
//func TestManager_SearchProcess(t *testing.T) {
// m := &Manager{storage.NewTestDB(t), &mockSingleSearch{}}
// uid := id.NewIdFromString("zezima", id.User, t)
// grp := cyclic.NewGroup(large.NewInt(107), large.NewInt(2))
// ct := single.NewContact(uid, grp.NewInt(42), grp.NewInt(43), singleUse.TagFP{}, 8)
// err := m.db.InsertUser(&storage.User{Id: uid.Marshal(), DhPub: []byte("DhPub")})
// if err != nil {
// t.Errorf("Failed to insert dummy user: %+v", err)
// }
//
// fid := []byte("factHash")
// searchMsg := &ud.SearchSend{Fact: []*ud.HashFact{{Hash: fid, Type: 0}}}
// payload, err := proto.Marshal(searchMsg)
// if err != nil {
// t.Errorf("Failed to marshal payload: %+v", err)
// }
//
// err = m.db.InsertFact(&storage.Fact{
// Hash: fid,
// UserId: uid.Marshal(),
// Fact: "water is wet",
// Type: 0,
// Verified: true,
// })
// if err != nil {
// t.Errorf("Failed to insert dummy fact: %+v", err)
// }
// expectedPayload, err := proto.Marshal(m.handleSearch(searchMsg, ct))
// if err != nil {
// t.Errorf("Failed to marshal message: %+v", err)
// }
//
// callbackChan := make(chan struct {
// payload []byte
// c single.Contact
// })
// callback := func(payload []byte, c single.Contact) {
// callbackChan <- struct {
// payload []byte
// c single.Contact
// }{payload: payload, c: c}
// }
// m.singleUse.RegisterCallback("", callback)
//
// m.searchCallback(payload, ct)
// results := <-callbackChan
// if !results.c.Equal(ct) {
// t.Errorf("Callback did not return the expected contact."+
// "\nexpected: %s\nreceived: %s", ct, results.c)
// }
// if !bytes.Equal(expectedPayload, results.payload) {
// t.Errorf("Callback did not return the expected payload."+
// "\nexpected: %+v\nreceived: %+v", expectedPayload, results.payload)
// }
//}
func TestManager_SearchProcess(t *testing.T) {
m := &Manager{storage.NewTestDB(t), &mockSingleSearch{}}
uid := id.NewIdFromString("zezima", id.User, t)
grp := cyclic.NewGroup(large.NewInt(107), large.NewInt(2))
ct := single.NewContact(uid, grp.NewInt(42), grp.NewInt(43), singleUse.TagFP{}, 8)
err := m.db.InsertUser(&storage.User{Id: uid.Marshal(), DhPub: []byte("DhPub")})
if err != nil {
t.Errorf("Failed to insert dummy user: %+v", err)
}
//func TestManager_handleSearch(t *testing.T) {
// m := &Manager{db: storage.NewTestDB(t)}
// uid := id.NewIdFromString("zezima", id.User, t)
// c := single.NewContact(uid, &cyclic.Int{}, &cyclic.Int{}, singleUse.TagFP{}, 8)
//
// expectedDhPub := []byte("DhPub")
// fid := []byte("factHash")
// f1 := &storage.Fact{
// Hash: fid,
// UserId: uid.Marshal(),
// Fact: "water is wet",
// Type: 1,
// Verified: true,
// }
// expectedContact := &ud.Contact{
// UserID: uid.Marshal(),
// PubKey: expectedDhPub,
// TrigFacts: []*ud.HashFact{{Hash: f1.Hash, Type: int32(f1.Type)}},
// }
// err := m.db.InsertUser(&storage.User{Id: uid.Marshal(), DhPub: expectedDhPub})
// if err != nil {
// t.Errorf("Failed to insert dummy user: %+v", err)
// }
//
// err = m.db.InsertFact(f1)
// if err != nil {
// t.Errorf("Failed to insert dummy fact: %+v", err)
// }
//
// resp := m.handleSearch(&ud.SearchSend{Fact: []*ud.HashFact{{Hash: fid, Type: 0}}}, c)
// if resp.Error != "" {
// t.Errorf("Failed to handle search: %+v", resp.Error)
// }
// if len(resp.Contacts) != 1 {
// t.Errorf("Did not receive expected number of contacts")
// }
// if !reflect.DeepEqual(expectedContact, resp.Contacts[0]) {
// t.Errorf("Did not received expected contact."+
// "\nexpected: %+v\nreceived: %+v", expectedContact, resp.Contacts[0])
// }
//
// resp = m.handleSearch(&ud.SearchSend{Fact: []*ud.HashFact{{Hash: fid, Type: int32(fact.Nickname)}}}, c)
// if resp.Error == "" {
// t.Errorf("Search should have returned error")
// }
// if len(resp.Contacts) != 0 {
// t.Errorf("Should not be able to search with nickname")
// }
//}
fid := []byte("factHash")
searchMsg := &ud.SearchSend{Fact: []*ud.HashFact{{Hash: fid, Type: 0}}}
payload, err := proto.Marshal(searchMsg)
if err != nil {
t.Errorf("Failed to marshal payload: %+v", err)
}
err = m.db.InsertFact(&storage.Fact{
Hash: fid,
UserId: uid.Marshal(),
Fact: "water is wet",
Type: 0,
Verified: true,
})
if err != nil {
t.Errorf("Failed to insert dummy fact: %+v", err)
}
expectedPayload, err := proto.Marshal(m.handleSearch(searchMsg, ct))
if err != nil {
t.Errorf("Failed to marshal message: %+v", err)
}
callbackChan := make(chan struct {
payload []byte
c single.Contact
})
callback := func(payload []byte, c single.Contact) {
callbackChan <- struct {
payload []byte
c single.Contact
}{payload: payload, c: c}
}
m.singleUse.RegisterCallback("", callback)
m.searchCallback(payload, ct)
results := <-callbackChan
if !results.c.Equal(ct) {
t.Errorf("Callback did not return the expected contact."+
"\nexpected: %s\nreceived: %s", ct, results.c)
}
if !bytes.Equal(expectedPayload, results.payload) {
t.Errorf("Callback did not return the expected payload."+
"\nexpected: %+v\nreceived: %+v", expectedPayload, results.payload)
}
}
func TestManager_handleSearch(t *testing.T) {
m := &Manager{db: storage.NewTestDB(t)}
uid := id.NewIdFromString("zezima", id.User, t)
c := single.NewContact(uid, &cyclic.Int{}, &cyclic.Int{}, singleUse.TagFP{}, 8)
expectedDhPub := []byte("DhPub")
fid := []byte("factHash")
f1 := &storage.Fact{
Hash: fid,
UserId: uid.Marshal(),
Fact: "water is wet",
Type: 1,
Verified: true,
}
expectedContact := &ud.Contact{
UserID: uid.Marshal(),
PubKey: expectedDhPub,
TrigFacts: []*ud.HashFact{{Hash: f1.Hash, Type: int32(f1.Type)}},
}
err := m.db.InsertUser(&storage.User{Id: uid.Marshal(), DhPub: expectedDhPub})
if err != nil {
t.Errorf("Failed to insert dummy user: %+v", err)
}
err = m.db.InsertFact(f1)
if err != nil {
t.Errorf("Failed to insert dummy fact: %+v", err)
}
resp := m.handleSearch(&ud.SearchSend{Fact: []*ud.HashFact{{Hash: fid, Type: 0}}}, c)
if resp.Error != "" {
t.Errorf("Failed to handle search: %+v", resp.Error)
}
if len(resp.Contacts) != 1 {
t.Errorf("Did not receive expected number of contacts")
}
if !reflect.DeepEqual(expectedContact, resp.Contacts[0]) {
t.Errorf("Did not received expected contact."+
"\nexpected: %+v\nreceived: %+v", expectedContact, resp.Contacts[0])
}
resp = m.handleSearch(&ud.SearchSend{Fact: []*ud.HashFact{{Hash: fid, Type: int32(fact.Nickname)}}}, c)
if resp.Error == "" {
t.Errorf("Search should have returned error")
}
if len(resp.Contacts) != 0 {
t.Errorf("Should not be able to search with nickname")
}
}
// mockSingleSearch is used to test the search function, which uses the single-
// use manager. It adheres to the SingleInterface interface.
type mockSingleSearch struct {
callback func(payload []byte, c single.Contact)
}
func (s *mockSingleSearch) RegisterCallback(_ string, callback single.ReceiveComm) {
s.callback = callback
}
func (s *mockSingleSearch) RespondSingleUse(partner single.Contact, payload []byte, _ time.Duration) error {
go s.callback(payload, partner)
return nil
}
func (s *mockSingleSearch) StartProcesses() (stoppable.Stoppable, error) {
return stoppable.NewSingle(""), nil
}
//// mockSingleSearch is used to test the search function, which uses the single-
//// use manager. It adheres to the SingleInterface interface.
//type mockSingleSearch struct {
// callback func(payload []byte, c single.Contact)
//}
//
//func (s *mockSingleSearch) RegisterCallback(_ string, callback single.ReceiveComm) {
// s.callback = callback
//}
//
//func (s *mockSingleSearch) RespondSingleUse(partner single.Contact, payload []byte, _ time.Duration) error {
// go s.callback(payload, partner)
// return nil
//}
//
//func (s *mockSingleSearch) StartProcesses() (stoppable.Stoppable, error) {
// return stoppable.NewSingle(""), nil
//}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment