diff --git a/contact/contact.go b/contact/contact.go deleted file mode 100644 index 25cfe3dd7feafe65fb40c7f08f210d1953f17b04..0000000000000000000000000000000000000000 --- a/contact/contact.go +++ /dev/null @@ -1,96 +0,0 @@ -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 -} diff --git a/contact/contact_test.go b/contact/contact_test.go deleted file mode 100644 index 2b983e19ca93f0f6d7a87fa8086d583b435e112d..0000000000000000000000000000000000000000 --- a/contact/contact_test.go +++ /dev/null @@ -1,170 +0,0 @@ -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") - } -} diff --git a/contact/factList.go b/contact/factList.go deleted file mode 100644 index 029c5f463b912ec1ef9988cc41ef163c22e3d2ba..0000000000000000000000000000000000000000 --- a/contact/factList.go +++ /dev/null @@ -1,31 +0,0 @@ -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 -} diff --git a/contact/factList_test.go b/contact/factList_test.go deleted file mode 100644 index 4283acaa19b0d56a1c95df4b969403b2da31f5fb..0000000000000000000000000000000000000000 --- a/contact/factList_test.go +++ /dev/null @@ -1,81 +0,0 @@ -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!") - } -} diff --git a/contact/fact.go b/fact/fact.go similarity index 64% rename from contact/fact.go rename to fact/fact.go index a71c501612b38658bb2b903175eec8dadbb5ad28..5771376cc168b14d9888d79f4b6eda36e907a89e 100644 --- a/contact/fact.go +++ b/fact/fact.go @@ -1,4 +1,4 @@ -package contact +package fact import "errors" @@ -15,27 +15,21 @@ func NewFact(ft FactType, fact string) (Fact, error) { }, 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 func (f Fact) Stringify() string { return f.T.Stringify() + f.Fact } 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 { 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) } diff --git a/fact/factList.go b/fact/factList.go new file mode 100644 index 0000000000000000000000000000000000000000..7648c0fb27bcc6318e9fe7ecf97fea75eb0d774d --- /dev/null +++ b/fact/factList.go @@ -0,0 +1,44 @@ +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 +} diff --git a/fact/factList_test.go b/fact/factList_test.go new file mode 100644 index 0000000000000000000000000000000000000000..68f4e995c2226e66fa34af796c6179506b122112 --- /dev/null +++ b/fact/factList_test.go @@ -0,0 +1 @@ +package fact diff --git a/contact/fact_test.go b/fact/fact_test.go similarity index 72% rename from contact/fact_test.go rename to fact/fact_test.go index 9b30a4a4ca92fbce2b610347a9e11ccbdba7e4ff..b89e12c042abf1bbff7a9b2272a0485f7b4ba313 100644 --- a/contact/fact_test.go +++ b/fact/fact_test.go @@ -1,4 +1,4 @@ -package contact +package fact import ( "reflect" @@ -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 // The output is verified to work in the test below func TestFact_Stringify(t *testing.T) { diff --git a/contact/type.go b/fact/type.go similarity index 93% rename from contact/type.go rename to fact/type.go index 87c8eca03752de6daeda2821b77b6533d47775ee..32a48e3614da00cb720a62fdc2e30cb5da5976cd 100644 --- a/contact/type.go +++ b/fact/type.go @@ -1,4 +1,4 @@ -package contact +package fact import ( "fmt" @@ -41,12 +41,12 @@ func (t FactType) Stringify() string { } func UnstringifyFactType(s string) (FactType, error) { - switch s[0] { - case 'U': + switch s { + case "U": return Username, nil - case 'E': + case "E": return Email, nil - case 'P': + case "P": return Phone, nil } return 3, errors.Errorf("Unknown Fact FactType: %s", s) diff --git a/contact/type_test.go b/fact/type_test.go similarity index 99% rename from contact/type_test.go rename to fact/type_test.go index 51fcd6775e81aa67fd3c08aa3578329fd17fc12e..3a1e7d136a8735d9fdeaa5dc088a695f786bf080 100644 --- a/contact/type_test.go +++ b/fact/type_test.go @@ -1,4 +1,4 @@ -package contact +package fact import ( "testing"