Esempio n. 1
0
func TestDecodeBitsString(t *testing.T) {
	for _, tc := range bitTests {
		dec, err := zbase32.DecodeBitsString(tc.encoded, tc.bits)
		if err != nil {
			t.Errorf("DecodeBits %d bits from %q: error: %v", tc.bits, tc.encoded, err)
			continue
		}
		if g, e := dec, tc.decoded; !bytes.Equal(g, e) {
			t.Errorf("DecodeBits %d bits from %q, %x != %x", tc.bits, tc.encoded, g, e)
		}
	}
}
Esempio n. 2
0
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)
	}
}
Esempio n. 3
0
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)
	}
}