github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/key_list_unit_test.go (about)

     1  //go:build all || unit
     2  // +build all unit
     3  
     4  package hedera
     5  
     6  /*-
     7   *
     8   * Hedera Go SDK
     9   *
    10   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    11   *
    12   * Licensed under the Apache License, Version 2.0 (the "License");
    13   * you may not use this file except in compliance with the License.
    14   * You may obtain a copy of the License at
    15   *
    16   *      http://www.apache.org/licenses/LICENSE-2.0
    17   *
    18   * Unless required by applicable law or agreed to in writing, software
    19   * distributed under the License is distributed on an "AS IS" BASIS,
    20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    21   * See the License for the specific language governing permissions and
    22   * limitations under the License.
    23   *
    24   */
    25  
    26  import (
    27  	"testing"
    28  
    29  	"github.com/hashgraph/hedera-protobufs-go/services"
    30  	"github.com/stretchr/testify/assert"
    31  	"github.com/stretchr/testify/require"
    32  	"google.golang.org/protobuf/proto"
    33  )
    34  
    35  // Mock Key and PublicKey structs and methods for testing
    36  type MockKey struct {
    37  	data string
    38  }
    39  
    40  func (k MockKey) _ToProtoKey() *services.Key {
    41  	return &services.Key{Key: &services.Key_Ed25519{Ed25519: []byte(k.data)}}
    42  }
    43  
    44  func (k MockKey) String() string {
    45  	return k.data
    46  }
    47  
    48  func TestNewKeyList(t *testing.T) {
    49  	kl := NewKeyList()
    50  	assert.NotNil(t, kl)
    51  	assert.Equal(t, -1, kl.threshold)
    52  	assert.Empty(t, kl.keys)
    53  }
    54  
    55  func TestKeyListWithThreshold(t *testing.T) {
    56  	kl := KeyListWithThreshold(2)
    57  	assert.NotNil(t, kl)
    58  	assert.Equal(t, 2, kl.threshold)
    59  	assert.Empty(t, kl.keys)
    60  }
    61  
    62  func TestSetThreshold(t *testing.T) {
    63  	kl := NewKeyList()
    64  	kl.SetThreshold(3)
    65  	assert.Equal(t, 3, kl.threshold)
    66  }
    67  
    68  func TestAdd(t *testing.T) {
    69  	kl := NewKeyList()
    70  	key := MockKey{data: "key1"}
    71  	kl.Add(key)
    72  	assert.Len(t, kl.keys, 1)
    73  	assert.Equal(t, key, kl.keys[0])
    74  }
    75  
    76  func TestAddAll(t *testing.T) {
    77  	kl := NewKeyList()
    78  	keys := []Key{MockKey{data: "key1"}, MockKey{data: "key2"}}
    79  	kl.AddAll(keys)
    80  	assert.Len(t, kl.keys, 2)
    81  	assert.Equal(t, keys[0], kl.keys[0])
    82  	assert.Equal(t, keys[1], kl.keys[1])
    83  }
    84  
    85  func TestAddAllPublicKeys(t *testing.T) {
    86  	kl := NewKeyList()
    87  	keys := []PublicKey{{ed25519PublicKey: &_Ed25519PublicKey{keyData: []byte{1, 2}}}, {ed25519PublicKey: &_Ed25519PublicKey{keyData: []byte{1}}}}
    88  	kl.AddAllPublicKeys(keys)
    89  	assert.Len(t, kl.keys, 2)
    90  	assert.Equal(t, keys[0], kl.keys[0])
    91  	assert.Equal(t, keys[1], kl.keys[1])
    92  }
    93  
    94  func TestStringKeyList(t *testing.T) {
    95  	kl := KeyListWithThreshold(2)
    96  	key := MockKey{data: "key1"}
    97  	kl.Add(key)
    98  	expected := "{threshold:2,[key1]}"
    99  	assert.Equal(t, expected, kl.String())
   100  
   101  	kl2 := NewKeyList()
   102  	kl2.Add(key)
   103  	expected2 := "{[key1]}"
   104  	assert.Equal(t, expected2, kl2.String())
   105  }
   106  
   107  func TestToProtoKey(t *testing.T) {
   108  	kl := KeyListWithThreshold(2)
   109  	key := MockKey{data: "key1"}
   110  	kl.Add(key)
   111  	protoKey := kl._ToProtoKey()
   112  
   113  	expected := &services.Key{
   114  		Key: &services.Key_ThresholdKey{
   115  			ThresholdKey: &services.ThresholdKey{
   116  				Threshold: uint32(kl.threshold),
   117  				Keys: &services.KeyList{
   118  					Keys: []*services.Key{
   119  						{Key: &services.Key_Ed25519{Ed25519: []byte(key.data)}},
   120  					},
   121  				},
   122  			},
   123  		},
   124  	}
   125  
   126  	assert.True(t, proto.Equal(protoKey, expected))
   127  }
   128  
   129  func TestToProtoKeyList(t *testing.T) {
   130  	kl := NewKeyList()
   131  	key := MockKey{data: "key1"}
   132  	kl.Add(key)
   133  	protoKeyList := kl._ToProtoKeyList()
   134  
   135  	expected := &services.KeyList{
   136  		Keys: []*services.Key{
   137  			{Key: &services.Key_Ed25519{Ed25519: []byte(key.data)}},
   138  		},
   139  	}
   140  
   141  	assert.True(t, proto.Equal(protoKeyList, expected))
   142  }
   143  
   144  func TestKeyListFromProtobuf(t *testing.T) {
   145  	pk, _ := PrivateKeyFromStringEd25519("302e020100300506032b657004220420db484b828e64b2d8f12ce3c0a0e93a0b8cce7af1bb8f39c97732394482538e11")
   146  	protoKeyList := &services.KeyList{
   147  		Keys: []*services.Key{
   148  			{Key: &services.Key_Ed25519{Ed25519: pk.PublicKey().Bytes()}},
   149  		},
   150  	}
   151  
   152  	kl, err := _KeyListFromProtobuf(protoKeyList)
   153  	require.NoError(t, err)
   154  
   155  	assert.Len(t, kl.keys, 1)
   156  	assert.Equal(t, -1, kl.threshold)
   157  }
   158  
   159  func TestKeyListFromProtobuf_Nil(t *testing.T) {
   160  	kl, err := _KeyListFromProtobuf(nil)
   161  	assert.Error(t, errParameterNull, err)
   162  	assert.Equal(t, KeyList{}, kl)
   163  }