z_spec_test.go 7.1 KB

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