github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_decoder_native_slice_base64_test.go (about)

     1  package jzon
     2  
     3  import (
     4  	"encoding/base64"
     5  	"io"
     6  	"testing"
     7  )
     8  
     9  func TestValDecoder_Native_Base64(t *testing.T) {
    10  	f := func(t *testing.T, data string, ex error, initValue string) {
    11  		var p1 *[]byte
    12  		var p2 *[]byte
    13  		if initValue != "" {
    14  			b1 := append([]byte(nil), initValue...)
    15  			p1 = &b1
    16  			b2 := append([]byte(nil), initValue...)
    17  			p2 = &b2
    18  		}
    19  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
    20  	}
    21  	f2 := func(t *testing.T, data string, ex error) {
    22  		f(t, data, ex, "dummy")
    23  	}
    24  	t.Run("nil pointer", func(t *testing.T) {
    25  		f(t, "null", ErrNilPointerReceiver, "")
    26  	})
    27  	t.Run("eof", func(t *testing.T) {
    28  		f2(t, "", io.EOF)
    29  	})
    30  	t.Run("invalid first byte", func(t *testing.T) {
    31  		f2(t, `true`, UnexpectedByteError{})
    32  	})
    33  	t.Run("invalid null", func(t *testing.T) {
    34  		f2(t, "nul", io.EOF)
    35  	})
    36  	t.Run("null", func(t *testing.T) {
    37  		f2(t, "null", nil)
    38  	})
    39  	t.Run("invalid string", func(t *testing.T) {
    40  		f2(t, `"abc`, io.EOF)
    41  	})
    42  	t.Run("invalid base64", func(t *testing.T) {
    43  		f2(t, `"abc"`, base64.CorruptInputError(0))
    44  	})
    45  	t.Run("valid base64", func(t *testing.T) {
    46  		f2(t, `"`+base64.StdEncoding.EncodeToString(
    47  			[]byte("abc"))+`"`, nil)
    48  	})
    49  	t.Run("invalid array", func(t *testing.T) {
    50  		f2(t, `[`, io.EOF)
    51  	})
    52  	t.Run("empty array", func(t *testing.T) {
    53  		f2(t, `[]`, nil)
    54  	})
    55  	t.Run("invalid uint8", func(t *testing.T) {
    56  		f2(t, `[256]`, IntOverflowError{})
    57  	})
    58  	t.Run("invalid read more", func(t *testing.T) {
    59  		f2(t, `[1`, io.EOF)
    60  	})
    61  }
    62  
    63  type testByte byte
    64  
    65  func (tb *testByte) UnmarshalJSON(data []byte) error {
    66  	*tb = testByte(data[0] - '0' + 1)
    67  	return nil
    68  }
    69  
    70  func TestValDecoder_Native_Base64_Override(t *testing.T) {
    71  	f := func(t *testing.T, data string, ex error, p1, p2 interface{}) {
    72  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
    73  	}
    74  	t.Run("array", func(t *testing.T) {
    75  		arr1 := [...]testByte{'d', 'u', 'm', 'm', 'y'}
    76  		arr2 := [...]testByte{'d', 'u', 'm', 'm', 'y'}
    77  		f(t, `"`+base64.StdEncoding.EncodeToString(
    78  			[]byte("abc"))+`"`, UnexpectedByteError{}, &arr1, &arr2)
    79  	})
    80  	t.Run("invalid element type", func(t *testing.T) {
    81  		arr1 := []int8{'d', 'u', 'm', 'm', 'y'}
    82  		arr2 := []int8{'d', 'u', 'm', 'm', 'y'}
    83  		f(t, `"`+base64.StdEncoding.EncodeToString(
    84  			[]byte("abc"))+`"`, UnexpectedByteError{}, &arr1, &arr2)
    85  	})
    86  	t.Run("invalid base64", func(t *testing.T) {
    87  		arr1 := []testByte{'d', 'u', 'm', 'm', 'y'}
    88  		arr2 := []testByte{'d', 'u', 'm', 'm', 'y'}
    89  		f(t, `"abc"`, base64.CorruptInputError(0), &arr1, &arr2)
    90  	})
    91  	t.Run("valid base64", func(t *testing.T) {
    92  		arr1 := []testByte{'d', 'u', 'm', 'm', 'y'}
    93  		arr2 := []testByte{'d', 'u', 'm', 'm', 'y'}
    94  		f(t, `"`+base64.StdEncoding.EncodeToString(
    95  			[]byte("abc"))+`"`, nil, &arr1, &arr2)
    96  	})
    97  	t.Run("slice", func(t *testing.T) {
    98  		arr1 := []testByte{'d', 'u', 'm', 'm', 'y'}
    99  		arr2 := []testByte{'d', 'u', 'm', 'm', 'y'}
   100  		f(t, ` [ 1, 2, 3 ] `, nil, &arr1, &arr2)
   101  	})
   102  }