|
| 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 TestUUIDGeneration(t *testing.T) { |
| 26 | + uuid := GenerateUUID() |
| 27 | + if len(uuid) != 36 { |
| 28 | + t.Fatalf("UUID length is not correct. Expected = 36, Got = %d", len(uuid)) |
| 29 | + } |
| 30 | + uuid2 := GenerateUUID() |
| 31 | + if uuid == uuid2 { |
| 32 | + t.Fatalf("Two UUIDs are equal. This should never occur") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestTimestamp(t *testing.T) { |
| 37 | + for i := 0; i < 10; i++ { |
| 38 | + t.Logf("timestamp now: %v", CreateUtcTimestamp()) |
| 39 | + time.Sleep(200 * time.Millisecond) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestToChaincodeArgs(t *testing.T) { |
| 44 | + expected := [][]byte{[]byte("foo"), []byte("bar")} |
| 45 | + actual := ToChaincodeArgs("foo", "bar") |
| 46 | + if len(expected) != len(actual) { |
| 47 | + t.Fatalf("Got %v, expected %v", actual, expected) |
| 48 | + } |
| 49 | + for i := range expected { |
| 50 | + if !bytes.Equal(expected[i], actual[i]) { |
| 51 | + t.Fatalf("Got %v, expected %v", actual, expected) |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestConcatenateBytesNormal(t *testing.T) { |
| 57 | + first := []byte("first") |
| 58 | + second := []byte("second") |
| 59 | + third := []byte("third") |
| 60 | + |
| 61 | + result := ConcatenateBytes(first, second, third) |
| 62 | + expected := []byte("firstsecondthird") |
| 63 | + if !bytes.Equal(result, expected) { |
| 64 | + t.Errorf("Did not concatenate bytes correctly, expected %s, got %s", expected, result) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestConcatenateBytesNil(t *testing.T) { |
| 69 | + first := []byte("first") |
| 70 | + second := []byte(nil) |
| 71 | + third := []byte("third") |
| 72 | + |
| 73 | + result := ConcatenateBytes(first, second, third) |
| 74 | + expected := []byte("firstthird") |
| 75 | + if !bytes.Equal(result, expected) { |
| 76 | + t.Errorf("Did not concatenate bytes correctly, expected %s, got %s", expected, result) |
| 77 | + } |
| 78 | +} |
0 commit comments