z_spec_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. 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. 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(string(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. coverage: 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.coverage[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.coverage[p]; !ok {
  205. panic(fmt.Sprintf("Not found in spec: %q, %q", sec, s))
  206. }
  207. sc.coverage[specPart{sec, s}] = true
  208. }
  209. }
  210. var whitespaceRx = regexp.MustCompile(`\s+`)
  211. func parseSentences(sens string) []string {
  212. sens = strings.TrimSpace(sens)
  213. if sens == "" {
  214. return nil
  215. }
  216. ss := strings.Split(whitespaceRx.ReplaceAllString(sens, " "), ". ")
  217. for i, s := range ss {
  218. s = strings.TrimSpace(s)
  219. if !strings.HasSuffix(s, ".") {
  220. s += "."
  221. }
  222. ss[i] = s
  223. }
  224. return ss
  225. }
  226. func TestSpecParseSentences(t *testing.T) {
  227. tests := []struct {
  228. ss string
  229. want []string
  230. }{
  231. {"Sentence 1. Sentence 2.",
  232. []string{
  233. "Sentence 1.",
  234. "Sentence 2.",
  235. }},
  236. {"Sentence 1. \nSentence 2.\tSentence 3.",
  237. []string{
  238. "Sentence 1.",
  239. "Sentence 2.",
  240. "Sentence 3.",
  241. }},
  242. }
  243. for i, tt := range tests {
  244. got := parseSentences(tt.ss)
  245. if !reflect.DeepEqual(got, tt.want) {
  246. t.Errorf("%d: got = %q, want %q", i, got, tt.want)
  247. }
  248. }
  249. }
  250. func TestSpecCoverage(t *testing.T) {
  251. if !*coverSpec {
  252. t.Skip()
  253. }
  254. var (
  255. list []specPart
  256. cv = defaultSpecCoverage.coverage
  257. total = len(cv)
  258. complete = 0
  259. )
  260. for sp, touched := range defaultSpecCoverage.coverage {
  261. if touched {
  262. complete++
  263. } else {
  264. list = append(list, sp)
  265. }
  266. }
  267. sort.Stable(bySpecSection(list))
  268. if testing.Short() {
  269. list = list[:5]
  270. }
  271. for _, p := range list {
  272. t.Errorf("\tSECTION %s: %s", p.section, p.sentence)
  273. }
  274. t.Logf("%d/%d (%d%%) sentances covered", complete, total, (complete/total)*100)
  275. }
  276. func attrSig(se xml.StartElement) string {
  277. var names []string
  278. for _, attr := range se.Attr {
  279. if attr.Name.Local == "fmt" {
  280. names = append(names, "fmt-"+attr.Value)
  281. } else {
  282. names = append(names, attr.Name.Local)
  283. }
  284. }
  285. sort.Strings(names)
  286. return strings.Join(names, ",")
  287. }
  288. func attrValue(se xml.StartElement, attr string) string {
  289. for _, a := range se.Attr {
  290. if a.Name.Local == attr {
  291. return a.Value
  292. }
  293. }
  294. panic("unknown attribute " + attr)
  295. }
  296. func TestSpecPartLess(t *testing.T) {
  297. tests := []struct {
  298. sec1, sec2 string
  299. want bool
  300. }{
  301. {"6.2.1", "6.2", false},
  302. {"6.2", "6.2.1", true},
  303. {"6.10", "6.10.1", true},
  304. {"6.10", "6.1.1", false}, // 10, not 1
  305. {"6.1", "6.1", false}, // equal, so not less
  306. }
  307. for _, tt := range tests {
  308. got := (specPart{tt.sec1, "foo"}).Less(specPart{tt.sec2, "foo"})
  309. if got != tt.want {
  310. t.Errorf("Less(%q, %q) = %v; want %v", tt.sec1, tt.sec2, got, tt.want)
  311. }
  312. }
  313. }