Exemplo n.º 1
0
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)
	}
}
Exemplo n.º 2
0
func TestEncodeBitsString(t *testing.T) {
	for _, tc := range bitTests {
		s := zbase32.EncodeBitsToString(tc.decoded, tc.bits)
		if g, e := s, tc.encoded; g != e {
			t.Errorf("EncodeBitsToString %d bits of %x wrong result: %q != %q", tc.bits, tc.decoded, g, e)
			continue
		}
	}
}
Exemplo 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)
	}
}