Skip to content

Commit

Permalink
* Remove STR field from DirectoryResponse (Close #181)
Browse files Browse the repository at this point in the history
* Rewrite ConsistencyChecks.go: separate UpdateSTR and VerifyConsistency logic

* Comment some tests which will be rewritten later
  • Loading branch information
vqhuy committed Jan 10, 2018
1 parent 295c4ad commit 529d97f
Show file tree
Hide file tree
Showing 11 changed files with 534 additions and 509 deletions.
478 changes: 239 additions & 239 deletions application/server/server_test.go

Large diffs are not rendered by default.

196 changes: 96 additions & 100 deletions cli/coniksclient/internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ package cmd

import (
"log"
"net/url"
"os"
"strconv"
"strings"

"github.com/coniks-sys/coniks-go/application"
clientapp "github.com/coniks-sys/coniks-go/application/client"
"github.com/coniks-sys/coniks-go/application/testutil"
"github.com/coniks-sys/coniks-go/cli"
"github.com/coniks-sys/coniks-go/protocol"
"github.com/coniks-sys/coniks-go/protocol/client"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
Expand Down Expand Up @@ -106,109 +102,109 @@ func run(cmd *cobra.Command, args []string) {
}

func register(cc *client.ConsistencyChecks, conf *clientapp.Config, name string, key string) string {
req, err := clientapp.CreateRegistrationMsg(name, []byte(key))
if err != nil {
return ("Couldn't marshal registration request!")
}
// req, err := clientapp.CreateRegistrationMsg(name, []byte(key))
// if err != nil {
// return ("Couldn't marshal registration request!")
// }

var res []byte
regAddress := conf.RegAddress
if regAddress == "" {
// fallback to conf.Address if empty
regAddress = conf.Address
}
u, _ := url.Parse(regAddress)
switch u.Scheme {
case "tcp":
res, err = testutil.NewTCPClient(req, regAddress)
if err != nil {
return ("Error while receiving response: " + err.Error())
}
case "unix":
res, err = testutil.NewUnixClient(req, regAddress)
if err != nil {
return ("Error while receiving response: " + err.Error())
}
default:
return ("Invalid config!")
}
// var res []byte
// regAddress := conf.RegAddress
// if regAddress == "" {
// // fallback to conf.Address if empty
// regAddress = conf.Address
// }
// u, _ := url.Parse(regAddress)
// switch u.Scheme {
// case "tcp":
// res, err = testutil.NewTCPClient(req, regAddress)
// if err != nil {
// return ("Error while receiving response: " + err.Error())
// }
// case "unix":
// res, err = testutil.NewUnixClient(req, regAddress)
// if err != nil {
// return ("Error while receiving response: " + err.Error())
// }
// default:
// return ("Invalid config!")
// }

response := application.UnmarshalResponse(protocol.RegistrationType, res)
err = cc.HandleResponse(protocol.RegistrationType, response, name, []byte(key))
switch err {
case protocol.CheckBadSTR:
// FIXME: remove me
return ("Error: " + err.Error() + ". Maybe the client missed an epoch in between two commands, monitoring isn't supported yet.")
case nil:
switch response.Error {
case protocol.ReqSuccess:
return ("Succesfully registered name: " + name)
case protocol.ReqNameExisted:
return ("Name is already registered.")
}
case protocol.CheckBindingsDiffer:
switch response.Error {
case protocol.ReqNameExisted:
return (`Are you trying to update your binding? Unfortunately, KeyChange isn't supported yet.`)
case protocol.ReqSuccess:
recvKey, err := response.GetKey()
if err != nil {
return ("Oops! The server snuck in some other key. However, I cannot get the key from the response, error: " + err.Error())
}
return ("Oops! The server snuck in some other key. [" + string(recvKey) + "] was registered instead of [" + string(key) + "]")
}
default:
return ("Error: " + err.Error())
}
// response := application.UnmarshalResponse(protocol.RegistrationType, res)
// err = cc.HandleResponse(protocol.RegistrationType, response, name, []byte(key))
// switch err {
// case protocol.CheckBadSTR:
// // FIXME: remove me
// return ("Error: " + err.Error() + ". Maybe the client missed an epoch in between two commands, monitoring isn't supported yet.")
// case nil:
// switch response.Error {
// case protocol.ReqSuccess:
// return ("Succesfully registered name: " + name)
// case protocol.ReqNameExisted:
// return ("Name is already registered.")
// }
// case protocol.CheckBindingsDiffer:
// switch response.Error {
// case protocol.ReqNameExisted:
// return (`Are you trying to update your binding? Unfortunately, KeyChange isn't supported yet.`)
// case protocol.ReqSuccess:
// recvKey, err := response.GetKey()
// if err != nil {
// return ("Oops! The server snuck in some other key. However, I cannot get the key from the response, error: " + err.Error())
// }
// return ("Oops! The server snuck in some other key. [" + string(recvKey) + "] was registered instead of [" + string(key) + "]")
// }
// default:
// return ("Error: " + err.Error())
// }
return ""
}

func keyLookup(cc *client.ConsistencyChecks, conf *clientapp.Config, name string) string {
req, err := clientapp.CreateKeyLookupMsg(name)
if err != nil {
return ("Couldn't marshal key lookup request!")
}
// req, err := clientapp.CreateKeyLookupMsg(name)
// if err != nil {
// return ("Couldn't marshal key lookup request!")
// }

var res []byte
u, _ := url.Parse(conf.Address)
switch u.Scheme {
case "tcp":
res, err = testutil.NewTCPClient(req, conf.Address)
if err != nil {
return ("Error while receiving response: " + err.Error())
}
case "unix":
res, err = testutil.NewUnixClient(req, conf.Address)
if err != nil {
return ("Error while receiving response: " + err.Error())
}
default:
return ("Invalid config!")
}
// var res []byte
// u, _ := url.Parse(conf.Address)
// switch u.Scheme {
// case "tcp":
// res, err = testutil.NewTCPClient(req, conf.Address)
// if err != nil {
// return ("Error while receiving response: " + err.Error())
// }
// case "unix":
// res, err = testutil.NewUnixClient(req, conf.Address)
// if err != nil {
// return ("Error while receiving response: " + err.Error())
// }
// default:
// return ("Invalid config!")
// }

response := application.UnmarshalResponse(protocol.KeyLookupType, res)
if key, ok := cc.Bindings[name]; ok {
err = cc.HandleResponse(protocol.KeyLookupType, response, name, []byte(key))
} else {
err = cc.HandleResponse(protocol.KeyLookupType, response, name, nil)
}
switch err {
case protocol.CheckBadSTR:
// FIXME: remove me
return ("Error: " + err.Error() + ". Maybe the client missed an epoch in between two commands, monitoring isn't supported yet.")
case nil:
switch response.Error {
case protocol.ReqSuccess:
key, err := response.GetKey()
if err != nil {
return ("Cannot get the key from the response, error: " + err.Error())
}
return ("Found! Key bound to name is: [" + string(key) + "]")
case protocol.ReqNameNotFound:
return ("Name isn't registered.")
}
default:
return ("Error: " + err.Error())
}
// response := application.UnmarshalResponse(protocol.KeyLookupType, res)
// if key, ok := cc.Bindings[name]; ok {
// err = cc.HandleResponse(protocol.KeyLookupType, response, name, []byte(key))
// } else {
// err = cc.HandleResponse(protocol.KeyLookupType, response, name, nil)
// }
// switch err {
// case protocol.CheckBadSTR:
// // FIXME: remove me
// return ("Error: " + err.Error() + ". Maybe the client missed an epoch in between two commands, monitoring isn't supported yet.")
// case nil:
// switch response.Error {
// case protocol.ReqSuccess:
// key, err := response.GetKey()
// if err != nil {
// return ("Cannot get the key from the response, error: " + err.Error())
// }
// return ("Found! Key bound to name is: [" + string(key) + "]")
// case protocol.ReqNameNotFound:
// return ("Name isn't registered.")
// }
// default:
// return ("Error: " + err.Error())
// }
return ""
}
6 changes: 4 additions & 2 deletions merkletree/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ func StaticPAD(t *testing.T, ad AssocData) *PAD {
if err != nil {
t.Fatal(err)
}
str := NewSTR(pad.signKey, pad.ad, staticTree(t), 0, []byte{})
str := NewSTR(pad.signKey, pad.ad, StaticTree(t), 0, []byte{})
pad.latestSTR = str
pad.snapshots[0] = pad.latestSTR
return pad
}

func staticTree(t *testing.T) *MerkleTree {
// StaticTree returns an empty tree with empty nonce for _tests_.
func StaticTree(t *testing.T) *MerkleTree {
m, err := NewMerkleTree()
if err != nil {
t.Fatal(err)
}
m.nonce = []byte{}
m.recomputeHash()
return m
}

Expand Down
18 changes: 4 additions & 14 deletions protocol/auditor/common_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package auditor

import (
"bytes"
"encoding/hex"
"testing"

Expand All @@ -18,10 +17,10 @@ func TestComputeDirectoryIdentity(t *testing.T) {
for _, tc := range []struct {
name string
str *protocol.DirSTR
want []byte
want string
}{
{"normal", str0, hex2bin("fd0584f79054f8113f21e5450e0ad21c9221fc159334c7bc1644e3e2a0fb5060")},
{"panic", str1, []byte{}},
{"normal", str0, "b2c6300df0d0d0fb26c3be959a33cc978fc1969090fd19d95dd76cd43b809949"},
{"panic", str1, ""},
} {
t.Run(tc.name, func(t *testing.T) {
if tc.name == "panic" {
Expand All @@ -31,18 +30,9 @@ func TestComputeDirectoryIdentity(t *testing.T) {
}
}()
}
if got, want := ComputeDirectoryIdentity(tc.str), tc.want; !bytes.Equal(got[:], want) {
if got, want := ComputeDirectoryIdentity(tc.str), tc.want; want != hex.EncodeToString(got[:]) {
t.Errorf("ComputeDirectoryIdentity() = %#v, want %#v", got, want)
}
})
}
}

// decode hex string to byte array
func hex2bin(h string) []byte {
result, err := hex.DecodeString(h)
if err != nil {
panic(err)
}
return result
}
Loading

0 comments on commit 529d97f

Please sign in to comment.