github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/communication/codec_bytes_test.go (about) 1 /* 2 * Copyright (C) 2017 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package communication 19 20 import ( 21 "testing" 22 23 "github.com/mysteriumnetwork/node/testkit/assertkit" 24 "github.com/stretchr/testify/assert" 25 ) 26 27 var _ Codec = &codecBytes{} 28 29 func TestCodecBytesPack(t *testing.T) { 30 table := []struct { 31 payload interface{} 32 expectedData []byte 33 expectedErrorMsg string 34 }{ 35 {`hello`, []byte(`hello`), ""}, 36 {`hello "name"`, []byte(`hello "name"`), ""}, 37 {nil, []byte{}, ""}, 38 {true, []byte{}, "can't pack payload: true"}, 39 } 40 41 codec := codecBytes{} 42 for _, tt := range table { 43 data, err := codec.Pack(tt.payload) 44 45 assert.Exactly(t, tt.expectedData, data) 46 assertkit.EqualOptionalError(t, err, tt.expectedErrorMsg) 47 } 48 } 49 50 func TestCodecBytesUnpackToString(t *testing.T) { 51 codec := codecBytes{} 52 53 var payload string 54 err := codec.Unpack([]byte(`hello`), &payload) 55 56 assert.Error(t, err) 57 assert.EqualError(t, err, "can't unpack to payload: *string") 58 } 59 60 func TestCodecBytesUnpackToByte(t *testing.T) { 61 codec := codecBytes{} 62 63 var payload []byte 64 err := codec.Unpack([]byte(`hello`), payload) 65 66 assert.Error(t, err) 67 assert.EqualError(t, err, "can't unpack to payload: []uint8") 68 } 69 70 func TestCodecBytesUnpack(t *testing.T) { 71 table := []struct { 72 data []byte 73 expectedPayload []byte 74 }{ 75 {[]byte(`hello`), []byte(`hello`)}, 76 {[]byte(`hello "name"`), []byte(`hello "name"`)}, 77 {[]byte{}, []byte{}}, 78 } 79 80 codec := codecBytes{} 81 for _, tt := range table { 82 var payload []byte 83 err := codec.Unpack(tt.data, &payload) 84 85 assert.NoError(t, err) 86 assert.Exactly(t, tt.expectedPayload, payload) 87 } 88 }