xref: /aosp_15_r20/external/tink/go/testutil/testutil_test.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1// Copyright 2018 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15////////////////////////////////////////////////////////////////////////////////
16
17package testutil_test
18
19import (
20	"bytes"
21	"encoding/hex"
22	"testing"
23
24	"github.com/google/tink/go/subtle/random"
25	"github.com/google/tink/go/testutil"
26	"github.com/google/tink/go/tink"
27)
28
29func TestDummyAEAD(t *testing.T) {
30	// Assert that DummyAEAD implements the AEAD interface.
31	var _ tink.AEAD = (*testutil.DummyAEAD)(nil)
32
33	// try to encrypt/decrypt some data
34	data := []byte{0, 1, 1, 2, 3, 5}
35	associatedData := []byte{3, 1, 4, 1, 5}
36
37	dummy := &testutil.DummyAEAD{Name: "name"}
38	cipher, err := dummy.Encrypt(data, associatedData)
39	if err != nil {
40		t.Fatalf("DummyAEAD.Encrypt(%+v, %+v) gave error: %v", data, associatedData, err)
41	}
42	decrypt, err := dummy.Decrypt(cipher, associatedData)
43	if err != nil {
44		t.Fatalf("DummyAEAD.Decrypt(ciphertext, %+v) gave errr: %v", associatedData, err)
45	}
46	if !bytes.Equal(data, decrypt) {
47		t.Errorf("DummyAEAD round-tripped data %+v back to %+v", data, decrypt)
48	}
49}
50
51func TestDummySigner(t *testing.T) {
52	var _ tink.Signer = testutil.NewDummySigner("name")
53}
54
55func TestDummyVerifier(t *testing.T) {
56	var _ tink.Verifier = testutil.NewDummyVerifier("name")
57}
58
59func TestDummySignerVerifier(t *testing.T) {
60	signer := testutil.NewDummySigner("")
61	verifier := testutil.NewDummyVerifier("")
62
63	data := []byte{2, 7, 1, 8, 2, 8}
64	if err := verifier.Verify(nil, data); err == nil {
65		t.Errorf("DummyVerifier.Verify(invalid signature, %+v) succeeded; want error", data)
66	}
67
68	sig, err := signer.Sign(data)
69	if err != nil {
70		t.Fatalf("DummySigner.Sign(%+v) gave error: %v", data, err)
71	}
72	if err := verifier.Verify(sig, data); err != nil {
73		t.Errorf("DummyVerifier.Verify(valid signature, %+v) gave error: %v", data, err)
74	}
75}
76
77func TestDummyMAC(t *testing.T) {
78	// Assert that DummyMAC implements the MAC interface.
79	var _ tink.MAC = (*testutil.DummyMAC)(nil)
80	// try to compute mac
81	data := []byte{1, 2, 3, 4, 5}
82	dummyMAC := &testutil.DummyMAC{Name: "Mac12347"}
83	digest, err := dummyMAC.ComputeMAC(data)
84	if err != nil {
85		t.Errorf("unexpected error: %s", err)
86	}
87	if !bytes.Equal(append(data, dummyMAC.Name...), digest) {
88		t.Errorf("incorrect digest")
89	}
90	if err := dummyMAC.VerifyMAC(nil, nil); err != nil {
91		t.Errorf("unexpected result of VerifyMAC")
92	}
93}
94
95func fillByteArray(b byte, length int) []byte {
96	result := []byte{}
97	for i := 0; i < length; i++ {
98		result = append(result, b)
99	}
100	return result
101}
102
103func TestUniformString(t *testing.T) {
104	if err := testutil.ZTestUniformString(fillByteArray(0xaa, 32)); err != nil {
105		t.Errorf("Expected repeated 0xaa string to pass: %v", err)
106	}
107	if err := testutil.ZTestUniformString(fillByteArray(0x00, 32)); err == nil {
108		t.Errorf("Expected to fail uniform distribution test for 32 zero bytes")
109	}
110	if err := testutil.ZTestUniformString(random.GetRandomBytes(32)); err != nil {
111		t.Errorf("Expected random string to pass randomness test: %v", err)
112	}
113}
114
115func TestCrossCorrelationUniformString(t *testing.T) {
116	if err := testutil.ZTestCrosscorrelationUniformStrings(fillByteArray(0xaa, 32),
117		fillByteArray(0x99, 32)); err != nil {
118		t.Errorf("Expected 0xaa and 0x99 repeated 32 times each to have no cross correlation: %v", err)
119	}
120	if err := testutil.ZTestCrosscorrelationUniformStrings(fillByteArray(0xaa, 32),
121		fillByteArray(0xaa, 32)); err == nil {
122		t.Errorf("Expected 0xaa repeated 32 times to be cross correlated with itself")
123	}
124	if err := testutil.ZTestCrosscorrelationUniformStrings(random.GetRandomBytes(32),
125		random.GetRandomBytes(32)); err != nil {
126		t.Errorf("Expected random 32 byte strings to not be crosscorrelated: %v", err)
127	}
128}
129
130func TestAutocorrelationUniformString(t *testing.T) {
131	if err := testutil.ZTestAutocorrelationUniformString(fillByteArray(0xaa, 32)); err == nil {
132		t.Errorf("Expected repeated string to show autocorrelation")
133	}
134	if err := testutil.ZTestAutocorrelationUniformString([]byte(
135		"This is a text that is only ascii characters and therefore " +
136			"not random. It needs quite a few characters before it has " +
137			"enough to find a pattern, though, as it is text.")); err == nil {
138		t.Errorf("Expected longish English ASCII test to be autocorrelated")
139	}
140	if err := testutil.ZTestAutocorrelationUniformString(random.GetRandomBytes(32)); err != nil {
141		t.Errorf("Expected random 32 byte string to show not autocorrelation: %v", err)
142	}
143}
144
145func TestGenerateMutations(t *testing.T) {
146	original := random.GetRandomBytes(8)
147	mutations := testutil.GenerateMutations(original)
148	seen := make(map[string]bool)
149	for i, mutation := range mutations {
150		if bytes.Compare(original, mutation) == 0 {
151			t.Errorf("Expected mutation %x to differ from original %x", mutation, original)
152		}
153		mutationHex := hex.EncodeToString(mutation)
154		if seen[mutationHex] {
155			t.Errorf("Mutation %d (%s) matches an earlier mutation", i, mutationHex)
156		}
157		seen[mutationHex] = true
158	}
159}
160