github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/util/utils_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "bytes" 21 "testing" 22 "time" 23 ) 24 25 func TestComputeSHA256(t *testing.T) { 26 if !bytes.Equal(ComputeSHA256([]byte("foobar")), ComputeSHA256([]byte("foobar"))) { 27 t.Fatalf("Expected hashes to match, but they did not match") 28 } 29 if bytes.Equal(ComputeSHA256([]byte("foobar1")), ComputeSHA256([]byte("foobar2"))) { 30 t.Fatalf("Expected hashes to be different, but they match") 31 } 32 } 33 34 func TestComputeSHA3256(t *testing.T) { 35 if !bytes.Equal(ComputeSHA3256([]byte("foobar")), ComputeSHA3256([]byte("foobar"))) { 36 t.Fatalf("Expected hashes to match, but they did not match") 37 } 38 if bytes.Equal(ComputeSHA3256([]byte("foobar1")), ComputeSHA3256([]byte("foobar2"))) { 39 t.Fatalf("Expected hashed to be different, but they match") 40 } 41 } 42 43 func TestUUIDGeneration(t *testing.T) { 44 uuid := GenerateUUID() 45 if len(uuid) != 36 { 46 t.Fatalf("UUID length is not correct. Expected = 36, Got = %d", len(uuid)) 47 } 48 uuid2 := GenerateUUID() 49 if uuid == uuid2 { 50 t.Fatalf("Two UUIDs are equal. This should never occur") 51 } 52 } 53 54 func TestTimestamp(t *testing.T) { 55 for i := 0; i < 10; i++ { 56 t.Logf("timestamp now: %v", CreateUtcTimestamp()) 57 time.Sleep(200 * time.Millisecond) 58 } 59 } 60 61 func TestToChaincodeArgs(t *testing.T) { 62 expected := [][]byte{[]byte("foo"), []byte("bar")} 63 actual := ToChaincodeArgs("foo", "bar") 64 if len(expected) != len(actual) { 65 t.Fatalf("Got %v, expected %v", actual, expected) 66 } 67 for i := range expected { 68 if !bytes.Equal(expected[i], actual[i]) { 69 t.Fatalf("Got %v, expected %v", actual, expected) 70 } 71 } 72 } 73 74 func TestMetadataSignatureBytesNormal(t *testing.T) { 75 first := []byte("first") 76 second := []byte("second") 77 third := []byte("third") 78 79 result := ConcatenateBytes(first, second, third) 80 expected := []byte("firstsecondthird") 81 if !bytes.Equal(result, expected) { 82 t.Errorf("Did not concatenate bytes correctly, expected %s, got %s", expected, result) 83 } 84 } 85 86 func TestMetadataSignatureBytesNil(t *testing.T) { 87 first := []byte("first") 88 second := []byte(nil) 89 third := []byte("third") 90 91 result := ConcatenateBytes(first, second, third) 92 expected := []byte("firstthird") 93 if !bytes.Equal(result, expected) { 94 t.Errorf("Did not concatenate bytes correctly, expected %s, got %s", expected, result) 95 } 96 }