-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Group common test utililty in an internal package
PiperOrigin-RevId: 713949926 Change-Id: Ief74f897f2397fbf55c9e463b0ea8468fd39d7d7
- Loading branch information
1 parent
3a315f7
commit 8cca13f
Showing
7 changed files
with
152 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package testutil provides testing utilities for AEAD primitives. | ||
package testutil | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/tink-crypto/tink-go/v2/subtle/random" | ||
"github.com/tink-crypto/tink-go/v2/tink" | ||
) | ||
|
||
// EncryptDecrypt encrypts and decrypts random data using the given AEAD | ||
// primitives. | ||
func EncryptDecrypt(encryptor, decryptor tink.AEAD) error { | ||
// Try to encrypt and decrypt random data. | ||
pt := random.GetRandomBytes(32) | ||
aad := random.GetRandomBytes(32) | ||
ct, err := encryptor.Encrypt(pt, aad) | ||
if err != nil { | ||
return fmt.Errorf("encryptor.Encrypt() err = %v, want nil", err) | ||
} | ||
decrypted, err := decryptor.Decrypt(ct, aad) | ||
if err != nil { | ||
return fmt.Errorf("decryptor.Decrypt() err = %v, want nil", err) | ||
} | ||
if !bytes.Equal(decrypted, pt) { | ||
return fmt.Errorf("decryptor.Decrypt() = %v, want %v", decrypted, pt) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package testutil_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/tink-crypto/tink-go/v2/aead/internal/testutil" | ||
"github.com/tink-crypto/tink-go/v2/aead/subtle" | ||
"github.com/tink-crypto/tink-go/v2/subtle/random" | ||
tinktestutil "github.com/tink-crypto/tink-go/v2/testutil" | ||
) | ||
|
||
func TestEncryptDecryptFailsWithFailingAEAD(t *testing.T) { | ||
failingAEAD := tinktestutil.NewAlwaysFailingAead(fmt.Errorf("test error")) | ||
a, err := subtle.NewAESGCM(random.GetRandomBytes(32)) | ||
if err != nil { | ||
t.Fatalf("subtle.NewAESGCM() err = %v, want nil", err) | ||
} | ||
if err := testutil.EncryptDecrypt(failingAEAD, a); err == nil { | ||
t.Errorf("EncryptDecrypt(failingAEAD, a) err = nil, want non-nil") | ||
} | ||
if err := testutil.EncryptDecrypt(a, failingAEAD); err == nil { | ||
t.Errorf("EncryptDecrypt(a, failingAEAD) err = nil, want non-nil") | ||
} | ||
if err := testutil.EncryptDecrypt(failingAEAD, failingAEAD); err == nil { | ||
t.Errorf("EncryptDecrypt(failingAEAD, failingAEAD) err = nil, want non-nil") | ||
} | ||
} | ||
|
||
func TestEncryptDecryptWorks(t *testing.T) { | ||
keyBytes := random.GetRandomBytes(32) | ||
encryptor, err := subtle.NewAESGCM(keyBytes) | ||
if err != nil { | ||
t.Fatalf("subtle.NewAESGCM() err = %v, want nil", err) | ||
} | ||
decryptor, err := subtle.NewAESGCM(keyBytes) | ||
if err != nil { | ||
t.Fatalf("subtle.NewAESGCM() err = %v, want nil", err) | ||
} | ||
if err := testutil.EncryptDecrypt(encryptor, decryptor); err != nil { | ||
t.Fatalf("subtle.NewAESGCM() err = %v, want nil", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.