github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/api/verify_test.go (about) 1 /* 2 * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved. 3 * This software is released under GPL3. 4 * The full license information can be found under: 5 * https://www.gnu.org/licenses/gpl-3.0.en.html 6 * 7 */ 8 9 package api 10 11 import ( 12 "encoding/json" 13 "testing" 14 "time" 15 16 "github.com/ethereum/go-ethereum/common" 17 "github.com/stretchr/testify/assert" 18 "github.com/vchain-us/vcn/pkg/meta" 19 "gopkg.in/yaml.v2" 20 ) 21 22 const ( 23 zeroMetahash = "04d05179816469d9d1d3493b695df3396b10617035c9621bbb8757cfa0efb0f4" 24 zeroUnknownMetahash = "83e7f28a74ada954937e609a2b7966edc39497d53203094b9097d3684f9a27ec" 25 emptyJSON = "null" 26 emptyYAML = "null\n" 27 ) 28 29 func TestBlockchainVerification(t *testing.T) { 30 var bv *BlockchainVerification 31 32 // Nil 33 assert.False(t, bv.Trusted()) 34 assert.True(t, bv.Unknown()) 35 assert.Empty(t, bv.MetaHash()) 36 assert.Empty(t, bv.SignerID()) 37 assert.Empty(t, bv.Date()) 38 j, err := json.Marshal(bv) 39 assert.NoError(t, err) 40 assert.Equal(t, emptyJSON, string(j)) 41 y, err := yaml.Marshal(bv) 42 assert.NoError(t, err) 43 assert.Equal(t, emptyYAML, string(y)) 44 45 bv = &BlockchainVerification{} 46 // Zero value 47 assert.True(t, bv.Trusted()) 48 assert.False(t, bv.Unknown()) 49 assert.Equal(t, zeroMetahash, bv.MetaHash()) 50 assert.Empty(t, bv.SignerID()) 51 assert.Empty(t, bv.Date()) 52 j, err = json.Marshal(bv) 53 assert.NoError(t, err) 54 assert.NotEqual(t, emptyJSON, string(j)) 55 y, err = yaml.Marshal(bv) 56 assert.NoError(t, err) 57 assert.NotEqual(t, emptyYAML, string(y)) 58 59 bv = &BlockchainVerification{ 60 Status: meta.StatusUnknown, 61 } 62 // Default status 63 assert.False(t, bv.Trusted()) 64 assert.True(t, bv.Unknown()) 65 assert.Equal(t, zeroUnknownMetahash, bv.MetaHash()) 66 assert.Empty(t, bv.SignerID()) 67 assert.Empty(t, bv.Date()) 68 j, err = json.Marshal(bv) 69 assert.NoError(t, err) 70 assert.NotEqual(t, emptyJSON, string(j)) 71 y, err = yaml.Marshal(bv) 72 assert.NoError(t, err) 73 assert.NotEqual(t, emptyYAML, string(y)) 74 } 75 76 func TestJSONUnmarshalBlockchainVerification(t *testing.T) { 77 v := BlockchainVerification{} 78 79 data := []byte(`{ 80 "level": 0, 81 "owner": "", 82 "status": 2, 83 "timestamp": "" 84 }`) 85 86 err := json.Unmarshal(data, &v) 87 assert.NoError(t, err) 88 assert.Equal(t, BlockchainVerification{Status: meta.Status(2)}, v) 89 90 v = BlockchainVerification{} 91 92 data = []byte(`{ 93 "level": 3, 94 "owner": "0x0123456789abcdef0123456789abcdef01234567", 95 "status": 0, 96 "timestamp": "2019-06-26T16:20:25Z" 97 }`) 98 99 timestamp, _ := time.Parse(time.RFC3339, "2019-06-26T16:20:25Z") 100 101 err = json.Unmarshal(data, &v) 102 assert.NoError(t, err) 103 assert.Equal(t, BlockchainVerification{ 104 Level: meta.Level(3), 105 Owner: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"), 106 Status: meta.Status(0), 107 Timestamp: timestamp, 108 }, v) 109 } 110 111 func TestYAMLUnmarshalBlockchainVerification(t *testing.T) { 112 v := BlockchainVerification{} 113 114 data := []byte(`level: 0 115 owner: "" 116 status: 2 117 timestamp: "" 118 `) 119 120 err := yaml.Unmarshal(data, &v) 121 assert.NoError(t, err) 122 assert.Equal(t, BlockchainVerification{Status: meta.Status(2)}, v) 123 124 v = BlockchainVerification{} 125 126 data = []byte(`level: 3 127 owner: "0x0123456789abcdef0123456789abcdef01234567" 128 status: 0 129 timestamp: "2019-06-26T16:20:25Z" 130 `) 131 132 timestamp, _ := time.Parse(time.RFC3339, "2019-06-26T16:20:25Z") 133 134 err = yaml.Unmarshal(data, &v) 135 assert.NoError(t, err) 136 assert.Equal(t, BlockchainVerification{ 137 Level: meta.Level(3), 138 Owner: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"), 139 Status: meta.Status(0), 140 Timestamp: timestamp, 141 }, v) 142 }