git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/crypto/compare_test.go (about)

     1  package crypto
     2  
     3  import "testing"
     4  
     5  func TestConstantTimeCompare(t *testing.T) {
     6  	a := []byte("helloWorld")
     7  	b := []byte("helloWorld")
     8  	c := []byte("helloWarld")
     9  	d := []byte("helloWorl")
    10  
    11  	if !ConstantTimeCompare(a, a) {
    12  		t.Errorf("%s != %s", a, b)
    13  	}
    14  
    15  	if !ConstantTimeCompare(a, b) {
    16  		t.Errorf("%s != %s", a, b)
    17  	}
    18  
    19  	if ConstantTimeCompare(a, c) {
    20  		t.Errorf("%s == %s", a, c)
    21  	}
    22  
    23  	if ConstantTimeCompare(a, d) {
    24  		t.Errorf("%s == %s", a, d)
    25  	}
    26  
    27  	if ConstantTimeCompare(nil, a) {
    28  		t.Errorf("nil == %s", a)
    29  	}
    30  
    31  	if ConstantTimeCompare(a, nil) {
    32  		t.Errorf("%s == nil", a)
    33  	}
    34  
    35  	if !ConstantTimeCompare(nil, nil) {
    36  		t.Error("nil != nil")
    37  	}
    38  }