Skip to content
Snippets Groups Projects
Commit 7d28417f authored by Niamh Nikali's avatar Niamh Nikali
Browse files

Use peppa fact package instead of auth

parent adfa0ef9
No related branches found
No related tags found
No related merge requests found
package contact
import (
"encoding/json"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/xx_network/primitives/id"
"strings"
)
const factDelimiter = ","
const factBreak = ";"
// Contact implements the Contact interface defined in interface/contact.go,
// in go, the structure is meant to be edited directly, the functions are for
// bindings compatibility
type Contact struct {
ID *id.ID
DhPubKey []byte
OwnershipProof []byte
Facts []Fact
}
// GetID returns the user ID for this user.
func (c Contact) GetID() []byte {
return c.ID.Bytes()
}
// GetDHPublicKey returns the public key associated with the Contact.
func (c Contact) GetDHPublicKey() []byte {
return c.DhPubKey
}
// GetDHPublicKey returns hash of a DH proof of key ownership.
func (c Contact) GetOwnershipProof() []byte {
return c.OwnershipProof
}
// Returns a fact list for adding and getting facts to and from the contact
func (c Contact) GetFactList() FactList {
return FactList{source: &c}
}
// json marshals the contact
func (c Contact) Marshal() ([]byte, error) {
return json.Marshal(&c)
}
// converts facts to a delineated string with an ending character for transfer
// over the network
func (c Contact) StringifyFacts() string {
stringList := make([]string, len(c.Facts))
for index, f := range c.Facts {
stringList[index] = f.Stringify()
}
return strings.Join(stringList, factDelimiter) + factBreak
}
func Unmarshal(b []byte) (Contact, error) {
c := Contact{}
err := json.Unmarshal(b, &c)
if err != nil {
return c, err
}
for i, fact := range c.Facts {
if !fact.T.IsValid() {
return Contact{}, errors.Errorf("Fact %v/%v has invalid "+
"type: %s", i, len(c.Facts), fact.T)
}
}
return c, nil
}
// splits the "facts" portion of the payload from the rest and returns them as
// facts
func UnstringifyFacts(s string) ([]Fact, string, error) {
parts := strings.SplitN(s, factBreak, 1)
if len(parts) != 2 {
return nil, "", errors.New("Invalid fact string passed")
}
factStrings := strings.Split(parts[0], factDelimiter)
var factList []Fact
for _, fString := range factStrings {
fact, err := UnstringifyFact(fString)
if err != nil {
jww.WARN.Printf("Fact failed to unstringify, dropped: %s",
err)
} else {
factList = append(factList, fact)
}
}
return factList, parts[1], nil
}
package contact
import (
"encoding/json"
"gitlab.com/xx_network/primitives/id"
"reflect"
"testing"
)
// Test that GetID returns an id.ID in the Contact
func TestContact_GetID(t *testing.T) {
C := Contact{ID: &id.DummyUser}
if !reflect.DeepEqual(C.GetID(), id.DummyUser.Bytes()) {
t.Error("Did not return the correct ID")
}
}
// Test that GetDHPublicKey returns the cyclic int in Contact
func TestContact_GetDHPublicKey(t *testing.T) {
// TODO: Find a test key
C := Contact{DhPubKey: []byte{5}}
dh := C.GetDHPublicKey()
if !reflect.DeepEqual(dh, []byte{5}) {
t.Error("Returned DH key did not match expected DH key")
}
}
// Test that GetOwnershipProof returns the []byte in Contact
func TestContact_GetOwnershipProof(t *testing.T) {
C := Contact{OwnershipProof: []byte{30, 40, 50}}
if !reflect.DeepEqual([]byte{30, 40, 50}, C.GetOwnershipProof()) {
t.Error("Returned proof key did not match expected proof")
}
}
// Test that GetFactList returns a FactList and our Fact is in it
func TestContact_GetFactList(t *testing.T) {
FL := new([]Fact)
C := Contact{Facts: *FL}
C.Facts = append(C.Facts, Fact{
Fact: "testing",
T: Phone,
})
gFL := C.GetFactList()
if !reflect.DeepEqual(C.Facts[0], gFL.Get(0)) {
t.Error("Expected Fact and got Fact did not match")
}
}
// Test that Marshal can complete without error, the output should
// be verified with the test below
func TestContact_Marshal(t *testing.T) {
C := Contact{
ID: &id.DummyUser,
DhPubKey: []byte{5},
OwnershipProof: []byte{30, 40, 50},
Facts: []Fact{{
Fact: "testing",
T: Email,
}},
}
M, err := C.Marshal()
if err != nil {
t.Error(err)
}
t.Log(M)
}
// Test that Unmarshal successfully unmarshals the Contact marshalled
// above
func TestContact_Unmarshal(t *testing.T) {
// Expected contact
E := Contact{
ID: &id.DummyUser,
DhPubKey: []byte{5},
OwnershipProof: []byte{30, 40, 50},
Facts: []Fact{{
Fact: "testing",
T: Email,
}},
}
// Marshalled contact, gotten from above test
M := []byte{123, 34, 73, 68, 34, 58, 91, 49, 48, 48, 44, 49, 49, 55,
44, 49, 48, 57, 44, 49, 48, 57, 44, 49, 50, 49, 44, 48, 44, 48,
44, 48, 44, 48, 44, 48, 44, 48, 44, 48, 44, 48, 44, 48, 44, 48,
44, 48, 44, 48, 44, 48, 44, 48, 44, 48, 44, 48, 44, 48, 44, 48,
44, 48, 44, 48, 44, 48, 44, 48, 44, 48, 44, 48, 44, 48, 44, 48,
44, 48, 44, 51, 93, 44, 34, 68, 104, 80, 117, 98, 75, 101, 121,
34, 58, 123, 125, 44, 34, 79, 119, 110, 101, 114, 115, 104, 105,
112, 80, 114, 111, 111, 102, 34, 58, 34, 72, 105, 103, 121, 34,
44, 34, 70, 97, 99, 116, 115, 34, 58, 91, 123, 34, 70, 97, 99,
116, 34, 58, 34, 116, 101, 115, 116, 105, 110, 103, 34, 44, 34,
84, 34, 58, 49, 125, 93, 125}
C, err := Unmarshal(M)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(C, E) {
t.Error("Expected and Unmarshaled contact are not equal")
}
}
// Test that Unmarshalling a contact with a bad FactType Fact fails
func TestContact_UnmarshalBadFact(t *testing.T) {
C := Contact{Facts: []Fact{{
Fact: "testing",
T: 200,
}}}
M, err := json.Marshal(&C)
if err != nil {
t.Error(err)
}
_, err = Unmarshal(M)
if err == nil {
t.Error("Unmarshalling Contact containing Fact with an invalid Fact type should've errored")
}
}
// Test that StringifyFacts can complete without error,
// the output should be verified with the test below
func TestContact_StringifyFacts(t *testing.T) {
C := Contact{Facts: []Fact{
{
Fact: "testing",
T: Phone,
},
{
Fact: "othertest",
T: Email,
},
}}
S := C.StringifyFacts()
t.Log(S)
}
// Test that UnstringifyFacts successfully unstingify-ies
// the FactList stringified above
// NOTE: This test does not pass, for reason "Invalid fact string passed"
func TestUnstringifyFacts(t *testing.T) {
E := Contact{Facts: []Fact{
{
Fact: "testing",
T: Phone,
},
{
Fact: "othertest",
T: Email,
},
}}
FL, S, err := UnstringifyFacts("Ptesting,Eothertest;")
if err != nil {
t.Error(err)
}
t.Log(S)
if !reflect.DeepEqual(E.Facts, FL) {
t.Error("Expected FactList and got FactList do not match")
}
}
package contact
import (
"github.com/pkg/errors"
)
type FactList struct {
source *Contact
}
func (fl FactList) Num() int {
return len(fl.source.Facts)
}
func (fl FactList) Get(i int) Fact {
return fl.source.Facts[i]
}
func (fl FactList) Add(fact string, factType int) error {
ft := FactType(factType)
if !ft.IsValid() {
return errors.New("Invalid fact type")
}
f, err := NewFact(ft, fact)
if err != nil {
return err
}
fl.source.Facts = append(fl.source.Facts, f)
return nil
}
package contact
import (
"reflect"
"testing"
)
// Test that the num function returns the correct number of Facts
func TestFactList_Num(t *testing.T) {
e1 := Fact{
Fact: "testing",
T: Phone,
}
e2 := Fact{
Fact: "othertest",
T: Email,
}
Fs := new([]Fact)
C := Contact{Facts: *Fs}
FL := FactList{source: &C}
FL.source.Facts = append(FL.source.Facts, e1)
FL.source.Facts = append(FL.source.Facts, e2)
if FL.Num() != 2 {
t.Error("Num returned incorrect number of Facts in FactList")
}
}
// Test the get function gets the right Fact at the index
func TestFactList_Get(t *testing.T) {
e := Fact{
Fact: "testing",
T: Phone,
}
Fs := new([]Fact)
C := Contact{Facts: *Fs}
FL := FactList{source: &C}
FL.source.Facts = append(FL.source.Facts, e)
if !reflect.DeepEqual(e, FL.Get(0)) {
t.Error("Expected fact does not match got fact")
}
}
// Test the add function adds a Fact to the source correctly
func TestFactList_Add(t *testing.T) {
// Expected fact
e := Fact{
Fact: "testing",
T: Phone,
}
Fs := new([]Fact)
C := Contact{Facts: *Fs}
FL := FactList{source: &C}
err := FL.Add("testing", int(Phone))
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(e, FL.source.Facts[0]) {
t.Error("Expected fact does not match added fact")
}
}
func TestFactList_AddInvalidType(t *testing.T) {
Fs := new([]Fact)
C := Contact{Facts: *Fs}
FL := FactList{source: &C}
err := FL.Add("testing", 200)
if err == nil {
t.Error("Adding a Fact to FactList with type 200 did not return an error!")
}
}
package contact package fact
import "errors" import "errors"
...@@ -15,27 +15,21 @@ func NewFact(ft FactType, fact string) (Fact, error) { ...@@ -15,27 +15,21 @@ func NewFact(ft FactType, fact string) (Fact, error) {
}, nil }, nil
} }
func (f Fact) Get() string {
return f.Fact
}
func (f Fact) Type() int {
return int(f.T)
}
// marshal is for transmission for UDB, not a part of the fact interface // marshal is for transmission for UDB, not a part of the fact interface
func (f Fact) Stringify() string { func (f Fact) Stringify() string {
return f.T.Stringify() + f.Fact return f.T.Stringify() + f.Fact
} }
func UnstringifyFact(s string) (Fact, error) { func UnstringifyFact(s string) (Fact, error) {
ft, err := UnstringifyFactType(s) if len(s) < 1 {
return Fact{}, errors.New("stringified facts must at least have a type at the start")
}
T := s[:1]
fact := s[1:]
ft, err := UnstringifyFactType(T)
if err != nil { if err != nil {
return Fact{}, err return Fact{}, err
} }
if len(s) < 1 {
return Fact{}, errors.New("cannot unstringify a fact that's just its type")
}
return NewFact(ft, s[1:]) return NewFact(ft, fact)
} }
package fact
import (
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"strings"
)
type FactList []Fact
const factDelimiter = ","
const factBreak = ";"
func (fl FactList) Stringify() string {
stringList := make([]string, len(fl))
for index, f := range fl {
stringList[index] = f.Stringify()
}
return strings.Join(stringList, factDelimiter) + factBreak
}
// unstrignifys facts followed by a facts break and with arbatrary data
// atttached at the end
func UnstringifyFactList(s string) ([]Fact, string, error) {
parts := strings.SplitN(s, factBreak, 1)
if len(parts) != 2 {
return nil, "", errors.New("Invalid fact string passed")
}
factStrings := strings.Split(parts[0], factDelimiter)
var factList []Fact
for _, fString := range factStrings {
fact, err := UnstringifyFact(fString)
if err != nil {
jww.WARN.Printf("Fact failed to unstringify, dropped: %s",
err)
} else {
factList = append(factList, fact)
}
}
return factList, parts[1], nil
}
package fact
package contact package fact
import ( import (
"reflect" "reflect"
...@@ -23,30 +23,6 @@ func TestNewFact(t *testing.T) { ...@@ -23,30 +23,6 @@ func TestNewFact(t *testing.T) {
} }
} }
// Test Get() function correctly gets the fact string
func TestFact_Get(t *testing.T) {
f := Fact{
Fact: "testing",
T: 1,
}
if f.Get() != f.Fact {
t.Errorf("f.Get() did not return the same value as f.Fact")
}
}
// Test Type() function correctly gets the type number
func TestFact_Type(t *testing.T) {
f := Fact{
Fact: "testing",
T: 1,
}
if f.Type() != int(f.T) {
t.Errorf("f.Type() did not return the same value as int(f.T)")
}
}
// Test Stringify() creates a string of the Fact // Test Stringify() creates a string of the Fact
// The output is verified to work in the test below // The output is verified to work in the test below
func TestFact_Stringify(t *testing.T) { func TestFact_Stringify(t *testing.T) {
......
package contact package fact
import ( import (
"fmt" "fmt"
...@@ -41,12 +41,12 @@ func (t FactType) Stringify() string { ...@@ -41,12 +41,12 @@ func (t FactType) Stringify() string {
} }
func UnstringifyFactType(s string) (FactType, error) { func UnstringifyFactType(s string) (FactType, error) {
switch s[0] { switch s {
case 'U': case "U":
return Username, nil return Username, nil
case 'E': case "E":
return Email, nil return Email, nil
case 'P': case "P":
return Phone, nil return Phone, nil
} }
return 3, errors.Errorf("Unknown Fact FactType: %s", s) return 3, errors.Errorf("Unknown Fact FactType: %s", s)
......
package contact package fact
import ( import (
"testing" "testing"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment