...

Source file src/github.com/googleapis/enterprise-certificate-proxy/linux/client_test.go

Documentation: github.com/googleapis/enterprise-certificate-proxy/linux

     1  // Copyright 2023 Google LLC.
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  //     https://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package linux
    15  
    16  import (
    17  	"bytes"
    18  	"crypto"
    19  	"crypto/rsa"
    20  	"flag"
    21  	"testing"
    22  )
    23  
    24  const (
    25  	testModule  = "/usr/local/lib/softhsm/libsofthsm2.so"
    26  	testLabel   = "Demo Object"
    27  	testUserPin = "0000"
    28  )
    29  
    30  var testSlot = *flag.String("testSlot", "", "libsofthsm2 slot location")
    31  
    32  func TestEncrypt(t *testing.T) {
    33  	sk, err := NewSecureKey(testModule, testSlot, testLabel, testUserPin)
    34  	if err != nil {
    35  		t.Errorf("Client Encrypt: error generating secure key, %q", err)
    36  	}
    37  	message := "Plain text to encrypt"
    38  	bMessage := []byte(message)
    39  	//Softhsm only supports SHA1
    40  	_, err = sk.Encrypt(nil, bMessage, crypto.SHA1)
    41  	if err != nil {
    42  		t.Errorf("Client Encrypt error: %q", err)
    43  	}
    44  }
    45  
    46  func TestDecrypt(t *testing.T) {
    47  	sk, err := NewSecureKey(testModule, testSlot, testLabel, testUserPin)
    48  	if err != nil {
    49  		t.Errorf("Client Decrypt: error generating secure key, %q", err)
    50  	}
    51  	message := "Plain text to encrypt"
    52  	bMessage := []byte(message)
    53  	//Softhsm only supports SHA1
    54  	cipher, err := sk.Encrypt(nil, bMessage, crypto.SHA1)
    55  	if err != nil {
    56  		t.Errorf("Client Encrypt error: %q", err)
    57  	}
    58  	decrypted, err := sk.Decrypt(nil, cipher, &rsa.OAEPOptions{Hash: crypto.SHA1})
    59  	if err != nil {
    60  		t.Fatalf("Client Decrypt error: %v", err)
    61  	}
    62  	decrypted = bytes.Trim(decrypted, "\x00")
    63  	if string(decrypted) != message {
    64  		t.Errorf("Client Decrypt error: expected %q, got %q", message, string(decrypted))
    65  	}
    66  }
    67  

View as plain text