File: main.go

package info (click to toggle)
golang-google-protobuf 1.36.7-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 14,996 kB
  • sloc: sh: 94; makefile: 4
file content (137 lines) | stat: -rw-r--r-- 4,948 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Program generate-corpus generates a seed corpus for the fuzzers.
//
// This command is not run automatically because its output is not stable.
// It's present in source control mainly as documentation of where the seed
// corpus came from.
package main

import (
	"crypto/sha1"
	"fmt"
	"log"
	"os"

	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/encoding/prototext"
	"google.golang.org/protobuf/proto"

	fuzzpb "google.golang.org/protobuf/internal/testprotos/fuzz"
	testpb "google.golang.org/protobuf/internal/testprotos/test"
)

var messages = []proto.Message{
	&fuzzpb.Fuzz{
		TestAllTypes: &testpb.TestAllTypes{
			OptionalInt32:      proto.Int32(1001),
			OptionalInt64:      proto.Int64(1002),
			OptionalUint32:     proto.Uint32(1003),
			OptionalUint64:     proto.Uint64(1004),
			OptionalSint32:     proto.Int32(1005),
			OptionalSint64:     proto.Int64(1006),
			OptionalFixed32:    proto.Uint32(1007),
			OptionalFixed64:    proto.Uint64(1008),
			OptionalSfixed32:   proto.Int32(1009),
			OptionalSfixed64:   proto.Int64(1010),
			OptionalFloat:      proto.Float32(1011.5),
			OptionalDouble:     proto.Float64(1012.5),
			OptionalBool:       proto.Bool(true),
			OptionalString:     proto.String("string"),
			OptionalBytes:      []byte("bytes"),
			OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
			Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
				A: proto.Int32(1017),
			},
			OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
				A: proto.Int32(42),
				Corecursive: &testpb.TestAllTypes{
					OptionalInt32: proto.Int32(43),
				},
			},
			RepeatedInt32:    []int32{1001, 2001},
			RepeatedInt64:    []int64{1002, 2002},
			RepeatedUint32:   []uint32{1003, 2003},
			RepeatedUint64:   []uint64{1004, 2004},
			RepeatedSint32:   []int32{1005, 2005},
			RepeatedSint64:   []int64{1006, 2006},
			RepeatedFixed32:  []uint32{1007, 2007},
			RepeatedFixed64:  []uint64{1008, 2008},
			RepeatedSfixed32: []int32{1009, 2009},
			RepeatedSfixed64: []int64{1010, 2010},
			RepeatedFloat:    []float32{1011.5, 2011.5},
			RepeatedDouble:   []float64{1012.5, 2012.5},
			RepeatedBool:     []bool{true, false},
			RepeatedString:   []string{"foo", "bar"},
			RepeatedBytes:    [][]byte{[]byte("FOO"), []byte("BAR")},
			RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
				testpb.TestAllTypes_FOO,
				testpb.TestAllTypes_BAR,
			},
			RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
				{A: proto.Int32(1)},
				nil,
				{A: proto.Int32(2)},
			},
			Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
				{A: proto.Int32(1017)},
				nil,
				{A: proto.Int32(2017)},
			},
			MapInt32Int32:       map[int32]int32{1056: 1156, 2056: 2156},
			MapInt64Int64:       map[int64]int64{1057: 1157, 2057: 2157},
			MapUint32Uint32:     map[uint32]uint32{1058: 1158, 2058: 2158},
			MapUint64Uint64:     map[uint64]uint64{1059: 1159, 2059: 2159},
			MapSint32Sint32:     map[int32]int32{1060: 1160, 2060: 2160},
			MapSint64Sint64:     map[int64]int64{1061: 1161, 2061: 2161},
			MapFixed32Fixed32:   map[uint32]uint32{1062: 1162, 2062: 2162},
			MapFixed64Fixed64:   map[uint64]uint64{1063: 1163, 2063: 2163},
			MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
			MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
			MapInt32Float:       map[int32]float32{1066: 1166.5, 2066: 2166.5},
			MapInt32Double:      map[int32]float64{1067: 1167.5, 2067: 2167.5},
			MapBoolBool:         map[bool]bool{true: false, false: true},
			MapStringString:     map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
			MapStringBytes:      map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
			MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
				"71.1.key": {A: proto.Int32(1171)},
				"71.2.key": {A: proto.Int32(2171)},
			},
			MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
				"73.1.key": testpb.TestAllTypes_FOO,
				"73.2.key": testpb.TestAllTypes_BAR,
			},
			OneofField: &testpb.TestAllTypes_OneofUint32{1111},
		},
	},
}

func main() {
	for _, m := range messages {
		wire, err := proto.Marshal(m)
		if err != nil {
			log.Fatal(err)
		}
		if err := os.WriteFile(fmt.Sprintf("internal/fuzz/wirefuzz/corpus/%x", sha1.Sum(wire)), wire, 0777); err != nil {
			log.Fatal(err)
		}

		text, err := prototext.Marshal(m)
		if err != nil {
			log.Fatal(err)
		}
		if err := os.WriteFile(fmt.Sprintf("internal/fuzz/textfuzz/corpus/%x", sha1.Sum(text)), text, 0777); err != nil {
			log.Fatal(err)
		}

		json, err := protojson.Marshal(m)
		if err != nil {
			log.Fatal(err)
		}
		if err := os.WriteFile(fmt.Sprintf("internal/fuzz/jsonfuzz/corpus/%x", sha1.Sum(json)), json, 0777); err != nil {
			log.Fatal(err)
		}
	}
}