Skip to content
Open
42 changes: 42 additions & 0 deletions api/gather.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package api

import (
"github.com/chromium/hstspreload/chromium/preloadlist"
)

type PolicyType int

const (
bulk18Week PolicyType = iota
bulk1Year
)

func (pt PolicyType) String() string {
return []string{"bulk-18-weeks", "bulk-1-year"}[pt]
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PolicyType can be a string and then the slice isn't needed. If PolicyType is defined to be a string, then the preloadlist.Entry struct can have its Policy field be of type PolicyType. See the preload status field in DomainState as an example.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


// GetBulk18Weeks reuturns a list of entries from the preloadlist that have "bulk-18-weeks" policy type
func GetBulk18WeeksDomains(list preloadlist.PreloadList) []preloadlist.Entry {
var domains []preloadlist.Entry

for _, entry := range list.Entries {
if entry.Policy == bulk18Week.String() {
domains = append(domains, entry)
}
}

return domains
}

// GetBulk1Year returns a list of entries from the preloadlist that have "bulk-1-year" policy type
func GetBulk1YearDomains(list preloadlist.PreloadList) []preloadlist.Entry {
var domains []preloadlist.Entry

for _, entry := range list.Entries {
if entry.Policy == bulk1Year.String() {
domains = append(domains, entry)
}
}

return domains
}
40 changes: 40 additions & 0 deletions api/gather_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package api

import (
"reflect"
"testing"

"github.com/chromium/hstspreload/chromium/preloadlist"
)

var TestPreloadList = preloadlist.PreloadList{Entries: []preloadlist.Entry{
{Name: "garron.net", Mode: "force-https", IncludeSubDomains: true, Policy: "bulk-18-weeks"},
{Name: "example.com", Mode: "force-https", IncludeSubDomains: false, Policy: "bulk-18-weeks"},
{Name: "gmail.com", Mode: "force-https", IncludeSubDomains: false, Policy: ""},
{Name: "google.com", Mode: "", IncludeSubDomains: false, Policy: "custom"},
{Name: "pinned.badssl.com", Mode: "", IncludeSubDomains: false, Policy: "bulk-1-year"}},
}

var expected18weeks = []preloadlist.Entry{
{Name: "garron.net", Mode: "force-https", IncludeSubDomains: true, Policy: "bulk-18-weeks"},
{Name: "example.com", Mode: "force-https", IncludeSubDomains: false, Policy: "bulk-18-weeks"},
}
var expected1year = []preloadlist.Entry{
{Name: "pinned.badssl.com", Mode: "", IncludeSubDomains: false, Policy: "bulk-1-year"},
}

func TestGetBulk18WeeksDomains(t *testing.T) {
domains18weeks := GetBulk18WeeksDomains(TestPreloadList)

if !reflect.DeepEqual(domains18weeks, expected18weeks) {
t.Errorf("bulk18week domains does not match expected")
}
}

func TestGetBulk1YearDomains(t *testing.T) {
domains1year := GetBulk1YearDomains(TestPreloadList)

if !reflect.DeepEqual(domains1year, expected1year) {
t.Errorf("bulk1year domains does not match")
}
}