* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
395 lines
9.3 KiB
Go
395 lines
9.3 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package bsr_test
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/boundary/internal/bsr"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type testChunk struct {
|
|
*bsr.BaseChunk
|
|
Data []byte
|
|
err error
|
|
}
|
|
|
|
// MarshalData serializes the data portion of a chunk.
|
|
func (t *testChunk) MarshalData(_ context.Context) ([]byte, error) {
|
|
if t.err != nil {
|
|
return nil, t.err
|
|
}
|
|
return t.Data, nil
|
|
}
|
|
|
|
func gziped(d string) string {
|
|
var buf bytes.Buffer
|
|
w := gzip.NewWriter(&buf)
|
|
w.Write([]byte(d))
|
|
w.Close()
|
|
return buf.String()
|
|
}
|
|
|
|
func TestChunkEncoder(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
ts := time.Date(2023, time.March, 16, 10, 47, 3, 14, time.UTC)
|
|
|
|
cases := []struct {
|
|
name string
|
|
c bsr.Compression
|
|
e bsr.Encryption
|
|
chunks []bsr.Chunk
|
|
want []byte
|
|
}{
|
|
{
|
|
"header-no-compression",
|
|
bsr.NoCompression,
|
|
bsr.NoEncryption,
|
|
[]bsr.Chunk{
|
|
&bsr.HeaderChunk{
|
|
BaseChunk: &bsr.BaseChunk{
|
|
Protocol: "TEST",
|
|
Direction: bsr.Inbound,
|
|
Timestamp: bsr.NewTimestamp(ts),
|
|
Type: bsr.ChunkHeader,
|
|
},
|
|
Compression: bsr.NoCompression,
|
|
Encryption: bsr.NoEncryption,
|
|
SessionId: "sess_123456789",
|
|
},
|
|
},
|
|
[]byte(
|
|
"" + // so everything else aligns better
|
|
"\x00\x00\x00\x10" + // length
|
|
"TEST" + // protocol
|
|
"HEAD" + // type
|
|
"\x01" + // direction
|
|
"\x00\x00\x00\x00\x64\x12\xf3\xa7" + // time seconds
|
|
"\x00\x00\x00\x0e" + // time nanoseconds
|
|
"\x00" + // compression method
|
|
"\x00" + // encryption method
|
|
"sess_123456789" + // data
|
|
"\xbe\x4c\x7c\x20" + // crc
|
|
"",
|
|
),
|
|
},
|
|
{
|
|
"header-end-no-compression",
|
|
bsr.NoCompression,
|
|
bsr.NoEncryption,
|
|
[]bsr.Chunk{
|
|
&bsr.HeaderChunk{
|
|
BaseChunk: &bsr.BaseChunk{
|
|
Protocol: "TEST",
|
|
Direction: bsr.Inbound,
|
|
Timestamp: bsr.NewTimestamp(ts),
|
|
Type: bsr.ChunkHeader,
|
|
},
|
|
Compression: bsr.NoCompression,
|
|
Encryption: bsr.NoEncryption,
|
|
SessionId: "sess_123456789",
|
|
},
|
|
&bsr.EndChunk{
|
|
BaseChunk: &bsr.BaseChunk{
|
|
Protocol: "TEST",
|
|
Direction: bsr.Inbound,
|
|
Timestamp: bsr.NewTimestamp(ts.Add(time.Nanosecond * 5)),
|
|
Type: bsr.ChunkEnd,
|
|
},
|
|
},
|
|
},
|
|
[]byte(
|
|
"" + // header
|
|
"\x00\x00\x00\x10" + // length
|
|
"TEST" + // protocol
|
|
"HEAD" + // type
|
|
"\x01" + // direction
|
|
"\x00\x00\x00\x00\x64\x12\xf3\xa7" + // time seconds
|
|
"\x00\x00\x00\x0e" + // time nanoseconds
|
|
"\x00" + // compression method
|
|
"\x00" + // encryption method
|
|
"sess_123456789" + // data
|
|
"\xbe\x4c\x7c\x20" + // crc
|
|
"" + // end
|
|
"\x00\x00\x00\x00" + // length
|
|
"TEST" + // protocol
|
|
"DONE" + // type
|
|
"\x01" + // direction
|
|
"\x00\x00\x00\x00\x64\x12\xf3\xa7" + // time seconds
|
|
"\x00\x00\x00\x13" + // time nanoseconds
|
|
"\x50\x91\xfe\x72" + // crc
|
|
"",
|
|
),
|
|
},
|
|
{
|
|
"header-test-end-no-compression",
|
|
bsr.NoCompression,
|
|
bsr.NoEncryption,
|
|
[]bsr.Chunk{
|
|
&bsr.HeaderChunk{
|
|
BaseChunk: &bsr.BaseChunk{
|
|
Protocol: "TEST",
|
|
Direction: bsr.Inbound,
|
|
Timestamp: bsr.NewTimestamp(ts),
|
|
Type: bsr.ChunkHeader,
|
|
},
|
|
Compression: bsr.NoCompression,
|
|
Encryption: bsr.NoEncryption,
|
|
SessionId: "sess_123456789",
|
|
},
|
|
&testChunk{
|
|
BaseChunk: &bsr.BaseChunk{
|
|
Protocol: "TEST",
|
|
Direction: bsr.Inbound,
|
|
Timestamp: bsr.NewTimestamp(ts),
|
|
Type: "TEST",
|
|
},
|
|
Data: []byte("foo"),
|
|
},
|
|
&bsr.EndChunk{
|
|
BaseChunk: &bsr.BaseChunk{
|
|
Protocol: "TEST",
|
|
Direction: bsr.Inbound,
|
|
Timestamp: bsr.NewTimestamp(ts.Add(time.Nanosecond * 5)),
|
|
Type: bsr.ChunkEnd,
|
|
},
|
|
},
|
|
},
|
|
[]byte(
|
|
"" + // header
|
|
"\x00\x00\x00\x10" + // length
|
|
"TEST" + // protocol
|
|
"HEAD" + // type
|
|
"\x01" + // direction
|
|
"\x00\x00\x00\x00\x64\x12\xf3\xa7" + // time seconds
|
|
"\x00\x00\x00\x0e" + // time nanoseconds
|
|
"\x00" + // compression method
|
|
"\x00" + // encryption method
|
|
"sess_123456789" + // data
|
|
"\xbe\x4c\x7c\x20" + // crc
|
|
"" + // test
|
|
"\x00\x00\x00\x03" + // length
|
|
"TEST" + // protocol
|
|
"TEST" + // type
|
|
"\x01" + // direction
|
|
"\x00\x00\x00\x00\x64\x12\xf3\xa7" + // time seconds
|
|
"\x00\x00\x00\x0e" + // time nanoseconds
|
|
"foo" + // data
|
|
"\xa4\x6e\x48\x70" + // crc
|
|
"" + // end
|
|
"\x00\x00\x00\x00" + // length
|
|
"TEST" + // protocol
|
|
"DONE" + // type
|
|
"\x01" + // direction
|
|
"\x00\x00\x00\x00\x64\x12\xf3\xa7" + // time seconds
|
|
"\x00\x00\x00\x13" + // time nanoseconds
|
|
"\x50\x91\xfe\x72" + // crc
|
|
"",
|
|
),
|
|
},
|
|
{
|
|
"header-test-end-gzip-compression",
|
|
bsr.GzipCompression,
|
|
bsr.NoEncryption,
|
|
[]bsr.Chunk{
|
|
&bsr.HeaderChunk{
|
|
BaseChunk: &bsr.BaseChunk{
|
|
Protocol: "TEST",
|
|
Direction: bsr.Inbound,
|
|
Timestamp: bsr.NewTimestamp(ts),
|
|
Type: bsr.ChunkHeader,
|
|
},
|
|
Compression: bsr.GzipCompression,
|
|
Encryption: bsr.NoEncryption,
|
|
SessionId: "sess_123456789",
|
|
},
|
|
&testChunk{
|
|
BaseChunk: &bsr.BaseChunk{
|
|
Protocol: "TEST",
|
|
Direction: bsr.Inbound,
|
|
Timestamp: bsr.NewTimestamp(ts),
|
|
Type: "TEST",
|
|
},
|
|
Data: []byte("foo"),
|
|
},
|
|
&bsr.EndChunk{
|
|
BaseChunk: &bsr.BaseChunk{
|
|
Protocol: "TEST",
|
|
Direction: bsr.Inbound,
|
|
Timestamp: bsr.NewTimestamp(ts.Add(time.Nanosecond * 5)),
|
|
Type: bsr.ChunkEnd,
|
|
},
|
|
},
|
|
},
|
|
[]byte(
|
|
"" + // header
|
|
"\x00\x00\x00\x10" + // length
|
|
"TEST" + // protocol
|
|
"HEAD" + // type
|
|
"\x01" + // direction
|
|
"\x00\x00\x00\x00\x64\x12\xf3\xa7" + // time seconds
|
|
"\x00\x00\x00\x0e" + // time nanoseconds
|
|
"\x01" + // compression method
|
|
"\x00" + // encryption method
|
|
"sess_123456789" + // data
|
|
"\x10\x24\xed\xb1" + // crc
|
|
"" + // test
|
|
"\x00\x00\x00\x1b" + // length
|
|
"TEST" + // protocol
|
|
"TEST" + // type
|
|
"\x01" + // direction
|
|
"\x00\x00\x00\x00\x64\x12\xf3\xa7" + // time seconds
|
|
"\x00\x00\x00\x0e" + // time nanoseconds
|
|
gziped("foo") + // data
|
|
"\x29\x8e\x22\x12" + // crc
|
|
"" + // end
|
|
"\x00\x00\x00\x00" + // length
|
|
"TEST" + // protocol
|
|
"DONE" + // type
|
|
"\x01" + // direction
|
|
"\x00\x00\x00\x00\x64\x12\xf3\xa7" + // time seconds
|
|
"\x00\x00\x00\x13" + // time nanoseconds
|
|
"\x50\x91\xfe\x72" + // crc
|
|
"",
|
|
),
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
enc, err := bsr.NewChunkEncoder(ctx, &buf, tc.c, tc.e)
|
|
require.NoError(t, err)
|
|
|
|
var wrote int
|
|
for _, c := range tc.chunks {
|
|
w, err := enc.Encode(ctx, c)
|
|
require.NoError(t, err)
|
|
wrote += w
|
|
}
|
|
err = enc.Close()
|
|
require.NoError(t, err)
|
|
|
|
got := buf.Bytes()
|
|
assert.Equal(t, len(tc.want), wrote)
|
|
assert.Equal(t, tc.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
type errorWriter struct{}
|
|
|
|
func (e errorWriter) Write(_ []byte) (int, error) {
|
|
return 0, fmt.Errorf("write error")
|
|
}
|
|
|
|
func TestChunkEncoderEncodeError(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
ts := time.Date(2023, time.March, 16, 10, 47, 3, 14, time.UTC)
|
|
|
|
cases := []struct {
|
|
name string
|
|
w io.Writer
|
|
c bsr.Compression
|
|
e bsr.Encryption
|
|
chunk bsr.Chunk
|
|
want error
|
|
}{
|
|
{
|
|
"chunk-marshal-error",
|
|
func() io.Writer { var buf bytes.Buffer; return &buf }(),
|
|
bsr.NoCompression,
|
|
bsr.NoEncryption,
|
|
&testChunk{
|
|
BaseChunk: &bsr.BaseChunk{
|
|
Protocol: "TEST",
|
|
Direction: bsr.Inbound,
|
|
Timestamp: bsr.NewTimestamp(ts),
|
|
Type: "TEST",
|
|
},
|
|
err: fmt.Errorf("marshal error"),
|
|
},
|
|
fmt.Errorf("marshal error"),
|
|
},
|
|
{
|
|
"writer-error",
|
|
func() io.Writer { return errorWriter{} }(),
|
|
bsr.NoCompression,
|
|
bsr.NoEncryption,
|
|
&testChunk{
|
|
BaseChunk: &bsr.BaseChunk{
|
|
Protocol: "TEST",
|
|
Direction: bsr.Inbound,
|
|
Timestamp: bsr.NewTimestamp(ts),
|
|
Type: "TEST",
|
|
},
|
|
Data: []byte("foo"),
|
|
},
|
|
fmt.Errorf("write error"),
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
enc, err := bsr.NewChunkEncoder(ctx, tc.w, tc.c, tc.e)
|
|
require.NoError(t, err)
|
|
|
|
_, err = enc.Encode(ctx, tc.chunk)
|
|
assert.EqualError(t, tc.want, err.Error())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestChunkEncoderErrors(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
cases := []struct {
|
|
name string
|
|
w io.Writer
|
|
c bsr.Compression
|
|
e bsr.Encryption
|
|
want error
|
|
}{
|
|
{
|
|
"invalid-compression",
|
|
func() io.Writer { var buf bytes.Buffer; return &buf }(),
|
|
bsr.Compression(255),
|
|
bsr.NoEncryption,
|
|
errors.New("bsr.NewChunkEncoder: invalid compression: invalid parameter"),
|
|
},
|
|
{
|
|
"invalid-encryption",
|
|
func() io.Writer { var buf bytes.Buffer; return &buf }(),
|
|
bsr.NoCompression,
|
|
bsr.Encryption(255),
|
|
errors.New("bsr.NewChunkEncoder: invalid encryption: invalid parameter"),
|
|
},
|
|
{
|
|
"nil-writer",
|
|
nil,
|
|
bsr.NoCompression,
|
|
bsr.NoEncryption,
|
|
errors.New("bsr.NewChunkEncoder: writer cannot be nil: invalid parameter"),
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
_, err := bsr.NewChunkEncoder(ctx, tc.w, tc.c, tc.e)
|
|
require.EqualError(t, tc.want, err.Error())
|
|
})
|
|
}
|
|
}
|