coverage.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package hubtest
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "regexp"
  8. "sort"
  9. "strings"
  10. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  11. log "github.com/sirupsen/logrus"
  12. )
  13. type ParserCoverage struct {
  14. Parser string
  15. TestsCount int
  16. PresentIn map[string]bool //poorman's set
  17. }
  18. type ScenarioCoverage struct {
  19. Scenario string
  20. TestsCount int
  21. PresentIn map[string]bool
  22. }
  23. func (h *HubTest) GetParsersCoverage() ([]ParserCoverage, error) {
  24. var coverage []ParserCoverage
  25. if _, ok := h.HubIndex.Data[cwhub.PARSERS]; !ok {
  26. return coverage, fmt.Errorf("no parsers in hub index")
  27. }
  28. //populate from hub, iterate in alphabetical order
  29. var pkeys []string
  30. for pname := range h.HubIndex.Data[cwhub.PARSERS] {
  31. pkeys = append(pkeys, pname)
  32. }
  33. sort.Strings(pkeys)
  34. for _, pname := range pkeys {
  35. coverage = append(coverage, ParserCoverage{
  36. Parser: pname,
  37. TestsCount: 0,
  38. PresentIn: make(map[string]bool),
  39. })
  40. }
  41. //parser the expressions a-la-oneagain
  42. passerts, err := filepath.Glob(".tests/*/parser.assert")
  43. if err != nil {
  44. return coverage, fmt.Errorf("while find parser asserts : %s", err)
  45. }
  46. for _, assert := range passerts {
  47. file, err := os.Open(assert)
  48. if err != nil {
  49. return coverage, fmt.Errorf("while reading %s : %s", assert, err)
  50. }
  51. scanner := bufio.NewScanner(file)
  52. for scanner.Scan() {
  53. assertLine := regexp.MustCompile(`^results\["[^"]+"\]\["(?P<parser>[^"]+)"\]\[[0-9]+\]\.Evt\..*`)
  54. line := scanner.Text()
  55. log.Debugf("assert line : %s", line)
  56. match := assertLine.FindStringSubmatch(line)
  57. if len(match) == 0 {
  58. log.Debugf("%s doesn't match", line)
  59. continue
  60. }
  61. sidx := assertLine.SubexpIndex("parser")
  62. capturedParser := match[sidx]
  63. for idx, pcover := range coverage {
  64. if pcover.Parser == capturedParser {
  65. coverage[idx].TestsCount++
  66. coverage[idx].PresentIn[assert] = true
  67. continue
  68. }
  69. parserNameSplit := strings.Split(pcover.Parser, "/")
  70. parserNameOnly := parserNameSplit[len(parserNameSplit)-1]
  71. if parserNameOnly == capturedParser {
  72. coverage[idx].TestsCount++
  73. coverage[idx].PresentIn[assert] = true
  74. continue
  75. }
  76. capturedParserSplit := strings.Split(capturedParser, "/")
  77. capturedParserName := capturedParserSplit[len(capturedParserSplit)-1]
  78. if capturedParserName == parserNameOnly {
  79. coverage[idx].TestsCount++
  80. coverage[idx].PresentIn[assert] = true
  81. continue
  82. }
  83. if capturedParserName == parserNameOnly+"-logs" {
  84. coverage[idx].TestsCount++
  85. coverage[idx].PresentIn[assert] = true
  86. continue
  87. }
  88. }
  89. }
  90. file.Close()
  91. }
  92. return coverage, nil
  93. }
  94. func (h *HubTest) GetScenariosCoverage() ([]ScenarioCoverage, error) {
  95. var coverage []ScenarioCoverage
  96. if _, ok := h.HubIndex.Data[cwhub.SCENARIOS]; !ok {
  97. return coverage, fmt.Errorf("no scenarios in hub index")
  98. }
  99. //populate from hub, iterate in alphabetical order
  100. var pkeys []string
  101. for scenarioName := range h.HubIndex.Data[cwhub.SCENARIOS] {
  102. pkeys = append(pkeys, scenarioName)
  103. }
  104. sort.Strings(pkeys)
  105. for _, scenarioName := range pkeys {
  106. coverage = append(coverage, ScenarioCoverage{
  107. Scenario: scenarioName,
  108. TestsCount: 0,
  109. PresentIn: make(map[string]bool),
  110. })
  111. }
  112. //parser the expressions a-la-oneagain
  113. passerts, err := filepath.Glob(".tests/*/scenario.assert")
  114. if err != nil {
  115. return coverage, fmt.Errorf("while find scenario asserts : %s", err)
  116. }
  117. for _, assert := range passerts {
  118. file, err := os.Open(assert)
  119. if err != nil {
  120. return coverage, fmt.Errorf("while reading %s : %s", assert, err)
  121. }
  122. scanner := bufio.NewScanner(file)
  123. for scanner.Scan() {
  124. assertLine := regexp.MustCompile(`^results\[[0-9]+\].Overflow.Alert.GetScenario\(\) == "(?P<scenario>[^"]+)"`)
  125. line := scanner.Text()
  126. log.Debugf("assert line : %s", line)
  127. match := assertLine.FindStringSubmatch(line)
  128. if len(match) == 0 {
  129. log.Debugf("%s doesn't match", line)
  130. continue
  131. }
  132. sidx := assertLine.SubexpIndex("scenario")
  133. scanner_name := match[sidx]
  134. for idx, pcover := range coverage {
  135. if pcover.Scenario == scanner_name {
  136. coverage[idx].TestsCount++
  137. coverage[idx].PresentIn[assert] = true
  138. continue
  139. }
  140. scenarioNameSplit := strings.Split(pcover.Scenario, "/")
  141. scenarioNameOnly := scenarioNameSplit[len(scenarioNameSplit)-1]
  142. if scenarioNameOnly == scanner_name {
  143. coverage[idx].TestsCount++
  144. coverage[idx].PresentIn[assert] = true
  145. continue
  146. }
  147. fixedProbingWord := strings.ReplaceAll(pcover.Scenario, "probbing", "probing")
  148. fixedProbingAssert := strings.ReplaceAll(scanner_name, "probbing", "probing")
  149. if fixedProbingWord == fixedProbingAssert {
  150. coverage[idx].TestsCount++
  151. coverage[idx].PresentIn[assert] = true
  152. continue
  153. }
  154. if fmt.Sprintf("%s-detection", pcover.Scenario) == scanner_name {
  155. coverage[idx].TestsCount++
  156. coverage[idx].PresentIn[assert] = true
  157. continue
  158. }
  159. if fmt.Sprintf("%s-detection", fixedProbingWord) == fixedProbingAssert {
  160. coverage[idx].TestsCount++
  161. coverage[idx].PresentIn[assert] = true
  162. continue
  163. }
  164. }
  165. }
  166. file.Close()
  167. }
  168. return coverage, nil
  169. }