1// Copyright 2021 The BoringSSL Authors
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//     https://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//go:build ignore
16
17// trimvectors takes an ACVP vector set file and discards all but a single test
18// from each test group, and also discards any test that serializes to more than
19// 4096 bytes. This hope is that this achieves good coverage without having to
20// check in megabytes worth of JSON files.
21package main
22
23import (
24	"bytes"
25	"cmp"
26	"encoding/json"
27	"os"
28	"slices"
29)
30
31func main() {
32	var vectorSets []any
33	decoder := json.NewDecoder(os.Stdin)
34	if err := decoder.Decode(&vectorSets); err != nil {
35		panic(err)
36	}
37
38	// The first element is the metadata which is left unmodified.
39	for i := 1; i < len(vectorSets); i++ {
40		vectorSet := vectorSets[i].(map[string]any)
41		testGroups := vectorSet["testGroups"].([]any)
42		for _, testGroupInterface := range testGroups {
43			testGroup := testGroupInterface.(map[string]any)
44			tests := testGroup["tests"].([]any)
45
46			// Take only the smallest test.
47			type testAndSize struct {
48				test any
49				size int
50			}
51			var testsAndSizes []testAndSize
52
53			for _, test := range tests {
54				var b bytes.Buffer
55				encoder := json.NewEncoder(&b)
56				if err := encoder.Encode(test); err != nil {
57					panic(err)
58				}
59				testsAndSizes = append(testsAndSizes, testAndSize{test, b.Len()})
60			}
61
62			slices.SortFunc(testsAndSizes, func(a, b testAndSize) int {
63				return cmp.Compare(a.size, b.size)
64			})
65			testGroup["tests"] = []any{testsAndSizes[0].test}
66		}
67	}
68
69	encoder := json.NewEncoder(os.Stdout)
70	encoder.SetIndent("", "  ")
71	if err := encoder.Encode(vectorSets); err != nil {
72		panic(err)
73	}
74}
75