Skip to content

Commit

Permalink
Rename test helper functions newXYZ => mustCreateXYZ
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 699966611
Change-Id: Ic800c98fc061ac4738e3c259657df5938839477c
  • Loading branch information
morambro authored and copybara-github committed Nov 25, 2024
1 parent df2de5b commit 8782422
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 56 deletions.
14 changes: 7 additions & 7 deletions aead/aesgcm/aesgcm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func hexDecode(t *testing.T, hexStr string) []byte {
return x
}

func newKey(t *testing.T, keyValue []byte, keyID uint32, opts aesgcm.ParametersOpts) *aesgcm.Key {
func mustCreateKey(t *testing.T, keyValue []byte, keyID uint32, opts aesgcm.ParametersOpts) *aesgcm.Key {
t.Helper()
params, err := aesgcm.NewParameters(opts)
if err != nil {
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestAESGCMAEADWorks(t *testing.T) {
}{
{
name: fmt.Sprintf("AES-%d-TINK", len(key1)*8),
key: newKey(t, key1, 0x223455ab, aesgcm.ParametersOpts{
key: mustCreateKey(t, key1, 0x223455ab, aesgcm.ParametersOpts{
KeySizeInBytes: len(key1),
IVSizeInBytes: 12,
TagSizeInBytes: 16,
Expand All @@ -221,7 +221,7 @@ func TestAESGCMAEADWorks(t *testing.T) {
},
{
name: fmt.Sprintf("AES-%d-CRUNCHY", len(key1)*8),
key: newKey(t, key1, 0x223455ab, aesgcm.ParametersOpts{
key: mustCreateKey(t, key1, 0x223455ab, aesgcm.ParametersOpts{
KeySizeInBytes: len(key1),
IVSizeInBytes: 12,
TagSizeInBytes: 16,
Expand All @@ -232,7 +232,7 @@ func TestAESGCMAEADWorks(t *testing.T) {
},
{
name: fmt.Sprintf("AES-%d-RAW", len(key1)*8),
key: newKey(t, key1, 0, aesgcm.ParametersOpts{
key: mustCreateKey(t, key1, 0, aesgcm.ParametersOpts{
KeySizeInBytes: len(key1),
IVSizeInBytes: 12,
TagSizeInBytes: 16,
Expand All @@ -243,7 +243,7 @@ func TestAESGCMAEADWorks(t *testing.T) {
},
{
name: fmt.Sprintf("AES-%d-TINK", len(key2)*8),
key: newKey(t, key2, 0x223455ab, aesgcm.ParametersOpts{
key: mustCreateKey(t, key2, 0x223455ab, aesgcm.ParametersOpts{
KeySizeInBytes: len(key2),
IVSizeInBytes: 12,
TagSizeInBytes: 16,
Expand All @@ -254,7 +254,7 @@ func TestAESGCMAEADWorks(t *testing.T) {
},
{
name: fmt.Sprintf("AES-%d-CRUNCHY", len(key2)*8),
key: newKey(t, key2, 0x223455ab, aesgcm.ParametersOpts{
key: mustCreateKey(t, key2, 0x223455ab, aesgcm.ParametersOpts{
KeySizeInBytes: len(key2),
IVSizeInBytes: 12,
TagSizeInBytes: 16,
Expand All @@ -265,7 +265,7 @@ func TestAESGCMAEADWorks(t *testing.T) {
},
{
name: fmt.Sprintf("AES-%d-RAW", len(key2)*8),
key: newKey(t, key2, 0, aesgcm.ParametersOpts{
key: mustCreateKey(t, key2, 0, aesgcm.ParametersOpts{
KeySizeInBytes: len(key2),
IVSizeInBytes: 12,
TagSizeInBytes: 16,
Expand Down
26 changes: 13 additions & 13 deletions aead/aesgcm/primitive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type testCase struct {
opts ParametersOpts
}

func newKey(t *testing.T, keyData []byte, opts ParametersOpts) *Key {
func mustCreateKey(t *testing.T, keyData []byte, opts ParametersOpts) *Key {
t.Helper()
params, err := NewParameters(opts)
if err != nil {
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestNewAEADFailures(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
key := newKey(t, random.GetRandomBytes(uint32(tc.opts.KeySizeInBytes)), tc.opts)
key := mustCreateKey(t, random.GetRandomBytes(uint32(tc.opts.KeySizeInBytes)), tc.opts)
if _, err := NewAEAD(key); err == nil {
t.Errorf("NewAEAD(%v) err = nil, want error", key)
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func TestAEAD(t *testing.T) {
} {
t.Run(tc.name, func(t *testing.T) {
keyValue := random.GetRandomBytes(uint32(tc.opts.KeySizeInBytes))
key := newKey(t, keyValue, tc.opts)
key := mustCreateKey(t, keyValue, tc.opts)
aead, err := NewAEAD(key)
if err != nil {
t.Fatalf("NewAEAD(%v) err = %v, want nil", key, err)
Expand Down Expand Up @@ -246,7 +246,7 @@ func TestAEADDecryptFailsIfCiphertextIsCorruptedOrTruncated(t *testing.T) {
ad := random.GetRandomBytes(33)
key := random.GetRandomBytes(16)
pt := random.GetRandomBytes(32)
a, err := NewAEAD(newKey(t, key, ParametersOpts{KeySizeInBytes: 16, IVSizeInBytes: 12, TagSizeInBytes: 16, Variant: VariantTink}))
a, err := NewAEAD(mustCreateKey(t, key, ParametersOpts{KeySizeInBytes: 16, IVSizeInBytes: 12, TagSizeInBytes: 16, Variant: VariantTink}))
if err != nil {
t.Fatalf("NewAEAD() err = %q, want nil", err)
}
Expand Down Expand Up @@ -305,7 +305,7 @@ func TestAEADEncryptUsesRandomNonce(t *testing.T) {
TagSizeInBytes: 16,
Variant: VariantTink,
}
key := newKey(t, keyValue, opts)
key := mustCreateKey(t, keyValue, opts)
a, err := NewAEAD(key)
if err != nil {
t.Fatalf("NewAEAD() err = %q, want nil", err)
Expand Down Expand Up @@ -346,7 +346,7 @@ func TestAEADWycheproofCases(t *testing.T) {
combinedCt = append(combinedCt, tc.Iv...)
combinedCt = append(combinedCt, tc.Ct...)
combinedCt = append(combinedCt, tc.Tag...)
key := newKey(t, tc.Key, ParametersOpts{
key := mustCreateKey(t, tc.Key, ParametersOpts{
KeySizeInBytes: len(tc.Key),
IVSizeInBytes: len(tc.Iv),
TagSizeInBytes: len(tc.Tag),
Expand Down Expand Up @@ -398,7 +398,7 @@ func TestPrimitiveCreator(t *testing.T) {
}{
{
name: fmt.Sprintf("%d-bit key, Tink Variant", len(key1)*8),
key: newKey(t, key1, ParametersOpts{
key: mustCreateKey(t, key1, ParametersOpts{
KeySizeInBytes: len(key1),
IVSizeInBytes: 12,
TagSizeInBytes: 16,
Expand All @@ -409,7 +409,7 @@ func TestPrimitiveCreator(t *testing.T) {
},
{
name: fmt.Sprintf("%d-bit key, Crunchy Variant", len(key1)*8),
key: newKey(t, key1, ParametersOpts{
key: mustCreateKey(t, key1, ParametersOpts{
KeySizeInBytes: len(key1),
IVSizeInBytes: 12,
TagSizeInBytes: 16,
Expand All @@ -420,7 +420,7 @@ func TestPrimitiveCreator(t *testing.T) {
},
{
name: fmt.Sprintf("%d-bit key, No Prefix Variant", len(key1)*8),
key: newKey(t, key1, ParametersOpts{
key: mustCreateKey(t, key1, ParametersOpts{
KeySizeInBytes: len(key1),
IVSizeInBytes: 12,
TagSizeInBytes: 16,
Expand All @@ -431,7 +431,7 @@ func TestPrimitiveCreator(t *testing.T) {
},
{
name: fmt.Sprintf("%d-bit key, Tink Variant", len(key2)*8),
key: newKey(t, key2, ParametersOpts{
key: mustCreateKey(t, key2, ParametersOpts{
KeySizeInBytes: len(key2),
IVSizeInBytes: 12,
TagSizeInBytes: 16,
Expand All @@ -442,7 +442,7 @@ func TestPrimitiveCreator(t *testing.T) {
},
{
name: fmt.Sprintf("%d-bit key, Crunchy Variant", len(key2)*8),
key: newKey(t, key2, ParametersOpts{
key: mustCreateKey(t, key2, ParametersOpts{
KeySizeInBytes: len(key2),
IVSizeInBytes: 12,
TagSizeInBytes: 16,
Expand All @@ -453,7 +453,7 @@ func TestPrimitiveCreator(t *testing.T) {
},
{
name: fmt.Sprintf("%d-bit key, No Prefix Variant", len(key2)*8),
key: newKey(t, key2, ParametersOpts{
key: mustCreateKey(t, key2, ParametersOpts{
KeySizeInBytes: len(key2),
IVSizeInBytes: 12,
TagSizeInBytes: 16,
Expand Down Expand Up @@ -500,7 +500,7 @@ func TestPrimitiveCreatorInvalidParameters(t *testing.T) {
Variant: variant,
}
keyData := random.GetRandomBytes(keySize)
key := newKey(t, keyData, opts)
key := mustCreateKey(t, keyData, opts)
if _, err := primitiveConstructor(key); err == nil {
t.Errorf("primitiveConstructor(key) err = nil, want error")
}
Expand Down
28 changes: 14 additions & 14 deletions aead/aesgcm/protoserialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestParseKeyFails(t *testing.T) {
}
}

func newKeySerialization(t *testing.T, keyData *tinkpb.KeyData, outputPrefixType tinkpb.OutputPrefixType, idRequirement uint32) *protoserialization.KeySerialization {
func mustCreateKeySerialization(t *testing.T, keyData *tinkpb.KeyData, outputPrefixType tinkpb.OutputPrefixType, idRequirement uint32) *protoserialization.KeySerialization {
t.Helper()
ks, err := protoserialization.NewKeySerialization(keyData, outputPrefixType, idRequirement)
if err != nil {
Expand All @@ -165,7 +165,7 @@ func TestParseKey(t *testing.T) {
}{
{
name: "key with TINK output prefix type",
keySerialization: newKeySerialization(t, &tinkpb.KeyData{
keySerialization: mustCreateKeySerialization(t, &tinkpb.KeyData{
TypeUrl: "type.googleapis.com/google.crypto.tink.AesGcmKey",
Value: serializedKey,
KeyMaterialType: tinkpb.KeyData_SYMMETRIC,
Expand All @@ -174,7 +174,7 @@ func TestParseKey(t *testing.T) {
},
{
name: "key with CRUNCHY output prefix type",
keySerialization: newKeySerialization(t, &tinkpb.KeyData{
keySerialization: mustCreateKeySerialization(t, &tinkpb.KeyData{
TypeUrl: "type.googleapis.com/google.crypto.tink.AesGcmKey",
Value: serializedKey,
KeyMaterialType: tinkpb.KeyData_SYMMETRIC,
Expand All @@ -183,7 +183,7 @@ func TestParseKey(t *testing.T) {
},
{
name: "key with RAW output prefix type",
keySerialization: newKeySerialization(t, &tinkpb.KeyData{
keySerialization: mustCreateKeySerialization(t, &tinkpb.KeyData{
TypeUrl: "type.googleapis.com/google.crypto.tink.AesGcmKey",
Value: serializedKey,
KeyMaterialType: tinkpb.KeyData_SYMMETRIC,
Expand Down Expand Up @@ -293,7 +293,7 @@ func TestSerializeKey(t *testing.T) {
{
name: "key with TINK output prefix type",
variant: VariantTink,
wantKeySerialization: newKeySerialization(t, &tinkpb.KeyData{
wantKeySerialization: mustCreateKeySerialization(t, &tinkpb.KeyData{
TypeUrl: "type.googleapis.com/google.crypto.tink.AesGcmKey",
Value: serializedProtoKey,
KeyMaterialType: tinkpb.KeyData_SYMMETRIC,
Expand All @@ -302,7 +302,7 @@ func TestSerializeKey(t *testing.T) {
{
name: "key with CRUNCHY output prefix type",
variant: VariantCrunchy,
wantKeySerialization: newKeySerialization(t, &tinkpb.KeyData{
wantKeySerialization: mustCreateKeySerialization(t, &tinkpb.KeyData{
TypeUrl: "type.googleapis.com/google.crypto.tink.AesGcmKey",
Value: serializedProtoKey,
KeyMaterialType: tinkpb.KeyData_SYMMETRIC,
Expand All @@ -312,7 +312,7 @@ func TestSerializeKey(t *testing.T) {
// No key ID is set for keys with no prefix.
name: "key with RAW output prefix type",
variant: VariantNoPrefix,
wantKeySerialization: newKeySerialization(t, &tinkpb.KeyData{
wantKeySerialization: mustCreateKeySerialization(t, &tinkpb.KeyData{
TypeUrl: "type.googleapis.com/google.crypto.tink.AesGcmKey",
Value: serializedProtoKey,
KeyMaterialType: tinkpb.KeyData_SYMMETRIC,
Expand Down Expand Up @@ -378,7 +378,7 @@ func TestSerializeParametersFailsWithWrongParameters(t *testing.T) {
}
}

func newKeyTemplate(t *testing.T, outputPrefixType tinkpb.OutputPrefixType, keySizeInBytes uint32) *tinkpb.KeyTemplate {
func mustCreateKeyTemplate(t *testing.T, outputPrefixType tinkpb.OutputPrefixType, keySizeInBytes uint32) *tinkpb.KeyTemplate {
t.Helper()
format := &aesgcmpb.AesGcmKeyFormat{
KeySize: keySizeInBytes,
Expand Down Expand Up @@ -408,7 +408,7 @@ func TestSerializeParameters(t *testing.T) {
tagSizeInBytes: 16,
variant: VariantTink,
},
wantKeyTemplate: newKeyTemplate(t, tinkpb.OutputPrefixType_TINK, 16),
wantKeyTemplate: mustCreateKeyTemplate(t, tinkpb.OutputPrefixType_TINK, 16),
},
{
name: "AES256-GCM TINK output prefix type",
Expand All @@ -418,7 +418,7 @@ func TestSerializeParameters(t *testing.T) {
tagSizeInBytes: 16,
variant: VariantTink,
},
wantKeyTemplate: newKeyTemplate(t, tinkpb.OutputPrefixType_TINK, 32),
wantKeyTemplate: mustCreateKeyTemplate(t, tinkpb.OutputPrefixType_TINK, 32),
},
{
name: "AES128-GCM RAW output prefix type",
Expand All @@ -428,7 +428,7 @@ func TestSerializeParameters(t *testing.T) {
tagSizeInBytes: 16,
variant: VariantNoPrefix,
},
wantKeyTemplate: newKeyTemplate(t, tinkpb.OutputPrefixType_RAW, 16),
wantKeyTemplate: mustCreateKeyTemplate(t, tinkpb.OutputPrefixType_RAW, 16),
},
{
name: "AES256-GCM RAW output prefix type",
Expand All @@ -438,7 +438,7 @@ func TestSerializeParameters(t *testing.T) {
tagSizeInBytes: 16,
variant: VariantNoPrefix,
},
wantKeyTemplate: newKeyTemplate(t, tinkpb.OutputPrefixType_RAW, 32),
wantKeyTemplate: mustCreateKeyTemplate(t, tinkpb.OutputPrefixType_RAW, 32),
},
{
name: "AES128-GCM CRUNCHY output prefix type",
Expand All @@ -448,7 +448,7 @@ func TestSerializeParameters(t *testing.T) {
tagSizeInBytes: 16,
variant: VariantCrunchy,
},
wantKeyTemplate: newKeyTemplate(t, tinkpb.OutputPrefixType_CRUNCHY, 16),
wantKeyTemplate: mustCreateKeyTemplate(t, tinkpb.OutputPrefixType_CRUNCHY, 16),
},
{
name: "AES256-GCM CRUNCHY output prefix type",
Expand All @@ -458,7 +458,7 @@ func TestSerializeParameters(t *testing.T) {
tagSizeInBytes: 16,
variant: VariantCrunchy,
},
wantKeyTemplate: newKeyTemplate(t, tinkpb.OutputPrefixType_CRUNCHY, 32),
wantKeyTemplate: mustCreateKeyTemplate(t, tinkpb.OutputPrefixType_CRUNCHY, 32),
},
} {
t.Run(tc.name, func(t *testing.T) {
Expand Down
Loading

0 comments on commit 8782422

Please sign in to comment.