|
| 1 | +// Copyright (C) 2019-2022 Algorand, Inc. |
| 2 | +// This file is part of go-algorand |
| 3 | +// |
| 4 | +// go-algorand is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as |
| 6 | +// published by the Free Software Foundation, either version 3 of the |
| 7 | +// License, or (at your option) any later version. |
| 8 | +// |
| 9 | +// go-algorand 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 Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with go-algorand. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package transactions_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/algorand/go-algorand/data/basics" |
| 24 | + "github.com/algorand/go-algorand/data/transactions" |
| 25 | + "github.com/algorand/go-algorand/data/txntest" |
| 26 | + "github.com/algorand/go-algorand/protocol" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | +) |
| 29 | + |
| 30 | +func decode(data string, v interface{}) error { |
| 31 | + err := protocol.DecodeJSON([]byte(data), v) |
| 32 | + if err != nil { |
| 33 | + panic(err) |
| 34 | + } |
| 35 | + return err |
| 36 | +} |
| 37 | + |
| 38 | +func compact(data []byte) string { |
| 39 | + return strings.ReplaceAll(strings.ReplaceAll(string(data), " ", ""), "\n", "") |
| 40 | +} |
| 41 | + |
| 42 | +// TestJsonMarshal ensures that BoxRef names are b64 encoded, since they may not be characters. |
| 43 | +func TestJsonMarshal(t *testing.T) { |
| 44 | + marshal := protocol.EncodeJSON(transactions.BoxRef{Index: 4, Name: "joe"}) |
| 45 | + require.Equal(t, `{"i":4,"n":"am9l"}`, compact(marshal)) |
| 46 | + |
| 47 | + marshal = protocol.EncodeJSON(transactions.BoxRef{Index: 0, Name: "joe"}) |
| 48 | + require.Equal(t, `{"n":"am9l"}`, compact(marshal)) |
| 49 | + |
| 50 | + marshal = protocol.EncodeJSON(transactions.BoxRef{Index: 1, Name: ""}) |
| 51 | + require.Equal(t, `{"i":1}`, compact(marshal)) |
| 52 | + |
| 53 | + marshal = protocol.EncodeJSON(transactions.BoxRef{Index: 0, Name: ""}) |
| 54 | + require.Equal(t, `{}`, compact(marshal)) |
| 55 | +} |
| 56 | + |
| 57 | +// TestJsonUnmarshal ensures that BoxRef unmarshaling expects b64 names |
| 58 | +func TestJsonUnmarshal(t *testing.T) { |
| 59 | + var br transactions.BoxRef |
| 60 | + |
| 61 | + decode(`{"i":4,"n":"am9l"}`, &br) |
| 62 | + require.Equal(t, transactions.BoxRef{Index: 4, Name: "joe"}, br) |
| 63 | + |
| 64 | + decode(`{"n":"am9l"}`, &br) |
| 65 | + require.Equal(t, transactions.BoxRef{Index: 0, Name: "joe"}, br) |
| 66 | + |
| 67 | + decode(`{"i":4}`, &br) |
| 68 | + require.Equal(t, transactions.BoxRef{Index: 4, Name: ""}, br) |
| 69 | + |
| 70 | + decode(`{}`, &br) |
| 71 | + require.Equal(t, transactions.BoxRef{Index: 0, Name: ""}, br) |
| 72 | +} |
| 73 | + |
| 74 | +// TestTxnJson tests a few more things about how our Transactions get JSON |
| 75 | +// encoded. These things could change without breaking the protocol, should stay |
| 76 | +// the same for the sake of REST API compatibility. |
| 77 | +func TestTxnJson(t *testing.T) { |
| 78 | + txn := txntest.Txn{ |
| 79 | + Sender: basics.Address{0x01, 0x02, 0x03}, |
| 80 | + } |
| 81 | + marshal := protocol.EncodeJSON(txn.Txn()) |
| 82 | + require.Contains(t, compact(marshal), `"snd":"AEBA`) |
| 83 | + |
| 84 | + txn = txntest.Txn{ |
| 85 | + Boxes: []transactions.BoxRef{{Index: 3, Name: "john"}}, |
| 86 | + } |
| 87 | + marshal = protocol.EncodeJSON(txn.Txn()) |
| 88 | + require.Contains(t, compact(marshal), `"apbx":[{"i":3,"n":"am9obg=="}]`) |
| 89 | + |
| 90 | + marshal = protocol.EncodeJSON(txn.Txn()) |
| 91 | +} |
0 commit comments