Skip to content
Snippets Groups Projects
Commit b77794b2 authored by Jake Taylor's avatar Jake Taylor :lips:
Browse files

Fixed tests;

Removed inx from pushkey
parent 3d360bef
No related branches found
No related tags found
No related merge requests found
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2018 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package udb
import (
"gitlab.com/privategrity/crypto/format" // <-- FIXME: this is annoying, WHY?
"testing"
)
// Hack around the interface for client to do what we need for testing.
func NewMessage(msg string) (format.MessageInterface, error) {
msgs, err := format.NewMessage(1, 2, msg)
return msgs[0], err
}
// Test with an unknown function
func TestReceiveMessage(t *testing.T) {
msg, err := NewMessage("Hello, World!")
if err != nil {
t.Errorf("Could not smoke test ReceiveMessage: %v", err)
}
ReceiveMessage(msg)
}
func TestBrokenMessage(t *testing.T) {
brokenMsg := "foo '" // From shellwords test cases
msg, _ := NewMessage(brokenMsg)
ReceiveMessage(msg)
// We are only making sure this doesn't crash the program.
}
......@@ -13,7 +13,6 @@ import (
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/privategrity/client/parse"
"gitlab.com/privategrity/user-discovery-bot/storage"
"strconv"
)
const REGISTER_USAGE = "Usage: 'REGISTER [EMAIL] [email-address] " +
......@@ -34,7 +33,7 @@ func Register(userId uint64, args []string) {
RegErr := func(msg string) {
Send(userId, msg, parse.Type_UDB_REGISTER_RESPONSE)
Send(userId, REGISTER_USAGE, parse.Type_UDB_REGISTER_RESPONSE)
jww.INFO.Printf("User %d error: %s", userId, msg)
jww.INFO.Printf("Register user %d error: %s", userId, msg)
}
if len(args) != 3 {
RegErr("Invalid command syntax!")
......@@ -76,49 +75,42 @@ func Register(userId uint64, args []string) {
Send(userId, "REGISTRATION COMPLETE", parse.Type_UDB_REGISTER_RESPONSE)
}
const PUSHKEY_USAGE = "Usage: 'PUSHKEY [temp-key-id] [starting-byte-index] " +
const PUSHKEY_USAGE = "Usage: 'PUSHKEY [temp-key-id] " +
"[base64-encoded-bytestream]'"
const PUSHKEY_SIZE = 256 // 2048 bits
var tempKeys = make(map[string][]byte)
var tempKeysState = make(map[string][]bool)
// PushKey adds a key to the registration database and links it by fingerprint
// The PushKey command has the form PUSHKEY KEYID IDX KEYMAT
// The PushKey command has the form PUSHKEY KEYID KEYMAT
// WHERE:
// - KEYID = The Key ID -- not the same as the fingerprint
// - IDX = byte index of the key
// - KEYMAT = The part of the key corresponding to that index, in BASE64
// PushKey returns an ACK that it received the command OR a success/failure
// once it receives all pieces of the key.
func PushKey(userId uint64, args []string) {
jww.INFO.Printf("PushKey %d:, %v", userId, args)
jww.INFO.Printf("PushKey %d, %v", userId, args)
PushErr := func(msg string) {
Send(userId, msg, parse.Type_UDB_PUSH_KEY_RESPONSE)
Send(userId, PUSHKEY_USAGE, parse.Type_UDB_PUSH_KEY_RESPONSE)
jww.INFO.Printf("User %d error: %s", userId, msg)
jww.INFO.Printf("PushKey user %d error: %s", userId, msg)
}
if len(args) != 3 {
if len(args) != 2 {
PushErr("Invalid command syntax!")
return
}
keyId := args[0]
keyIdxStr := args[1]
keyMat := args[2]
keyMat := args[1]
keyIdx := 0
// Decode keyMat
// FIXME: Not sure I like having to base64 stuff here, but it's this or hex
// Maybe add suppor to client for these pubkey conversions?
newKeyBytes, decErr := base64.StdEncoding.DecodeString(keyMat)
if decErr != nil {
PushErr("Could not decode new key bytes, it must be in base64!")
return
}
// Parse index
keyIdx, pErr := strconv.Atoi(keyIdxStr)
if pErr != nil || keyIdx < 0 || (keyIdx+len(newKeyBytes)) > PUSHKEY_SIZE {
PushErr("Invalid key index!")
PushErr(fmt.Sprintf("Could not decode new key bytes, "+
"it must be in base64! %s", decErr))
return
}
......
......@@ -9,7 +9,8 @@ package udb
import (
"encoding/base64"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/privategrity/crypto/format"
"gitlab.com/privategrity/client/parse"
"gitlab.com/privategrity/client/user"
"gitlab.com/privategrity/user-discovery-bot/storage"
"os"
"testing"
......@@ -17,12 +18,27 @@ import (
type DummySender struct{}
func (d DummySender) Send(messageInterface format.MessageInterface) error {
var rl = RegisterListener{}
var sl = SearchListener{}
var pl = PushKeyListener{}
var gl = GetKeyListener{}
func (d DummySender) Send(recipientID user.ID, msg string) error {
// do nothing
jww.INFO.Printf("DummySender!")
return nil
}
// Hack around the interface for client to do what we need for testing.
func NewMessage(msg string, msgType parse.Type) *parse.Message {
// Create the message body and assign its type
tmp := parse.TypedBody{
Type: msgType,
Body: []byte(msg),
}
return &parse.Message{TypedBody: tmp}
}
func TestMain(m *testing.M) {
UdbSender = DummySender{}
jww.SetStdoutThreshold(jww.LevelDebug)
......@@ -34,37 +50,25 @@ func TestMain(m *testing.M) {
// not sure how I feel about it.
func TestRegisterHappyPath(t *testing.T) {
DataStore = storage.NewRamStorage()
pubKeyBits := []string{
"S8KXBczy0jins9uS4LgBPt0bkFl8t00MnZmExQ6GcOcu8O7DKgAsNz" +
"LU7a+gMTbIsS995IL/kuFF8wcBaQJBY23095PMSQ/nMuetzhk9HdXxrGIiKBo3C/n4SClp" +
"q4H+PoF9XziEVKua8JxGM2o83KiCK3tNUpaZbAAElkjueY4=",
"8Lg/eoeKGgPlleTYfO3JyGfnwBtLi73ti0h2dBQWW94JTqTQDr+z" +
"xVpLzdgTt+87TkAl0yXu9mOUXqGJ+51lTcRlIdIpWpfgUbibdRme8IThg0RNCF31ESKCts" +
"o8gJ8mSVljIXxrC+Uuoi+Gl1LNN5nPARykatx0Y70xNdJd2BQ=",
}
pubKeyBits := "S8KXBczy0jins9uS4LgBPt0bkFl8t00MnZmExQ6GcOcu8O7DKgAsNzLU7a" +
"+gMTbIsS995IL/kuFF8wcBaQJBY23095PMSQ/nMuetzhk9HdXxrGIiKBo3C/n4SClpq4H+PoF9XziEVKua8JxGM2o83KiCK3tNUpaZbAAElkjueY7wuD96h4oaA+WV5Nh87cnIZ+fAG0uLve2LSHZ0FBZb3glOpNAOv7PFWkvN2BO37ztOQCXTJe72Y5ReoYn7nWVNxGUh0ilal+BRuJt1GZ7whOGDRE0IXfURIoK2yjyAnyZJWWMhfGsL5S6iL4aXUs03mc8BHKRq3HRjvTE10l3YFA=="
pubKey := make([]byte, 256)
for i := range pubKeyBits {
bytes, _ := base64.StdEncoding.DecodeString(pubKeyBits[i])
for j := range bytes {
pubKey[j+i*128] = bytes[j]
}
}
pubKey, _ = base64.StdEncoding.DecodeString(pubKeyBits)
fingerprint := "8oKh7TYG4KxQcBAymoXPBHSD/uga9pX3Mn/jKhvcD8M="
msgs := []string{
"PUSHKEY myKeyId 0 " + pubKeyBits[0],
"PUSHKEY myKeyId 128 " + pubKeyBits[1],
"PUSHKEY myKeyId " + pubKeyBits,
"REGISTER EMAIL rick@privategrity.com " + fingerprint,
"GETKEY " + fingerprint,
}
for i := range msgs {
msg, err := NewMessage(msgs[i])
if err != nil {
t.Errorf("Error generating message: %v", err)
}
ReceiveMessage(msg)
}
msg := NewMessage(msgs[0], parse.Type_UDB_PUSH_KEY)
pl.Hear(msg, false)
msg = NewMessage(msgs[1], parse.Type_UDB_REGISTER)
rl.Hear(msg, false)
msg = NewMessage(msgs[2], parse.Type_UDB_GET_KEY)
gl.Hear(msg, false)
// Assert expected state
k, ok := DataStore.GetKey(fingerprint)
......@@ -77,9 +81,9 @@ func TestRegisterHappyPath(t *testing.T) {
}
}
u, ok2 := DataStore.GetUserKey(uint64(1))
u, ok2 := DataStore.GetUserKey(uint64(0))
if !ok2 {
t.Errorf("Could not retriever user key 1!")
t.Errorf("Could not retrieve user key 1!")
}
if u != fingerprint {
t.Errorf("GetUserKey fingerprint mismatch: %s v %s", u, fingerprint)
......@@ -90,7 +94,7 @@ func TestRegisterHappyPath(t *testing.T) {
t.Errorf("Could not retrieve by e-mail address!")
}
if ks[0] != fingerprint {
t.Errorf("GetKeys fingerprint mismatch: %v v %s", ks, fingerprint)
t.Errorf("GetKeys fingerprint mismatch: %v v %s", ks[0], fingerprint)
}
}
......@@ -104,12 +108,12 @@ func TestInvalidRegistrationCommands(t *testing.T) {
"vcD8M=",
}
for i := range msgs {
msg, err := NewMessage(msgs[i])
if err != nil {
t.Errorf("Error generating message: %v", err)
}
ReceiveMessage(msg)
msg := NewMessage(msgs[0], parse.Type_UDB_PUSH_KEY)
pl.Hear(msg, false)
for i := 1; i < len(msgs); i++ {
msg = NewMessage(msgs[i], parse.Type_UDB_REGISTER)
rl.Hear(msg, false)
_, ok := DataStore.GetKey("8oKh7TYG4KxQcBAymoXPBHSD/uga9pX3Mn/jKh")
if ok {
t.Errorf("Data store key 8oKh7TYG4KxQcBAymoXPBHSD/uga9pX3Mn/jKh should" +
......
......@@ -7,6 +7,7 @@
package udb
import (
"gitlab.com/privategrity/client/parse"
"gitlab.com/privategrity/user-discovery-bot/storage"
"testing"
)
......@@ -15,23 +16,16 @@ func TestSearchHappyPath(t *testing.T) {
DataStore = storage.NewRamStorage()
// Load a user
TestRegisterHappyPath(t)
fingerprint := "8oKh7TYG4KxQcBAymoXPBHSD/uga9pX3Mn/jKhvcD8M="
// NOTE: This is kind of hard, since we can't see the response and search
// does not modify data we can check
// TODO: Monkeypatch send so we can verify? -- this is tested in integration,
// so.. low priority.
msgs := []string{
"SEARCH EMAIL rick@privategrity.com",
"GETKEY " + fingerprint,
}
for i := range msgs {
msg, err := NewMessage(msgs[i])
if err != nil {
t.Errorf("Error generating message: %v", err)
}
ReceiveMessage(msg)
}
msg := NewMessage(msgs[0], parse.Type_UDB_SEARCH)
sl.Hear(msg, false)
}
// Test invalid search type
......@@ -42,26 +36,17 @@ func TestSearch_Invalid_Type(t *testing.T) {
"GETKEY " + fingerprint,
}
for i := range msgs {
msg, err := NewMessage(msgs[i])
if err != nil {
t.Errorf("Error generating message: %v", err)
}
ReceiveMessage(msg)
}
msg := NewMessage(msgs[0], parse.Type_UDB_SEARCH)
sl.Hear(msg, false)
}
// Test invalid user
func TestSearch_Invalid_User(t *testing.T) {
DataStore = storage.NewRamStorage()
msgs := []string{
"SEARCH EMAIL cat@privategrity.com",
}
for i := range msgs {
msg, err := NewMessage(msgs[i])
if err != nil {
t.Errorf("Error generating message: %v", err)
}
ReceiveMessage(msg)
}
msg := NewMessage(msgs[0], parse.Type_UDB_SEARCH)
sl.Hear(msg, false)
}
......@@ -9,7 +9,7 @@ package udb
import (
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/privategrity/client/io"
"gitlab.com/privategrity/client/api"
"gitlab.com/privategrity/client/parse"
"gitlab.com/privategrity/client/user"
)
......@@ -17,30 +17,34 @@ import (
// Sender interface -- the api is broken here (does not return the error), so
// we reimplement a new interface...
type Sender interface {
Send(userID user.ID, msg string) error
Send(recipientID user.ID, msg string) error
}
// ApiSender calls the api send function
type APISender struct{}
// Send calls the api send function
func (a APISender) Send(userID user.ID, msg string) error {
return io.Messaging.SendMessage(userID, msg)
func (a APISender) Send(recipientID user.ID, msg string) error {
return api.Send(api.APIMessage{
Payload: msg,
SenderID: UDB_USERID,
RecipientID: recipientID,
})
}
// UdbSender is the sender interface to use
var UdbSender Sender = APISender{}
// Wrap the API Send function (useful for mock tests)
func Send(userID uint64, msg string, msgType parse.Type) {
func Send(recipientID uint64, msg string, msgType parse.Type) {
// Create the message body and assign its type
message := string(parse.Pack(&parse.TypedBody{
Type: msgType,
Body: []byte(msg),
}))
// Send the message
sendErr := UdbSender.Send(user.ID(userID), message)
sendErr := UdbSender.Send(user.ID(recipientID), message)
if sendErr != nil {
jww.ERROR.Printf("Error responding to %d: %s", userID, sendErr)
jww.ERROR.Printf("Error responding to %d: %s", recipientID, sendErr)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment