forked from ooni/probe-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_session.go
180 lines (169 loc) · 4.76 KB
/
generate_session.go
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// +build ignore
package main
import (
"log"
"os"
"text/template"
)
var sessionTemplate = template.Must(template.New("code").Parse(`
// Maybe{{ .Name }} is like {{ .Name }} except that we return the real
// value only if the privacy settings allows us. Otherwise, we just
// return the default value, model.Default{{ .Name }}.
func (s *Session) Maybe{{ .Name }}() {{ .Type }} {
out := model.Default{{ .Name }}
if s.privacySettings.{{ .Setting }} {
out = s.{{ .Name }}()
}
return out
}`))
var sessionTestTemplate = template.Must(template.New("test").Parse(`
func TestSession{{ .Name }}WorksAsIntended(t *testing.T) {
sess := &Session{location: &model.LocationInfo{
{{ .LocationInfoName }}: {{ .LocationInfoValue }},
}}
t.Run("with false setting", func(t *testing.T) {
sess.privacySettings.{{ .Setting }} = false
out := sess.Maybe{{ .Name }}()
if out != model.Default{{ .Name }} {
t.Fatal("not the value we expected")
}
})
t.Run("with true setting", func(t *testing.T) {
sess.privacySettings.{{ .Setting }} = true
out := sess.Maybe{{ .Name }}()
if out != {{ .ValueForTesting }} {
t.Fatal("not the value we expected")
}
})
}`))
type Variable struct {
LocationInfoName string
LocationInfoValue string
Name string
Setting string
Type string
ValueForTesting string
}
var Variables = []Variable{{
LocationInfoName: "ProbeIP",
LocationInfoValue: `"8.8.8.8"`,
Name: "ProbeIP",
Setting: "IncludeIP",
Type: "string",
ValueForTesting: `"8.8.8.8"`,
}, {
LocationInfoName: "ASN",
LocationInfoValue: `30722`,
Name: "ProbeASN",
Setting: "IncludeASN",
Type: "uint",
ValueForTesting: `30722`,
}, {
LocationInfoName: "ASN",
LocationInfoValue: `30722`,
Name: "ProbeASNString",
Setting: "IncludeASN",
Type: "string",
ValueForTesting: `"AS30722"`,
}, {
LocationInfoName: "CountryCode",
LocationInfoValue: `"IT"`,
Name: "ProbeCC",
Setting: "IncludeCountry",
Type: "string",
ValueForTesting: `"IT"`,
}, {
LocationInfoName: "NetworkName",
LocationInfoValue: `"Vodafone Italia"`,
Name: "ProbeNetworkName",
Setting: "IncludeASN",
Type: "string",
ValueForTesting: `"Vodafone Italia"`,
}, {
LocationInfoName: "ResolverIP",
LocationInfoValue: `"9.9.9.9"`,
Name: "ResolverIP",
Setting: "IncludeIP",
Type: "string",
ValueForTesting: `"9.9.9.9"`,
}, {
LocationInfoName: "ResolverASN",
LocationInfoValue: `44`,
Name: "ResolverASN",
Setting: "IncludeASN",
Type: "uint",
ValueForTesting: `44`,
}, {
LocationInfoName: "ResolverASN",
LocationInfoValue: `44`,
Name: "ResolverASNString",
Setting: "IncludeASN",
Type: "string",
ValueForTesting: `"AS44"`,
}, {
LocationInfoName: "ResolverNetworkName",
LocationInfoValue: `"Google LLC"`,
Name: "ResolverNetworkName",
Setting: "IncludeASN",
Type: "string",
ValueForTesting: `"Google LLC"`,
}}
func writestring(fp *os.File, s string) {
if _, err := fp.Write([]byte(s)); err != nil {
log.Fatal(err)
}
}
func withFile(filepath string, do func(fp *os.File)) {
fp, err := os.Create(filepath)
if err != nil {
log.Fatal(err)
}
do(fp)
if err := fp.Close(); err != nil {
log.Fatal(err)
}
}
func writeSessionGeneratedGo() {
withFile("session_generated.go", func(fp *os.File) {
writestring(fp, "// Code generated by go generate; DO NOT EDIT.\n")
writestring(fp, "\n")
writestring(fp, "package engine\n")
writestring(fp, "\n")
writestring(fp, "import \"github.com/ooni/probe-engine/model\"\n")
writestring(fp, "\n")
writestring(fp, "//go:generate go run generate_session.go")
writestring(fp, "\n")
for _, variable := range Variables {
if err := sessionTemplate.Execute(fp, variable); err != nil {
log.Fatal(err)
}
writestring(fp, "\n")
}
})
}
func writeSessionGeneratedTestGo() {
withFile("session_generated_test.go", func(fp *os.File) {
writestring(fp, "// Code generated by go generate; DO NOT EDIT.\n")
writestring(fp, "\n")
writestring(fp, "package engine\n")
writestring(fp, "\n")
writestring(fp, "import (\n")
writestring(fp, "\t\"testing\"\n")
writestring(fp, "\n")
writestring(fp, "\t\"github.com/ooni/probe-engine/model\"\n")
writestring(fp, ")\n")
writestring(fp, "\n")
writestring(fp, "//go:generate go run generate_session.go")
writestring(fp, "\n")
for _, variable := range Variables {
if err := sessionTestTemplate.Execute(fp, variable); err != nil {
log.Fatal(err)
}
writestring(fp, "\n")
}
})
}
func main() {
writeSessionGeneratedGo()
writeSessionGeneratedTestGo()
}