f96cc9c955
* HTTPRanger * Migrate eestream library to use ReadCloser instead of Reader * Use MultiReadCloser instead of MultiReader * Adapt example cli cmds to latest serverbox changes * Close LazyReadCloser only if only generated * Close ReadClosers sequentially * Close response body on unexpected status code * Avoid double close of the pipe reader * Better formatting for imports * Exit instead of panicking
47 lines
990 B
Go
47 lines
990 B
Go
// Copyright (C) 2018 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package eestream
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"io/ioutil"
|
|
"testing"
|
|
)
|
|
|
|
func randData(amount int) []byte {
|
|
buf := make([]byte, amount)
|
|
_, err := rand.Read(buf)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return buf
|
|
}
|
|
|
|
func TestSecretbox(t *testing.T) {
|
|
var key [32]byte
|
|
copy(key[:], randData(32))
|
|
var firstNonce [24]byte
|
|
copy(firstNonce[:], randData(24))
|
|
encrypter, err := NewSecretboxEncrypter(&key, &firstNonce, 4*1024)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data := randData(encrypter.InBlockSize() * 10)
|
|
encrypted := TransformReader(
|
|
ioutil.NopCloser(bytes.NewReader(data)), encrypter, 0)
|
|
decrypter, err := NewSecretboxDecrypter(&key, &firstNonce, 4*1024)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
decrypted := TransformReader(encrypted, decrypter, 0)
|
|
data2, err := ioutil.ReadAll(decrypted)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.Equal(data, data2) {
|
|
t.Fatalf("encryption/decryption failed")
|
|
}
|
|
}
|