github.com/minio/console@v1.4.1/pkg/auth/utils/utils_test.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package utils
    18  
    19  import (
    20  	"crypto/sha1"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"golang.org/x/crypto/pbkdf2"
    25  )
    26  
    27  func TestRandomCharString(t *testing.T) {
    28  	funcAssert := assert.New(t)
    29  	// Test-1 : RandomCharString() should return string with expected length
    30  	length := 32
    31  	token := RandomCharString(length)
    32  	funcAssert.Equal(length, len(token))
    33  	// Test-2 : RandomCharString() should output random string, new generated string should not be equal to the previous one
    34  	newToken := RandomCharString(length)
    35  	funcAssert.NotEqual(token, newToken)
    36  }
    37  
    38  func TestComputeHmac256(t *testing.T) {
    39  	funcAssert := assert.New(t)
    40  	// Test-1 : ComputeHmac256() should return the right Hmac256 string based on a derived key
    41  	derivedKey := pbkdf2.Key([]byte("secret"), []byte("salt"), 4096, 32, sha1.New)
    42  	message := "hello world"
    43  	expectedHmac := "5r32q7W+0hcBnqzQwJJUDzVGoVivXGSodTcHSqG/9Q8="
    44  	hmac := ComputeHmac256(message, derivedKey)
    45  	funcAssert.Equal(hmac, expectedHmac)
    46  }