/Tests/SwiftProtobufTests/Test_TextFormat_Unknown.swift

https://github.com/apple/swift-protobuf · Swift · 286 lines · 212 code · 41 blank · 33 comment · 17 complexity · acb18e25070541d8e38c33866e456542 MD5 · raw file

  1. // Tests/SwiftProtobufTests/Test_TextFormat_Unknown.swift - Exercise unknown field text format coding
  2. //
  3. // Copyright (c) 2014 - 2016 Apple Inc. and the project authors
  4. // Licensed under Apache License v2.0 with Runtime Library Exception
  5. //
  6. // See LICENSE.txt for license information:
  7. // https://github.com/apple/swift-protobuf/blob/master/LICENSE.txt
  8. //
  9. // -----------------------------------------------------------------------------
  10. ///
  11. /// This is a set of tests for text format protobuf files.
  12. ///
  13. // -----------------------------------------------------------------------------
  14. import Foundation
  15. import XCTest
  16. @testable import SwiftProtobuf
  17. class Test_TextFormat_Unknown: XCTestCase, PBTestHelpers {
  18. typealias MessageTestType = ProtobufUnittest_TestEmptyMessage
  19. func test_unknown_varint() throws {
  20. let bytes = Data([8, 0])
  21. let msg = try MessageTestType(serializedData: bytes)
  22. let text = msg.textFormatString()
  23. XCTAssertEqual(text, "1: 0\n")
  24. do {
  25. let _ = try MessageTestType(textFormatString: text)
  26. XCTFail("Shouldn't get here")
  27. } catch TextFormatDecodingError.unknownField {
  28. // This is what should have happened.
  29. }
  30. var options = TextFormatEncodingOptions()
  31. options.printUnknownFields = false
  32. let textWithoutUnknowns = msg.textFormatString(options: options)
  33. XCTAssertEqual(textWithoutUnknowns, "")
  34. }
  35. func test_unknown_fixed64() throws {
  36. let bytes = Data([9, 0, 1, 2, 3, 4, 5, 6, 7])
  37. let msg = try MessageTestType(serializedData: bytes)
  38. let text = msg.textFormatString()
  39. XCTAssertEqual(text, "1: 0x0706050403020100\n")
  40. do {
  41. let _ = try MessageTestType(textFormatString: text)
  42. XCTFail("Shouldn't get here")
  43. } catch TextFormatDecodingError.unknownField {
  44. // This is what should have happened.
  45. }
  46. var options = TextFormatEncodingOptions()
  47. options.printUnknownFields = false
  48. let textWithoutUnknowns = msg.textFormatString(options: options)
  49. XCTAssertEqual(textWithoutUnknowns, "")
  50. }
  51. func test_unknown_lengthDelimited_string() throws {
  52. let bytes = Data([10, 3, 97, 98, 99])
  53. let msg = try MessageTestType(serializedData: bytes)
  54. let text = msg.textFormatString()
  55. XCTAssertEqual(text, "1: \"abc\"\n")
  56. do {
  57. let _ = try MessageTestType(textFormatString: text)
  58. XCTFail("Shouldn't get here")
  59. } catch TextFormatDecodingError.unknownField {
  60. // This is what should have happened.
  61. }
  62. var options = TextFormatEncodingOptions()
  63. options.printUnknownFields = false
  64. let textWithoutUnknowns = msg.textFormatString(options: options)
  65. XCTAssertEqual(textWithoutUnknowns, "")
  66. }
  67. func test_unknown_lengthDelimited_message() throws {
  68. // If inner data looks like a message, display it as such:
  69. let bytes = Data([10, 6, 8, 1, 18, 2, 97, 98])
  70. let msg = try MessageTestType(serializedData: bytes)
  71. let text = msg.textFormatString()
  72. XCTAssertEqual(text, "1 {\n 1: 1\n 2: \"ab\"\n}\n")
  73. do {
  74. let _ = try MessageTestType(textFormatString: text)
  75. XCTFail("Shouldn't get here")
  76. } catch TextFormatDecodingError.unknownField {
  77. // This is what should have happened.
  78. }
  79. var options = TextFormatEncodingOptions()
  80. options.printUnknownFields = false
  81. let textWithoutUnknowns = msg.textFormatString(options: options)
  82. XCTAssertEqual(textWithoutUnknowns, "")
  83. }
  84. func test_unknown_lengthDelimited_notmessage() throws {
  85. // Inner data is almost a message, but has an error at the end...
  86. // This should cause it to be displayed as a string.
  87. let bytes = Data([10, 6, 8, 1, 18, 3, 97, 98])
  88. let msg = try MessageTestType(serializedData: bytes)
  89. let text = msg.textFormatString()
  90. XCTAssertEqual(text, "1: \"\\b\\001\\022\\003ab\"\n")
  91. do {
  92. let _ = try MessageTestType(textFormatString: text)
  93. XCTFail("Shouldn't get here")
  94. } catch TextFormatDecodingError.unknownField {
  95. // This is what should have happened.
  96. }
  97. var options = TextFormatEncodingOptions()
  98. options.printUnknownFields = false
  99. let textWithoutUnknowns = msg.textFormatString(options: options)
  100. XCTAssertEqual(textWithoutUnknowns, "")
  101. }
  102. func test_unknown_lengthDelimited_nested_message() throws {
  103. let bytes = Data([8, 1, 18, 6, 8, 2, 18, 2, 8, 3])
  104. let msg = try MessageTestType(serializedData: bytes)
  105. let text = msg.textFormatString()
  106. XCTAssertEqual(text, "1: 1\n2 {\n 1: 2\n 2 {\n 1: 3\n }\n}\n")
  107. do {
  108. let _ = try MessageTestType(textFormatString: text)
  109. XCTFail("Shouldn't get here")
  110. } catch TextFormatDecodingError.unknownField {
  111. // This is what should have happened.
  112. }
  113. var options = TextFormatEncodingOptions()
  114. options.printUnknownFields = false
  115. let textWithoutUnknowns = msg.textFormatString(options: options)
  116. XCTAssertEqual(textWithoutUnknowns, "")
  117. }
  118. func test_unknown_lengthDelimited_nested_message_recursion_limits() throws {
  119. let kNestingDepth = 10000
  120. let kTag = FieldTag(fieldNumber: 1, wireFormat: .lengthDelimited)
  121. let kTagSize = Int32(kTag.encodedSize)
  122. var lengths: [Int32] = []
  123. lengths.reserveCapacity(kNestingDepth)
  124. lengths.append(0)
  125. for _ in 0..<(kNestingDepth - 1) {
  126. lengths.append(
  127. kTagSize + Int32(Varint.encodedSize(of: lengths.last!)) + lengths.last!)
  128. }
  129. var bytes = Data()
  130. for len in lengths.lazy.reversed() {
  131. bytes.appendStartField(tag: kTag)
  132. bytes.appendVarInt(value: len)
  133. }
  134. let msg = try MessageTestType(serializedData: bytes)
  135. let text = msg.textFormatString()
  136. // Internally, the limit is 10, so we'll get 10 objects and then a
  137. // string for the bytes.
  138. let expectedPrefix = "1 {\n 1 {\n 1 {\n 1 {\n 1 {\n 1 {\n 1 {\n 1 {\n 1 {\n 1 {\n 1: \""
  139. let expectedSuffix = "\"\n }\n }\n }\n }\n }\n }\n }\n }\n }\n}\n"
  140. XCTAssertTrue(text.hasPrefix(expectedPrefix))
  141. XCTAssertTrue(text.hasSuffix(expectedSuffix))
  142. do {
  143. let _ = try MessageTestType(textFormatString: text)
  144. XCTFail("Shouldn't get here")
  145. } catch TextFormatDecodingError.unknownField {
  146. // This is what should have happened.
  147. }
  148. var options = TextFormatEncodingOptions()
  149. options.printUnknownFields = false
  150. let textWithoutUnknowns = msg.textFormatString(options: options)
  151. XCTAssertEqual(textWithoutUnknowns, "")
  152. }
  153. func test_unknown_group() throws {
  154. let bytes = Data([8, 1, 19, 26, 2, 8, 1, 20])
  155. let msg = try MessageTestType(serializedData: bytes)
  156. let text = msg.textFormatString()
  157. XCTAssertEqual(text, "1: 1\n2 {\n 3 {\n 1: 1\n }\n}\n")
  158. do {
  159. let _ = try MessageTestType(textFormatString: text)
  160. XCTFail("Shouldn't get here")
  161. } catch TextFormatDecodingError.unknownField {
  162. // This is what should have happened.
  163. }
  164. var options = TextFormatEncodingOptions()
  165. options.printUnknownFields = false
  166. let textWithoutUnknowns = msg.textFormatString(options: options)
  167. XCTAssertEqual(textWithoutUnknowns, "")
  168. }
  169. func test_unknown_nested_group() throws {
  170. let bytes = Data([8, 1, 19, 26, 2, 8, 1, 35, 40, 7, 36, 20])
  171. let msg = try MessageTestType(serializedData: bytes)
  172. let text = msg.textFormatString()
  173. XCTAssertEqual(text, "1: 1\n2 {\n 3 {\n 1: 1\n }\n 4 {\n 5: 7\n }\n}\n")
  174. do {
  175. let _ = try MessageTestType(textFormatString: text)
  176. XCTFail("Shouldn't get here")
  177. } catch TextFormatDecodingError.unknownField {
  178. // This is what should have happened.
  179. }
  180. var options = TextFormatEncodingOptions()
  181. options.printUnknownFields = false
  182. let textWithoutUnknowns = msg.textFormatString(options: options)
  183. XCTAssertEqual(textWithoutUnknowns, "")
  184. }
  185. func test_unknown_nested_group_no_recursion_limits() throws {
  186. let kNestingDepth = 10000
  187. let kFieldNum = 1
  188. let kTagStart = FieldTag(fieldNumber: kFieldNum, wireFormat: .startGroup)
  189. let kTagEnd = FieldTag(fieldNumber: kFieldNum, wireFormat: .endGroup)
  190. var bytes = Data(capacity: kNestingDepth *
  191. (Varint.encodedSize(of: kTagStart.rawValue) + Varint.encodedSize(of: kTagEnd.rawValue)))
  192. for _ in 0..<kNestingDepth {
  193. bytes.appendStartField(tag: kTagStart)
  194. }
  195. for _ in 0..<kNestingDepth {
  196. bytes.appendStartField(tag: kTagEnd)
  197. }
  198. // If we try to parse `data`, the binary decode recursion budget will
  199. // come into play, instead directly add the data into the unknown fields
  200. // (via the @testable interface).
  201. var msg = MessageTestType()
  202. msg.unknownFields.append(protobufData: bytes)
  203. let text = msg.textFormatString()
  204. // Internally, groups don't use recursion, so this will be fully nested.
  205. var expected = ""
  206. var indent = ""
  207. for _ in 0..<kNestingDepth {
  208. expected.append(indent)
  209. expected.append("1 {\n")
  210. indent.append(" ")
  211. }
  212. for _ in 0..<kNestingDepth {
  213. indent.removeLast(2)
  214. expected.append(indent)
  215. expected.append("}\n")
  216. }
  217. XCTAssertEqual(text, expected)
  218. do {
  219. let _ = try MessageTestType(textFormatString: text)
  220. XCTFail("Shouldn't get here")
  221. } catch TextFormatDecodingError.unknownField {
  222. // This is what should have happened.
  223. }
  224. var options = TextFormatEncodingOptions()
  225. options.printUnknownFields = false
  226. let textWithoutUnknowns = msg.textFormatString(options: options)
  227. XCTAssertEqual(textWithoutUnknowns, "")
  228. }
  229. func test_unknown_fixed32() throws {
  230. let bytes = Data([13, 0, 1, 2, 3])
  231. let msg = try MessageTestType(serializedData: bytes)
  232. let text = msg.textFormatString()
  233. XCTAssertEqual(text, "1: 0x03020100\n")
  234. do {
  235. let _ = try MessageTestType(textFormatString: text)
  236. XCTFail("Shouldn't get here")
  237. } catch TextFormatDecodingError.unknownField {
  238. // This is what should have happened.
  239. }
  240. var options = TextFormatEncodingOptions()
  241. options.printUnknownFields = false
  242. let textWithoutUnknowns = msg.textFormatString(options: options)
  243. XCTAssertEqual(textWithoutUnknowns, "")
  244. }
  245. }