Example #1
0
// Tests tokenization
func BenchmarkRoundTrip(b *testing.B) {
	b.StopTimer()
	log.SetFlags(log.Ltime | log.Lshortfile)
	session, err := mgo.Dial("localhost")
	if err != nil {
		b.Fatal("Could not connect to MongoDB:", err)
	}
	db := session.DB("test_gokenizer_tokenizer")
	err = db.DropDatabase()
	if err != nil {
		b.Fatal("Could not drop test db:", err)
	}
	tokenizer := NewMongoTokenizer(db)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		orig := goutil.RandAlphanumeric(8, 8)
		token, err := tokenizer.Tokenize(orig)
		if err != nil {
			b.Error("Tokenize error:", err)
		}
		var detok string // Result of detokenization - should be same as orig
		detok, err = tokenizer.Detokenize(token)
		if err != nil {
			b.Error("Detokenize error:", err)
		}
		if detok != orig {
			msg := "Detokenization failed: '%s' != '%s'."
			msg = fmt.Sprintf(msg, orig, detok)
			b.Error(msg)
		}
	}
}
Example #2
0
// Tests tokenization
func TestTokenize(t *testing.T) {
	go runServer(t)
	var err error
	//
	// Prepare some random data
	//
	reqid := goutil.RandAlphanumeric(8, 8)
	origData := make(map[string]string)
	for i := 0; i < 10; i++ {
		fieldname := goutil.RandAlphanumeric(8, 8)
		field := goutil.RandAlphanumeric(8, 8)
		origData[fieldname] = field
	}
	//
	// Setup API connection
	//
	ws := getWebsocket(t)
	dec := json.NewDecoder(ws)
	enc := json.NewEncoder(ws)
	//
	// Tokenize
	//
	req := JsonTokenizeRequest{
		ReqId: reqid,
		Data:  origData,
	}
	if err = enc.Encode(req); err != nil {
		t.Fatal(err)
	}
	var resp TokenizeReponse
	if err = dec.Decode(&resp); err != nil {
		t.Fatal(err)
	}
	// Since the FakeTokenizer returns the original string as the token string,
	// we can easily check whether the API is properly handling our request.
	for field, orig := range origData {
		token := resp.Data[field]
		if orig != token {
			msg := fmt.Sprintf("Tokenization failure: %s != %s", orig, token)
			t.Error(msg)
		}
	}
	//
	// Detokenize
	//
}
Example #3
0
// Tests tokenization
func TestRoundTrip(t *testing.T) {
	log.SetFlags(log.Ltime | log.Lshortfile)
	var token string
	log.Println("Connecting to MongoDB...")
	session, err := mgo.Dial("localhost")
	if err != nil {
		t.Fatal("Could not connect to MongoDB:", err)
	}
	db := session.DB("test_gokenizer_tokenizer")
	err = db.DropDatabase()
	if err != nil {
		t.Fatal(err)
	}
	tokenizer := NewMongoTokenizer(db)
	orig := goutil.RandAlphanumeric(8, 8)
	token, err = tokenizer.Tokenize(orig)
	if err != nil {
		t.Error("Tokenize error:", err)
	}
	var repeat string
	repeat, err = tokenizer.Tokenize(orig)
	if err != nil {
		t.Error("Tokenize error:", err)
	}
	if repeat != token {
		t.Error("Got a different token on second try:", orig, token, repeat)
	}
	var detok string // Result of detokenization - should be same as orig
	detok, err = tokenizer.Detokenize(token)
	if err != nil {
		t.Error("Detokenize error:", err)
	}
	if detok != orig {
		msg := "Detokenization failed: '%s' != '%s'."
		msg = fmt.Sprintf(msg, orig, detok)
		t.Error(msg)
	}
}