github.com/openebs/api@v1.12.0/pkg/util/hash_test.go (about) 1 // Copyright © 2020 The OpenEBS Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package util 16 17 import ( 18 "sort" 19 "testing" 20 ) 21 22 func TestHashList(t *testing.T) { 23 fakeTestList := map[string][]string{ 24 "list1": []string{}, 25 "list2": []string{"list-1", "list-2", "list-3"}, 26 "list3": []string{"list-4", "list-5", "list-6"}, 27 } 28 29 for _, test := range fakeTestList { 30 fakeHash, err := Hash(test) 31 if err != nil { 32 t.Errorf("Failed to calculate the hash expected string but got: '%s' Error: '%v'", fakeHash, err) 33 } 34 } 35 fakeHash, err := Hash(fakeTestList) 36 if err != nil { 37 t.Errorf("Failed to calculate the hash expected string but got: '%s' Error: '%v'", fakeHash, err) 38 } 39 } 40 41 func TestHashStruct(t *testing.T) { 42 fakeStruct := map[string]struct { 43 name string 44 list []string 45 }{ 46 "struct1": { 47 name: "", 48 list: []string{}, 49 }, 50 "struct2": { 51 name: "abcdefgh", 52 list: []string{"abc-1", "abc-2", "abc-3"}, 53 }, 54 "struct3": { 55 name: "jklmnop", 56 list: []string{"abcde", "abcdf", "hash"}, 57 }, 58 } 59 for _, test := range fakeStruct { 60 fakeHash, err := Hash(test) 61 if err != nil { 62 t.Errorf("Failed to calculate the hash expected string but got: '%s' Error: '%v'", fakeHash, err) 63 } 64 } 65 fakeHash, err := Hash(fakeStruct) 66 if err != nil { 67 t.Errorf("Failed to calculate the hash expected string but got: '%s' Error: '%v'", fakeHash, err) 68 } 69 } 70 71 func TestHashInnerStruct(t *testing.T) { 72 type fakeStruct struct { 73 name string 74 list []string 75 } 76 fakeComplexStruct := map[int]struct { 77 innerStructNum int 78 innerStruct struct { 79 name string 80 list []string 81 } 82 }{ 83 1: { 84 innerStructNum: 1, 85 innerStruct: fakeStruct{ 86 name: "", 87 list: []string{}, 88 }, 89 }, 90 2: { 91 innerStructNum: 2, 92 innerStruct: fakeStruct{ 93 name: "hashInnerStruct", 94 list: []string{"hash1", "hash2", "hash3", "hash4"}, 95 }, 96 }, 97 } 98 for _, test := range fakeComplexStruct { 99 fakeHash, err := Hash(test) 100 if err != nil { 101 t.Errorf("Failed to calculate the hash expected string but got: '%s' Error: '%v'", fakeHash, err) 102 } 103 } 104 fakeHash, err := Hash(fakeComplexStruct) 105 if err != nil { 106 t.Errorf("Failed to calculate the hash expected string but got: '%s' Error: '%v'", fakeHash, err) 107 } 108 } 109 110 func TestHashOutput(t *testing.T) { 111 list1 := []string{"one", "three", "two", "four"} 112 list2 := []string{"four", "one", "three", "two"} 113 sort.Strings(list1) 114 sort.Strings(list2) 115 hash1, err := Hash(list1) 116 if err != nil { 117 t.Errorf("Failed to calculate the hash expected string but got: '%s' Error: '%v'", hash1, err) 118 } 119 hash2, err := Hash(list2) 120 if err != nil { 121 t.Errorf("Failed to calculate the hash expected string but got: '%s' Error: '%v'", hash2, err) 122 } 123 if hash1 != hash2 { 124 t.Errorf("hash value didn't matched for the same list") 125 } 126 }