From d7fb52b735cbcacf1690bda0041bab27646e17a1 Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Wed, 14 Jun 2023 14:37:09 +0200 Subject: [PATCH] check whether rolie service document contains all feeds --- cmd/csaf_checker/roliecheck.go | 54 +++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/cmd/csaf_checker/roliecheck.go b/cmd/csaf_checker/roliecheck.go index c3b2be6..b36942d 100644 --- a/cmd/csaf_checker/roliecheck.go +++ b/cmd/csaf_checker/roliecheck.go @@ -285,7 +285,7 @@ func (p *processor) serviceCheck(url string, feeds [][]csaf.Feed) error { return errContinue } - rolieService, err := func() (any, error) { + rolieService, err := func() (*csaf.ROLIEServiceDocument, error) { defer res.Body.Close() return csaf.LoadROLIEServiceDocument(res.Body) }() @@ -295,11 +295,57 @@ func (p *processor) serviceCheck(url string, feeds [][]csaf.Feed) error { return errContinue } - // TODO: Use me! - _ = rolieService - _ = feeds + // Build lists of all feeds in feeds and in the Service Document + sfeeds := []string{} + ffeeds := []string{} + for _, col := range rolieService.Service.Workspace { + for _, fd := range col.Collection { + sfeeds = append(sfeeds, fd.HRef) + } + } + for _, r := range feeds { + for _, s := range r { + ffeeds = append(ffeeds, string(*s.URL)) + } + } + // Check if ROLIE Service Document contains exactly all ROLIE feeds + m1, m2 := sameContentsSS(sfeeds, ffeeds) + if len(m1) != 0 { + p.badROLIEservice.error("The ROLIE service document contains nonexistent feed entries: %v", m1) + } + if len(m2) != 0 { + p.badROLIEservice.error("The ROLIE service document is missing feed entries: %v", m2) + } return nil // TODO: Check conformity with RFC8322 } + +// sameContents checks if two slices slice1 and slice2 of strings contains the same strings +// returns two slices of all strings missing in the respective other slice +func sameContentsSS(slice1 []string, slice2 []string) ([]string, []string) { + m1 := []string{} + m2 := []string{} + for _, e1 := range slice1 { + if !containsAllSS(slice2, e1) { + m1 = append(m1, e1) + } + } + for _, e2 := range slice2 { + if !containsAllSS(slice1, e2) { + m2 = append(m2, e2) + } + } + return m1, m2 +} + +// containsAllSS checks if a slice of strings contains a string +func containsAllSS(slice []string, str string) bool { + for _, e := range slice { + if e == str { + return true + } + } + return false +}