1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
package zbase32_test
import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"testing"
"testing/quick"
"github.com/tv42/zbase32"
)
var python = flag.Bool("python", false, "run comparison tests against Python zbase32 library")
func calcBits(orig []byte, partial uint) int {
bits := 8 * uint(len(orig))
partial &= 7
if bits > partial {
bits -= partial
mask := ^(byte(1<<partial) - 1)
orig[len(orig)-1] &= mask
}
return int(bits)
}
func TestQuickRoundtripBits(t *testing.T) {
fn := func(orig []byte, partial uint) bool {
bits := calcBits(orig, partial)
encoded := zbase32.EncodeBitsToString(orig, bits)
decoded, err := zbase32.DecodeBitsString(encoded, bits)
if err != nil {
t.Logf("orig=\t%x", orig)
t.Logf("bits=\t%d", bits)
t.Logf("enc=\t%q", encoded)
t.Fatalf("encode-decode roundtrip gave error: %v", err)
}
if !bytes.Equal(orig, decoded) {
t.Logf("orig=\t%x", orig)
t.Logf("dec=\t%x", decoded)
t.Logf("bits=\t%d", bits)
t.Logf("enc=\t%q", encoded)
return false
}
return true
}
if err := quick.Check(fn, nil); err != nil {
t.Fatal(err)
}
}
func TestQuickRoundtripBytes(t *testing.T) {
fn := func(orig []byte) bool {
encoded := zbase32.EncodeToString(orig)
decoded, err := zbase32.DecodeString(encoded)
if err != nil {
t.Logf("orig=\t%x", orig)
t.Logf("enc=\t%q", encoded)
t.Fatalf("encode-decode roundtrip gave error: %v", err)
}
if !bytes.Equal(orig, decoded) {
t.Logf("orig=\t%x", orig)
t.Logf("dec=\t%x", decoded)
t.Logf("enc=\t%q", encoded)
return false
}
return true
}
if err := quick.Check(fn, nil); err != nil {
t.Fatal(err)
}
}
func TestQuickPythonEncodeBits(t *testing.T) {
if !*python {
t.Skip("Skipping, use -python to enable")
}
us := func(orig []byte, partial uint) (string, error) {
bits := calcBits(orig, partial)
encoded := zbase32.EncodeBitsToString(orig, bits)
return encoded, nil
}
them := func(orig []byte, partial uint) (string, error) {
// the python library raises IndexError on zero-length input
if len(orig) == 0 {
return "", nil
}
bits := calcBits(orig, partial)
cmd := exec.Command("python", "-c", `
import sys, zbase32
orig = sys.stdin.read()
bits = int(sys.argv[1])
sys.stdout.write(zbase32.b2a_l(orig, bits))
`,
strconv.Itoa(bits),
)
cmd.Stdin = bytes.NewReader(orig)
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("cannot run python: %v", err)
}
return string(output), nil
}
if err := quick.CheckEqual(us, them, nil); err != nil {
t.Fatal(err)
}
}
func TestQuickPythonDecodeBits(t *testing.T) {
if !*python {
t.Skip("Skipping, use -python to enable")
}
us := func(orig []byte, partial uint) ([]byte, error) {
bits := calcBits(orig, partial)
encoded := zbase32.EncodeBitsToString(orig, bits)
return zbase32.DecodeBitsString(encoded, bits)
}
them := func(orig []byte, partial uint) ([]byte, error) {
// the python library raises IndexError on zero-length input
if len(orig) == 0 {
return []byte{}, nil
}
bits := calcBits(orig, partial)
encoded := zbase32.EncodeBitsToString(orig, bits)
cmd := exec.Command("python", "-c", `
import sys, zbase32
enc = sys.stdin.read()
bits = int(sys.argv[1])
sys.stdout.write(zbase32.a2b_l(enc, bits))
`,
strconv.Itoa(bits),
)
cmd.Stdin = strings.NewReader(encoded)
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("cannot run python: %v", err)
}
return output, nil
}
if err := quick.CheckEqual(us, them, nil); err != nil {
t.Fatal(err)
}
}
func TestQuickPythonEncodeBytes(t *testing.T) {
if !*python {
t.Skip("Skipping, use -python to enable")
}
us := func(orig []byte) (string, error) {
encoded := zbase32.EncodeToString(orig)
return encoded, nil
}
them := func(orig []byte) (string, error) {
// the python library raises IndexError on zero-length input
if len(orig) == 0 {
return "", nil
}
cmd := exec.Command("python", "-c", `
import sys, zbase32
orig = sys.stdin.read()
sys.stdout.write(zbase32.b2a(orig))
`)
cmd.Stdin = bytes.NewReader(orig)
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("cannot run python: %v", err)
}
return string(output), nil
}
if err := quick.CheckEqual(us, them, nil); err != nil {
t.Fatal(err)
}
}
func TestQuickPythonDecodeBytes(t *testing.T) {
if !*python {
t.Skip("Skipping, use -python to enable")
}
us := func(orig []byte) ([]byte, error) {
encoded := zbase32.EncodeToString(orig)
return zbase32.DecodeString(encoded)
}
them := func(orig []byte) ([]byte, error) {
// the python library raises IndexError on zero-length input
if len(orig) == 0 {
return []byte{}, nil
}
encoded := zbase32.EncodeToString(orig)
cmd := exec.Command("python", "-c", `
import sys, zbase32
enc = sys.stdin.read()
sys.stdout.write(zbase32.a2b(enc))
`)
cmd.Stdin = strings.NewReader(encoded)
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("cannot run python: %v", err)
}
return output, nil
}
if err := quick.CheckEqual(us, them, nil); err != nil {
t.Fatal(err)
}
}
|