github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/pkg/fftypes/bytetypes_test.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package fftypes
    18  
    19  import (
    20  	"context"
    21  	"crypto/sha256"
    22  	"encoding/hex"
    23  	"encoding/json"
    24  	"fmt"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestUnmarshalBytes32(t *testing.T) {
    31  	b := NewRandB32()
    32  	var empty Bytes32
    33  	hexString := hex.EncodeToString(b[0:32])
    34  	emptyJson := []byte(`{}`)
    35  	jsonWithValues := []byte(fmt.Sprintf(`{"value":"%s","valuePtr":"0x%s"}`, hexString, hexString))
    36  	type testStruct struct {
    37  		Value    Bytes32  `json:"value"`
    38  		ValuePtr *Bytes32 `json:"valuePtr,omitempty"`
    39  	}
    40  	fmt.Printf("with: '%s' without: '%s'", jsonWithValues, emptyJson)
    41  	var s1, s2 testStruct
    42  	err := json.Unmarshal(emptyJson, &s1)
    43  	assert.NoError(t, err)
    44  	assert.Equal(t, empty, s1.Value)
    45  	assert.Nil(t, s1.ValuePtr)
    46  	err = json.Unmarshal(jsonWithValues, &s2)
    47  	assert.NoError(t, err)
    48  	assert.Equal(t, *b, s2.Value)
    49  	assert.Equal(t, b, s2.ValuePtr)
    50  }
    51  
    52  func TestMarshalBytes32(t *testing.T) {
    53  	b := NewRandB32()
    54  	hexString := b.String()
    55  	type testStruct struct {
    56  		Value    Bytes32  `json:"value"`
    57  		ValuePtr *Bytes32 `json:"valuePtr,omitempty"`
    58  	}
    59  	structWithValues := &testStruct{
    60  		ValuePtr: b,
    61  		Value:    *b,
    62  	}
    63  	json1, err := json.Marshal(structWithValues)
    64  	assert.NoError(t, err)
    65  	assert.Equal(t, fmt.Sprintf(`{"value":"%s","valuePtr":"%s"}`, hexString, hexString), string(json1))
    66  
    67  	structWithoutValues := &testStruct{}
    68  	json2, err := json.Marshal(structWithoutValues)
    69  	assert.NoError(t, err)
    70  	assert.Equal(t, `{"value":"0000000000000000000000000000000000000000000000000000000000000000"}`, string(json2))
    71  }
    72  
    73  func TestScanBytes32(t *testing.T) {
    74  
    75  	b32 := &Bytes32{}
    76  
    77  	b32.Scan(nil)
    78  	assert.Equal(t, *b32, Bytes32{})
    79  
    80  	b32.Scan("")
    81  	assert.Equal(t, *b32, Bytes32{})
    82  
    83  	rand := NewRandB32()
    84  	b32.Scan(rand.String())
    85  	assert.Equal(t, b32, rand)
    86  
    87  	b32 = &Bytes32{}
    88  
    89  	b32.Scan([]byte{})
    90  	assert.Equal(t, *b32, Bytes32{})
    91  
    92  	b32.Scan([]byte(rand.String()))
    93  	assert.Equal(t, b32, rand)
    94  
    95  	b32.Scan(rand[:])
    96  	assert.Equal(t, b32, rand)
    97  
    98  	err := b32.Scan(12345)
    99  	assert.Error(t, err)
   100  
   101  }
   102  
   103  func TestValueBytes32(t *testing.T) {
   104  	b32 := NewRandB32()
   105  	s, _ := b32.Value()
   106  	assert.Equal(t, b32.String(), s)
   107  
   108  	b32 = nil
   109  	s, _ = b32.Value()
   110  	assert.Nil(t, s)
   111  
   112  }
   113  
   114  func TestValueBytes32Nil(t *testing.T) {
   115  	var b32 *Bytes32
   116  	assert.Equal(t, "", b32.String())
   117  }
   118  
   119  func TestUUIDBytes(t *testing.T) {
   120  	u := NewUUID()
   121  	b := UUIDBytes(u)
   122  	assert.Equal(t, b[0:16], u[0:16])
   123  }
   124  
   125  func TestHashResult(t *testing.T) {
   126  	v := []byte(`abcdefghijklmnopqrstuvwxyz`)
   127  	hash := sha256.New()
   128  	_, err := hash.Write(v)
   129  	assert.NoError(t, err)
   130  	h1 := sha256.Sum256(v)
   131  	h2 := HashResult(hash)
   132  	assert.Equal(t, [32]byte(h1), [32]byte(*h2))
   133  }
   134  
   135  func TestSafeEqualsBytes32(t *testing.T) {
   136  
   137  	var b1, b2 *Bytes32
   138  	assert.True(t, b1.Equals(b2))
   139  	b1 = NewRandB32()
   140  	assert.False(t, b1.Equals(b2))
   141  	var vb2 Bytes32
   142  	vb2 = *b1
   143  	b2 = &vb2
   144  	assert.True(t, b1.Equals(b2))
   145  	b2 = NewRandB32()
   146  	assert.False(t, b1.Equals(b2))
   147  
   148  }
   149  
   150  func TestParseBytes32(t *testing.T) {
   151  	b32, err := ParseBytes32(context.Background(), "0xd907ee03ecbcfb416ce89d957682e8ef41ac548b0b571f65cb196f2b0ab4da05")
   152  	assert.NoError(t, err)
   153  	assert.Equal(t, "d907ee03ecbcfb416ce89d957682e8ef41ac548b0b571f65cb196f2b0ab4da05", b32.String())
   154  
   155  	_, err = ParseBytes32(context.Background(), "")
   156  	assert.Regexp(t, "FF10232", err)
   157  
   158  	_, err = ParseBytes32(context.Background(), "!!!!d907ee03ecbcfb416ce89d957682e8ef41ac548b0b571f65cb196f2b0ab4")
   159  	assert.Regexp(t, "FF10231", err)
   160  }