github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/security/compare_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package security 19 20 import ( 21 "testing" 22 ) 23 24 func TestSecureCompare(t *testing.T) { 25 type args struct { 26 given []byte 27 actual []byte 28 } 29 tests := []struct { 30 name string 31 args args 32 want bool 33 }{ 34 { 35 name: "not equal, same size", 36 args: args{ 37 given: []byte{0x01}, 38 actual: []byte{0x02}, 39 }, 40 want: false, 41 }, 42 { 43 name: "not equal, different size", 44 args: args{ 45 given: []byte{0x01, 0x02}, 46 actual: []byte{0x02}, 47 }, 48 want: false, 49 }, 50 { 51 name: "equal, different size", 52 args: args{ 53 given: []byte{0x00}, 54 actual: []byte{}, 55 }, 56 want: false, 57 }, 58 { 59 name: "equal, same size", 60 args: args{ 61 given: []byte{0x01}, 62 actual: []byte{0x01}, 63 }, 64 want: true, 65 }, 66 } 67 for _, tt := range tests { 68 t.Run(tt.name, func(t *testing.T) { 69 if got := SecureCompare(tt.args.given, tt.args.actual); got != tt.want { 70 t.Errorf("SecureCompare() = %v, want %v", got, tt.want) 71 } 72 }) 73 } 74 } 75 76 func TestSecureCompareString(t *testing.T) { 77 type args struct { 78 given string 79 actual string 80 } 81 tests := []struct { 82 name string 83 args args 84 want bool 85 }{ 86 { 87 name: "not equal, same size", 88 args: args{ 89 given: "a", 90 actual: "b", 91 }, 92 want: false, 93 }, 94 { 95 name: "not equal, different size", 96 args: args{ 97 given: "ab", 98 actual: "a", 99 }, 100 want: false, 101 }, 102 { 103 name: "equal, different size", 104 args: args{ 105 given: "\x00", 106 actual: "", 107 }, 108 want: false, 109 }, 110 { 111 name: "equal, same size", 112 args: args{ 113 given: "a", 114 actual: "a", 115 }, 116 want: true, 117 }, 118 } 119 for _, tt := range tests { 120 t.Run(tt.name, func(t *testing.T) { 121 if got := SecureCompareString(tt.args.given, tt.args.actual); got != tt.want { 122 t.Errorf("SecureCompareString() = %v, want %v", got, tt.want) 123 } 124 }) 125 } 126 }