1// SPDX-License-Identifier: ISC 2// Copyright (c) 2014-2020 Bitmark Inc. 3// Use of this source code is governed by an ISC 4// license that can be found in the LICENSE file. 5 6package ownership 7 8import ( 9 "bytes" 10 "testing" 11 12 "github.com/bitmark-inc/bitmarkd/merkle" 13 "github.com/bitmark-inc/bitmarkd/transactionrecord" 14 "github.com/bitmark-inc/bitmarkd/util" 15) 16 17// test the packing/unpacking of owner data record 18// 19// ensures that pack->unpack returns the same original value 20func TestPackAssetData(t *testing.T) { 21 22 expectedTxId := merkle.Digest{ 23 0xa7, 0x4a, 0x90, 0xc2, 0xff, 0x76, 0x34, 0x7a, 24 0x9d, 0x34, 0x19, 0xe9, 0x20, 0x2f, 0x02, 0xd8, 25 0xff, 0x5d, 0xdd, 0xa2, 0x7c, 0xc1, 0x7b, 0xa1, 26 0x71, 0xbc, 0x7c, 0x68, 0xbc, 0xc9, 0xce, 0x49, 27 } 28 29 expectedAssetId := transactionrecord.AssetIdentifier{ 30 0x59, 0xd0, 0x61, 0x55, 0xd2, 0x5d, 0xff, 0xdb, 31 0x98, 0x27, 0x29, 0xde, 0x8d, 0xce, 0x9d, 0x78, 32 0x55, 0xca, 0x09, 0x4d, 0x8b, 0xab, 0x81, 0x24, 33 0xb3, 0x47, 0xc4, 0x06, 0x68, 0x47, 0x70, 0x56, 34 0xb3, 0xc2, 0x7c, 0xcb, 0x7d, 0x71, 0xb5, 0x40, 35 0x43, 0xd2, 0x07, 0xcc, 0xd1, 0x87, 0x64, 0x2b, 36 0xf9, 0xc8, 0x46, 0x6f, 0x9a, 0x8d, 0x0d, 0xbe, 37 0xfb, 0x4c, 0x41, 0x63, 0x3a, 0x7e, 0x39, 0xef, 38 } 39 40 expected := PackedOwnerData{ 41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 42 0x39, 0xa7, 0x4a, 0x90, 0xc2, 0xff, 0x76, 0x34, 43 0x7a, 0x9d, 0x34, 0x19, 0xe9, 0x20, 0x2f, 0x02, 44 0xd8, 0xff, 0x5d, 0xdd, 0xa2, 0x7c, 0xc1, 0x7b, 45 0xa1, 0x71, 0xbc, 0x7c, 0x68, 0xbc, 0xc9, 0xce, 46 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 47 0xd2, 0x59, 0xd0, 0x61, 0x55, 0xd2, 0x5d, 0xff, 48 0xdb, 0x98, 0x27, 0x29, 0xde, 0x8d, 0xce, 0x9d, 49 0x78, 0x55, 0xca, 0x09, 0x4d, 0x8b, 0xab, 0x81, 50 0x24, 0xb3, 0x47, 0xc4, 0x06, 0x68, 0x47, 0x70, 51 0x56, 0xb3, 0xc2, 0x7c, 0xcb, 0x7d, 0x71, 0xb5, 52 0x40, 0x43, 0xd2, 0x07, 0xcc, 0xd1, 0x87, 0x64, 53 0x2b, 0xf9, 0xc8, 0x46, 0x6f, 0x9a, 0x8d, 0x0d, 54 0xbe, 0xfb, 0x4c, 0x41, 0x63, 0x3a, 0x7e, 0x39, 55 0xef, 56 } 57 a := AssetOwnerData{ 58 transferBlockNumber: 12345, 59 issueTxId: expectedTxId, 60 issueBlockNumber: 1234, 61 assetId: expectedAssetId, 62 } 63 64 // test the packer 65 packed := a.Pack() 66 67 // if either of above fail we will have the message _without_ a signature 68 if !bytes.Equal(packed, expected) { 69 t.Errorf("pack record: %x expected: %x", packed, expected) 70 t.Errorf("*** GENERATED Packed:\n%s", util.FormatBytes("expected", packed)) 71 t.Fatal("fatal error") 72 } 73 74 unpacked, err := packed.Unpack() 75 if nil != err { 76 t.Fatalf("unpack error: %s", err) 77 } 78 79 ownerdata, ok := unpacked.(*AssetOwnerData) 80 if !ok { 81 t.Fatalf("unpacked to wrong type: %v", unpacked) 82 } 83 84 if a != *ownerdata { 85 t.Errorf("record: %+v expected: %+v", ownerdata, a) 86 } 87} 88 89// test the packing/unpacking of owner data record 90// 91// ensures that pack->unpack returns the same original value 92func TestPackBlockData(t *testing.T) { 93 94 expectedTxId := merkle.Digest{ 95 0xa7, 0x4a, 0x90, 0xc2, 0xff, 0x76, 0x34, 0x7a, 96 0x9d, 0x34, 0x19, 0xe9, 0x20, 0x2f, 0x02, 0xd8, 97 0xff, 0x5d, 0xdd, 0xa2, 0x7c, 0xc1, 0x7b, 0xa1, 98 0x71, 0xbc, 0x7c, 0x68, 0xbc, 0xc9, 0xce, 0x49, 99 } 100 101 expected := PackedOwnerData{ 102 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 103 0x39, 0xa7, 0x4a, 0x90, 0xc2, 0xff, 0x76, 0x34, 104 0x7a, 0x9d, 0x34, 0x19, 0xe9, 0x20, 0x2f, 0x02, 105 0xd8, 0xff, 0x5d, 0xdd, 0xa2, 0x7c, 0xc1, 0x7b, 106 0xa1, 0x71, 0xbc, 0x7c, 0x68, 0xbc, 0xc9, 0xce, 107 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 108 0xd2, 109 } 110 a := BlockOwnerData{ 111 transferBlockNumber: 12345, 112 issueTxId: expectedTxId, 113 issueBlockNumber: 1234, 114 } 115 116 // test the packer 117 packed := a.Pack() 118 119 // if either of above fail we will have the message _without_ a signature 120 if !bytes.Equal(packed, expected) { 121 t.Errorf("pack record: %x expected: %x", packed, expected) 122 t.Errorf("*** GENERATED Packed:\n%s", util.FormatBytes("expected", packed)) 123 t.Fatal("fatal error") 124 } 125 126 unpacked, err := packed.Unpack() 127 if nil != err { 128 t.Fatalf("unpack error: %s", err) 129 } 130 131 ownerdata, ok := unpacked.(*BlockOwnerData) 132 if !ok { 133 t.Fatalf("unpacked to wrong type: %v", unpacked) 134 } 135 136 if a != *ownerdata { 137 t.Errorf("record: %+v expected: %+v", ownerdata, a) 138 } 139} 140 141// test the packing/unpacking of owner data record 142// 143// ensures that pack->unpack returns the same original value 144func TestPackShareData(t *testing.T) { 145 146 expectedTxId := merkle.Digest{ 147 0xa7, 0x4a, 0x90, 0xc2, 0xff, 0x76, 0x34, 0x7a, 148 0x9d, 0x34, 0x19, 0xe9, 0x20, 0x2f, 0x02, 0xd8, 149 0xff, 0x5d, 0xdd, 0xa2, 0x7c, 0xc1, 0x7b, 0xa1, 150 0x71, 0xbc, 0x7c, 0x68, 0xbc, 0xc9, 0xce, 0x49, 151 } 152 153 expectedAssetId := transactionrecord.AssetIdentifier{ 154 0x59, 0xd0, 0x61, 0x55, 0xd2, 0x5d, 0xff, 0xdb, 155 0x98, 0x27, 0x29, 0xde, 0x8d, 0xce, 0x9d, 0x78, 156 0x55, 0xca, 0x09, 0x4d, 0x8b, 0xab, 0x81, 0x24, 157 0xb3, 0x47, 0xc4, 0x06, 0x68, 0x47, 0x70, 0x56, 158 0xb3, 0xc2, 0x7c, 0xcb, 0x7d, 0x71, 0xb5, 0x40, 159 0x43, 0xd2, 0x07, 0xcc, 0xd1, 0x87, 0x64, 0x2b, 160 0xf9, 0xc8, 0x46, 0x6f, 0x9a, 0x8d, 0x0d, 0xbe, 161 0xfb, 0x4c, 0x41, 0x63, 0x3a, 0x7e, 0x39, 0xef, 162 } 163 164 expected := PackedOwnerData{ 165 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 166 0x39, 0xa7, 0x4a, 0x90, 0xc2, 0xff, 0x76, 0x34, 167 0x7a, 0x9d, 0x34, 0x19, 0xe9, 0x20, 0x2f, 0x02, 168 0xd8, 0xff, 0x5d, 0xdd, 0xa2, 0x7c, 0xc1, 0x7b, 169 0xa1, 0x71, 0xbc, 0x7c, 0x68, 0xbc, 0xc9, 0xce, 170 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 171 0xd2, 0x59, 0xd0, 0x61, 0x55, 0xd2, 0x5d, 0xff, 172 0xdb, 0x98, 0x27, 0x29, 0xde, 0x8d, 0xce, 0x9d, 173 0x78, 0x55, 0xca, 0x09, 0x4d, 0x8b, 0xab, 0x81, 174 0x24, 0xb3, 0x47, 0xc4, 0x06, 0x68, 0x47, 0x70, 175 0x56, 0xb3, 0xc2, 0x7c, 0xcb, 0x7d, 0x71, 0xb5, 176 0x40, 0x43, 0xd2, 0x07, 0xcc, 0xd1, 0x87, 0x64, 177 0x2b, 0xf9, 0xc8, 0x46, 0x6f, 0x9a, 0x8d, 0x0d, 178 0xbe, 0xfb, 0x4c, 0x41, 0x63, 0x3a, 0x7e, 0x39, 179 0xef, 180 } 181 a := ShareOwnerData{ 182 transferBlockNumber: 12345, 183 issueTxId: expectedTxId, 184 issueBlockNumber: 1234, 185 assetId: expectedAssetId, 186 } 187 188 // test the packer 189 packed := a.Pack() 190 191 // if either of above fail we will have the message _without_ a signature 192 if !bytes.Equal(packed, expected) { 193 t.Errorf("pack record: %x expected: %x", packed, expected) 194 t.Errorf("*** GENERATED Packed:\n%s", util.FormatBytes("expected", packed)) 195 t.Fatal("fatal error") 196 } 197 198 unpacked, err := packed.Unpack() 199 if nil != err { 200 t.Fatalf("unpack error: %s", err) 201 } 202 203 ownerdata, ok := unpacked.(*ShareOwnerData) 204 if !ok { 205 t.Fatalf("unpacked to wrong type: %v", unpacked) 206 } 207 208 if a != *ownerdata { 209 t.Errorf("record: %+v expected: %+v", ownerdata, a) 210 } 211} 212