z_spec_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package http2
  5. import (
  6. "bytes"
  7. "encoding/xml"
  8. "flag"
  9. "fmt"
  10. "io"
  11. "os"
  12. "reflect"
  13. "regexp"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. "testing"
  19. )
  20. var coverSpec = flag.Bool("coverspec", false, "Run spec coverage tests")
  21. // The global map of sentence coverage for the http2 spec.
  22. var defaultSpecCoverage specCoverage
  23. var loadSpecOnce sync.Once
  24. func loadSpec() {
  25. if f, err := os.Open("testdata/draft-ietf-httpbis-http2.xml"); err != nil {
  26. panic(err)
  27. } else {
  28. defaultSpecCoverage = readSpecCov(f)
  29. f.Close()
  30. }
  31. }
  32. // covers marks all sentences for section sec in defaultSpecCoverage. Sentences not
  33. // "covered" will be included in report outputted by TestSpecCoverage.
  34. func covers(sec, sentences string) {
  35. loadSpecOnce.Do(loadSpec)
  36. defaultSpecCoverage.cover(sec, sentences)
  37. }
  38. type specPart struct {
  39. section string
  40. sentence string
  41. }
  42. func (ss specPart) Less(oo specPart) bool {
  43. atoi := func(s string) int {
  44. n, err := strconv.Atoi(s)
  45. if err != nil {
  46. panic(err)
  47. }
  48. return n
  49. }
  50. a := strings.Split(ss.section, ".")
  51. b := strings.Split(oo.section, ".")
  52. for len(a) > 0 {
  53. if len(b) == 0 {
  54. return false
  55. }
  56. x, y := atoi(a[0]), atoi(b[0])
  57. if x == y {
  58. a, b = a[1:], b[1:]
  59. continue
  60. }
  61. return x < y
  62. }
  63. if len(b) > 0 {
  64. return true
  65. }
  66. return false
  67. }
  68. type bySpecSection []specPart
  69. func (a bySpecSection) Len() int { return len(a) }
  70. func (a bySpecSection) Less(i, j int) bool { return a[i].Less(a[j]) }
  71. func (a bySpecSection) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  72. type specCoverage struct {
  73. coverage map[specPart]bool
  74. d *xml.Decoder
  75. }
  76. func joinSection(sec []int) string {
  77. s := fmt.Sprintf("%d", sec[0])
  78. for _, n := range sec[1:] {
  79. s = fmt.Sprintf("%s.%d", s, n)
  80. }
  81. return s
  82. }
  83. func (sc specCoverage) readSection(sec []int) {
  84. var (
  85. buf = new(bytes.Buffer)
  86. sub = 0
  87. )
  88. for {
  89. tk, err := sc.d.Token()
  90. if err != nil {
  91. if err == io.EOF {
  92. return
  93. }
  94. panic(err)
  95. }
  96. switch v := tk.(type) {
  97. case xml.StartElement:
  98. if skipElement(v) {
  99. if err := sc.d.Skip(); err != nil {
  100. panic(err)
  101. }
  102. if v.Name.Local == "section" {
  103. sub++
  104. }
  105. break
  106. }
  107. switch v.Name.Local {
  108. case "section":
  109. sub++
  110. sc.readSection(append(sec, sub))
  111. case "xref":
  112. buf.Write(sc.readXRef(v))
  113. }
  114. case xml.CharData:
  115. if len(sec) == 0 {
  116. break
  117. }
  118. buf.Write(v)
  119. case xml.EndElement:
  120. if v.Name.Local == "section" {
  121. sc.addSentences(joinSection(sec), buf.String())
  122. return
  123. }
  124. }
  125. }
  126. }
  127. func (sc specCoverage) readXRef(se xml.StartElement) []byte {
  128. var b []byte
  129. for {
  130. tk, err := sc.d.Token()
  131. if err != nil {
  132. panic(err)
  133. }
  134. switch v := tk.(type) {
  135. case xml.CharData:
  136. if b != nil {
  137. panic("unexpected CharData")
  138. }
  139. b = []byte(string(v))
  140. case xml.EndElement:
  141. if v.Name.Local != "xref" {
  142. panic("expected </xref>")
  143. }
  144. if b != nil {
  145. return b
  146. }
  147. sig := attrSig(se)
  148. switch sig {
  149. case "target":
  150. return []byte(fmt.Sprintf("[%s]", attrValue(se, "target")))
  151. case "fmt-of,rel,target", "fmt-,,rel,target":
  152. return []byte(fmt.Sprintf("[%s, %s]", attrValue(se, "target"), attrValue(se, "rel")))
  153. case "fmt-of,sec,target", "fmt-,,sec,target":
  154. return []byte(fmt.Sprintf("[section %s of %s]", attrValue(se, "sec"), attrValue(se, "target")))
  155. case "fmt-of,rel,sec,target":
  156. return []byte(fmt.Sprintf("[section %s of %s, %s]", attrValue(se, "sec"), attrValue(se, "target"), attrValue(se, "rel")))
  157. default:
  158. panic(fmt.Sprintf("unknown attribute signature %q in %#v", sig, fmt.Sprintf("%#v", se)))
  159. }
  160. default:
  161. panic(fmt.Sprintf("unexpected tag %q", v))
  162. }
  163. }
  164. }
  165. var skipAnchor = map[string]bool{
  166. "intro": true,
  167. "Overview": true,
  168. }
  169. var skipTitle = map[string]bool{
  170. "Acknowledgements": true,
  171. "Change Log": true,
  172. "Document Organization": true,
  173. "Conventions and Terminology": true,
  174. }
  175. func skipElement(s xml.StartElement) bool {
  176. switch s.Name.Local {
  177. case "artwork":
  178. return true
  179. case "section":
  180. for _, attr := range s.Attr {
  181. switch attr.Name.Local {
  182. case "anchor":
  183. if skipAnchor[attr.Value] || strings.HasPrefix(attr.Value, "changes.since.") {
  184. return true
  185. }
  186. case "title":
  187. if skipTitle[attr.Value] {
  188. return true
  189. }
  190. }
  191. }
  192. }
  193. return false
  194. }
  195. func readSpecCov(r io.Reader) specCoverage {
  196. sc := specCoverage{
  197. coverage: map[specPart]bool{},
  198. d: xml.NewDecoder(r)}
  199. sc.readSection(nil)
  200. return sc
  201. }
  202. func (sc specCoverage) addSentences(sec string, sentence string) {
  203. for _, s := range parseSentences(sentence) {
  204. sc.coverage[specPart{sec, s}] = false
  205. }
  206. }
  207. func (sc specCoverage) cover(sec string, sentence string) {
  208. for _, s := range parseSentences(sentence) {
  209. p := specPart{sec, s}
  210. if _, ok := sc.coverage[p]; !ok {
  211. panic(fmt.Sprintf("Not found in spec: %q, %q", sec, s))
  212. }
  213. sc.coverage[specPart{sec, s}] = true
  214. }
  215. }
  216. var whitespaceRx = regexp.MustCompile(`\s+`)
  217. func parseSentences(sens string) []string {
  218. sens = strings.TrimSpace(sens)
  219. if sens == "" {
  220. return nil
  221. }
  222. ss := strings.Split(whitespaceRx.ReplaceAllString(sens, " "), ". ")
  223. for i, s := range ss {
  224. s = strings.TrimSpace(s)
  225. if !strings.HasSuffix(s, ".") {
  226. s += "."
  227. }
  228. ss[i] = s
  229. }
  230. return ss
  231. }
  232. func TestSpecParseSentences(t *testing.T) {
  233. tests := []struct {
  234. ss string
  235. want []string
  236. }{
  237. {"Sentence 1. Sentence 2.",
  238. []string{
  239. "Sentence 1.",
  240. "Sentence 2.",
  241. }},
  242. {"Sentence 1. \nSentence 2.\tSentence 3.",
  243. []string{
  244. "Sentence 1.",
  245. "Sentence 2.",
  246. "Sentence 3.",
  247. }},
  248. }
  249. for i, tt := range tests {
  250. got := parseSentences(tt.ss)
  251. if !reflect.DeepEqual(got, tt.want) {
  252. t.Errorf("%d: got = %q, want %q", i, got, tt.want)
  253. }
  254. }
  255. }
  256. func TestSpecCoverage(t *testing.T) {
  257. if !*coverSpec {
  258. t.Skip()
  259. }
  260. loadSpecOnce.Do(loadSpec)
  261. var (
  262. list []specPart
  263. cv = defaultSpecCoverage.coverage
  264. total = len(cv)
  265. complete = 0
  266. )
  267. for sp, touched := range defaultSpecCoverage.coverage {
  268. if touched {
  269. complete++
  270. } else {
  271. list = append(list, sp)
  272. }
  273. }
  274. sort.Stable(bySpecSection(list))
  275. if testing.Short() && len(list) > 5 {
  276. list = list[:5]
  277. }
  278. for _, p := range list {
  279. t.Errorf("\tSECTION %s: %s", p.section, p.sentence)
  280. }
  281. t.Logf("%d/%d (%d%%) sentences covered", complete, total, (complete/total)*100)
  282. }
  283. func attrSig(se xml.StartElement) string {
  284. var names []string
  285. for _, attr := range se.Attr {
  286. if attr.Name.Local == "fmt" {
  287. names = append(names, "fmt-"+attr.Value)
  288. } else {
  289. names = append(names, attr.Name.Local)
  290. }
  291. }
  292. sort.Strings(names)
  293. return strings.Join(names, ",")
  294. }
  295. func attrValue(se xml.StartElement, attr string) string {
  296. for _, a := range se.Attr {
  297. if a.Name.Local == attr {
  298. return a.Value
  299. }
  300. }
  301. panic("unknown attribute " + attr)
  302. }
  303. func TestSpecPartLess(t *testing.T) {
  304. tests := []struct {
  305. sec1, sec2 string
  306. want bool
  307. }{
  308. {"6.2.1", "6.2", false},
  309. {"6.2", "6.2.1", true},
  310. {"6.10", "6.10.1", true},
  311. {"6.10", "6.1.1", false}, // 10, not 1
  312. {"6.1", "6.1", false}, // equal, so not less
  313. }
  314. for _, tt := range tests {
  315. got := (specPart{tt.sec1, "foo"}).Less(specPart{tt.sec2, "foo"})
  316. if got != tt.want {
  317. t.Errorf("Less(%q, %q) = %v; want %v", tt.sec1, tt.sec2, got, tt.want)
  318. }
  319. }
  320. }