Test upkeep. Currently 80.3%

This commit is contained in:
Netscape Navigator 2020-12-12 09:36:43 -06:00
parent b9e52c98c5
commit c777cc7529
6 changed files with 49 additions and 25 deletions

View File

@ -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())
},
}

View File

@ -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)
}

View File

@ -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()

View File

@ -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

View File

@ -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.
}
}

View File

@ -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