xref: /aosp_15_r20/external/tink/go/testutil/wycheproofutil_test.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1// Copyright 2019 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	"encoding/json"
21	"os"
22	"testing"
23
24	"github.com/google/tink/go/testutil"
25)
26
27func TestPopulateSuite(t *testing.T) {
28	testutil.SkipTestIfTestSrcDirIsNotSet(t)
29
30	// TODO(175520475): Test the HexBytes type.
31	type AeadTest struct {
32		testutil.WycheproofCase
33		Key        string `json:"key"`
34		IV         string `json:"iv"`
35		AAD        string `json:"aad"`
36		Message    string `json:"msg"`
37		Ciphertext string `json:"ct"`
38		Tag        string `json:"tag"`
39	}
40
41	type AeadGroup struct {
42		testutil.WycheproofGroup
43		Tests []*AeadTest `json:"tests"`
44	}
45
46	type AeadSuite struct {
47		testutil.WycheproofSuite
48		TestGroups []*AeadGroup `json:"testGroups"`
49	}
50
51	suite := new(AeadSuite)
52	if err := testutil.PopulateSuite(suite, "aes_gcm_test.json"); err != nil {
53		t.Fatalf("error populating suite: %s", err)
54	}
55
56	if suite.Algorithm != "AES-GCM" {
57		t.Errorf("suite.Algorithm=%s, want AES-GCM", suite.Algorithm)
58	}
59
60	if suite.TestGroups[0].Tests[0].Key == "" {
61		t.Error("suite.TestGroups[0].Tests[0].Key is empty")
62	}
63}
64
65func TestPopulateSuite_FileOpenError(t *testing.T) {
66	testutil.SkipTestIfTestSrcDirIsNotSet(t)
67
68	suite := new(testutil.WycheproofSuite)
69	err := testutil.PopulateSuite(suite, "NON_EXISTENT_FILE")
70	if err == nil {
71		t.Error("succeeded with non-existent file")
72	}
73	if _, ok := err.(*os.PathError); !ok {
74		t.Errorf("unexpected error for non-existent file: %s", err)
75	}
76}
77
78func TestPopulateSuite_DecodeError(t *testing.T) {
79	testutil.SkipTestIfTestSrcDirIsNotSet(t)
80
81	var suite *testutil.WycheproofSuite
82	err := testutil.PopulateSuite(suite, "aes_gcm_test.json")
83	if err == nil {
84		t.Error("succeeded with nil suite")
85	}
86	if _, ok := err.(*json.InvalidUnmarshalError); !ok {
87		t.Errorf("unexpected error for decode error: %s", err)
88	}
89}
90