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")
}
Example #2
0
// AppActiveScanHooksTaskV1Handler actives a scan task
func AppActiveScanHooksTaskV1Handler(ctx *macaron.Context) (int, []byte) {
	namespace := ctx.Params(":namespace")
	repository := ctx.Params(":repository")

	var r models.ScanHookRegist
	rID, err := r.FindID("appv1", namespace, repository)
	if err != nil {
		log.Errorf("[%s] scan hook callback error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Donnot have registed scan plugin"})
		return http.StatusBadRequest, result
	}

	a := models.ArtifactV1{
		OS:   ctx.Params(":os"),
		Arch: ctx.Params(":arch"),
		App:  ctx.Params(":app"),
		Tag:  ctx.Params(":tag"),
	}
	a, err = a.Get()
	if err != nil {
		log.Errorf("[%s] scan hook callback error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Cannot find artifactv1"})
		return http.StatusBadRequest, result
	}

	// create a task
	var t models.ScanHookTask
	tID, err := t.Put(rID, a.Path)
	if err != nil {
		log.Errorf("[%s] scan hook callback error: %s", ctx.Req.RequestURI, err.Error())

		result, _ := json.Marshal(map[string]string{"Error": "Fail to create a scan task"})
		return http.StatusBadRequest, result
	}

	idBytes, err := utils.TokenMarshal(tID, setting.ScanKey)

	val := struct {
		TaskID string
	}{TaskID: string(idBytes)}

	return httpRet("AppV1 Active Scan Hook Task", val, nil)
}