xref: /aosp_15_r20/external/golang-protobuf/internal/cmd/generate-corpus/main.go (revision 1c12ee1efe575feb122dbf939ff15148a3b3e8f2)
1// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Program generate-corpus generates a seed corpus for the fuzzers.
6//
7// This command is not run automatically because its output is not stable.
8// It's present in source control mainly as documentation of where the seed
9// corpus came from.
10package main
11
12import (
13	"crypto/sha1"
14	"fmt"
15	"io/ioutil"
16	"log"
17
18	"google.golang.org/protobuf/encoding/protojson"
19	"google.golang.org/protobuf/encoding/prototext"
20	"google.golang.org/protobuf/proto"
21
22	fuzzpb "google.golang.org/protobuf/internal/testprotos/fuzz"
23	testpb "google.golang.org/protobuf/internal/testprotos/test"
24)
25
26var messages = []proto.Message{
27	&fuzzpb.Fuzz{
28		TestAllTypes: &testpb.TestAllTypes{
29			OptionalInt32:      proto.Int32(1001),
30			OptionalInt64:      proto.Int64(1002),
31			OptionalUint32:     proto.Uint32(1003),
32			OptionalUint64:     proto.Uint64(1004),
33			OptionalSint32:     proto.Int32(1005),
34			OptionalSint64:     proto.Int64(1006),
35			OptionalFixed32:    proto.Uint32(1007),
36			OptionalFixed64:    proto.Uint64(1008),
37			OptionalSfixed32:   proto.Int32(1009),
38			OptionalSfixed64:   proto.Int64(1010),
39			OptionalFloat:      proto.Float32(1011.5),
40			OptionalDouble:     proto.Float64(1012.5),
41			OptionalBool:       proto.Bool(true),
42			OptionalString:     proto.String("string"),
43			OptionalBytes:      []byte("bytes"),
44			OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
45			Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
46				A: proto.Int32(1017),
47			},
48			OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
49				A: proto.Int32(42),
50				Corecursive: &testpb.TestAllTypes{
51					OptionalInt32: proto.Int32(43),
52				},
53			},
54			RepeatedInt32:    []int32{1001, 2001},
55			RepeatedInt64:    []int64{1002, 2002},
56			RepeatedUint32:   []uint32{1003, 2003},
57			RepeatedUint64:   []uint64{1004, 2004},
58			RepeatedSint32:   []int32{1005, 2005},
59			RepeatedSint64:   []int64{1006, 2006},
60			RepeatedFixed32:  []uint32{1007, 2007},
61			RepeatedFixed64:  []uint64{1008, 2008},
62			RepeatedSfixed32: []int32{1009, 2009},
63			RepeatedSfixed64: []int64{1010, 2010},
64			RepeatedFloat:    []float32{1011.5, 2011.5},
65			RepeatedDouble:   []float64{1012.5, 2012.5},
66			RepeatedBool:     []bool{true, false},
67			RepeatedString:   []string{"foo", "bar"},
68			RepeatedBytes:    [][]byte{[]byte("FOO"), []byte("BAR")},
69			RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
70				testpb.TestAllTypes_FOO,
71				testpb.TestAllTypes_BAR,
72			},
73			RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
74				{A: proto.Int32(1)},
75				nil,
76				{A: proto.Int32(2)},
77			},
78			Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
79				{A: proto.Int32(1017)},
80				nil,
81				{A: proto.Int32(2017)},
82			},
83			MapInt32Int32:       map[int32]int32{1056: 1156, 2056: 2156},
84			MapInt64Int64:       map[int64]int64{1057: 1157, 2057: 2157},
85			MapUint32Uint32:     map[uint32]uint32{1058: 1158, 2058: 2158},
86			MapUint64Uint64:     map[uint64]uint64{1059: 1159, 2059: 2159},
87			MapSint32Sint32:     map[int32]int32{1060: 1160, 2060: 2160},
88			MapSint64Sint64:     map[int64]int64{1061: 1161, 2061: 2161},
89			MapFixed32Fixed32:   map[uint32]uint32{1062: 1162, 2062: 2162},
90			MapFixed64Fixed64:   map[uint64]uint64{1063: 1163, 2063: 2163},
91			MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
92			MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
93			MapInt32Float:       map[int32]float32{1066: 1166.5, 2066: 2166.5},
94			MapInt32Double:      map[int32]float64{1067: 1167.5, 2067: 2167.5},
95			MapBoolBool:         map[bool]bool{true: false, false: true},
96			MapStringString:     map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
97			MapStringBytes:      map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
98			MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
99				"71.1.key": {A: proto.Int32(1171)},
100				"71.2.key": {A: proto.Int32(2171)},
101			},
102			MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
103				"73.1.key": testpb.TestAllTypes_FOO,
104				"73.2.key": testpb.TestAllTypes_BAR,
105			},
106			OneofField: &testpb.TestAllTypes_OneofUint32{1111},
107		},
108	},
109}
110
111func main() {
112	for _, m := range messages {
113		wire, err := proto.Marshal(m)
114		if err != nil {
115			log.Fatal(err)
116		}
117		if err := ioutil.WriteFile(fmt.Sprintf("internal/fuzz/wirefuzz/corpus/%x", sha1.Sum(wire)), wire, 0777); err != nil {
118			log.Fatal(err)
119		}
120
121		text, err := prototext.Marshal(m)
122		if err != nil {
123			log.Fatal(err)
124		}
125		if err := ioutil.WriteFile(fmt.Sprintf("internal/fuzz/textfuzz/corpus/%x", sha1.Sum(text)), text, 0777); err != nil {
126			log.Fatal(err)
127		}
128
129		json, err := protojson.Marshal(m)
130		if err != nil {
131			log.Fatal(err)
132		}
133		if err := ioutil.WriteFile(fmt.Sprintf("internal/fuzz/jsonfuzz/corpus/%x", sha1.Sum(json)), json, 0777); err != nil {
134			log.Fatal(err)
135		}
136	}
137}
138