Browse Source

Test upkeep. Currently 80.3%

main
Netscape Navigator 11 months ago
parent
commit
c777cc7529
  1. 2
      project/cli.go
  2. 17
      project/db.go
  3. 2
      project/message.go
  4. 21
      project/util.go
  5. 28
      project/util_test.go
  6. 4
      project/verify.go

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

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

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

21
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

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

4
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

Loading…
Cancel
Save