...

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

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

     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 darwin
    15  
    16  import (
    17  	"bytes"
    18  	"crypto"
    19  	"crypto/rsa"
    20  	"testing"
    21  )
    22  
    23  const testIssuer = "TestIssuer"
    24  
    25  func TestClientEncrypt(t *testing.T) {
    26  	secureKey, err := NewSecureKey(testIssuer)
    27  	if err != nil {
    28  		t.Errorf("Cred: got %v, want nil err", err)
    29  		return
    30  	}
    31  	plaintext := []byte("Plain text to encrypt")
    32  	_, err = secureKey.Encrypt(nil, plaintext, crypto.SHA256)
    33  	if err != nil {
    34  		t.Errorf("Client API encryption: got %v, want nil err", err)
    35  		return
    36  	}
    37  }
    38  
    39  func TestClientDecrypt(t *testing.T) {
    40  	secureKey, err := NewSecureKey(testIssuer)
    41  	if err != nil {
    42  		t.Errorf("Cred: got %v, want nil err", err)
    43  		return
    44  	}
    45  	byteSlice := []byte("Plain text to encrypt")
    46  	ciphertext, _ := secureKey.Encrypt(nil, byteSlice, crypto.SHA256)
    47  	plaintext, err := secureKey.Decrypt(nil, ciphertext, &rsa.OAEPOptions{Hash: crypto.SHA256})
    48  	if err != nil {
    49  		t.Errorf("Client API decryption: got %v, want nil err", err)
    50  		return
    51  	}
    52  	if !bytes.Equal(byteSlice, plaintext) {
    53  		t.Errorf("Decryption message does not match original: got %v, want %v", plaintext, byteSlice)
    54  	}
    55  }
    56  

View as plain text