diff --git a/go.mod b/go.mod index 8b362ab5f4e650d7ada1a5c3f89c508d1650dbe4..eb99950d83c5cfff094d9bf28ec667ce78ffe3ae 100644 --- a/go.mod +++ b/go.mod @@ -15,11 +15,11 @@ require ( github.com/spf13/jwalterweatherman v1.1.0 github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.6.2 - gitlab.com/elixxir/comms v0.0.0-20200909214857-b106ce82d9e8 - gitlab.com/elixxir/crypto v0.0.0-20200907171019-008a9d4aa264 + gitlab.com/elixxir/comms v0.0.0-20200915164538-d15120cc8fa5 + gitlab.com/elixxir/crypto v0.0.0-20200915172523-b2f2c63560c9 gitlab.com/elixxir/ekv v0.1.1 - gitlab.com/elixxir/primitives v0.0.0-20200907165319-16ed0124890b - gitlab.com/xx_network/comms v0.0.0-20200909172255-8dd37d862d9e + gitlab.com/elixxir/primitives v0.0.0-20200915181454-994a60142fce + gitlab.com/xx_network/comms v0.0.0-20200915154643-d533291041b7 gitlab.com/xx_network/crypto v0.0.0-20200812183430-c77a5281c686 gitlab.com/xx_network/primitives v0.0.0-20200812183720-516a65a4a9b2 golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a diff --git a/go.sum b/go.sum index abb72be586beadee430189799f67271aa627d98e..3ca95138c4db96fc0e144040ac955d4d842ed2e4 100644 --- a/go.sum +++ b/go.sum @@ -159,6 +159,7 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/zeebo/assert v0.0.0-20181109011804-10f827ce2ed6/go.mod h1:yssERNPivllc1yU3BvpjYI5BUW+zglcz6QWqeVRL5t0= github.com/zeebo/assert v1.1.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0= +github.com/zeebo/blake3 v0.0.4 h1:vtZ4X8B2lKXZFg2Xyg6Wo36mvmnJvc2VQYTtA4RDCkI= github.com/zeebo/blake3 v0.0.4/go.mod h1:YOZo8A49yNqM0X/Y+JmDUZshJWLt1laHsNSn5ny2i34= github.com/zeebo/pcg v0.0.0-20181207190024-3cdc6b625a05/go.mod h1:Gr+78ptB0MwXxm//LBaEvBiaXY7hXJ6KGe2V32X2F6E= gitlab.com/elixxir/comms v0.0.0-20200810165153-3039323b5656 h1:A5S3E7EPL95s3+PGhgAiwnMaa7VcWj8/RtBur3lxdOw= @@ -179,6 +180,10 @@ gitlab.com/elixxir/comms v0.0.0-20200903181126-c92d7a304999 h1:fxrqseawjJ2q6Z3NZ gitlab.com/elixxir/comms v0.0.0-20200903181126-c92d7a304999/go.mod h1:HW3Ige10aeJeyb2fcQ/YOBPiyzY/4jHau1Cj6/1WBHc= gitlab.com/elixxir/comms v0.0.0-20200909214857-b106ce82d9e8 h1:y+0aElewUD0ceZHvf96+s8oUZnfBMD9aflOBztMNlZ0= gitlab.com/elixxir/comms v0.0.0-20200909214857-b106ce82d9e8/go.mod h1:ZMwe9xZbPY1s81CVvy51hh+96b/supA797uxXEJJqRE= +gitlab.com/elixxir/comms v0.0.0-20200915163543-c2f080316df8 h1:qLOm7w/+Uco7jOJmZ1HjPGC3uwHu3Z21OCMb+hKfd20= +gitlab.com/elixxir/comms v0.0.0-20200915163543-c2f080316df8/go.mod h1:O5e4ISYYofsrrVD5Qzt1Ah8z0Fr2YqTXFwUJjmAiSjk= +gitlab.com/elixxir/comms v0.0.0-20200915164538-d15120cc8fa5 h1:QnIlpEi6dln6MIPvcgkOVV9+OFWP6HCSmjys8UgAoho= +gitlab.com/elixxir/comms v0.0.0-20200915164538-d15120cc8fa5/go.mod h1:O5e4ISYYofsrrVD5Qzt1Ah8z0Fr2YqTXFwUJjmAiSjk= gitlab.com/elixxir/crypto v0.0.0-20200804182833-984246dea2c4 h1:28ftZDeYEko7xptCZzeFWS1Iam95dj46TWFVVlKmw6A= gitlab.com/elixxir/crypto v0.0.0-20200804182833-984246dea2c4/go.mod h1:ucm9SFKJo+K0N2GwRRpaNr+tKXMIOVWzmyUD0SbOu2c= gitlab.com/elixxir/crypto v0.0.0-20200805174804-bdf909f2a16d/go.mod h1:cu6uNoANVLV0J6HyTL6KqVtVyh9SHU1RjJhytYlsbVQ= @@ -202,6 +207,8 @@ gitlab.com/elixxir/crypto v0.0.0-20200827170914-14227f20900c h1:1vkxQ0Ol/Kr6szWa gitlab.com/elixxir/crypto v0.0.0-20200827170914-14227f20900c/go.mod h1:D65u4dPjMLSHiENn7fvnleWUcuuSeT48Ttw760Wt3xQ= gitlab.com/elixxir/crypto v0.0.0-20200907171019-008a9d4aa264 h1:8jk4yHw15PpjTPHMpHo5g7io/qp+ryHBxe6S8vllfLs= gitlab.com/elixxir/crypto v0.0.0-20200907171019-008a9d4aa264/go.mod h1:zUczcFuZGqLchDX1sjgBo189soeDK2p5Mx+GNNrkTLI= +gitlab.com/elixxir/crypto v0.0.0-20200915172523-b2f2c63560c9 h1:omuo67rgWzlorzLlQQumUkES1w3M5elKFq4PmB+HjJw= +gitlab.com/elixxir/crypto v0.0.0-20200915172523-b2f2c63560c9/go.mod h1:zUczcFuZGqLchDX1sjgBo189soeDK2p5Mx+GNNrkTLI= gitlab.com/elixxir/ekv v0.0.0-20200729182028-159355ea5842 h1:m1zDQ6UadpuMnV7nvnyR+DUXE3AisRnVjajTb1xZE4c= gitlab.com/elixxir/ekv v0.0.0-20200729182028-159355ea5842/go.mod h1:bXY0kgbV5BHYda4YY5/hiG5bjimGK+R3PYub5yM9C/s= gitlab.com/elixxir/ekv v0.1.1 h1:Em3rF8sv+tNbQGXbcpYzAS2blWRAP708JGhYlkN74Kg= @@ -225,6 +232,10 @@ gitlab.com/elixxir/primitives v0.0.0-20200903200059-7dcf9b844a40 h1:ZFwNuC0s8JeK gitlab.com/elixxir/primitives v0.0.0-20200903200059-7dcf9b844a40/go.mod h1:kNp47yPqja2lHSiS4DddTvFpB/4D9dB2YKnw5c+LJCE= gitlab.com/elixxir/primitives v0.0.0-20200907165319-16ed0124890b h1:d1ttSIOWxWytnTpjO9hyr4KFtv/dmDBYuK59EP0sjAQ= gitlab.com/elixxir/primitives v0.0.0-20200907165319-16ed0124890b/go.mod h1:kNp47yPqja2lHSiS4DddTvFpB/4D9dB2YKnw5c+LJCE= +gitlab.com/elixxir/primitives v0.0.0-20200910160236-9ccbae60724d h1:XsJHSTmP/7hL5HgO6qztgUbR/rXJwZeulWH8XnYaVbk= +gitlab.com/elixxir/primitives v0.0.0-20200910160236-9ccbae60724d/go.mod h1:pJx2DZk9s8vVMnLN7x0hIPngDjbNSdOP6kk3RLlRxHg= +gitlab.com/elixxir/primitives v0.0.0-20200915181454-994a60142fce h1:TaDQ8jQlHECVxLi2xe0BqX6P9/9/Hz5zAVIJDm6CaxM= +gitlab.com/elixxir/primitives v0.0.0-20200915181454-994a60142fce/go.mod h1:kNp47yPqja2lHSiS4DddTvFpB/4D9dB2YKnw5c+LJCE= gitlab.com/xx_network/comms v0.0.0-20200805174823-841427dd5023/go.mod h1:owEcxTRl7gsoM8c3RQ5KAm5GstxrJp5tn+6JfQ4z5Hw= gitlab.com/xx_network/comms v0.0.0-20200806235452-3a82720833ba h1:7nozLSNBX0CfP53DDiDNLJx9obhYGfGf5na0/c9rMso= gitlab.com/xx_network/comms v0.0.0-20200806235452-3a82720833ba/go.mod h1:idLzPGYig57XE7xuU93OlIF9s6NgSJj7OArQvsd5DjY= @@ -235,6 +246,10 @@ gitlab.com/xx_network/comms v0.0.0-20200825213037-f58fa7c0a641 h1:d48S6FLIUJa1RM gitlab.com/xx_network/comms v0.0.0-20200825213037-f58fa7c0a641/go.mod h1:idLzPGYig57XE7xuU93OlIF9s6NgSJj7OArQvsd5DjY= gitlab.com/xx_network/comms v0.0.0-20200909172255-8dd37d862d9e h1:TUHn4Mg1hYHT5mMmCjsh6p+joJK2zwoLgq2x3LC2LUk= gitlab.com/xx_network/comms v0.0.0-20200909172255-8dd37d862d9e/go.mod h1:+jEkDQKoK51WLl2ZZuxfAZkz6YFbUQ+oZfH0dt2wIF0= +gitlab.com/xx_network/comms v0.0.0-20200910173932-bd179f5fee4f h1:ExTCqEoro7VuS1eOs889STiYfvR0m1RMDytwOHGW/+k= +gitlab.com/xx_network/comms v0.0.0-20200910173932-bd179f5fee4f/go.mod h1:+jEkDQKoK51WLl2ZZuxfAZkz6YFbUQ+oZfH0dt2wIF0= +gitlab.com/xx_network/comms v0.0.0-20200915154643-d533291041b7 h1:lPx1wpkjNpwLaZ0pyd7/iCcdITjT+eCMmb0HXCVoIkk= +gitlab.com/xx_network/comms v0.0.0-20200915154643-d533291041b7/go.mod h1:+jEkDQKoK51WLl2ZZuxfAZkz6YFbUQ+oZfH0dt2wIF0= gitlab.com/xx_network/crypto v0.0.0-20200806202113-978fa1984bbf/go.mod h1:i0df/q6dDCBiscgD51fMoS2U2TBrm6LcyN822JmB5Tw= gitlab.com/xx_network/crypto v0.0.0-20200806235322-ede3c15881ce h1:gypNBUl2guESEv4MDgH+miwYqR4jPoWM8dLt2Zs5gIs= gitlab.com/xx_network/crypto v0.0.0-20200806235322-ede3c15881ce/go.mod h1:i0df/q6dDCBiscgD51fMoS2U2TBrm6LcyN822JmB5Tw= diff --git a/network/send.go b/network/send.go index 12f6087f47790c33c98b95e4c4943a9c9e8fee2c..77c70bf1932dfee8368ad627079c8f9dae2c09d4 100644 --- a/network/send.go +++ b/network/send.go @@ -7,19 +7,10 @@ package network import ( - "github.com/golang-collections/collections/set" "github.com/pkg/errors" "gitlab.com/elixxir/client/context/message" "gitlab.com/elixxir/client/context/params" - "gitlab.com/elixxir/client/context/stoppable" - "gitlab.com/elixxir/comms/mixmessages" - "gitlab.com/elixxir/comms/network" - "gitlab.com/elixxir/crypto/csprng" - "gitlab.com/elixxir/primitives/format" - "gitlab.com/xx_network/comms/connect" "gitlab.com/xx_network/primitives/id" - "time" - jww "github.com/spf13/jwalterweatherman" ) // SendE2E sends an end-to-end payload to the provided recipient with @@ -46,109 +37,5 @@ func (m *Manager) SendUnsafe(msg message.Send) ([]id.Round, error) { return nil, nil } -// SendCMIX sends a "raw" CMIX message payload to the provided -// recipient. Note that both SendE2E and SendUnsafe call SendCMIX. -// Returns the round ID of the round the payload was sent or an error -// if it fails. -func (m *Manager) SendCMIX(msg format.Message, param params.CMIX) (id.Round, error) { - if !m.health.IsRunning() { - return 0, errors.New("Cannot send cmix message when the " + - "network is not healthy") - } - - return m.sendCMIX(msg, param) -} - -// Internal send e2e which bypasses the network check, for use in SendE2E and -// SendUnsafe which do their own network checks -func (m *Manager) sendCMIX(msg format.Message, param params.CMIX) (id.Round, error) { - - timeStart := time.Now() - attempted := set.New() - - for numRoundTries := uint(0); numRoundTries < param.RoundTries; numRoundTries++ { - elapsed := time.Now().Sub(timeStart) - if elapsed < param.Timeout { - return 0, errors.New("Sending cmix message timed out") - } - remainingTime := param.Timeout - elapsed - - //find the best round to send to, excluding roudn which have been attempted - bestRound, _ := m.instance.GetWaitingRounds().GetUpcomingRealtime(remainingTime, attempted) - topology, firstNode := buildToplogy(bestRound.Topology) - - //get they keys for the round, reject if any nodes do not have - //keying relationships - roundKeys, missingKeys := m.Context.Session.Cmix().GetRoundKeys(topology) - if len(missingKeys) > 0 { - go handleMissingNodeKeys(missingKeys) - continue - } - - //get the gateway to transmit to - firstGateway := firstNode.DeepCopy() - firstGateway.SetType(id.Gateway) - transmitGateway, ok := m.Comms.GetHost(firstGateway) - if !ok { - jww.ERROR.Printf("Failed to get host for gateway %s", transmitGateway) - continue - } - - //cutoff - - //encrypt the message - salt := make([]byte, 32) - _, err := csprng.NewSystemRNG().Read(salt) - if err != nil { - return 0, errors.WithMessage(err, "Failed to generate "+ - "salt, this should never happen") - } - - encMsg, kmacs := roundKeys.Encrypt(msg, salt) - - //build the message payload - msgPacket := &mixmessages.Slot{ - SenderID: m.uid.Bytes(), - PayloadA: encMsg.GetPayloadA(), - PayloadB: encMsg.GetPayloadB(), - Salt: salt, - KMACs: kmacs, - } - - //create the wrapper to the gateway - msg := &mixmessages.GatewaySlot{ - Message: msgPacket, - RoundID: bestRound.ID, - } - - //Add the mac proving ownership - msg.MAC = roundKeys.MakeClientGatewayKey(salt, network.GenerateSlotDigest(msg)) - - //Send the payload - gwSlotResp, err := m.Comms.SendPutMessage(transmitGateway, msg) - //if the comm errors or the message fails to send, continue retrying. - //return if it sends properly - if err != nil { - jww.ERROR.Printf("Failed to send message to %s: %s", - transmitGateway, err) - } else if gwSlotResp.Accepted { - return id.Round(bestRound.ID), nil - } - - //add the round on to the list of attempted so it is not tried again - attempted.Insert(bestRound) - } - - return 0, errors.New("failed to send the message") -} - -func buildToplogy(nodes [][]byte) (*connect.Circuit, *id.ID) { - idList := make([]*id.ID, len(nodes)) - for i, n := range nodes { - - } - -} -func handleMissingNodeKeys(nodes []*id.ID) {} diff --git a/network/sendCmix.go b/network/sendCmix.go index 1ae2e9d505591696853ae1c2f171bd6c7edcfbe1..31e7639c8ba7f3e74f5b5831b5d6d29a58b981be 100644 --- a/network/sendCmix.go +++ b/network/sendCmix.go @@ -1 +1,130 @@ package network + +import ( + "github.com/golang-collections/collections/set" + "github.com/pkg/errors" + jww "github.com/spf13/jwalterweatherman" + "gitlab.com/elixxir/client/context/params" + "gitlab.com/elixxir/comms/mixmessages" + "gitlab.com/elixxir/comms/network" + "gitlab.com/elixxir/crypto/csprng" + "gitlab.com/elixxir/primitives/format" + "gitlab.com/xx_network/comms/connect" + "gitlab.com/xx_network/primitives/id" + "time" +) + +// SendCMIX sends a "raw" CMIX message payload to the provided +// recipient. Note that both SendE2E and SendUnsafe call SendCMIX. +// Returns the round ID of the round the payload was sent or an error +// if it fails. +func (m *Manager) SendCMIX(msg format.Message, param params.CMIX) (id.Round, error) { + if !m.health.IsRunning() { + return 0, errors.New("Cannot send cmix message when the " + + "network is not healthy") + } + + return m.sendCMIX(msg, param) +} + +// Internal send e2e which bypasses the network check, for use in SendE2E and +// SendUnsafe which do their own network checks +func (m *Manager) sendCMIX(msg format.Message, param params.CMIX) (id.Round, error) { + + timeStart := time.Now() + attempted := set.New() + + for numRoundTries := uint(0); numRoundTries < param.RoundTries; numRoundTries++ { + elapsed := time.Now().Sub(timeStart) + if elapsed < param.Timeout { + return 0, errors.New("Sending cmix message timed out") + } + remainingTime := param.Timeout - elapsed + + //find the best round to send to, excluding roudn which have been attempted + bestRound, _ := m.instance.GetWaitingRounds().GetUpcomingRealtime(remainingTime, attempted) + topology, err := buildToplogy(bestRound.Topology) + if err == nil { + jww.ERROR.Printf("Failed to use topology for round %v: %s", bestRound.ID, err) + continue + } + + //get they keys for the round, reject if any nodes do not have + //keying relationships + roundKeys, missingKeys := m.Context.Session.Cmix().GetRoundKeys(topology) + if len(missingKeys) > 0 { + go handleMissingNodeKeys(missingKeys) + continue + } + + //get the gateway to transmit to + firstGateway := topology.GetNodeAtIndex(0).DeepCopy() + firstGateway.SetType(id.Gateway) + transmitGateway, ok := m.Comms.GetHost(firstGateway) + if !ok { + jww.ERROR.Printf("Failed to get host for gateway %s", transmitGateway) + continue + } + + //encrypt the message + salt := make([]byte, 32) + _, err := csprng.NewSystemRNG().Read(salt) + if err != nil { + return 0, errors.WithMessage(err, "Failed to generate "+ + "salt, this should never happen") + } + + encMsg, kmacs := roundKeys.Encrypt(msg, salt) + + //build the message payload + msgPacket := &mixmessages.Slot{ + SenderID: m.uid.Bytes(), + PayloadA: encMsg.GetPayloadA(), + PayloadB: encMsg.GetPayloadB(), + Salt: salt, + KMACs: kmacs, + } + + //create the wrapper to the gateway + msg := &mixmessages.GatewaySlot{ + Message: msgPacket, + RoundID: bestRound.ID, + } + + //Add the mac proving ownership + msg.MAC = roundKeys.MakeClientGatewayKey(salt, network.GenerateSlotDigest(msg)) + + //Send the payload + gwSlotResp, err := m.Comms.SendPutMessage(transmitGateway, msg) + //if the comm errors or the message fails to send, continue retrying. + //return if it sends properly + if err != nil { + jww.ERROR.Printf("Failed to send message to %s: %s", + transmitGateway, err) + } else if gwSlotResp.Accepted { + return id.Round(bestRound.ID), nil + } + + //add the round on to the list of attempted so it is not tried again + attempted.Insert(bestRound) + } + + return 0, errors.New("failed to send the message") +} + +func buildToplogy(nodes [][]byte) (*connect.Circuit, error) { + idList := make([]*id.ID, len(nodes)) + for i, n := range nodes { + nid, err := id.Unmarshal(n) + if err != nil { + return nil, errors.WithMessagef(err, "Failed to "+ + "convert topology on node %v/%v {raw id: %v}", i, len(nodes), n) + } + idList[i] = nid + } + topology := connect.NewCircuit(idList) + return topology, nil + +} + +func handleMissingNodeKeys(nodes []*id.ID) {} diff --git a/network/sendUnsafe.go b/network/sendUnsafe.go new file mode 100644 index 0000000000000000000000000000000000000000..1ae2e9d505591696853ae1c2f171bd6c7edcfbe1 --- /dev/null +++ b/network/sendUnsafe.go @@ -0,0 +1 @@ +package network diff --git a/storage/e2e/key_test.go b/storage/e2e/key_test.go index a40addefa57bdf58f4b40f7778fc94998c44f1a5..00fc39822d65eae78fed0479a19b17f828a66426 100644 --- a/storage/e2e/key_test.go +++ b/storage/e2e/key_test.go @@ -194,10 +194,9 @@ func getSession(t *testing.T) *Session { ctx := &context{ fa: &fps, grp: grp, - kv: versioned.NewKV(make(ekv.Memstore)), } - keyState, err := newStateVector(ctx, "keyState", rand.Uint32()) + keyState, err := newStateVector(versioned.NewKV(make(ekv.Memstore)), "keyState", rand.Uint32()) if err != nil { panic(err) } diff --git a/storage/e2e/session.go b/storage/e2e/session.go index d690de2eacb41b632cc89aad79b7e5cf337e4eed..92752f9530196aee44db410b2be7176452f545a3 100644 --- a/storage/e2e/session.go +++ b/storage/e2e/session.go @@ -12,7 +12,6 @@ import ( "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/storage/versioned" - "gitlab.com/elixxir/crypto/csprng" "gitlab.com/elixxir/crypto/cyclic" dh "gitlab.com/elixxir/crypto/diffieHellman" "gitlab.com/elixxir/crypto/e2e" @@ -489,8 +488,10 @@ func (s *Session) generate(kv *versioned.KV) *versioned.KV { //generate private key if it is not present if s.myPrivKey == nil { + stream := s.manager.ctx.rng.GetStream() s.myPrivKey = dh.GeneratePrivateKey(dh.DefaultPrivateKeyLength, grp, - csprng.NewSystemRNG()) + stream) + stream.Close() } // compute the base key if it is not already there diff --git a/storage/e2e/sessionBuff.go b/storage/e2e/sessionBuff.go index f700f130cd65e594b8e68022bdb3aa4b52a04f38..5f47d8412ffe2ec60c3a71ab26f96c5b43f4e095 100644 --- a/storage/e2e/sessionBuff.go +++ b/storage/e2e/sessionBuff.go @@ -128,7 +128,7 @@ func (sb *sessionBuff) AddSession(s *Session) { sb.addSession(s) if err := sb.save(); err != nil { - key := makeSessionBuffKey(sb.key, sb.manager.partner) + key := makeSessionBuffKey(sb.key) jww.FATAL.Printf("Failed to save Session Buffer %s after "+ "adding session %s: %s", key, s, err) } diff --git a/storage/e2e/session_test.go b/storage/e2e/session_test.go index b0de6c52c2543c2326cacf2d0ad170f0d7cca1e0..663e3d344f73cb50aa9d82fd539246bf77cc2cc8 100644 --- a/storage/e2e/session_test.go +++ b/storage/e2e/session_test.go @@ -5,6 +5,7 @@ import ( "gitlab.com/elixxir/client/storage/versioned" "gitlab.com/elixxir/crypto/csprng" dh "gitlab.com/elixxir/crypto/diffieHellman" + "gitlab.com/elixxir/crypto/fastRNG" "gitlab.com/elixxir/ekv" "gitlab.com/xx_network/primitives/id" "reflect" @@ -24,7 +25,7 @@ func TestSession_generate_noPrivateKeyReceive(t *testing.T) { ctx := &context{ fa: &fps, grp: grp, - kv: versioned.NewKV(make(ekv.Memstore)), + rng: fastRNG.NewStreamGenerator(1, 0, csprng.NewSystemRNG), } //build the session @@ -38,7 +39,7 @@ func TestSession_generate_noPrivateKeyReceive(t *testing.T) { } //run the generate command - s.generate() + s.generate(versioned.NewKV(make(ekv.Memstore))) //check that it generated a private key if s.myPrivKey == nil { @@ -85,7 +86,6 @@ func TestSession_generate_PrivateKeySend(t *testing.T) { ctx := &context{ fa: &fps, grp: grp, - kv: versioned.NewKV(make(ekv.Memstore)), } //build the session @@ -100,7 +100,7 @@ func TestSession_generate_PrivateKeySend(t *testing.T) { } //run the generate command - s.generate() + s.generate(versioned.NewKV(make(ekv.Memstore))) //check that it generated a private key if s.myPrivKey.Cmp(myPrivKey) != 0 { @@ -165,7 +165,7 @@ func TestSession_Load(t *testing.T) { t.Fatal(err) } // Load another, hopefully identical session from the storage - sessionB, err := loadSession(sessionA.manager, makeSessionKey(sessionA.GetID())) + sessionB, err := loadSession(sessionA.manager, versioned.NewKV(make(ekv.Memstore))) if err != nil { t.Fatal(err) } @@ -296,11 +296,11 @@ func TestSession_Delete(t *testing.T) { s.Delete() // Getting the keys that should have been stored should now result in an error - _, err = s.manager.ctx.kv.Get(makeStateVectorKey(keyEKVPrefix, s.GetID())) + _, err = s.kv.Get(stateVectorKey) if err == nil { t.Error("State vector was gettable") } - _, err = s.manager.ctx.kv.Get(makeSessionKey(s.GetID())) + _, err = s.kv.Get(sessionKey) if err == nil { t.Error("Session was gettable") } @@ -312,10 +312,10 @@ func TestSession_Delete(t *testing.T) { // that will also get caught by the other error first. So it's only practical // to test the one error. func TestSession_PopKey_Error(t *testing.T) { - s, ctx := makeTestSession(t) + s, _ := makeTestSession(t) // Construct a specific state vector that will quickly run out of keys var err error - s.keyState, err = newStateVector(ctx, makeStateVectorKey(keyEKVPrefix, s.GetID()), 0) + s.keyState, err = newStateVector(s.kv, "", 0) if err != nil { t.Fatal(err) } @@ -349,10 +349,10 @@ func TestSession_PopReKey(t *testing.T) { // PopRekey should not return the next key if there are no more keys available // in the state vector func TestSession_PopReKey_Err(t *testing.T) { - s, ctx := makeTestSession(t) + s, _ := makeTestSession(t) // Construct a specific state vector that will quickly run out of keys var err error - s.keyState, err = newStateVector(ctx, makeStateVectorKey(keyEKVPrefix, s.GetID()), 0) + s.keyState, err = newStateVector(s.kv, "", 0) if err != nil { t.Fatal(err) } @@ -413,9 +413,9 @@ func TestSession_IsConfirmed(t *testing.T) { // Shows that Status can result in all possible statuses func TestSession_Status(t *testing.T) { - s, ctx := makeTestSession(t) + s, _ := makeTestSession(t) var err error - s.keyState, err = newStateVector(ctx, makeStateVectorKey(keyEKVPrefix, s.GetID()), 500) + s.keyState, err = newStateVector(s.kv, "", 500) if err != nil { t.Fatal(err) } @@ -441,7 +441,7 @@ func TestSession_Status(t *testing.T) { // Tests that state transitions as documented don't cause panics // Tests that the session saves or doesn't save when appropriate func TestSession_SetNegotiationStatus(t *testing.T) { - s, ctx := makeTestSession(t) + s, _ := makeTestSession(t) // Normal paths: SetNegotiationStatus should not fail // Use timestamps to determine whether a save has occurred s.negotiationStatus = Sending @@ -451,7 +451,7 @@ func TestSession_SetNegotiationStatus(t *testing.T) { if s.negotiationStatus != Sent { t.Error("SetNegotiationStatus didn't set the negotiation status") } - object, err := ctx.kv.Get(makeSessionKey(s.GetID())) + object, err := s.kv.Get(sessionKey) if err != nil { t.Fatal(err) } @@ -465,7 +465,7 @@ func TestSession_SetNegotiationStatus(t *testing.T) { if s.negotiationStatus != Confirmed { t.Error("SetNegotiationStatus didn't set the negotiation status") } - object, err = ctx.kv.Get(makeSessionKey(s.GetID())) + object, err = s.kv.Get(sessionKey) if err != nil { t.Fatal(err) } @@ -480,7 +480,7 @@ func TestSession_SetNegotiationStatus(t *testing.T) { if s.negotiationStatus != NewSessionCreated { t.Error("SetNegotiationStatus didn't set the negotiation status") } - object, err = ctx.kv.Get(makeSessionKey(s.GetID())) + object, err = s.kv.Get(sessionKey) if err != nil { t.Fatal(err) } @@ -497,7 +497,7 @@ func TestSession_SetNegotiationStatus(t *testing.T) { if s.negotiationStatus != Unconfirmed { t.Error("SetNegotiationStatus didn't set the negotiation status") } - object, err = ctx.kv.Get(makeSessionKey(s.GetID())) + object, err = s.kv.Get(sessionKey) if err != nil { t.Fatal(err) } @@ -510,7 +510,7 @@ func TestSession_SetNegotiationStatus(t *testing.T) { if s.negotiationStatus != Confirmed { t.Error("SetNegotiationStatus didn't set the negotiation status") } - object, err = ctx.kv.Get(makeSessionKey(s.GetID())) + object, err = s.kv.Get(sessionKey) if err != nil { t.Fatal(err) } @@ -600,9 +600,10 @@ func makeTestSession(t *testing.T) (*Session, *context) { ctx := &context{ fa: &fps, grp: grp, - kv: versioned.NewKV(make(ekv.Memstore)), } + kv := versioned.NewKV(make(ekv.Memstore)) + s := &Session{ baseKey: baseKey, myPrivKey: myPrivKey, @@ -610,13 +611,16 @@ func makeTestSession(t *testing.T) (*Session, *context) { params: GetDefaultSessionParams(), manager: &Manager{ ctx: ctx, + kv: kv, }, + kv: kv, t: Receive, negotiationStatus: Confirmed, ttl: 5, } var err error - s.keyState, err = newStateVector(ctx, makeStateVectorKey(keyEKVPrefix, s.GetID()), 1024) + s.keyState, err = newStateVector(s.kv, + "", 1024) if err != nil { panic(err) } diff --git a/storage/e2e/stateVector_test.go b/storage/e2e/stateVector_test.go index 164d0eeb1d11f35360c2cd803b2ba41344313ea4..063dcadfe66ffbc93a108aa35a1b15d2bbeaf5a6 100644 --- a/storage/e2e/stateVector_test.go +++ b/storage/e2e/stateVector_test.go @@ -37,11 +37,8 @@ func TestStateVector_GetNumUsed(t *testing.T) { } func TestStateVector_GetNumKeys(t *testing.T) { - ctx := context{ - kv: versioned.NewKV(make(ekv.Memstore)), - } const numKeys = 32 - sv, err := newStateVector(&ctx, "key", numKeys) + sv, err := newStateVector(versioned.NewKV(make(ekv.Memstore)), "key", numKeys) if err != nil { t.Fatal(err) } @@ -58,11 +55,8 @@ func TestStateVector_Next(t *testing.T) { // Expected results: all keynums, and beyond the last key expectedFirstAvail := []uint32{139, 145, 300, 360, 420, 761, 868, 875, 893, 995} - ctx := context{ - kv: versioned.NewKV(make(ekv.Memstore)), - } const numKeys = 1000 - sv, err := newStateVector(&ctx, "key", numKeys) + sv, err := newStateVector(versioned.NewKV(make(ekv.Memstore)), "key", numKeys) if err != nil { t.Fatal(err) } @@ -114,11 +108,8 @@ func TestStateVector_Use(t *testing.T) { // These keyNums will be set to dirty with Use keyNums := []uint32{139, 145, 300, 360, 420, 761, 868, 875, 893, 995} - ctx := context{ - kv: versioned.NewKV(make(ekv.Memstore)), - } const numKeys = 1000 - sv, err := newStateVector(&ctx, "key", numKeys) + sv, err := newStateVector(versioned.NewKV(make(ekv.Memstore)), "key", numKeys) if err != nil { t.Fatal(err) } @@ -150,11 +141,8 @@ func TestStateVector_Used(t *testing.T) { // These keyNums should be used keyNums := []uint32{139, 145, 300, 360, 420, 761, 868, 875, 893, 995} - ctx := context{ - kv: versioned.NewKV(make(ekv.Memstore)), - } const numKeys = 1000 - sv, err := newStateVector(&ctx, "key", numKeys) + sv, err := newStateVector(versioned.NewKV(make(ekv.Memstore)), "key", numKeys) if err != nil { t.Fatal(err) } @@ -181,11 +169,8 @@ func TestStateVector_GetUsedKeyNums(t *testing.T) { // These keyNums should be used keyNums := []uint32{139, 145, 300, 360, 420, 761, 868, 875, 893, 995} - ctx := context{ - kv: versioned.NewKV(make(ekv.Memstore)), - } const numKeys = 1000 - sv, err := newStateVector(&ctx, "key", numKeys) + sv, err := newStateVector(versioned.NewKV(make(ekv.Memstore)), "key", numKeys) if err != nil { t.Fatal(err) } @@ -205,11 +190,8 @@ func TestStateVector_GetUnusedKeyNums(t *testing.T) { // These keyNums should not be used keyNums := []uint32{139, 145, 300, 360, 420, 761, 868, 875, 893, 995} - ctx := context{ - kv: versioned.NewKV(make(ekv.Memstore)), - } const numKeys = 1000 - sv, err := newStateVector(&ctx, "key", numKeys) + sv, err := newStateVector(versioned.NewKV(make(ekv.Memstore)), "key", numKeys) if err != nil { t.Fatal(err) } @@ -232,10 +214,8 @@ func TestStateVector_GetUnusedKeyNums(t *testing.T) { func TestLoadStateVector(t *testing.T) { keyNums := []uint32{139, 145, 300, 360, 420, 761, 868, 875, 893, 995} const numKeys = 1000 - ctx := context{ - kv: versioned.NewKV(make(ekv.Memstore)), - } - sv, err := newStateVector(&ctx, "key", numKeys) + + sv, err := newStateVector(versioned.NewKV(make(ekv.Memstore)), "key", numKeys) if err != nil { t.Fatal(err) } @@ -247,7 +227,7 @@ func TestLoadStateVector(t *testing.T) { if err != nil { t.Fatal(err) } - sv2, err := loadStateVector(&ctx, "key") + sv2, err := loadStateVector(versioned.NewKV(make(ekv.Memstore)), "key") if err != nil { t.Fatal(err) }