Skip to content

Commit 4f8077b

Browse files
go: golint/go vet fixes (google#1370)
1 parent 90f996e commit 4f8077b

File tree

6 files changed

+105
-55
lines changed

6 files changed

+105
-55
lines changed

go/client/logclient_test.go

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,15 @@ func TestGetSTH(t *testing.T) {
193193
}
194194

195195
func TestAddChainRetries(t *testing.T) {
196-
retryAfter := 0
196+
retryAfter := 0 * time.Second
197197
currentFailures := 0
198198
failuresBeforeSuccess := 0
199199
hs := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
200200
if failuresBeforeSuccess > 0 && currentFailures < failuresBeforeSuccess {
201201
currentFailures++
202202
if retryAfter != 0 {
203203
if retryAfter > 0 {
204-
w.Header().Add("Retry-After", strconv.Itoa(retryAfter))
204+
w.Header().Add("Retry-After", strconv.Itoa(int(retryAfter.Seconds())))
205205
}
206206
w.WriteHeader(503)
207207
return
@@ -228,29 +228,64 @@ func TestAddChainRetries(t *testing.T) {
228228
}
229229
const leeway = time.Millisecond * 100
230230
const leewayRatio = 0.1 // 10%
231-
const instant = time.Millisecond
232-
const fiveSeconds = time.Second * 5
233-
const sevenSeconds = time.Second * 7 // = 1 + 2 + 4
234231

235232
tests := []struct {
236-
deadlineLength int
233+
deadlineLength time.Duration // -1 indicates no deadline
237234
expected time.Duration
238-
retryAfter int // -1 indicates: generate 503 with no Retry-After
235+
retryAfter time.Duration // -1 indicates: generate 503 with no Retry-After
239236
failuresBeforeSuccess int
240237
success bool
241238
}{
242-
{-1, instant, 0, 0, true},
243-
{-1, sevenSeconds, -1, 3, true},
244-
{6, fiveSeconds, 5, 1, true},
245-
{5, fiveSeconds, 10, 1, false},
246-
{10, fiveSeconds, 1, 5, true},
247-
{1, instant * 10, 0, 10, true},
239+
{
240+
deadlineLength: -1,
241+
expected: 1 * time.Millisecond,
242+
retryAfter: 0,
243+
failuresBeforeSuccess: 0,
244+
success: true,
245+
},
246+
{
247+
deadlineLength: -1,
248+
expected: 7 * time.Second, /* 1 + 2 + 4 */
249+
retryAfter: -1,
250+
failuresBeforeSuccess: 3,
251+
success: true,
252+
},
253+
{
254+
deadlineLength: 6 * time.Second,
255+
expected: 5 * time.Second,
256+
retryAfter: 5 * time.Second,
257+
failuresBeforeSuccess: 1,
258+
success: true,
259+
},
260+
{
261+
deadlineLength: 5 * time.Second,
262+
expected: 5 * time.Second,
263+
retryAfter: 10 * time.Second,
264+
failuresBeforeSuccess: 1,
265+
success: false,
266+
},
267+
{
268+
deadlineLength: 10 * time.Second,
269+
expected: 5 * time.Second,
270+
retryAfter: 1 * time.Second,
271+
failuresBeforeSuccess: 5,
272+
success: true,
273+
},
274+
{
275+
deadlineLength: 1 * time.Second,
276+
expected: 10 * time.Millisecond,
277+
retryAfter: 0,
278+
failuresBeforeSuccess: 10,
279+
success: true,
280+
},
248281
}
249282

250283
for i, test := range tests {
251284
deadline := context.Background()
252285
if test.deadlineLength >= 0 {
253-
deadline, _ = context.WithDeadline(context.Background(), time.Now().Add(time.Duration(test.deadlineLength)*time.Second))
286+
var cancel context.CancelFunc
287+
deadline, cancel = context.WithDeadline(context.Background(), time.Now().Add(test.deadlineLength))
288+
defer cancel()
254289
}
255290
retryAfter = test.retryAfter
256291
failuresBeforeSuccess = test.failuresBeforeSuccess

go/jsonclient/client_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ func TestPostAndParseWithRetry(t *testing.T) {
267267
}
268268
ctx := context.Background()
269269
if test.deadlineSecs >= 0 {
270-
ctx, _ = context.WithDeadline(context.Background(), time.Now().Add(time.Duration(test.deadlineSecs)*time.Second))
270+
var cancel context.CancelFunc
271+
ctx, cancel = context.WithDeadline(context.Background(), time.Now().Add(time.Duration(test.deadlineSecs)*time.Second))
272+
defer cancel()
271273
}
272274

273275
var result TestStruct

go/scanner/main/scanner.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import (
1818
)
1919

2020
const (
21-
// A regex which cannot match any input
22-
MatchesNothingRegex = "a^"
21+
// matchesNothingRegex is a regex which cannot match any input.
22+
matchesNothingRegex = "a^"
2323
)
2424

25-
var logUri = flag.String("log_uri", "http://ct.googleapis.com/aviator", "CT log base URI")
25+
var logURI = flag.String("log_uri", "http://ct.googleapis.com/aviator", "CT log base URI")
2626
var matchSubjectRegex = flag.String("match_subject_regex", ".*", "Regex to match CN/SAN")
2727
var matchIssuerRegex = flag.String("match_issuer_regex", "", "Regex to match in issuer CN")
2828
var precertsOnly = flag.Bool("precerts_only", false, "Only match precerts")
@@ -67,7 +67,7 @@ func createRegexes(regexValue string) (*regexp.Regexp, *regexp.Regexp) {
6767
precertRegex := regexp.MustCompile(regexValue)
6868
switch *precertsOnly {
6969
case true:
70-
certRegex = regexp.MustCompile(MatchesNothingRegex)
70+
certRegex = regexp.MustCompile(matchesNothingRegex)
7171
case false:
7272
certRegex = precertRegex
7373
}
@@ -90,17 +90,16 @@ func createMatcherFromFlags() (scanner.Matcher, error) {
9090
return nil, fmt.Errorf("Invalid serialNumber %s", *serialNumber)
9191
}
9292
return scanner.MatchSerialNumber{SerialNumber: sn}, nil
93-
} else {
94-
certRegex, precertRegex := createRegexes(*matchSubjectRegex)
95-
return scanner.MatchSubjectRegex{
96-
CertificateSubjectRegex: certRegex,
97-
PrecertificateSubjectRegex: precertRegex}, nil
9893
}
94+
certRegex, precertRegex := createRegexes(*matchSubjectRegex)
95+
return scanner.MatchSubjectRegex{
96+
CertificateSubjectRegex: certRegex,
97+
PrecertificateSubjectRegex: precertRegex}, nil
9998
}
10099

101100
func main() {
102101
flag.Parse()
103-
logClient, err := client.New(*logUri, &http.Client{
102+
logClient, err := client.New(*logURI, &http.Client{
104103
Transport: &httpclient.Transport{
105104
ConnectTimeout: 10 * time.Second,
106105
RequestTimeout: 30 * time.Second,

go/scanner/scanner.go

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,61 +16,71 @@ import (
1616
"golang.org/x/net/context"
1717
)
1818

19-
// Clients wishing to implement their own Matchers should implement this interface:
19+
// Matcher describes how to match certificates and precertificates; clients should implement this interface
20+
// to perform their own match criteria.
2021
type Matcher interface {
2122
// CertificateMatches is called by the scanner for each X509 Certificate found in the log.
22-
// The implementation should return |true| if the passed Certificate is interesting, and |false| otherwise.
23+
// The implementation should return true if the passed Certificate is interesting, and false otherwise.
2324
CertificateMatches(*x509.Certificate) bool
2425

2526
// PrecertificateMatches is called by the scanner for each CT Precertificate found in the log.
26-
// The implementation should return |true| if the passed Precertificate is interesting, and |false| otherwise.
27+
// The implementation should return true if the passed Precertificate is interesting, and false otherwise.
2728
PrecertificateMatches(*ct.Precertificate) bool
2829
}
2930

3031
// MatchAll is a Matcher which will match every possible Certificate and Precertificate.
3132
type MatchAll struct{}
3233

34+
// CertificateMatches returns true if the given cert should match; in this case, always.
3335
func (m MatchAll) CertificateMatches(_ *x509.Certificate) bool {
3436
return true
3537
}
3638

39+
// PrecertificateMatches returns true if the given precert should match, in this case, always.
3740
func (m MatchAll) PrecertificateMatches(_ *ct.Precertificate) bool {
3841
return true
3942
}
4043

4144
// MatchNone is a Matcher which will never match any Certificate or Precertificate.
4245
type MatchNone struct{}
4346

47+
// CertificateMatches returns true if the given cert should match; in this case, never.
4448
func (m MatchNone) CertificateMatches(_ *x509.Certificate) bool {
4549
return false
4650
}
4751

52+
// PrecertificateMatches returns true if the given cert should match; in this case, never.
4853
func (m MatchNone) PrecertificateMatches(_ *ct.Precertificate) bool {
4954
return false
5055
}
5156

57+
// MatchSerialNumber performs a match for a specific serial number.
5258
type MatchSerialNumber struct {
5359
SerialNumber big.Int
5460
}
5561

62+
// CertificateMatches returns true if the given cert should match; in this
63+
// case, only if the serial number matches.
5664
func (m MatchSerialNumber) CertificateMatches(c *x509.Certificate) bool {
5765
return c.SerialNumber.String() == m.SerialNumber.String()
5866
}
5967

68+
// PrecertificateMatches returns true if the given cert should match; in this
69+
// case, only if the serial number matches.
6070
func (m MatchSerialNumber) PrecertificateMatches(p *ct.Precertificate) bool {
6171
return p.TBSCertificate.SerialNumber.String() == m.SerialNumber.String()
6272
}
6373

64-
// MatchSubjectRegex is a Matcher which will use |CertificateSubjectRegex| and |PrecertificateSubjectRegex|
74+
// MatchSubjectRegex is a Matcher which will use CertificateSubjectRegex and PrecertificateSubjectRegex
6575
// to determine whether Certificates and Precertificates are interesting.
66-
// The two regexes are tested against Subject Common Name as well as all
76+
// The two regexes are tested against Subject CN (Common Name) as well as all
6777
// Subject Alternative Names
6878
type MatchSubjectRegex struct {
6979
CertificateSubjectRegex *regexp.Regexp
7080
PrecertificateSubjectRegex *regexp.Regexp
7181
}
7282

73-
// Returns true if either CN or any SAN of |c| matches |CertificateSubjectRegex|.
83+
// CertificateMatches returns true if either CN or any SAN of c matches m.CertificateSubjectRegex.
7484
func (m MatchSubjectRegex) CertificateMatches(c *x509.Certificate) bool {
7585
if m.CertificateSubjectRegex.FindStringIndex(c.Subject.CommonName) != nil {
7686
return true
@@ -83,7 +93,7 @@ func (m MatchSubjectRegex) CertificateMatches(c *x509.Certificate) bool {
8393
return false
8494
}
8595

86-
// Returns true if either CN or any SAN of |p| matches |PrecertificatesubjectRegex|.
96+
// PrecertificateMatches returns true if either CN or any SAN of p matches m.PrecertificateSubjectRegex.
8797
func (m MatchSubjectRegex) PrecertificateMatches(p *ct.Precertificate) bool {
8898
if m.PrecertificateSubjectRegex.FindStringIndex(p.TBSCertificate.Subject.CommonName) != nil {
8999
return true
@@ -96,16 +106,18 @@ func (m MatchSubjectRegex) PrecertificateMatches(p *ct.Precertificate) bool {
96106
return false
97107
}
98108

99-
// Matches on issuer cn by regex
109+
// MatchIssuerRegex matches on issuer CN (common name) by regex
100110
type MatchIssuerRegex struct {
101111
CertificateIssuerRegex *regexp.Regexp
102112
PrecertificateIssuerRegex *regexp.Regexp
103113
}
104114

115+
// CertificateMatches returns true if the given cert's CN matches.
105116
func (m MatchIssuerRegex) CertificateMatches(c *x509.Certificate) bool {
106117
return m.CertificateIssuerRegex.FindStringIndex(c.Issuer.CommonName) != nil
107118
}
108119

120+
// PrecertificateMatches returns true if the given precert's CN matches.
109121
func (m MatchIssuerRegex) PrecertificateMatches(p *ct.Precertificate) bool {
110122
return m.PrecertificateIssuerRegex.FindStringIndex(p.TBSCertificate.Issuer.CommonName) != nil
111123
}
@@ -135,7 +147,7 @@ type ScannerOptions struct {
135147
Quiet bool
136148
}
137149

138-
// Creates a new ScannerOptions struct with sensible defaults
150+
// DefaultScannerOptions creates a new ScannerOptions struct with sensible defaults.
139151
func DefaultScannerOptions() *ScannerOptions {
140152
return &ScannerOptions{
141153
Matcher: &MatchAll{},
@@ -189,7 +201,7 @@ type fetchRange struct {
189201
// nil.
190202
// Fatal errors will be logged, unparsableEntires will be incremented, and the
191203
// fatal error itself will be returned.
192-
// When |err| is nil, this method does nothing.
204+
// When err is nil, this method does nothing.
193205
func (s *Scanner) handleParseEntryError(err error, entryType ct.LogEntryType, index int64) error {
194206
if err == nil {
195207
// No error to handle
@@ -208,7 +220,7 @@ func (s *Scanner) handleParseEntryError(err error, entryType ct.LogEntryType, in
208220
return nil
209221
}
210222

211-
// Processes the given |entry| in the specified log.
223+
// Processes the given entry in the specified log.
212224
func (s *Scanner) processEntry(entry ct.LogEntry, foundCert func(*ct.LogEntry), foundPrecert func(*ct.LogEntry)) {
213225
atomic.AddInt64(&s.certsProcessed, 1)
214226
switch entry.Leaf.TimestampedEntry.EntryType {
@@ -245,8 +257,8 @@ func (s *Scanner) processEntry(entry ct.LogEntry, foundCert func(*ct.LogEntry),
245257
}
246258

247259
// Worker function to match certs.
248-
// Accepts MatcherJobs over the |entries| channel, and processes them.
249-
// Returns true over the |done| channel when the |entries| channel is closed.
260+
// Accepts MatcherJobs over the entries channel, and processes them.
261+
// Returns true over the done channel when the entries channel is closed.
250262
func (s *Scanner) matcherJob(id int, entries <-chan matcherJob, foundCert func(*ct.LogEntry), foundPrecert func(*ct.LogEntry), wg *sync.WaitGroup) {
251263
for e := range entries {
252264
s.processEntry(e.entry, foundCert, foundPrecert)
@@ -256,11 +268,11 @@ func (s *Scanner) matcherJob(id int, entries <-chan matcherJob, foundCert func(*
256268
}
257269

258270
// Worker function for fetcher jobs.
259-
// Accepts cert ranges to fetch over the |ranges| channel, and if the fetch is
271+
// Accepts cert ranges to fetch over the ranges channel, and if the fetch is
260272
// successful sends the individual LeafInputs out (as MatcherJobs) into the
261-
// |entries| channel for the matchers to chew on.
273+
// entries channel for the matchers to chew on.
262274
// Will retry failed attempts to retrieve ranges indefinitely.
263-
// Sends true over the |done| channel when the |ranges| channel is closed.
275+
// Sends true over the done channel when the ranges channel is closed.
264276
func (s *Scanner) fetcherJob(id int, ranges <-chan fetchRange, entries chan<- matcherJob, wg *sync.WaitGroup) {
265277
for r := range ranges {
266278
success := false
@@ -288,25 +300,21 @@ func (s *Scanner) fetcherJob(id int, ranges <-chan fetchRange, entries chan<- ma
288300
wg.Done()
289301
}
290302

291-
// Returns the smaller of |a| and |b|
292303
func min(a int64, b int64) int64 {
293304
if a < b {
294305
return a
295-
} else {
296-
return b
297306
}
307+
return b
298308
}
299309

300-
// Returns the larger of |a| and |b|
301310
func max(a int64, b int64) int64 {
302311
if a > b {
303312
return a
304-
} else {
305-
return b
306313
}
314+
return b
307315
}
308316

309-
// Pretty prints the passed in number of |seconds| into a more human readable
317+
// Pretty prints the passed in number of seconds into a more human readable
310318
// string.
311319
func humanTime(seconds int) string {
312320
nanos := time.Duration(seconds) * time.Second
@@ -328,10 +336,10 @@ func humanTime(seconds int) string {
328336
return s
329337
}
330338

331-
// Performs a scan against the Log.
332-
// For each x509 certificate found, |foundCert| will be called with the
339+
// Scan performs a scan against the Log.
340+
// For each x509 certificate found, foundCert will be called with the
333341
// index of the entry and certificate itself as arguments. For each precert
334-
// found, |foundPrecert| will be called with the index of the entry and the raw
342+
// found, foundPrecert will be called with the index of the entry and the raw
335343
// precert string as the arguments.
336344
//
337345
// This method blocks until the scan is complete.
@@ -397,8 +405,8 @@ func (s *Scanner) Scan(foundCert func(*ct.LogEntry),
397405
return nil
398406
}
399407

400-
// Creates a new Scanner instance using |client| to talk to the log, and taking
401-
// configuration options from |opts|.
408+
// NewScanner creates a new Scanner instance using client to talk to the log,
409+
// taking configuration options from opts.
402410
func NewScanner(client *client.LogClient, opts ScannerOptions) *Scanner {
403411
var scanner Scanner
404412
scanner.logClient = client

0 commit comments

Comments
 (0)