github.com/koko1123/flow-go-1@v0.29.6/ledger/trie_test.go (about)

     1  package ledger
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/fxamacker/cbor/v2"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // TestPayloadEquals tests equality of payloads.  It tests:
    12  // - equality of empty, nil, and not-empty payloads
    13  // - equality of payloads with different keys and same value
    14  // - equality of payloads with same key and different values
    15  // - and etc.
    16  func TestPayloadEquals(t *testing.T) {
    17  	nilPayload := (*Payload)(nil)
    18  	emptyPayload := EmptyPayload()
    19  
    20  	t.Run("nil vs empty", func(t *testing.T) {
    21  		require.True(t, nilPayload.Equals(emptyPayload))
    22  		require.True(t, emptyPayload.Equals(nilPayload))
    23  	})
    24  
    25  	t.Run("nil vs nil", func(t *testing.T) {
    26  		require.True(t, nilPayload.Equals(nilPayload))
    27  	})
    28  
    29  	t.Run("empty vs empty", func(t *testing.T) {
    30  		require.True(t, emptyPayload.Equals(emptyPayload))
    31  	})
    32  
    33  	t.Run("empty vs non-empty", func(t *testing.T) {
    34  		p := NewPayload(
    35  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02}}}},
    36  			[]byte{0x03, 0x04},
    37  		)
    38  		require.False(t, emptyPayload.Equals(p))
    39  		require.False(t, p.Equals(emptyPayload))
    40  	})
    41  
    42  	t.Run("nil vs non-empty", func(t *testing.T) {
    43  		p := NewPayload(
    44  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02}}}},
    45  			[]byte{0x03, 0x04},
    46  		)
    47  		require.False(t, nilPayload.Equals(p))
    48  		require.False(t, p.Equals(nilPayload))
    49  	})
    50  
    51  	t.Run("different key same value", func(t *testing.T) {
    52  		value := []byte{0x03, 0x04}
    53  
    54  		p := NewPayload(
    55  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02}}}},
    56  			value,
    57  		)
    58  		// p1.Key.KeyParts[0].Type is different
    59  		p1 := NewPayload(
    60  			Key{KeyParts: []KeyPart{{2, []byte{0x01, 0x02}}}},
    61  			value,
    62  		)
    63  		// p2.Key.KeyParts[0].Value is different
    64  		p2 := NewPayload(
    65  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02, 0x03}}}},
    66  			value,
    67  		)
    68  		// len(p3.Key.KeyParts) is different
    69  		p3 := NewPayload(
    70  			Key{KeyParts: []KeyPart{
    71  				{1, []byte{0x01, 0x02}},
    72  				{2, []byte{0x03, 0x04}}},
    73  			},
    74  			value,
    75  		)
    76  		require.False(t, p.Equals(p1))
    77  		require.False(t, p.Equals(p2))
    78  		require.False(t, p.Equals(p3))
    79  	})
    80  
    81  	t.Run("different key empty value", func(t *testing.T) {
    82  		value := []byte{}
    83  
    84  		p := NewPayload(
    85  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02}}}},
    86  			value,
    87  		)
    88  		// p1.Key.KeyParts[0].Type is different
    89  		p1 := NewPayload(
    90  			Key{KeyParts: []KeyPart{{2, []byte{0x01, 0x02}}}},
    91  			value,
    92  		)
    93  		// p2.Key.KeyParts[0].Value is different
    94  		p2 := NewPayload(
    95  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02, 0x03}}}},
    96  			value,
    97  		)
    98  		// len(p3.Key.KeyParts) is different
    99  		p3 := NewPayload(
   100  			Key{KeyParts: []KeyPart{
   101  				{1, []byte{0x01, 0x02}},
   102  				{2, []byte{0x03, 0x04}}},
   103  			},
   104  			value,
   105  		)
   106  		require.False(t, p.Equals(p1))
   107  		require.False(t, p.Equals(p2))
   108  		require.False(t, p.Equals(p3))
   109  	})
   110  
   111  	t.Run("same key different value", func(t *testing.T) {
   112  		key := Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02}}}}
   113  
   114  		p := NewPayload(
   115  			key,
   116  			[]byte{0x03, 0x04},
   117  		)
   118  		// p1.Value is nil
   119  		p1 := NewPayload(
   120  			key,
   121  			nil,
   122  		)
   123  		// p2.Value is empty
   124  		p2 := NewPayload(
   125  			key,
   126  			[]byte{},
   127  		)
   128  		// p3.Value length is different
   129  		p3 := NewPayload(
   130  			key,
   131  			[]byte{0x03},
   132  		)
   133  		// p4.Value data is different
   134  		p4 := NewPayload(
   135  			key,
   136  			[]byte{0x03, 0x05},
   137  		)
   138  		require.False(t, p.Equals(p1))
   139  		require.False(t, p.Equals(p2))
   140  		require.False(t, p.Equals(p3))
   141  		require.False(t, p.Equals(p4))
   142  	})
   143  
   144  	t.Run("same key same value", func(t *testing.T) {
   145  		p1 := NewPayload(
   146  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02}}}},
   147  			[]byte{0x03, 0x04},
   148  		)
   149  		p2 := NewPayload(
   150  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02}}}},
   151  			[]byte{0x03, 0x04},
   152  		)
   153  		require.True(t, p1.Equals(p2))
   154  		require.True(t, p2.Equals(p1))
   155  	})
   156  }
   157  
   158  // TestPayloadValueEquals tests equality of payload values.  It tests:
   159  // - equality of empty, nil, and not-empty payloads
   160  // - equality of payloads with different keys and same value
   161  // - equality of payloads with same key and different values
   162  // - and etc.
   163  func TestPayloadValuEquals(t *testing.T) {
   164  	nilPayload := (*Payload)(nil)
   165  	emptyPayload := EmptyPayload()
   166  
   167  	t.Run("nil vs empty", func(t *testing.T) {
   168  		require.True(t, nilPayload.ValueEquals(emptyPayload))
   169  		require.True(t, emptyPayload.ValueEquals(nilPayload))
   170  	})
   171  
   172  	t.Run("nil vs nil", func(t *testing.T) {
   173  		require.True(t, nilPayload.ValueEquals(nilPayload))
   174  	})
   175  
   176  	t.Run("empty vs empty", func(t *testing.T) {
   177  		require.True(t, emptyPayload.ValueEquals(emptyPayload))
   178  	})
   179  
   180  	t.Run("empty vs non-empty", func(t *testing.T) {
   181  		p := NewPayload(
   182  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02}}}},
   183  			[]byte{0x03, 0x04},
   184  		)
   185  		require.False(t, emptyPayload.ValueEquals(p))
   186  		require.False(t, p.ValueEquals(emptyPayload))
   187  	})
   188  
   189  	t.Run("nil vs non-empty", func(t *testing.T) {
   190  		p := NewPayload(
   191  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02}}}},
   192  			[]byte{0x03, 0x04},
   193  		)
   194  		require.False(t, nilPayload.ValueEquals(p))
   195  		require.False(t, p.ValueEquals(nilPayload))
   196  	})
   197  
   198  	t.Run("different key same value", func(t *testing.T) {
   199  		value := []byte{0x03, 0x04}
   200  
   201  		p := NewPayload(
   202  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02}}}},
   203  			value,
   204  		)
   205  		// p1.Key.KeyParts[0].Type is different
   206  		p1 := NewPayload(
   207  			Key{KeyParts: []KeyPart{{2, []byte{0x01, 0x02}}}},
   208  			value,
   209  		)
   210  		// p2.Key.KeyParts[0].Value is different
   211  		p2 := NewPayload(
   212  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02, 0x03}}}},
   213  			value,
   214  		)
   215  		// len(p3.Key.KeyParts) is different
   216  		p3 := NewPayload(
   217  			Key{KeyParts: []KeyPart{
   218  				{1, []byte{0x01, 0x02}},
   219  				{2, []byte{0x03, 0x04}}},
   220  			},
   221  			value,
   222  		)
   223  		require.True(t, p.ValueEquals(p1))
   224  		require.True(t, p.ValueEquals(p2))
   225  		require.True(t, p.ValueEquals(p3))
   226  	})
   227  
   228  	t.Run("different key empty value", func(t *testing.T) {
   229  		value := []byte{}
   230  
   231  		p := NewPayload(
   232  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02}}}},
   233  			value,
   234  		)
   235  		// p1.Key.KeyParts[0].Type is different
   236  		p1 := NewPayload(
   237  			Key{KeyParts: []KeyPart{{2, []byte{0x01, 0x02}}}},
   238  			value,
   239  		)
   240  		// p2.Key.KeyParts[0].Value is different
   241  		p2 := NewPayload(
   242  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02, 0x03}}}},
   243  			value,
   244  		)
   245  		// len(p3.Key.KeyParts) is different
   246  		p3 := NewPayload(
   247  			Key{KeyParts: []KeyPart{
   248  				{1, []byte{0x01, 0x02}},
   249  				{2, []byte{0x03, 0x04}}},
   250  			},
   251  			value,
   252  		)
   253  		require.True(t, p.ValueEquals(p1))
   254  		require.True(t, p.ValueEquals(p2))
   255  		require.True(t, p.ValueEquals(p3))
   256  	})
   257  
   258  	t.Run("same key different value", func(t *testing.T) {
   259  		key := Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02}}}}
   260  
   261  		p := NewPayload(
   262  			key,
   263  			[]byte{0x03, 0x04},
   264  		)
   265  		// p1.Value is nil
   266  		p1 := NewPayload(
   267  			key,
   268  			nil,
   269  		)
   270  		// p2.Value is empty
   271  		p2 := NewPayload(
   272  			key,
   273  			[]byte{},
   274  		)
   275  		// p3.Value length is different
   276  		p3 := NewPayload(
   277  			key,
   278  			[]byte{0x03},
   279  		)
   280  		// p4.Value data is different
   281  		p4 := NewPayload(
   282  			key,
   283  			[]byte{0x03, 0x05},
   284  		)
   285  		require.False(t, p.ValueEquals(p1))
   286  		require.False(t, p.ValueEquals(p2))
   287  		require.False(t, p.ValueEquals(p3))
   288  		require.False(t, p.ValueEquals(p4))
   289  	})
   290  
   291  	t.Run("same key same value", func(t *testing.T) {
   292  		p1 := NewPayload(
   293  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02}}}},
   294  			[]byte{0x03, 0x04},
   295  		)
   296  		p2 := NewPayload(
   297  			Key{KeyParts: []KeyPart{{1, []byte{0x01, 0x02}}}},
   298  			[]byte{0x03, 0x04},
   299  		)
   300  		require.True(t, p1.ValueEquals(p2))
   301  		require.True(t, p2.ValueEquals(p1))
   302  	})
   303  }
   304  
   305  func TestPayloadKey(t *testing.T) {
   306  	t.Run("nil payload", func(t *testing.T) {
   307  		var p *Payload
   308  		k, err := p.Key()
   309  		require.NoError(t, err)
   310  		require.Equal(t, Key{}, k)
   311  	})
   312  	t.Run("empty payload", func(t *testing.T) {
   313  		p := Payload{}
   314  		k, err := p.Key()
   315  		require.NoError(t, err)
   316  		require.Equal(t, Key{}, k)
   317  	})
   318  	t.Run("empty key", func(t *testing.T) {
   319  		p := NewPayload(Key{}, Value{})
   320  		k, err := p.Key()
   321  		require.NoError(t, err)
   322  		require.Equal(t, Key{}, k)
   323  	})
   324  	t.Run("key", func(t *testing.T) {
   325  		key := Key{KeyParts: []KeyPart{{Type: 0, Value: []byte("abc")}, {Type: 1, Value: []byte("def")}}}
   326  		value := Value([]byte{0, 1, 2})
   327  		p := NewPayload(key, value)
   328  		k, err := p.Key()
   329  		require.NoError(t, err)
   330  		require.Equal(t, key, k)
   331  	})
   332  }
   333  
   334  func TestPayloadValue(t *testing.T) {
   335  	t.Run("nil payload", func(t *testing.T) {
   336  		var p *Payload
   337  		require.Equal(t, 0, p.Value().Size())
   338  	})
   339  	t.Run("empty payload", func(t *testing.T) {
   340  		p := Payload{}
   341  		require.Equal(t, 0, p.Value().Size())
   342  	})
   343  	t.Run("empty value", func(t *testing.T) {
   344  		key := Key{KeyParts: []KeyPart{{Type: 0, Value: []byte("abc")}, {Type: 1, Value: []byte("def")}}}
   345  		p := NewPayload(key, Value{})
   346  		require.Equal(t, 0, p.Value().Size())
   347  	})
   348  	t.Run("value", func(t *testing.T) {
   349  		key := Key{KeyParts: []KeyPart{{Type: 0, Value: []byte("abc")}, {Type: 1, Value: []byte("def")}}}
   350  		value := Value([]byte{0, 1, 2})
   351  		p := NewPayload(key, value)
   352  		require.Equal(t, value, p.Value())
   353  	})
   354  }
   355  
   356  func TestPayloadJSONSerialization(t *testing.T) {
   357  	t.Run("nil payload", func(t *testing.T) {
   358  		encoded := []byte("null")
   359  
   360  		var p *Payload
   361  		b, err := json.Marshal(p)
   362  		require.NoError(t, err)
   363  		require.Equal(t, encoded, b)
   364  
   365  		var p2 *Payload
   366  		err = json.Unmarshal(b, &p2)
   367  		require.NoError(t, err)
   368  		require.Equal(t, p, p2)
   369  	})
   370  
   371  	t.Run("empty payload", func(t *testing.T) {
   372  		encoded := []byte(`{"Key":{"KeyParts":null},"Value":""}`)
   373  
   374  		var p Payload
   375  		b, err := json.Marshal(p)
   376  		require.NoError(t, err)
   377  		require.Equal(t, encoded, b)
   378  
   379  		var p2 Payload
   380  		err = json.Unmarshal(b, &p2)
   381  		require.NoError(t, err)
   382  
   383  		// Reset b to make sure that p2 doesn't share underlying data with JSON-encoded data.
   384  		for i := 0; i < len(b); i++ {
   385  			b[i] = 0
   386  		}
   387  
   388  		require.True(t, p2.IsEmpty())
   389  
   390  		k2, err := p2.Key()
   391  		require.NoError(t, err)
   392  		require.True(t, k2.Equals(&Key{}))
   393  
   394  		v2 := p2.Value()
   395  		require.True(t, v2.Equals(Value{}))
   396  	})
   397  
   398  	t.Run("empty key", func(t *testing.T) {
   399  		encoded := []byte(`{"Key":{"KeyParts":null},"Value":"000102"}`)
   400  
   401  		k := Key{}
   402  		v := []byte{0, 1, 2}
   403  		p := NewPayload(k, v)
   404  		b, err := json.Marshal(p)
   405  		require.NoError(t, err)
   406  		require.Equal(t, encoded, b)
   407  
   408  		var p2 Payload
   409  		err = json.Unmarshal(b, &p2)
   410  		require.NoError(t, err)
   411  
   412  		// Reset b to make sure that p2 doesn't share underlying data with JSON-encoded data.
   413  		for i := 0; i < len(b); i++ {
   414  			b[i] = 0
   415  		}
   416  
   417  		require.True(t, p.Equals(&p2))
   418  
   419  		k2, err := p2.Key()
   420  		require.NoError(t, err)
   421  		require.True(t, k2.Equals(&k))
   422  
   423  		v2 := p2.Value()
   424  		require.True(t, v2.Equals(v))
   425  	})
   426  
   427  	t.Run("empty value", func(t *testing.T) {
   428  		encoded := []byte(`{"Key":{"KeyParts":[{"Type":1,"Value":"0102"}]},"Value":""}`)
   429  
   430  		k := Key{KeyParts: []KeyPart{{1, []byte{1, 2}}}}
   431  		var v Value
   432  		p := NewPayload(k, v)
   433  		b, err := json.Marshal(p)
   434  		require.NoError(t, err)
   435  		require.Equal(t, encoded, b)
   436  
   437  		var p2 Payload
   438  		err = json.Unmarshal(b, &p2)
   439  		require.NoError(t, err)
   440  
   441  		// Reset b to make sure that p2 doesn't share underlying data with JSON-encoded data.
   442  		for i := 0; i < len(b); i++ {
   443  			b[i] = 0
   444  		}
   445  
   446  		require.True(t, p.Equals(&p2))
   447  
   448  		k2, err := p2.Key()
   449  		require.NoError(t, err)
   450  		require.True(t, k2.Equals(&k))
   451  
   452  		v2 := p2.Value()
   453  		require.True(t, v2.Equals(v))
   454  	})
   455  
   456  	t.Run("payload", func(t *testing.T) {
   457  		encoded := []byte(`{"Key":{"KeyParts":[{"Type":1,"Value":"0102"}]},"Value":"030405"}`)
   458  
   459  		k := Key{KeyParts: []KeyPart{{1, []byte{1, 2}}}}
   460  		v := Value{3, 4, 5}
   461  		p := NewPayload(k, v)
   462  		b, err := json.Marshal(p)
   463  		require.NoError(t, err)
   464  		require.Equal(t, []byte(encoded), b)
   465  
   466  		var p2 Payload
   467  		err = json.Unmarshal(b, &p2)
   468  		require.NoError(t, err)
   469  
   470  		// Reset b to make sure that p2 doesn't share underlying data with JSON-encoded data.
   471  		for i := 0; i < len(b); i++ {
   472  			b[i] = 0
   473  		}
   474  
   475  		require.True(t, p.Equals(&p2))
   476  
   477  		k2, err := p2.Key()
   478  		require.NoError(t, err)
   479  		require.True(t, k2.Equals(&k))
   480  
   481  		v2 := p2.Value()
   482  		require.True(t, v2.Equals(v))
   483  	})
   484  }
   485  
   486  func TestPayloadCBORSerialization(t *testing.T) {
   487  	t.Run("nil payload", func(t *testing.T) {
   488  		encoded := []byte{0xf6} // null
   489  
   490  		var p *Payload
   491  		b, err := cbor.Marshal(p)
   492  		require.NoError(t, err)
   493  		require.Equal(t, encoded, b)
   494  
   495  		var p2 *Payload
   496  		err = cbor.Unmarshal(b, &p2)
   497  		require.NoError(t, err)
   498  		require.Equal(t, p, p2)
   499  	})
   500  
   501  	t.Run("empty payload", func(t *testing.T) {
   502  		encoded := []byte{
   503  			0xa2,             // map(2)
   504  			0x63,             // text(3)
   505  			0x4b, 0x65, 0x79, // "Key"
   506  			0xa1,                                           // map(1)
   507  			0x68,                                           // text(8)
   508  			0x4b, 0x65, 0x79, 0x50, 0x61, 0x72, 0x74, 0x73, // "KeyParts"
   509  			0xf6,                         // null
   510  			0x65,                         // text(5)
   511  			0x56, 0x61, 0x6c, 0x75, 0x65, // "Value"
   512  			0xf6, // null
   513  		}
   514  
   515  		var p Payload
   516  		b, err := cbor.Marshal(p)
   517  		require.NoError(t, err)
   518  		require.Equal(t, encoded, b)
   519  
   520  		var p2 Payload
   521  		err = cbor.Unmarshal(b, &p2)
   522  		require.NoError(t, err)
   523  
   524  		// Reset b to make sure that p2 doesn't share underlying data with CBOR-encoded data.
   525  		for i := 0; i < len(b); i++ {
   526  			b[i] = 0
   527  		}
   528  
   529  		require.True(t, p2.IsEmpty())
   530  
   531  		k2, err := p2.Key()
   532  		require.NoError(t, err)
   533  		require.True(t, k2.Equals(&Key{}))
   534  
   535  		v2 := p2.Value()
   536  		require.True(t, v2.Equals(Value{}))
   537  	})
   538  
   539  	t.Run("empty key", func(t *testing.T) {
   540  		encoded := []byte{
   541  			0xa2,             // map(2)
   542  			0x63,             // text(3)
   543  			0x4b, 0x65, 0x79, // "Key"
   544  			0xa1,                                           // map(1)
   545  			0x68,                                           // text(8)
   546  			0x4b, 0x65, 0x79, 0x50, 0x61, 0x72, 0x74, 0x73, // "KeyParts"
   547  			0xf6,                         // null
   548  			0x65,                         // text(5)
   549  			0x56, 0x61, 0x6c, 0x75, 0x65, // "Value"
   550  			0x43,             // bytes(3)
   551  			0x00, 0x01, 0x02, // "\u0000\u0001\u0002"
   552  		}
   553  
   554  		k := Key{}
   555  		v := []byte{0, 1, 2}
   556  		p := NewPayload(k, v)
   557  		b, err := cbor.Marshal(p)
   558  		require.NoError(t, err)
   559  		require.Equal(t, encoded, b)
   560  
   561  		var p2 Payload
   562  		err = cbor.Unmarshal(b, &p2)
   563  		require.NoError(t, err)
   564  
   565  		// Reset b to make sure that p2 doesn't share underlying data with CBOR-encoded data.
   566  		for i := 0; i < len(b); i++ {
   567  			b[i] = 0
   568  		}
   569  
   570  		require.True(t, p.Equals(&p2))
   571  
   572  		k2, err := p2.Key()
   573  		require.NoError(t, err)
   574  		require.True(t, k2.Equals(&k))
   575  
   576  		v2 := p2.Value()
   577  		require.True(t, v2.Equals(v))
   578  	})
   579  
   580  	t.Run("empty value", func(t *testing.T) {
   581  		encoded := []byte{
   582  			0xa2,             // map(2)
   583  			0x63,             // text(3)
   584  			0x4b, 0x65, 0x79, // "Key"
   585  			0xa1,                                           // map(1)
   586  			0x68,                                           // text(8)
   587  			0x4b, 0x65, 0x79, 0x50, 0x61, 0x72, 0x74, 0x73, // "KeyParts"
   588  			0x81,                   // array(1)
   589  			0xa2,                   // map(2)
   590  			0x64,                   // text(4)
   591  			0x54, 0x79, 0x70, 0x65, // "Type"
   592  			0x01,                         // unsigned(1)
   593  			0x65,                         // text(5)
   594  			0x56, 0x61, 0x6c, 0x75, 0x65, // "Value"
   595  			0x42,       // bytes(2)
   596  			0x01, 0x02, // "\u0001\u0002"
   597  			0x65,                         // text(5)
   598  			0x56, 0x61, 0x6c, 0x75, 0x65, // "Value"
   599  			0xf6, // null
   600  		}
   601  
   602  		k := Key{KeyParts: []KeyPart{{1, []byte{1, 2}}}}
   603  		var v Value
   604  		p := NewPayload(k, v)
   605  		b, err := cbor.Marshal(p)
   606  		require.NoError(t, err)
   607  		require.Equal(t, encoded, b)
   608  
   609  		var p2 Payload
   610  		err = cbor.Unmarshal(b, &p2)
   611  		require.NoError(t, err)
   612  
   613  		// Reset b to make sure that p2 doesn't share underlying data with CBOR-encoded data.
   614  		for i := 0; i < len(b); i++ {
   615  			b[i] = 0
   616  		}
   617  
   618  		require.True(t, p.Equals(&p2))
   619  
   620  		k2, err := p2.Key()
   621  		require.NoError(t, err)
   622  		require.True(t, k2.Equals(&k))
   623  
   624  		v2 := p2.Value()
   625  		require.True(t, v2.Equals(v))
   626  	})
   627  
   628  	t.Run("payload", func(t *testing.T) {
   629  		encoded := []byte{
   630  			0xa2,             // map(2)
   631  			0x63,             // text(3)
   632  			0x4b, 0x65, 0x79, // "Key"
   633  			0xa1,                                           // map(1)
   634  			0x68,                                           // text(8)
   635  			0x4b, 0x65, 0x79, 0x50, 0x61, 0x72, 0x74, 0x73, // "KeyParts"
   636  			0x81,                   // array(1)
   637  			0xa2,                   // map(2)
   638  			0x64,                   // text(4)
   639  			0x54, 0x79, 0x70, 0x65, // "Type"
   640  			0x01,                         // unsigned(1)
   641  			0x65,                         // text(5)
   642  			0x56, 0x61, 0x6c, 0x75, 0x65, // "Value"
   643  			0x42,       // bytes(2)
   644  			0x01, 0x02, // "\u0001\u0002"
   645  			0x65,                         // text(5)
   646  			0x56, 0x61, 0x6c, 0x75, 0x65, // "Value"
   647  			0x43,             // bytes(3)
   648  			0x03, 0x04, 0x05, // "\u0003\u0004\u0005"
   649  		}
   650  
   651  		k := Key{KeyParts: []KeyPart{{1, []byte{1, 2}}}}
   652  		v := Value{3, 4, 5}
   653  		p := NewPayload(k, v)
   654  		b, err := cbor.Marshal(p)
   655  		require.NoError(t, err)
   656  		require.Equal(t, []byte(encoded), b)
   657  
   658  		var p2 Payload
   659  		err = cbor.Unmarshal(b, &p2)
   660  		require.NoError(t, err)
   661  
   662  		// Reset b to make sure that p2 doesn't share underlying data with CBOR-encoded data.
   663  		for i := 0; i < len(b); i++ {
   664  			b[i] = 0
   665  		}
   666  
   667  		require.True(t, p.Equals(&p2))
   668  
   669  		k2, err := p2.Key()
   670  		require.NoError(t, err)
   671  		require.True(t, k2.Equals(&k))
   672  
   673  		v2 := p2.Value()
   674  		require.True(t, v2.Equals(v))
   675  	})
   676  }