示例#1
0
func TestTokenMarshalUnmarshal(t *testing.T) {
	var fkey fernet.Key
	fkey.Generate()
	key := string(fkey.Encode())
	invalidKey := "invalidKey"

	var retInt int
	var testInt int
	testInt = 1024

	intResult, err := utils.TokenMarshal(testInt, invalidKey)
	assert.NotNil(t, err, "Fail to marshal int with invalid key")
	err = utils.TokenUnmarshal(string(intResult), key, &retInt)
	assert.NotNil(t, err, "Fail to unmarshal int with invalid key")

	intResult, err = utils.TokenMarshal(testInt, key)
	assert.Nil(t, err, "Fail to marshal int")
	err = utils.TokenUnmarshal(string(intResult), key, &retInt)
	assert.Nil(t, err, "Fail to unmarshal int")
	assert.Equal(t, testInt, retInt, "Fail to get the original int data")

	var retStr string
	var testStr string
	testStr = "hello, world"
	strResult, err := utils.TokenMarshal(testStr, key)
	assert.Nil(t, err, "Fail to marshal string")
	err = utils.TokenUnmarshal(string(strResult), key, &retStr)
	assert.Nil(t, err, "Fail to unmarshal string")
	assert.Equal(t, testStr, retStr, "Fail to get the original string data")
}
示例#2
0
func (t *ScanHookTask) Find(encodedCallbackID string) error {
	//TODO: update status and updatedAt
	var id int64
	err := utils.TokenUnmarshal(encodedCallbackID, setting.ScanKey, &id)

	return err
}
示例#3
0
func (t *ScanHookTask) UpdateResult(encodedCallbackID string, data []byte) error {
	var id int64
	err := utils.TokenUnmarshal(encodedCallbackID, setting.ScanKey, &id)
	if err != nil {
		return err
	}
	//TODO find task from db by id

	t.Result = string(data)
	t.Update("finish")

	return nil
}