Beispiel #1
0
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)
	}
}
Beispiel #2
0
func prettyPrintObject(o otm.Object, log *testing.T) {
	parent := ""
	for x := o.Parent(); !fn.IsNil(x); x = x.Parent() {
		parent = fmt.Sprint(x.Qualident(), "<-", parent)
	}
	log.Log(parent, o.Qualident())
	if o.ChildrenCount() > 0 {
		log.Log(":")
		for _x := range o.Children() {
			switch x := _x.(type) {
			case otm.Object:
				prettyPrintObject(x, log)
			case otm.Link:
				log.Log("@", x.Object().Qualident())
			case string, float64, int64, rune, tri.Trit:
				log.Log(_x)
			case []uint8:
				s := zbase32.EncodeToString(x)
				log.Log("zbase32(", s, ")", x)
			default:
				halt.As(100, reflect.TypeOf(x))
			}
		}
		log.Log(";")
	}
}
Beispiel #3
0
Datei: g.go Projekt: kpmy/ot
func (f *fm) object(o otm.Object) {
	q := o.Qualident()
	if q.Template != "" {
		f.Ident(q.Template)
		f.Char('~')
	}
	f.Ident(q.Class)
	if q.Identifier != "" {
		f.Char('(')
		f.Ident(q.Identifier)
		f.Char(')')
	}
	if o.ChildrenCount() > 0 {
		f.Char(':')
		for _x := range o.Children() {
			f.Char(' ')
			switch x := _x.(type) {
			case otm.Object:
				f.object(x)
			case string:
				f.stringValue(x)
			case int64:
				i := strconv.Itoa(int(x))
				f.RawString(i)
			case float64:
				if math.IsInf(x, 1) {
					f.RawString("inf")
				} else if math.IsInf(x, -1) {
					f.RawString("-inf")
				} else {
					f_ := strconv.FormatFloat(x, 'f', 8, 64)
					f.RawString(f_)
				}
			case rune:
				r := strconv.FormatUint(uint64(x), 16)
				f.RawString("0" + r + "U")
			case tri.Trit:
				f.Trinary(x)
			case []uint8:
				f.RawString(zbase32.EncodeToString(x))
			default:
				halt.As(100, reflect.TypeOf(x))
			}
		}
		f.Char(';')
	} else {
		f.Char(' ')
	}
}
Beispiel #4
0
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)
	}
}
Beispiel #5
0
func Example() {
	s := zbase32.EncodeToString([]byte{240, 191, 199})
	fmt.Println(s)
	// Output:
	// 6n9hq
}
Beispiel #6
0
// hashes a given name using a truncated sha1 hash
// 5 characters extracted from the zbase32 encoded hash provides
// enough entropy to avoid collisions
// zbase32: http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt
// is used to promote human-readable names
func hashString(s string) string {
	hash := sha1.Sum([]byte(s))
	return zbase32.EncodeToString(hash[:])[:5]
}