2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-04-16 01:48:19 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-10-18 12:10:29 +01:00
|
|
|
package encryption
|
2018-04-16 01:48:19 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIncrementBytes(t *testing.T) {
|
2018-10-03 09:55:42 +01:00
|
|
|
for i, test := range []struct {
|
2018-04-16 01:48:19 +01:00
|
|
|
inbuf []byte
|
|
|
|
amount int64
|
|
|
|
err bool
|
|
|
|
outbuf []byte
|
|
|
|
truncated bool
|
|
|
|
}{
|
|
|
|
{nil, 10, false, nil, true},
|
|
|
|
{nil, 0, false, nil, false},
|
|
|
|
{nil, -1, true, nil, false},
|
|
|
|
{nil, -1, true, nil, false},
|
|
|
|
{nil, -457, true, nil, false},
|
|
|
|
{[]byte{0}, 0, false, []byte{0}, false},
|
|
|
|
{[]byte{0}, 1, false, []byte{1}, false},
|
|
|
|
{[]byte{0}, 254, false, []byte{0xfe}, false},
|
|
|
|
{[]byte{1}, 254, false, []byte{0xff}, false},
|
|
|
|
{[]byte{0}, 255, false, []byte{0xff}, false},
|
2018-10-03 09:55:42 +01:00
|
|
|
{[]byte{1, 0, 0}, 3, false, []byte{4, 0, 0}, false},
|
2018-04-16 01:48:19 +01:00
|
|
|
{[]byte{0}, 256, false, []byte{0}, true},
|
|
|
|
{[]byte{0}, 257, false, []byte{1}, true},
|
2018-10-03 09:55:42 +01:00
|
|
|
{
|
|
|
|
[]byte("\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe"), 1, false,
|
|
|
|
[]byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff"), false},
|
|
|
|
{
|
|
|
|
[]byte("\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"), 1, false,
|
|
|
|
[]byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), true},
|
|
|
|
{
|
|
|
|
[]byte("\xff\xff\xff\xff\xff\xfe\xff\xff\xff\xfe"), 1, false,
|
|
|
|
[]byte("\x00\x00\x00\x00\x00\xff\xff\xff\xff\xfe"), false},
|
|
|
|
{
|
|
|
|
[]byte("\xfe\xff\x00\xff\xff\xfe\xff\xff\xff\xfe"), 0xff0001, false,
|
|
|
|
[]byte("\xff\xff\xff\xff\xff\xfe\xff\xff\xff\xfe"), false},
|
|
|
|
{
|
|
|
|
[]byte("\xfe\xff\x00\xff\xff\xff\xff\xff\xff\xff"), 0xff0002, false,
|
|
|
|
[]byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), true},
|
|
|
|
{
|
|
|
|
[]byte("\xfe\xff\x00\xff\xff\xff\xff\xff\xff\xff"), 0xff0003, false,
|
|
|
|
[]byte("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"), true},
|
2018-04-16 01:48:19 +01:00
|
|
|
} {
|
2018-10-03 09:55:42 +01:00
|
|
|
|
2018-04-16 01:48:19 +01:00
|
|
|
trunc, err := incrementBytes(test.inbuf, test.amount)
|
|
|
|
if err != nil {
|
|
|
|
if !test.err {
|
2018-10-03 09:55:42 +01:00
|
|
|
t.Fatalf("%d: unexpected err: %v", i, err)
|
2018-04-16 01:48:19 +01:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if test.err {
|
2018-10-03 09:55:42 +01:00
|
|
|
t.Fatalf("%d: err expected but no err happened", i)
|
2018-04-16 01:48:19 +01:00
|
|
|
}
|
|
|
|
if trunc != test.truncated {
|
2018-10-03 09:55:42 +01:00
|
|
|
t.Fatalf("%d: truncated rv mismatch", i)
|
2018-04-16 01:48:19 +01:00
|
|
|
}
|
|
|
|
if !bytes.Equal(test.outbuf, test.inbuf) {
|
2018-10-03 09:55:42 +01:00
|
|
|
t.Fatalf("%d: result mismatch\n%v\n%v", i, test.inbuf, test.outbuf)
|
2018-04-16 01:48:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|