Skip to content
This repository has been archived by the owner on Jul 20, 2022. It is now read-only.

Commit

Permalink
Code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
johandroz committed Jan 4, 2018
1 parent c2d78c0 commit 071f45e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
40 changes: 20 additions & 20 deletions fpe/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ const (
func TestNumRadix(t *testing.T) {
var x = []uint16{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
var radix uint32 = 20
var expected *big.Int = big.NewInt(28365650969)
var result *big.Int = numRadix(x, radix)
var expected = big.NewInt(28365650969)
var result = numRadix(x, radix)

assert.Equal(t, result, expected)
}

func TestNum(t *testing.T) {
var x = []byte{0x52, 0x1f, 0x6e, 0x4a, 0x88, 0xb7, 0xe0, 0x30}
var expected *big.Int = big.NewInt(5917569701788508208)
var result *big.Int = num(x)
var expected = big.NewInt(5917569701788508208)
var result = num(x)

assert.Equal(t, result, expected)
}

func TestStrMRadix(t *testing.T) {
var x *big.Int = big.NewInt(123456789)
var x = big.NewInt(123456789)
var expected = []uint16{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
var result []uint16 = strMRadix(10, 10, x)
var result = strMRadix(10, 10, x)

assert.Equal(t, result, expected)

Expand All @@ -43,7 +43,7 @@ func TestStrMRadix(t *testing.T) {
x.Exp(x, big.NewInt(int64(m)), nil)
x.Sub(x, big.NewInt(1))
result = strMRadix(radix, m, x)
var l uint32 = uint32(len(result))
var l = uint32(len(result))

assert.Equal(t, l, m)
for _, x := range result {
Expand All @@ -67,45 +67,45 @@ func TestStrMRadix(t *testing.T) {
func TestRev(t *testing.T) {
var x = []uint16{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var expected = []uint16{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
var result []uint16 = rev(x)
var result = rev(x)

assert.Equal(t, result, expected)
}

func TestRevB(t *testing.T) {
var x = []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
var expected = []byte{0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01}
var result []byte = RevB(x)
var result = RevB(x)

assert.Equal(t, result, expected)
}

func TestGetAsBBytes(t *testing.T) {
for b := 1; b <= 100; b++ {
var x *big.Int = big.NewInt(int64(b))
var x = big.NewInt(int64(b))
// Here we get 1 as 1 byte, 2 as 2 bytes, ...
var result []byte = getAsBBytes(x, uint64(b))
var result = getAsBBytes(x, uint64(b))
assert.Equal(t, len(result), b)

var v *big.Int = big.NewInt(0).SetBytes(result)
var v = big.NewInt(0).SetBytes(result)
assert.Equal(t, v, x)
}
// Test corner cases
for b := 1; b <= 100; b++ {
// x = 256^b - 1 (last value before overflow)
var x *big.Int = big.NewInt(256)
var x = big.NewInt(256)
x.Exp(x, big.NewInt(int64(b)), nil)
x.Sub(x, big.NewInt(1))

var result []byte = getAsBBytes(x, uint64(b))
var result = getAsBBytes(x, uint64(b))
assert.Equal(t, len(result), b)

for _, x := range result {
assert.Equal(t, x, uint8(0xff))
}
}
// Test overflow
var x *big.Int = big.NewInt(256)
var x = big.NewInt(256)
var b int64 = 10
x.Exp(x, big.NewInt(b), nil)

Expand All @@ -122,7 +122,7 @@ func TestNumeralStringToBytes(t *testing.T) {
var expected = []byte{
0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04,
0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09}
var result []byte = NumeralStringToBytes(x)
var result = NumeralStringToBytes(x)

assert.Equal(t, result, expected)

Expand All @@ -146,7 +146,7 @@ func TestBytesToNumeralString(t *testing.T) {
0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04,
0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09}
var expected = []uint16{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
var result []uint16 = BytesToNumeralString(x)
var result = BytesToNumeralString(x)

assert.Equal(t, result, expected)

Expand Down Expand Up @@ -186,9 +186,9 @@ func TestIsNumeralStringValid(t *testing.T) {
}

func TestXorBytes(t *testing.T) {
var x []byte = []byte{0x0F, 0x0F, 0x0F, 0x0F, 0x0F}
var y []byte = []byte{0xF0, 0xF0, 0xF0, 0xF0}
var expected []byte = []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x00}
var x = []byte{0x0F, 0x0F, 0x0F, 0x0F, 0x0F}
var y = []byte{0xF0, 0xF0, 0xF0, 0xF0}
var expected = []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x00}
var dst = make([]byte, len(x))

xorBytes(dst, x, y)
Expand Down
28 changes: 14 additions & 14 deletions fpe/ff1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2238,7 +2238,7 @@ func TestNewFF1Encrypter(t *testing.T) {
// Invalid tweak length
var f func()
f = func() {
var radix uint32 = uint32(maxRadixFF1)
var radix = uint32(maxRadixFF1)
var tweak = make([]byte, maxTweakLenFF1+1)
rand.Read(tweak)
NewFF1Encrypter(aesBlock, cbcMode, tweak, radix)
Expand All @@ -2247,27 +2247,27 @@ func TestNewFF1Encrypter(t *testing.T) {

// Invalid radix
f = func() {
var radix uint32 = uint32(maxRadixFF1 + 1)
var radix = uint32(maxRadixFF1 + 1)
var tweak = make([]byte, maxTweakLenFF1)
rand.Read(tweak)
NewFF1Encrypter(aesBlock, cbcMode, tweak, radix)
}
assert.Panics(t, f)

// Invalid Block
var invalidBlock *mockBlock = &mockBlock{}
var invalidBlock = &mockBlock{}
f = func() {
var radix uint32 = uint32(maxRadixFF1)
var radix = uint32(maxRadixFF1)
var tweak = make([]byte, maxTweakLenFF1)
rand.Read(tweak)
NewFF1Encrypter(invalidBlock, cbcMode, tweak, radix)
}
assert.Panics(t, f)

// Invalid BlockMode
var invalidBlockMode *mockBlockMode = &mockBlockMode{}
var invalidBlockMode = &mockBlockMode{}
f = func() {
var radix uint32 = uint32(maxRadixFF1)
var radix = uint32(maxRadixFF1)
var tweak = make([]byte, maxTweakLenFF1)
rand.Read(tweak)
NewFF1Encrypter(aesBlock, invalidBlockMode, tweak, radix)
Expand All @@ -2285,7 +2285,7 @@ func TestNewFF1Decrypter(t *testing.T) {
// Invalid tweak length
var f func()
f = func() {
var radix uint32 = uint32(maxRadixFF1)
var radix = uint32(maxRadixFF1)
var tweak = make([]byte, maxTweakLenFF1+1)
rand.Read(tweak)
NewFF1Decrypter(aesBlock, cbcMode, tweak, radix)
Expand All @@ -2294,27 +2294,27 @@ func TestNewFF1Decrypter(t *testing.T) {

// Invalid radix
f = func() {
var radix uint32 = uint32(maxRadixFF1 + 1)
var radix = uint32(maxRadixFF1 + 1)
var tweak = make([]byte, maxTweakLenFF1)
rand.Read(tweak)
NewFF1Decrypter(aesBlock, cbcMode, tweak, radix)
}
assert.Panics(t, f)

// Invalid Block
var invalidBlock *mockBlock = &mockBlock{}
var invalidBlock = &mockBlock{}
f = func() {
var radix uint32 = uint32(maxRadixFF1)
var radix = uint32(maxRadixFF1)
var tweak = make([]byte, maxTweakLenFF1)
rand.Read(tweak)
NewFF1Decrypter(invalidBlock, cbcMode, tweak, radix)
}
assert.Panics(t, f)

// Invalid BlockMode
var invalidBlockMode *mockBlockMode = &mockBlockMode{}
var invalidBlockMode = &mockBlockMode{}
f = func() {
var radix uint32 = uint32(maxRadixFF1)
var radix = uint32(maxRadixFF1)
var tweak = make([]byte, maxTweakLenFF1)
rand.Read(tweak)
NewFF1Decrypter(aesBlock, invalidBlockMode, tweak, radix)
Expand All @@ -2331,7 +2331,7 @@ func TestFF1CryptBlocks(t *testing.T) {
assert.Nil(t, err)
var cbcMode = cipher.NewCBCEncrypter(aesBlock, iv)

var ff1BlockMode []cipher.BlockMode = []cipher.BlockMode{
var ff1BlockMode = []cipher.BlockMode{
NewFF1Encrypter(aesBlock, cbcMode, tweak, radix),
NewFF1Decrypter(aesBlock, cbcMode, tweak, radix),
}
Expand Down Expand Up @@ -2517,7 +2517,7 @@ func TestSetFF1Radix(t *testing.T) {
var key, tweak, _ []byte = getRandomParameters(ff1DefaultKeySize, ff1DefaultTweakSize, blockSizeFF1)

var radix = uint32(rand.Intn(1000) + minRadixFF1)
var otherRadix uint32 = radix + 10
var otherRadix = radix + 10

type fpeWithSetRadix interface {
cipher.BlockMode
Expand Down
20 changes: 10 additions & 10 deletions fpe/ff3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2447,7 +2447,7 @@ func TestNewFF3Encrypter(t *testing.T) {
// Invalid tweak length
var f func()
f = func() {
var radix uint32 = uint32(maxRadixFF3)
var radix = uint32(maxRadixFF3)
var tweak = make([]byte, tweakLenFF3+1)
rand.Read(tweak)
NewFF3Encrypter(aesBlock, tweak, radix)
Expand All @@ -2456,17 +2456,17 @@ func TestNewFF3Encrypter(t *testing.T) {

// Invalid radix
f = func() {
var radix uint32 = uint32(maxRadixFF3 + 1)
var radix = uint32(maxRadixFF3 + 1)
var tweak = make([]byte, tweakLenFF3)
rand.Read(tweak)
NewFF3Encrypter(aesBlock, tweak, radix)
}
assert.Panics(t, f)

// Invalid Block
var invalidBlock *mockBlock = &mockBlock{}
var invalidBlock = &mockBlock{}
f = func() {
var radix uint32 = uint32(maxRadixFF3)
var radix = uint32(maxRadixFF3)
var tweak = make([]byte, tweakLenFF3)
rand.Read(tweak)
NewFF3Encrypter(invalidBlock, tweak, radix)
Expand All @@ -2484,7 +2484,7 @@ func TestNewFF3Decrypter(t *testing.T) {
// Invalid tweak length
var f func()
f = func() {
var radix uint32 = uint32(maxRadixFF3)
var radix = uint32(maxRadixFF3)
var tweak = make([]byte, tweakLenFF3+1)
rand.Read(tweak)
NewFF3Decrypter(aesBlock, tweak, radix)
Expand All @@ -2493,17 +2493,17 @@ func TestNewFF3Decrypter(t *testing.T) {

// Invalid radix
f = func() {
var radix uint32 = uint32(maxRadixFF3 + 1)
var radix = uint32(maxRadixFF3 + 1)
var tweak = make([]byte, tweakLenFF3)
rand.Read(tweak)
NewFF3Decrypter(aesBlock, tweak, radix)
}
assert.Panics(t, f)

// Invalid Block
var invalidBlock *mockBlock = &mockBlock{}
var invalidBlock = &mockBlock{}
f = func() {
var radix uint32 = uint32(maxRadixFF3)
var radix = uint32(maxRadixFF3)
var tweak = make([]byte, tweakLenFF3)
rand.Read(tweak)
NewFF3Decrypter(invalidBlock, tweak, radix)
Expand All @@ -2519,7 +2519,7 @@ func TestFF3CryptBlocks(t *testing.T) {
var aesBlock, err = aes.NewCipher(key)
assert.Nil(t, err)

var ff3BlockMode []cipher.BlockMode = []cipher.BlockMode{
var ff3BlockMode = []cipher.BlockMode{
NewFF3Encrypter(aesBlock, tweak, radix),
NewFF3Decrypter(aesBlock, tweak, radix),
}
Expand Down Expand Up @@ -2708,7 +2708,7 @@ func TestSetFF3Radix(t *testing.T) {
var key, tweak, _ []byte = getRandomParameters(ff3DefaultKeySize, tweakLenFF3, 0)

var radix = uint32(rand.Intn(20) + minRadixFF3)
var otherRadix uint32 = radix + 10
var otherRadix = radix + 10

type fpeWithSetRadix interface {
cipher.BlockMode
Expand Down

0 comments on commit 071f45e

Please sign in to comment.