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

     1  package crypto
     2  
     3  import "testing"
     4  
     5  func TestAEADKeySize(t *testing.T) {
     6  	if AEADKeySize != 32 {
     7  		t.Error("AEADKeySize != 32")
     8  	}
     9  }
    10  
    11  func TestAEADNonceSize(t *testing.T) {
    12  	if AEADNonceSize != 12 {
    13  		t.Error("AEADNonceSize != 12")
    14  	}
    15  }
    16  
    17  func TestNewAEADKey(t *testing.T) {
    18  	key, err := NewAEADKey()
    19  	if err != nil {
    20  		t.Error(err)
    21  	}
    22  
    23  	if len(key) != AEADKeySize {
    24  		t.Errorf("generated key have bad size (%d)", len(key))
    25  	}
    26  }
    27  
    28  func TestNewAEADNonce(t *testing.T) {
    29  	nonce, err := NewAEADNonce()
    30  	if err != nil {
    31  		t.Error(err)
    32  	}
    33  
    34  	if len(nonce) != AEADNonceSize {
    35  		t.Errorf("generated nonce have bad size (%d)", len(nonce))
    36  	}
    37  }
    38  
    39  func TestAEADSealDstNil(t *testing.T) {
    40  	data := []byte("this is a plaintext message")
    41  	ad := []byte("additional data")
    42  
    43  	nonce, err := NewAEADNonce()
    44  	if err != nil {
    45  		t.Error(err)
    46  	}
    47  	key, err := NewAEADKey()
    48  	if err != nil {
    49  		t.Error(err)
    50  	}
    51  	cipher, err := NewAEAD(key)
    52  	if err != nil {
    53  		t.Error(err)
    54  	}
    55  
    56  	ciphertext := cipher.Seal(nil, nonce, data, ad)
    57  	if ciphertext == nil {
    58  		t.Error("ciphertext is nil")
    59  	}
    60  }
    61  
    62  func TestEncryptDecryptWithNonce(t *testing.T) {
    63  	plaintext := []byte("this is a plaintext message")
    64  	ad := []byte("additional data")
    65  
    66  	key, err := NewAEADKey()
    67  	if err != nil {
    68  		t.Error(err)
    69  	}
    70  
    71  	cipherText, nonce, err := EncryptWithNonce(key, plaintext, ad)
    72  	if err != nil {
    73  		t.Error(err)
    74  	}
    75  
    76  	plaintext2, err := DecryptWithNonce(key, nonce, cipherText, ad)
    77  	if err != nil {
    78  		t.Error(err)
    79  	}
    80  
    81  	if string(plaintext) != string(plaintext2) {
    82  		t.Errorf("bad plaintext while decrypting: %s, expedting: %s", string(plaintext2), string(plaintext))
    83  	}
    84  }
    85  
    86  func TestEncryptDecrypt(t *testing.T) {
    87  	plaintext := []byte("this is a plaintext message")
    88  	ad := []byte("additional data")
    89  
    90  	key, err := NewAEADKey()
    91  	if err != nil {
    92  		t.Error(err)
    93  	}
    94  
    95  	cipherText, err := Encrypt(key, plaintext, ad)
    96  	if err != nil {
    97  		t.Error(err)
    98  	}
    99  
   100  	plaintext2, err := Decrypt(key, cipherText, ad)
   101  	if err != nil {
   102  		t.Error(err)
   103  	}
   104  
   105  	if string(plaintext) != string(plaintext2) {
   106  		t.Errorf("bad plaintext while decrypting: %s, expedting: %s", string(plaintext2), string(plaintext))
   107  	}
   108  }