xmlutil.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * This file is part of the libvirt-go-xml project
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. *
  22. * Copyright (C) 2017 Red Hat, Inc.
  23. *
  24. */
  25. package libvirtxml
  26. import (
  27. "encoding/xml"
  28. "fmt"
  29. "strconv"
  30. "strings"
  31. )
  32. type element struct {
  33. XMLNS string
  34. Name string
  35. Attrs map[string]string
  36. Content string
  37. Children []*element
  38. }
  39. type elementstack []*element
  40. func (s *elementstack) push(v *element) {
  41. *s = append(*s, v)
  42. }
  43. func (s *elementstack) pop() *element {
  44. res := (*s)[len(*s)-1]
  45. *s = (*s)[:len(*s)-1]
  46. return res
  47. }
  48. func getNamespaceURI(xmlnsMap map[string]string, xmlns string, name xml.Name) string {
  49. if name.Space != "" {
  50. uri, ok := xmlnsMap[name.Space]
  51. if !ok {
  52. return "undefined://" + name.Space
  53. } else {
  54. return uri
  55. }
  56. } else {
  57. return xmlns
  58. }
  59. }
  60. func xmlName(xmlns string, name xml.Name) string {
  61. if xmlns == "" {
  62. return name.Local
  63. }
  64. return name.Local + "(" + xmlns + ")"
  65. }
  66. func loadXML(xmlstr string, ignoreNSDecl bool) (*element, error) {
  67. xmlnsMap := make(map[string]string)
  68. xmlr := strings.NewReader(xmlstr)
  69. d := xml.NewDecoder(xmlr)
  70. var root *element
  71. stack := elementstack{}
  72. for {
  73. t, err := d.RawToken()
  74. if err != nil {
  75. return nil, err
  76. }
  77. var parent *element
  78. if root != nil {
  79. if len(stack) == 0 {
  80. return nil, fmt.Errorf("Unexpectedly empty stack")
  81. }
  82. parent = stack[len(stack)-1]
  83. }
  84. switch t := t.(type) {
  85. case xml.StartElement:
  86. xmlns := ""
  87. if parent != nil {
  88. xmlns = parent.XMLNS
  89. }
  90. for _, a := range t.Attr {
  91. if a.Name.Space == "xmlns" {
  92. xmlnsMap[a.Name.Local] = a.Value
  93. } else if a.Name.Space == "" && a.Name.Local == "xmlns" {
  94. xmlns = a.Value
  95. }
  96. }
  97. xmlns = getNamespaceURI(xmlnsMap, xmlns, t.Name)
  98. child := &element{
  99. XMLNS: xmlns,
  100. Name: xmlName(xmlns, t.Name),
  101. Attrs: make(map[string]string),
  102. }
  103. for _, a := range t.Attr {
  104. if a.Name.Space == "xmlns" {
  105. continue
  106. }
  107. if a.Name.Space == "" && a.Name.Local == "xmlns" {
  108. continue
  109. }
  110. attrNS := getNamespaceURI(xmlnsMap, "", a.Name)
  111. child.Attrs[xmlName(attrNS, a.Name)] = a.Value
  112. }
  113. stack.push(child)
  114. if root == nil {
  115. root = child
  116. } else {
  117. parent.Children = append(parent.Children, child)
  118. parent.Content = ""
  119. }
  120. case xml.EndElement:
  121. stack.pop()
  122. case xml.CharData:
  123. if parent != nil && len(parent.Children) == 0 {
  124. val := string(t)
  125. if strings.TrimSpace(val) != "" {
  126. parent.Content = val
  127. }
  128. }
  129. }
  130. if root != nil && len(stack) == 0 {
  131. break
  132. }
  133. }
  134. return root, nil
  135. }
  136. func testCompareValue(filename, path, key, expected, actual string) error {
  137. if expected == actual {
  138. return nil
  139. }
  140. i1, err1 := strconv.ParseInt(expected, 0, 64)
  141. i2, err2 := strconv.ParseInt(actual, 0, 64)
  142. if err1 == nil && err2 == nil && i1 == i2 {
  143. return nil
  144. }
  145. path = path + "/@" + key
  146. return fmt.Errorf("%s: %s: attribute actual value '%s' does not match expected value '%s'",
  147. filename, path, actual, expected)
  148. }
  149. func testCompareElement(filename, expectPath, actualPath string, expect, actual *element, extraExpectNodes, extraActualNodes map[string]bool) error {
  150. if expect.Name != actual.Name {
  151. return fmt.Errorf("%s: name '%s' doesn't match '%s'",
  152. expectPath, expect.Name, actual.Name)
  153. }
  154. expectAttr := expect.Attrs
  155. for key, val := range actual.Attrs {
  156. expectval, ok := expectAttr[key]
  157. if !ok {
  158. attrPath := actualPath + "/@" + key
  159. if _, ok := extraActualNodes[attrPath]; ok {
  160. continue
  161. }
  162. return fmt.Errorf("%s: %s: attribute in actual XML missing in expected XML",
  163. filename, attrPath)
  164. }
  165. err := testCompareValue(filename, actualPath, key, expectval, val)
  166. if err != nil {
  167. return err
  168. }
  169. delete(expectAttr, key)
  170. }
  171. for key, _ := range expectAttr {
  172. attrPath := expectPath + "/@" + key
  173. if _, ok := extraExpectNodes[attrPath]; ok {
  174. continue
  175. }
  176. return fmt.Errorf("%s: %s: attribute '%s' in expected XML missing in actual XML",
  177. filename, attrPath, expectAttr[key])
  178. }
  179. if expect.Content != actual.Content {
  180. return fmt.Errorf("%s: %s: actual content '%s' does not match expected '%s'",
  181. filename, actualPath, actual.Content, expect.Content)
  182. }
  183. used := make([]bool, len(actual.Children))
  184. expectChildIndexes := make(map[string]uint)
  185. actualChildIndexes := make(map[string]uint)
  186. for _, expectChild := range expect.Children {
  187. expectIndex, _ := expectChildIndexes[expectChild.Name]
  188. expectChildIndexes[expectChild.Name] = expectIndex + 1
  189. subExpectPath := fmt.Sprintf("%s/%s[%d]", expectPath, expectChild.Name, expectIndex)
  190. var actualChild *element = nil
  191. for i := 0; i < len(used); i++ {
  192. if !used[i] && actual.Children[i].Name == expectChild.Name {
  193. actualChild = actual.Children[i]
  194. used[i] = true
  195. break
  196. }
  197. }
  198. if actualChild == nil {
  199. if _, ok := extraExpectNodes[subExpectPath]; ok {
  200. continue
  201. }
  202. return fmt.Errorf("%s: %s: element in expected XML missing in actual XML",
  203. filename, subExpectPath)
  204. }
  205. actualIndex, _ := actualChildIndexes[actualChild.Name]
  206. actualChildIndexes[actualChild.Name] = actualIndex + 1
  207. subActualPath := fmt.Sprintf("%s/%s[%d]", actualPath, actualChild.Name, actualIndex)
  208. err := testCompareElement(filename, subExpectPath, subActualPath, expectChild, actualChild, extraExpectNodes, extraActualNodes)
  209. if err != nil {
  210. return err
  211. }
  212. }
  213. actualChildIndexes = make(map[string]uint)
  214. for i, actualChild := range actual.Children {
  215. actualIndex, _ := actualChildIndexes[actualChild.Name]
  216. actualChildIndexes[actualChild.Name] = actualIndex + 1
  217. if used[i] {
  218. continue
  219. }
  220. subActualPath := fmt.Sprintf("%s/%s[%d]", actualPath, actualChild.Name, actualIndex)
  221. if _, ok := extraActualNodes[subActualPath]; ok {
  222. continue
  223. }
  224. return fmt.Errorf("%s: %s: element in actual XML missing in expected XML",
  225. filename, subActualPath)
  226. }
  227. return nil
  228. }
  229. func makeExtraNodeMap(nodes []string) map[string]bool {
  230. ret := make(map[string]bool)
  231. for _, node := range nodes {
  232. ret[node] = true
  233. }
  234. return ret
  235. }
  236. func testCompareXML(filename, expectStr, actualStr string, extraExpectNodes, extraActualNodes []string) error {
  237. extraExpectNodeMap := makeExtraNodeMap(extraExpectNodes)
  238. extraActualNodeMap := makeExtraNodeMap(extraActualNodes)
  239. //fmt.Printf("%s\n", expectedstr)
  240. expectRoot, err := loadXML(expectStr, true)
  241. if err != nil {
  242. return err
  243. }
  244. //fmt.Printf("%s\n", actualstr)
  245. actualRoot, err := loadXML(actualStr, true)
  246. if err != nil {
  247. return err
  248. }
  249. if expectRoot.Name != actualRoot.Name {
  250. return fmt.Errorf("%s: /: expected root element '%s' does not match actual '%s'",
  251. filename, expectRoot.Name, actualRoot.Name)
  252. }
  253. err = testCompareElement(filename, "/"+expectRoot.Name+"[0]", "/"+actualRoot.Name+"[0]", expectRoot, actualRoot, extraExpectNodeMap, extraActualNodeMap)
  254. if err != nil {
  255. return err
  256. }
  257. return nil
  258. }