z_spec_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. // covers marks all sentences for section sec in defaultSpecCoverage. Sentences not
  31. // "covered" will be included in report outputed by TestSpecCoverage.
  32. func covers(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. coverage 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. if v.Name.Local == "section" {
  100. sub++
  101. }
  102. break
  103. }
  104. switch v.Name.Local {
  105. case "section":
  106. sub++
  107. sc.readSection(append(sec, sub))
  108. case "xref":
  109. buf.Write(sc.readXRef(v))
  110. }
  111. case xml.CharData:
  112. if len(sec) == 0 {
  113. break
  114. }
  115. buf.Write(v)
  116. case xml.EndElement:
  117. if v.Name.Local == "section" {
  118. sc.addSentences(joinSection(sec), buf.String())
  119. return
  120. }
  121. }
  122. }
  123. }
  124. func (sc specCoverage) readXRef(se xml.StartElement) []byte {
  125. var b []byte
  126. for {
  127. tk, err := sc.d.Token()
  128. if err != nil {
  129. panic(err)
  130. }
  131. switch v := tk.(type) {
  132. case xml.CharData:
  133. if b != nil {
  134. panic("unexpected CharData")
  135. }
  136. b = []byte(string(v))
  137. case xml.EndElement:
  138. if v.Name.Local != "xref" {
  139. panic("expected </xref>")
  140. }
  141. if b != nil {
  142. return b
  143. }
  144. sig := attrSig(se)
  145. switch sig {
  146. case "target":
  147. return []byte(fmt.Sprintf("[%s]", attrValue(se, "target")))
  148. case "fmt-of,rel,target", "fmt-,,rel,target":
  149. return []byte(fmt.Sprintf("[%s, %s]", attrValue(se, "target"), attrValue(se, "rel")))
  150. case "fmt-of,sec,target", "fmt-,,sec,target":
  151. return []byte(fmt.Sprintf("[section %s of %s]", attrValue(se, "sec"), attrValue(se, "target")))
  152. case "fmt-of,rel,sec,target":
  153. return []byte(fmt.Sprintf("[section %s of %s, %s]", attrValue(se, "sec"), attrValue(se, "target"), attrValue(se, "rel")))
  154. default:
  155. panic(fmt.Sprintf("unknown attribute signature %q in %#v", sig, fmt.Sprintf("%#v", se)))
  156. }
  157. default:
  158. panic(fmt.Sprintf("unexpected tag %q", v))
  159. }
  160. }
  161. }
  162. var skipAnchor = map[string]bool{
  163. "intro": true,
  164. "Overview": true,
  165. }
  166. var skipTitle = map[string]bool{
  167. "Acknowledgements": true,
  168. "Change Log": true,
  169. "Document Organization": true,
  170. "Conventions and Terminology": true,
  171. }
  172. func skipElement(s xml.StartElement) bool {
  173. switch s.Name.Local {
  174. case "artwork":
  175. return true
  176. case "section":
  177. for _, attr := range s.Attr {
  178. switch attr.Name.Local {
  179. case "anchor":
  180. if skipAnchor[attr.Value] || strings.HasPrefix(attr.Value, "changes.since.") {
  181. return true
  182. }
  183. case "title":
  184. if skipTitle[attr.Value] {
  185. return true
  186. }
  187. }
  188. }
  189. }
  190. return false
  191. }
  192. func readSpecCov(r io.Reader) specCoverage {
  193. sc := specCoverage{
  194. coverage: map[specPart]bool{},
  195. d: xml.NewDecoder(r)}
  196. sc.readSection(nil)
  197. return sc
  198. }
  199. func (sc specCoverage) addSentences(sec string, sentence string) {
  200. for _, s := range parseSentences(sentence) {
  201. sc.coverage[specPart{sec, s}] = false
  202. }
  203. }
  204. func (sc specCoverage) cover(sec string, sentence string) {
  205. for _, s := range parseSentences(sentence) {
  206. p := specPart{sec, s}
  207. if _, ok := sc.coverage[p]; !ok {
  208. panic(fmt.Sprintf("Not found in spec: %q, %q", sec, s))
  209. }
  210. sc.coverage[specPart{sec, s}] = true
  211. }
  212. }
  213. var whitespaceRx = regexp.MustCompile(`\s+`)
  214. func parseSentences(sens string) []string {
  215. sens = strings.TrimSpace(sens)
  216. if sens == "" {
  217. return nil
  218. }
  219. ss := strings.Split(whitespaceRx.ReplaceAllString(sens, " "), ". ")
  220. for i, s := range ss {
  221. s = strings.TrimSpace(s)
  222. if !strings.HasSuffix(s, ".") {
  223. s += "."
  224. }
  225. ss[i] = s
  226. }
  227. return ss
  228. }
  229. func TestSpecParseSentences(t *testing.T) {
  230. tests := []struct {
  231. ss string
  232. want []string
  233. }{
  234. {"Sentence 1. Sentence 2.",
  235. []string{
  236. "Sentence 1.",
  237. "Sentence 2.",
  238. }},
  239. {"Sentence 1. \nSentence 2.\tSentence 3.",
  240. []string{
  241. "Sentence 1.",
  242. "Sentence 2.",
  243. "Sentence 3.",
  244. }},
  245. }
  246. for i, tt := range tests {
  247. got := parseSentences(tt.ss)
  248. if !reflect.DeepEqual(got, tt.want) {
  249. t.Errorf("%d: got = %q, want %q", i, got, tt.want)
  250. }
  251. }
  252. }
  253. func TestSpecCoverage(t *testing.T) {
  254. if !*coverSpec {
  255. t.Skip()
  256. }
  257. var (
  258. list []specPart
  259. cv = defaultSpecCoverage.coverage
  260. total = len(cv)
  261. complete = 0
  262. )
  263. for sp, touched := range defaultSpecCoverage.coverage {
  264. if touched {
  265. complete++
  266. } else {
  267. list = append(list, sp)
  268. }
  269. }
  270. sort.Stable(bySpecSection(list))
  271. if testing.Short() && len(list) > 5 {
  272. list = list[:5]
  273. }
  274. for _, p := range list {
  275. t.Errorf("\tSECTION %s: %s", p.section, p.sentence)
  276. }
  277. t.Logf("%d/%d (%d%%) sentances covered", complete, total, (complete/total)*100)
  278. }
  279. func attrSig(se xml.StartElement) string {
  280. var names []string
  281. for _, attr := range se.Attr {
  282. if attr.Name.Local == "fmt" {
  283. names = append(names, "fmt-"+attr.Value)
  284. } else {
  285. names = append(names, attr.Name.Local)
  286. }
  287. }
  288. sort.Strings(names)
  289. return strings.Join(names, ",")
  290. }
  291. func attrValue(se xml.StartElement, attr string) string {
  292. for _, a := range se.Attr {
  293. if a.Name.Local == attr {
  294. return a.Value
  295. }
  296. }
  297. panic("unknown attribute " + attr)
  298. }
  299. func TestSpecPartLess(t *testing.T) {
  300. tests := []struct {
  301. sec1, sec2 string
  302. want bool
  303. }{
  304. {"6.2.1", "6.2", false},
  305. {"6.2", "6.2.1", true},
  306. {"6.10", "6.10.1", true},
  307. {"6.10", "6.1.1", false}, // 10, not 1
  308. {"6.1", "6.1", false}, // equal, so not less
  309. }
  310. for _, tt := range tests {
  311. got := (specPart{tt.sec1, "foo"}).Less(specPart{tt.sec2, "foo"})
  312. if got != tt.want {
  313. t.Errorf("Less(%q, %q) = %v; want %v", tt.sec1, tt.sec2, got, tt.want)
  314. }
  315. }
  316. }