map_test.go.in 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Protocol Buffers for Go with Gadgets
  2. //
  3. // Copyright (c) 2017, The GoGo Authors. All rights reserved.
  4. // http://github.com/gogo/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. package mapdefaults
  29. import (
  30. "testing"
  31. "github.com/gogo/protobuf/proto"
  32. )
  33. func TestUnmarshalImplicitDefaultKeyValue1(t *testing.T) {
  34. fm := &FakeMap{
  35. Entries: []*FakeMapEntry{
  36. &FakeMapEntry{
  37. Key: "foo",
  38. Value: "",
  39. },
  40. &FakeMapEntry{
  41. Key: "",
  42. Value: "bar",
  43. },
  44. &FakeMapEntry{
  45. Key: "as",
  46. Value: "df",
  47. },
  48. },
  49. }
  50. serializedMsg, err := proto.Marshal(fm)
  51. if err != nil {
  52. t.Fatalf("Failed to serialize msg: %s", err)
  53. }
  54. msg := MapTest{}
  55. err = proto.Unmarshal(serializedMsg, &msg)
  56. if err != nil {
  57. t.Fatalf("Unexpected error: %s", err)
  58. }
  59. strStr := msg.StrStr
  60. if len(strStr) != 3 {
  61. t.Fatal("StrStr map should have 3 key/value pairs")
  62. }
  63. val, ok := strStr["foo"]
  64. if !ok {
  65. t.Fatal("\"foo\" not found in StrStr map.")
  66. }
  67. if val != "" {
  68. t.Fatalf("Unexpected value for \"foo\": %s", val)
  69. }
  70. val, ok = strStr[""]
  71. if !ok {
  72. t.Fatal("\"\" not found in StrStr map.")
  73. }
  74. if val != "bar" {
  75. t.Fatalf("Unexpected value for \"\": %s", val)
  76. }
  77. val, ok = strStr["as"]
  78. if !ok {
  79. t.Fatal("\"as\" not found in StrStr map.")
  80. }
  81. if val != "df" {
  82. t.Fatalf("Unexpected value for \"as\": %s", val)
  83. }
  84. }
  85. func TestUnmarshalImplicitDefaultKeyValue2(t *testing.T) {
  86. fm := &FakeMap{
  87. Entries: []*FakeMapEntry{
  88. &FakeMapEntry{
  89. Key: "",
  90. Value: "",
  91. },
  92. },
  93. }
  94. serializedMsg, err := proto.Marshal(fm)
  95. if err != nil {
  96. t.Fatalf("Failed to serialize msg: %s", err)
  97. }
  98. // Sanity check
  99. if string(serializedMsg) != "\n\x00" {
  100. t.Fatal("Serialized bytes mismatched")
  101. }
  102. msg := MapTest{}
  103. err = proto.Unmarshal(serializedMsg, &msg)
  104. if err != nil {
  105. t.Fatalf("Unexpected error: %s", err)
  106. }
  107. strStr := msg.StrStr
  108. if len(strStr) != 1 {
  109. t.Fatal("StrStr map should have 1 key/value pairs")
  110. }
  111. val, ok := strStr[""]
  112. if !ok {
  113. t.Fatal("\"\" not found in StrStr map.")
  114. }
  115. if val != "" {
  116. t.Fatalf("Unexpected value for \"\": %s", val)
  117. }
  118. }
  119. func TestUnmarshalIgnoreUnknownField(t *testing.T) {
  120. fm := &FakeMap{
  121. Entries: []*FakeMapEntry{
  122. &FakeMapEntry{
  123. Key: "key",
  124. Value: "value",
  125. Other: "other",
  126. },
  127. },
  128. }
  129. serializedMsg, err := proto.Marshal(fm)
  130. if err != nil {
  131. t.Fatalf("Failed to serialize msg: %s", err)
  132. }
  133. msg := &MapTest{}
  134. err = proto.Unmarshal(serializedMsg, msg)
  135. if err != nil {
  136. var pb proto.Message = msg
  137. _, ok := pb.(proto.Unmarshaler)
  138. if !ok {
  139. // non-codegen implementation returns error when extra tags are
  140. // present.
  141. return
  142. }
  143. t.Fatalf("Unexpected error: %s", err)
  144. }
  145. strStr := msg.StrStr
  146. if len(strStr) != 1 {
  147. t.Fatal("StrStr map should have 1 key/value pairs")
  148. }
  149. val, ok := strStr["key"]
  150. if !ok {
  151. t.Fatal("\"key\" not found in StrStr map.")
  152. }
  153. if val != "value" {
  154. t.Fatalf("Unexpected value for \"value\": %s", val)
  155. }
  156. }