github.com/artpar/rclone@v1.67.3/backend/crypt/pkcs7/pkcs7_test.go (about)

     1  package pkcs7
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestPad(t *testing.T) {
    11  	for _, test := range []struct {
    12  		n        int
    13  		in       string
    14  		expected string
    15  	}{
    16  		{8, "", "\x08\x08\x08\x08\x08\x08\x08\x08"},
    17  		{8, "1", "1\x07\x07\x07\x07\x07\x07\x07"},
    18  		{8, "12", "12\x06\x06\x06\x06\x06\x06"},
    19  		{8, "123", "123\x05\x05\x05\x05\x05"},
    20  		{8, "1234", "1234\x04\x04\x04\x04"},
    21  		{8, "12345", "12345\x03\x03\x03"},
    22  		{8, "123456", "123456\x02\x02"},
    23  		{8, "1234567", "1234567\x01"},
    24  		{8, "abcdefgh", "abcdefgh\x08\x08\x08\x08\x08\x08\x08\x08"},
    25  		{8, "abcdefgh1", "abcdefgh1\x07\x07\x07\x07\x07\x07\x07"},
    26  		{8, "abcdefgh12", "abcdefgh12\x06\x06\x06\x06\x06\x06"},
    27  		{8, "abcdefgh123", "abcdefgh123\x05\x05\x05\x05\x05"},
    28  		{8, "abcdefgh1234", "abcdefgh1234\x04\x04\x04\x04"},
    29  		{8, "abcdefgh12345", "abcdefgh12345\x03\x03\x03"},
    30  		{8, "abcdefgh123456", "abcdefgh123456\x02\x02"},
    31  		{8, "abcdefgh1234567", "abcdefgh1234567\x01"},
    32  		{8, "abcdefgh12345678", "abcdefgh12345678\x08\x08\x08\x08\x08\x08\x08\x08"},
    33  		{16, "", "\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10"},
    34  		{16, "a", "a\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f"},
    35  	} {
    36  		actual := Pad(test.n, []byte(test.in))
    37  		assert.Equal(t, test.expected, string(actual), fmt.Sprintf("Pad %d %q", test.n, test.in))
    38  		recovered, err := Unpad(test.n, actual)
    39  		assert.NoError(t, err)
    40  		assert.Equal(t, []byte(test.in), recovered, fmt.Sprintf("Unpad %d %q", test.n, test.in))
    41  	}
    42  	assert.Panics(t, func() { Pad(1, []byte("")) }, "bad multiple")
    43  	assert.Panics(t, func() { Pad(256, []byte("")) }, "bad multiple")
    44  }
    45  
    46  func TestUnpad(t *testing.T) {
    47  	// We've tested the OK decoding in TestPad, now test the error cases
    48  	for _, test := range []struct {
    49  		n   int
    50  		in  string
    51  		err error
    52  	}{
    53  		{8, "", ErrorPaddingNotFound},
    54  		{8, "1", ErrorPaddingNotAMultiple},
    55  		{8, "12", ErrorPaddingNotAMultiple},
    56  		{8, "123", ErrorPaddingNotAMultiple},
    57  		{8, "1234", ErrorPaddingNotAMultiple},
    58  		{8, "12345", ErrorPaddingNotAMultiple},
    59  		{8, "123456", ErrorPaddingNotAMultiple},
    60  		{8, "1234567", ErrorPaddingNotAMultiple},
    61  		{8, "1234567\xFF", ErrorPaddingTooLong},
    62  		{8, "1234567\x09", ErrorPaddingTooLong},
    63  		{8, "1234567\x00", ErrorPaddingTooShort},
    64  		{8, "123456\x01\x02", ErrorPaddingNotAllTheSame},
    65  		{8, "\x07\x08\x08\x08\x08\x08\x08\x08", ErrorPaddingNotAllTheSame},
    66  	} {
    67  		result, actualErr := Unpad(test.n, []byte(test.in))
    68  		assert.Equal(t, test.err, actualErr, fmt.Sprintf("Unpad %d %q", test.n, test.in))
    69  		assert.Equal(t, result, []byte(nil))
    70  	}
    71  	assert.Panics(t, func() { _, _ = Unpad(1, []byte("")) }, "bad multiple")
    72  	assert.Panics(t, func() { _, _ = Unpad(256, []byte("")) }, "bad multiple")
    73  }