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 Long: `Prints the current Pigeon identity to screen. Prints 'NONE' if
not found.`, not found.`,
Run: func(cmd *cobra.Command, args []string) { 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) check(err1, "Failed to SetConfig (2): %s", err1)
} }
// GetConfig retrieves a key/value pair from the database. // GetConfig retrieves a key/value pair (or error) from the database.
func GetConfig(key string) []byte { func GetConfig(key string) ([]byte, error) {
var result string var result string
row := getDB().QueryRow("SELECT value FROM configs WHERE key=$1", key) row := getDB().QueryRow("SELECT value FROM configs WHERE key=$1", key)
err := row.Scan(&result) err := row.Scan(&result)
if err == sql.ErrNoRows { if err != nil {
panicf("CONFIG MISSING: %s", key) 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) 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 WHERE mhash = $1"
// const messageExistsQuery = "SELECT count(*) FROM messages"
func messageExists(mhash string) bool { func messageExists(mhash string) bool {
var count int var count int
db := getDB() db := getDB()

View File

@ -6,24 +6,23 @@ import (
"fmt" "fmt"
) )
func showIdentity() string { // Returns the current user's identity of the `NONE` value.
existingKey := GetConfig("public_key") func showPubKeyOrNone() string {
if len(existingKey) == 0 { existingKey, err := GetConfig("public_key")
if (err == sql.ErrNoRows) || (len(existingKey) == 0) {
return "NONE" return "NONE"
} }
return encodePeerMhash(existingKey) return encodePeerMhash(existingKey)
} }
func createOrShowIdentity() string { func createOrShowIdentity() string {
var pubKey []byte oldKey := showPubKeyOrNone()
oldKey := GetConfig("private_key") if oldKey == "NONE" {
if len(oldKey) == 0 { newPubKey, _ := CreateIdentity()
newKey, _ := CreateIdentity() return encodePeerMhash(newPubKey)
pubKey = newKey
} else {
pubKey = oldKey
} }
return encodePeerMhash(pubKey)
return oldKey
} }
// CreateIdentity is used by the CLI to create an ED25519 // CreateIdentity is used by the CLI to create an ED25519

View File

@ -2,14 +2,15 @@ package main
import ( import (
"bytes" "bytes"
"fmt"
"testing" "testing"
) )
func TestCreateIdentity(t *testing.T) { func TestCreateIdentity(t *testing.T) {
resetDB() resetDB()
pub, priv := CreateIdentity() pub, priv := CreateIdentity()
dbPubKey := GetConfig("public_key") dbPubKey := FetchConfig("public_key")
dbPrivKey := GetConfig("private_key") dbPrivKey := FetchConfig("private_key")
if !bytes.Equal(pub, dbPubKey) { if !bytes.Equal(pub, dbPubKey) {
t.Fail() t.Fail()
@ -19,3 +20,26 @@ func TestCreateIdentity(t *testing.T) {
t.Fail() 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" "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). // 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 must be 1-90 characters in length.
// A key cannot contain whitespace or control characters // A key cannot contain whitespace or control characters