Skip to content

Commit

Permalink
Merge pull request #103 from gregdhill/test-length-prefix
Browse files Browse the repository at this point in the history
add service test for downloading embedded metadata
  • Loading branch information
Greg Hill authored Mar 9, 2020
2 parents ba06ab7 + 299367e commit 9e1abfc
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions service_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package hoard

import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"testing"

"github.com/go-kit/kit/log"
Expand All @@ -16,6 +19,22 @@ import (
"google.golang.org/grpc"
)

func writeUIntBE(buffer []byte, value, offset, byteLength int64) error {
slice := make([]byte, byteLength)

buf := bytes.NewBuffer(slice)
err := binary.Write(buf, binary.BigEndian, value)
if err != nil {
return err
}

slice = buf.Bytes()
slice = slice[int64(len(slice))-byteLength : len(slice)]

copy(buffer[offset:], slice)
return nil
}

func TestService(t *testing.T) {
chunkSize := 67
salt, err := encryption.NewNonce(encryption.NonceSize)
Expand Down Expand Up @@ -71,6 +90,52 @@ func TestService(t *testing.T) {
require.NoError(t, err)
require.Equal(t, data, plaintext.GetBody())
})

t.Run("LengthPrefix", func(t *testing.T) {
const lengthPrefixByteLength = 4

meta, err := json.Marshal(&struct {
Name string
MimeType string
Tags []string
Agreement string
AssemblyEngine string
}{
Name: "document",
MimeType: ".docx",
})
require.NoError(t, err)

buffer := make([]byte, lengthPrefixByteLength)
err = writeUIntBE(buffer, int64(len(meta)), 0, lengthPrefixByteLength)
require.NoError(t, err)

data := make([]byte, 1000)
msg := append(buffer, meta...)
msg = append(msg, data...)

ref, err := service.grantService.Put(msg, []byte{})
require.NoError(t, err)

client := api.NewCleartextClient(conn)
getStream, err := client.Get(ctx)
require.NoError(t, err)
err = getStream.Send(ref)
require.NoError(t, err)
err = getStream.CloseSend()
require.NoError(t, err)

plaintext, err := ReceiveAllPlaintexts(getStream)
require.NoError(t, err)
require.Nil(t, plaintext.GetHead())
body := plaintext.GetBody()
size := binary.BigEndian.Uint32(body[:lengthPrefixByteLength])
head := body[lengthPrefixByteLength : size+lengthPrefixByteLength]
rest := body[size+lengthPrefixByteLength:]

require.Equal(t, meta, head)
require.Equal(t, data, rest)
})
})

t.Run("Grants", func(t *testing.T) {
Expand Down

0 comments on commit 9e1abfc

Please sign in to comment.