github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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 "strings" 22 "testing" 23 "time" 24 25 "github.com/op/go-logging" 26 ) 27 28 func TestComputeCryptoHash(t *testing.T) { 29 if bytes.Compare(ComputeSHA256([]byte("foobar")), ComputeSHA256([]byte("foobar"))) != 0 { 30 t.Fatalf("Expected hashes to match, but they did not match") 31 } 32 if bytes.Compare(ComputeSHA256([]byte("foobar1")), ComputeSHA256([]byte("foobar2"))) == 0 { 33 t.Fatalf("Expected hashes to be different, but they match") 34 } 35 } 36 37 func TestUUIDGeneration(t *testing.T) { 38 uuid := GenerateUUID() 39 if len(uuid) != 36 { 40 t.Fatalf("UUID length is not correct. Expected = 36, Got = %d", len(uuid)) 41 } 42 uuid2 := GenerateUUID() 43 if uuid == uuid2 { 44 t.Fatalf("Two UUIDs are equal. This should never occur") 45 } 46 } 47 48 func TestIntUUIDGeneration(t *testing.T) { 49 uuid := GenerateIntUUID() 50 51 uuid2 := GenerateIntUUID() 52 if uuid == uuid2 { 53 t.Fatalf("Two UUIDs are equal. This should never occur") 54 } 55 } 56 func TestTimestamp(t *testing.T) { 57 for i := 0; i < 10; i++ { 58 t.Logf("timestamp now: %v", CreateUtcTimestamp()) 59 time.Sleep(200 * time.Millisecond) 60 } 61 } 62 63 func TestGenerateHashFromSignature(t *testing.T) { 64 if bytes.Compare(GenerateHashFromSignature("aPath", []byte("aCtor12")), 65 GenerateHashFromSignature("aPath", []byte("aCtor12"))) != 0 { 66 t.Fatalf("Expected hashes to match, but they did not match") 67 } 68 if bytes.Compare(GenerateHashFromSignature("aPath", []byte("aCtor12")), 69 GenerateHashFromSignature("bPath", []byte("bCtor34"))) == 0 { 70 t.Fatalf("Expected hashes to be different, but they match") 71 } 72 } 73 74 func TestGeneratIDfromTxSHAHash(t *testing.T) { 75 txid := GenerateIDfromTxSHAHash([]byte("foobar")) 76 txid2 := GenerateIDfromTxSHAHash([]byte("foobar1")) 77 if txid == txid2 { 78 t.Fatalf("Two TxIDs are equal. This should never occur") 79 } 80 } 81 82 func TestGenerateIDWithAlg(t *testing.T) { 83 _, err := GenerateIDWithAlg("sha256", []byte{1, 1, 1, 1}) 84 if err != nil { 85 t.Fatalf("Decoder failure: %v", err) 86 } 87 } 88 89 func TestFindMissingElements(t *testing.T) { 90 all := []string{"a", "b", "c", "d"} 91 some := []string{"b", "c"} 92 expectedDelta := []string{"a", "d"} 93 actualDelta := FindMissingElements(all, some) 94 if len(expectedDelta) != len(actualDelta) { 95 t.Fatalf("Got %v, expected %v", actualDelta, expectedDelta) 96 } 97 for i := range expectedDelta { 98 if strings.Compare(expectedDelta[i], actualDelta[i]) != 0 { 99 t.Fatalf("Got %v, expected %v", actualDelta, expectedDelta) 100 } 101 } 102 } 103 104 // This test checks go-logging is thread safe with regard to 105 // concurrent SetLevel invocation and log invocations. 106 // Fails without the concurrency fix (adding RWLock to level.go) 107 // In case the go-logging will be overwritten and its concurrency fix 108 // will be regressed, this test should fail. 109 func TestConcurrencyNotFail(t *testing.T) { 110 logger := logging.MustGetLogger("test") 111 go func() { 112 for i := 0; i < 100; i++ { 113 logging.SetLevel(logging.Level(logging.DEBUG), "test") 114 } 115 }() 116 117 for i := 0; i < 100; i++ { 118 logger.Info("") 119 } 120 } 121 122 func TestMetadataSignatureBytesNormal(t *testing.T) { 123 first := []byte("first") 124 second := []byte("second") 125 third := []byte("third") 126 127 result := ConcatenateBytes(first, second, third) 128 expected := []byte("firstsecondthird") 129 if !bytes.Equal(result, expected) { 130 t.Errorf("Did not concatenate bytes correctly, expected %s, got %s", expected, result) 131 } 132 } 133 134 func TestMetadataSignatureBytesNil(t *testing.T) { 135 first := []byte("first") 136 second := []byte(nil) 137 third := []byte("third") 138 139 result := ConcatenateBytes(first, second, third) 140 expected := []byte("firstthird") 141 if !bytes.Equal(result, expected) { 142 t.Errorf("Did not concatenate bytes correctly, expected %s, got %s", expected, result) 143 } 144 }