ProtobufCodingTests.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2024, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import GRPCCore
  17. import GRPCProtobuf
  18. import SwiftProtobuf
  19. import XCTest
  20. final class ProtobufCodingTests: XCTestCase {
  21. func testSerializeDeserializeRoundtrip() throws {
  22. let message = Google_Protobuf_Timestamp.with {
  23. $0.seconds = 4
  24. }
  25. let serializer = ProtobufSerializer<Google_Protobuf_Timestamp>()
  26. let deserializer = ProtobufDeserializer<Google_Protobuf_Timestamp>()
  27. let bytes = try serializer.serialize(message)
  28. let roundTrip = try deserializer.deserialize(bytes)
  29. XCTAssertEqual(roundTrip, message)
  30. }
  31. func testSerializerError() throws {
  32. let message = TestMessage()
  33. let serializer = ProtobufSerializer<TestMessage>()
  34. XCTAssertThrowsError(
  35. try serializer.serialize(message)
  36. ) { error in
  37. XCTAssertEqual(
  38. error as? RPCError,
  39. RPCError(
  40. code: .invalidArgument,
  41. message:
  42. """
  43. Can't serialize message of type TestMessage.
  44. """
  45. )
  46. )
  47. }
  48. }
  49. func testDeserializerError() throws {
  50. let bytes = Array("%%%%%££££".utf8)
  51. let deserializer = ProtobufDeserializer<TestMessage>()
  52. XCTAssertThrowsError(
  53. try deserializer.deserialize(bytes)
  54. ) { error in
  55. XCTAssertEqual(
  56. error as? RPCError,
  57. RPCError(
  58. code: .invalidArgument,
  59. message:
  60. """
  61. Can't deserialize to message of type TestMessage
  62. """
  63. )
  64. )
  65. }
  66. }
  67. }
  68. struct TestMessage: SwiftProtobuf.Message {
  69. var text: String = ""
  70. var unknownFields = SwiftProtobuf.UnknownStorage()
  71. static let protoMessageName: String = "Test.ServiceRequest"
  72. init() {}
  73. mutating func decodeMessage<D>(decoder: inout D) throws where D: SwiftProtobuf.Decoder {
  74. throw RPCError(code: .internalError, message: "Decoding error")
  75. }
  76. func traverse<V>(visitor: inout V) throws where V: SwiftProtobuf.Visitor {
  77. throw RPCError(code: .internalError, message: "Traversing error")
  78. }
  79. public var isInitialized: Bool {
  80. if self.text.isEmpty { return false }
  81. return true
  82. }
  83. func isEqualTo(message: any SwiftProtobuf.Message) -> Bool {
  84. return false
  85. }
  86. }