github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/core/chaincode/platforms/util/utils_test.go (about) 1 package util 2 3 import ( 4 "bytes" 5 "encoding/hex" 6 "math/rand" 7 "testing" 8 "time" 9 10 "github.com/hyperledger/fabric/common/util" 11 ) 12 13 // TestHashContentChange changes a random byte in a content and checks for hash change 14 func TestHashContentChange(t *testing.T) { 15 b := []byte("firstcontent") 16 hash := util.ComputeSHA256(b) 17 18 b2 := []byte("To be, or not to be- that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die- to sleep- No more; and by a sleep to say we end The heartache, and the thousand natural shocks That flesh is heir to. 'Tis a consummation Devoutly to be wish'd.") 19 20 h1 := ComputeHash(b2, hash) 21 22 r := rand.New(rand.NewSource(time.Now().UnixNano())) 23 randIndex := (int(r.Uint32())) % len(b2) 24 25 randByte := byte((int(r.Uint32())) % 128) 26 27 //make sure the two bytes are different 28 for { 29 if randByte != b2[randIndex] { 30 break 31 } 32 33 randByte = byte((int(r.Uint32())) % 128) 34 } 35 36 //change a random byte 37 b2[randIndex] = randByte 38 39 //this is the core hash func under test 40 h2 := ComputeHash(b2, hash) 41 42 //the two hashes should be different 43 if bytes.Compare(h1, h2) == 0 { 44 t.Error("Hash expected to be different but is same") 45 } 46 } 47 48 // TestHashLenChange changes a random length of a content and checks for hash change 49 func TestHashLenChange(t *testing.T) { 50 b := []byte("firstcontent") 51 hash := util.ComputeSHA256(b) 52 53 b2 := []byte("To be, or not to be-") 54 55 h1 := ComputeHash(b2, hash) 56 57 r := rand.New(rand.NewSource(time.Now().UnixNano())) 58 randIndex := (int(r.Uint32())) % len(b2) 59 60 b2 = b2[0:randIndex] 61 62 h2 := ComputeHash(b2, hash) 63 64 //hash should be different 65 if bytes.Compare(h1, h2) == 0 { 66 t.Error("Hash expected to be different but is same") 67 } 68 } 69 70 // TestHashOrderChange changes a order of hash computation over a list of lines and checks for hash change 71 func TestHashOrderChange(t *testing.T) { 72 b := []byte("firstcontent") 73 hash := util.ComputeSHA256(b) 74 75 b2 := [][]byte{[]byte("To be, or not to be- that is the question:"), 76 []byte("Whether 'tis nobler in the mind to suffer"), 77 []byte("The slings and arrows of outrageous fortune"), 78 []byte("Or to take arms against a sea of troubles,"), 79 []byte("And by opposing end them."), 80 []byte("To die- to sleep- No more; and by a sleep to say we end"), 81 []byte("The heartache, and the thousand natural shocks"), 82 []byte("That flesh is heir to."), 83 []byte("'Tis a consummation Devoutly to be wish'd.")} 84 h1 := hash 85 86 for _, l := range b2 { 87 h1 = ComputeHash(l, h1) 88 } 89 90 r := rand.New(rand.NewSource(time.Now().UnixNano())) 91 randIndex1 := (int(r.Uint32())) % len(b2) 92 randIndex2 := (int(r.Uint32())) % len(b2) 93 94 //make sure the two indeces are different 95 for { 96 if randIndex2 != randIndex1 { 97 break 98 } 99 100 randIndex2 = (int(r.Uint32())) % len(b2) 101 } 102 103 //switch two arbitrary lines 104 tmp := b2[randIndex2] 105 b2[randIndex2] = b2[randIndex1] 106 b2[randIndex1] = tmp 107 108 h2 := hash 109 for _, l := range b2 { 110 h2 = ComputeHash(l, hash) 111 } 112 113 //hash should be different 114 if bytes.Compare(h1, h2) == 0 { 115 t.Error("Hash expected to be different but is same") 116 } 117 } 118 119 // TestHashOverFiles computes hash over a directory and ensures it matches precomputed, hardcoded, hash 120 func TestHashOverFiles(t *testing.T) { 121 b := []byte("firstcontent") 122 hash := util.ComputeSHA256(b) 123 124 hash, err := HashFilesInDir(".", "hashtestfiles1", hash, nil) 125 126 if err != nil { 127 t.Fail() 128 t.Logf("error : %s", err) 129 } 130 131 //as long as no files under "hashtestfiles1" are changed, hash should always compute to the following 132 expectedHash := "0c92180028200dfabd08d606419737f5cdecfcbab403e3f0d79e8d949f4775bc" 133 134 computedHash := hex.EncodeToString(hash[:]) 135 136 if expectedHash != computedHash { 137 t.Error("Hash expected to be unchanged") 138 } 139 } 140 141 func TestHashDiffDir(t *testing.T) { 142 b := []byte("firstcontent") 143 hash := util.ComputeSHA256(b) 144 145 hash1, err := HashFilesInDir(".", "hashtestfiles1", hash, nil) 146 if err != nil { 147 t.Errorf("Error getting code %s", err) 148 } 149 hash2, err := HashFilesInDir(".", "hashtestfiles2", hash, nil) 150 if err != nil { 151 t.Errorf("Error getting code %s", err) 152 } 153 if bytes.Compare(hash1, hash2) == 0 { 154 t.Error("Hash should be different for 2 different remote repos") 155 } 156 157 } 158 func TestHashSameDir(t *testing.T) { 159 b := []byte("firstcontent") 160 hash := util.ComputeSHA256(b) 161 162 hash1, err := HashFilesInDir(".", "hashtestfiles1", hash, nil) 163 if err != nil { 164 t.Errorf("Error getting code %s", err) 165 } 166 hash2, err := HashFilesInDir(".", "hashtestfiles1", hash, nil) 167 if err != nil { 168 t.Errorf("Error getting code %s", err) 169 } 170 if bytes.Compare(hash1, hash2) != 0 { 171 t.Error("Hash should be same across multiple downloads") 172 } 173 }