z_spec_test.go 7.3 KB

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