encoding_test.go (3325B)
1 // This file is part of taler-cashless2ecash. 2 // Copyright (C) 2024 Joel Häberli 3 // 4 // taler-cashless2ecash is free software: you can redistribute it and/or modify it 5 // under the terms of the GNU Affero General Public License as published 6 // by the Free Software Foundation, either version 3 of the License, 7 // or (at your option) any later version. 8 // 9 // taler-cashless2ecash is distributed in the hope that it will be useful, but 10 // WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 // 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 this program. If not, see <http://www.gnu.org/licenses/>. 16 // 17 // SPDX-License-Identifier: AGPL3.0-or-later 18 19 package internal_utils 20 21 import ( 22 "crypto/rand" 23 "testing" 24 ) 25 26 func TestWopidEncodeDecode(t *testing.T) { 27 28 wopid := make([]byte, 32) 29 n, err := rand.Read(wopid) 30 if err != nil || n != 32 { 31 t.Log("failed because retrieving random 32 bytes failed") 32 t.FailNow() 33 } 34 35 encodedWopid := FormatWopid(wopid) 36 t.Log("encoded wopid:", encodedWopid) 37 decodedWopid, err := ParseWopid(encodedWopid) 38 if err != nil { 39 t.Error(err) 40 t.FailNow() 41 } 42 43 if len(decodedWopid) != len(wopid) { 44 t.Log("uneven length.", len(decodedWopid), "!=", len(wopid)) 45 t.FailNow() 46 } 47 48 for i, b := range wopid { 49 50 if b != decodedWopid[i] { 51 t.Log("unequal at position", i) 52 t.FailNow() 53 } 54 } 55 } 56 57 func TestTalerBase32(t *testing.T) { 58 59 input := []byte("This is some text") 60 t.Log("in:", string(input)) 61 t.Log("in:", input) 62 encoded := TalerBinaryEncode(input) 63 t.Log("encoded:", encoded) 64 out, err := TalerBinaryDecode(encoded) 65 if err != nil { 66 t.Error(err) 67 t.FailNow() 68 } 69 t.Log("decoded:", out) 70 t.Log("decoded:", string(out)) 71 72 if len(out) != len(input) { 73 t.Log("uneven length.", len(out), "!=", len(input)) 74 t.FailNow() 75 } 76 77 for i, b := range input { 78 79 if b != out[i] { 80 t.Log("unequal at position", i) 81 t.FailNow() 82 } 83 } 84 } 85 86 func TestTalerBase32Rand32(t *testing.T) { 87 88 input := make([]byte, 32) 89 n, err := rand.Read(input) 90 if err != nil || n != 32 { 91 t.Log("failed because retrieving random 32 bytes failed") 92 t.FailNow() 93 } 94 95 t.Log("in:", input) 96 encoded := TalerBinaryEncode(input) 97 t.Log("encoded:", encoded) 98 out, err := TalerBinaryDecode(encoded) 99 if err != nil { 100 t.Error(err) 101 t.FailNow() 102 } 103 t.Log("decoded:", out) 104 t.Log("decoded:", string(out)) 105 106 if len(out) != len(input) { 107 t.Log("uneven length.", len(out), "!=", len(input)) 108 t.FailNow() 109 } 110 111 for i, b := range input { 112 113 if b != out[i] { 114 t.Log("unequal at position", i) 115 t.FailNow() 116 } 117 } 118 } 119 120 func TestTalerBase32Rand64(t *testing.T) { 121 122 input := make([]byte, 64) 123 n, err := rand.Read(input) 124 if err != nil || n != 64 { 125 t.Log("failed because retrieving random 64 bytes failed") 126 t.FailNow() 127 } 128 129 t.Log("in:", input) 130 encoded := TalerBinaryEncode(input) 131 t.Log("encoded:", encoded) 132 out, err := TalerBinaryDecode(encoded) 133 if err != nil { 134 t.Error(err) 135 t.FailNow() 136 } 137 t.Log("decoded:", out) 138 t.Log("decoded:", string(out)) 139 140 if len(out) != len(input) { 141 t.Log("uneven length.", len(out), "!=", len(input)) 142 t.FailNow() 143 } 144 145 for i, b := range input { 146 147 if b != out[i] { 148 t.Log("unequal at position", i) 149 t.FailNow() 150 } 151 } 152 }