From c777cc7529bfc53d0be8bc23ac9d0d9eb1a81ba3 Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Sat, 12 Dec 2020 09:36:43 -0600 Subject: [PATCH] Test upkeep. Currently 80.3% --- project/cli.go | 2 +- project/db.go | 17 ++++++++++++----- project/message.go | 2 -- project/util.go | 21 ++++++++++----------- project/util_test.go | 28 ++++++++++++++++++++++++++-- project/verify.go | 4 ---- 6 files changed, 49 insertions(+), 25 deletions(-) diff --git a/project/cli.go b/project/cli.go index b5b570f..0117e49 100644 --- a/project/cli.go +++ b/project/cli.go @@ -53,7 +53,7 @@ var identityShowCmd = &cobra.Command{ Long: `Prints the current Pigeon identity to screen. Prints 'NONE' if not found.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println(showIdentity()) + fmt.Println(showPubKeyOrNone()) }, } diff --git a/project/db.go b/project/db.go index dbe30ae..9aa6e89 100644 --- a/project/db.go +++ b/project/db.go @@ -46,14 +46,21 @@ func SetConfig(key string, value []byte) { check(err1, "Failed to SetConfig (2): %s", err1) } -// GetConfig retrieves a key/value pair from the database. -func GetConfig(key string) []byte { +// GetConfig retrieves a key/value pair (or error) from the database. +func GetConfig(key string) ([]byte, error) { var result string row := getDB().QueryRow("SELECT value FROM configs WHERE key=$1", key) err := row.Scan(&result) - if err == sql.ErrNoRows { - panicf("CONFIG MISSING: %s", key) + if err != nil { + return []byte{}, nil } - check(err, "SOmething else went wrong: %s", err) + return []byte(result), nil +} + +// FetchConfig retrieves a key/value pair from the database. +// Fetching an unset key will result in a panic. +func FetchConfig(key string) []byte { + result, err := GetConfig(key) + check(err, "Something else went wrong: %s", err) return []byte(result) } diff --git a/project/message.go b/project/message.go index 040ad22..bf14a54 100644 --- a/project/message.go +++ b/project/message.go @@ -2,8 +2,6 @@ package main const messageExistsQuery = "SELECT count(*) FROM messages WHERE mhash = $1" -// const messageExistsQuery = "SELECT count(*) FROM messages" - func messageExists(mhash string) bool { var count int db := getDB() diff --git a/project/util.go b/project/util.go index e00f0fb..a65f382 100644 --- a/project/util.go +++ b/project/util.go @@ -6,24 +6,23 @@ import ( "fmt" ) -func showIdentity() string { - existingKey := GetConfig("public_key") - if len(existingKey) == 0 { +// Returns the current user's identity of the `NONE` value. +func showPubKeyOrNone() string { + existingKey, err := GetConfig("public_key") + if (err == sql.ErrNoRows) || (len(existingKey) == 0) { return "NONE" } return encodePeerMhash(existingKey) } func createOrShowIdentity() string { - var pubKey []byte - oldKey := GetConfig("private_key") - if len(oldKey) == 0 { - newKey, _ := CreateIdentity() - pubKey = newKey - } else { - pubKey = oldKey + oldKey := showPubKeyOrNone() + if oldKey == "NONE" { + newPubKey, _ := CreateIdentity() + return encodePeerMhash(newPubKey) } - return encodePeerMhash(pubKey) + + return oldKey } // CreateIdentity is used by the CLI to create an ED25519 diff --git a/project/util_test.go b/project/util_test.go index 7c2b196..51bfcd8 100644 --- a/project/util_test.go +++ b/project/util_test.go @@ -2,14 +2,15 @@ package main import ( "bytes" + "fmt" "testing" ) func TestCreateIdentity(t *testing.T) { resetDB() pub, priv := CreateIdentity() - dbPubKey := GetConfig("public_key") - dbPrivKey := GetConfig("private_key") + dbPubKey := FetchConfig("public_key") + dbPrivKey := FetchConfig("private_key") if !bytes.Equal(pub, dbPubKey) { t.Fail() @@ -19,3 +20,26 @@ func TestCreateIdentity(t *testing.T) { t.Fail() } } + +func TestShowIdentity(t *testing.T) { + resetDB() + result1 := showPubKeyOrNone() + if result1 != "NONE" { + t.Fail() + } + result2 := createOrShowIdentity() + sigil := result2[0:5] + if sigil != "USER." { + t.Fail() + } + + if len(result2) != 57 { + t.Fail() + } + result3 := createOrShowIdentity() + if result2 != result3 { + fmt.Printf("=== result2: %s\n", result2) + fmt.Printf("=== result3: %s\n", result3) + t.Fail() //Expect createOrShowIdentity() to be idempotent. + } +} diff --git a/project/verify.go b/project/verify.go index 60018fa..3990979 100644 --- a/project/verify.go +++ b/project/verify.go @@ -8,10 +8,6 @@ import ( "strings" ) -func reconstructMessage(message pigeonMessage) (string, string) { - return "top_half", "bottom_half" -} - // Every body entry is a key value pair. Keys and values are separated by a : character (no spaces). // A key must be 1-90 characters in length. // A key cannot contain whitespace or control characters