github.com/gogo/protobuf@v1.3.2/test/issue449/issue449_test.go (about) 1 // Protocol Buffers for Go with Gadgets 2 // 3 // Copyright (c) 2018, 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 29 package issue449 30 31 import ( 32 "testing" 33 34 "github.com/gogo/protobuf/proto" 35 ) 36 37 func TestCodeGenMsgMarshalUnmarshal(t *testing.T) { 38 src := &CodeGenMsg{ 39 Int64ReqPtr: proto.Int64(111), 40 Int32OptPtr: proto.Int32(222), 41 Int64Req: 333, 42 Int32Opt: 444, 43 } 44 buf, err := proto.Marshal(src) 45 if err != nil { 46 t.Fatal(err) 47 } 48 dst := &CodeGenMsg{} 49 if err := proto.Unmarshal(buf, dst); err != nil { 50 t.Fatal(err) 51 } 52 if !src.Equal(dst) { 53 t.Fatal("message is not equals") 54 } 55 } 56 57 func TestNonCodeGenMsgMarshalUnmarshal(t *testing.T) { 58 src := &NonCodeGenMsg{ 59 Int64ReqPtr: proto.Int64(111), 60 Int32OptPtr: proto.Int32(222), 61 Int64Req: 333, 62 Int32Opt: 444, 63 } 64 buf, err := proto.Marshal(src) 65 if err != nil { 66 t.Fatal(err) 67 } 68 dst := &NonCodeGenMsg{} 69 if err := proto.Unmarshal(buf, dst); err != nil { 70 t.Fatal(err) 71 } 72 if !src.Equal(dst) { 73 t.Fatal("message is not equals") 74 } 75 } 76 77 func TestRequiredFieldCheck(t *testing.T) { 78 tbl := []struct { 79 In proto.Message 80 Success bool 81 }{ 82 // Generated Code Message 83 { 84 // filled message 85 In: &CodeGenMsg{ 86 Int64ReqPtr: proto.Int64(111), 87 Int32OptPtr: proto.Int32(222), 88 Int64Req: 333, 89 Int32Opt: 444, 90 }, 91 Success: true, 92 }, 93 { 94 // filled message (set zero value) 95 In: &CodeGenMsg{ 96 Int64ReqPtr: proto.Int64(0), 97 Int32OptPtr: proto.Int32(0), 98 Int64Req: 0, 99 Int32Opt: 0, 100 }, 101 Success: true, 102 }, 103 { 104 // non filled message (Int64ReqPtr is not set) 105 In: &CodeGenMsg{ 106 Int64Req: 333, 107 }, 108 Success: false, 109 }, 110 { 111 // non filled message (Int64Req is not set, but can't inspect) 112 In: &CodeGenMsg{ 113 Int64ReqPtr: proto.Int64(111), 114 }, 115 Success: true, 116 }, 117 118 // Non Generated Code Message 119 { 120 // filled message 121 In: &NonCodeGenMsg{ 122 Int64ReqPtr: proto.Int64(111), 123 Int32OptPtr: proto.Int32(222), 124 Int64Req: 333, 125 Int32Opt: 444, 126 }, 127 Success: true, 128 }, 129 { 130 // filled message (set zero value) 131 In: &NonCodeGenMsg{ 132 Int64ReqPtr: proto.Int64(0), 133 Int32OptPtr: proto.Int32(0), 134 Int64Req: 0, 135 Int32Opt: 0, 136 }, 137 Success: true, 138 }, 139 { 140 // non filled message (Int64ReqPtr is not set) 141 In: &NonCodeGenMsg{ 142 Int64Req: 333, 143 }, 144 Success: false, 145 }, 146 { 147 // non filled message (Int64Req is not set, but can't inspect) 148 In: &NonCodeGenMsg{ 149 Int64ReqPtr: proto.Int64(111), 150 }, 151 Success: true, 152 }, 153 } 154 for i, v := range tbl { 155 _, err := proto.Marshal(v.In) 156 switch v.Success { 157 case true: 158 if err != nil { 159 t.Fatalf("[%d] failed to proto.Marshal(%v), %s", i, v.In, err) 160 } 161 case false: 162 if err == nil { 163 t.Fatalf("[%d] required field check is not working", i) 164 } 165 if _, ok := err.(*proto.RequiredNotSetError); !ok { 166 t.Fatalf("[%d] failed to proto.Marshal(%v), %s", i, v.In, err) 167 } 168 } 169 } 170 }