// TestSHA256Verify
func TestSHA256Verify(t *testing.T) {
	_, path, _, _ := runtime.Caller(0)
	dir := filepath.Join(filepath.Dir(path), "testdata")

	testPubFile := filepath.Join(dir, "rsa_public_key.pem")
	testContentFile := filepath.Join(dir, "hello.txt")
	testSignFile := filepath.Join(dir, "hello.sig")

	pubBytes, _ := ioutil.ReadFile(testPubFile)
	signBytes, _ := ioutil.ReadFile(testSignFile)
	contentBytes, _ := ioutil.ReadFile(testContentFile)
	err := utils.SHA256Verify(pubBytes, contentBytes, signBytes)
	assert.Nil(t, err, "Fail to verify valid signed data")
	err = utils.SHA256Verify(pubBytes, []byte("Invalid content data"), signBytes)
	assert.NotNil(t, err, "Fail to verify invalid signed data")
}
// TestOper tests add/get/getmeta/getmetasign/list
func TestOper(t *testing.T) {
	var appV1Repo client.UpdateClientAppV1Repo

	validURL := getTestURL()

	// Skip the test if the testing enviornment is not ready
	if validURL == "" {
		fmt.Printf("Skip the '%s' test since the testing enviornment is not ready.\n", "List")
		return
	}

	testFiles := []string{"osA/archA/appA", "osB/archB/appB"}

	f, _ := appV1Repo.New(validURL)
	defer func() {
		for _, tf := range testFiles {
			err := f.Delete(tf)
			assert.Nil(t, err, "Fail to delete file")
		}
	}()

	// Init the data and also test the put function
	_, path, _, _ := runtime.Caller(0)
	for _, tf := range testFiles {
		file := filepath.Join(filepath.Dir(path), "testdata", tf)
		content, _ := ioutil.ReadFile(file)
		err := f.Put(tf, content, utils.EncryptNone)
		assert.Nil(t, err, "Fail to put file")
	}

	// Test list
	l, err := f.List()
	assert.Nil(t, err, "Fail to list")
	assert.Equal(t, len(l), 2, "Fail to list or something wrong in put")
	ok := (l[0] == testFiles[0] && l[1] == testFiles[1]) || (l[0] == testFiles[1] && l[1] == testFiles[0])
	assert.Equal(t, true, ok, "Fail to list the correct data")

	// Test get file
	fileBytes, err := f.GetFile(testFiles[0])
	assert.Nil(t, err, "Fail to get file")
	expectedBytes, _ := ioutil.ReadFile(filepath.Join(filepath.Dir(path), "testdata", testFiles[0]))
	assert.Equal(t, fileBytes, expectedBytes, "Fail to get the correct data")

	// Test get meta
	metaBytes, err := f.GetMeta()
	assert.Nil(t, err, "Fail to get meta file")

	// Test get metasign
	signBytes, err := f.GetMetaSign()
	assert.Nil(t, err, "Fail to get meta signature file")

	// Test get public key
	pubkeyBytes, err := f.GetPublicKey()
	assert.Nil(t, err, "Fail to get public key file")

	// VIP: Verify meta/sign with public to make real sure that everything works perfect
	err = utils.SHA256Verify(pubkeyBytes, metaBytes, signBytes)
	assert.Nil(t, err, "Fail to verify the meta data")
}
Beispiel #3
0
// Update the meta data, meta sign, public key
func (uc *UpdateClient) Update(repoURL string) error {
	repo, err := client.NewUCRepo(repoURL)
	if err != nil {
		return err
	}

	needUpdate := false
	remoteMetaBytes, err := repo.GetMeta()
	if err != nil {
		return err
	}

	localMetaFile := filepath.Join(uc.getCacheDir(), repo.NRString(), defaultMeta)
	localMetaBytes, err := ioutil.ReadFile(localMetaFile)
	if err != nil {
		needUpdate = true
	} else {
		var remoteMeta utils.Meta
		var localMeta utils.Meta
		json.Unmarshal(remoteMetaBytes, &remoteMeta)
		json.Unmarshal(localMetaBytes, &localMeta)
		needUpdate = localMeta.Before(remoteMeta)
	}

	if !needUpdate {
		fmt.Println("Already updated to the latest version.")
		return nil
	}

	uc.save(repo, defaultMeta, remoteMetaBytes)
	signBytes, _ := repo.GetMetaSign()
	pubBytes, _ := repo.GetPublicKey()
	err = utils.SHA256Verify(pubBytes, remoteMetaBytes, signBytes)
	if err != nil {
		fmt.Println("Fail to verify meta by public key")
		return err
	}

	metaURL, err := uc.save(repo, defaultMeta, remoteMetaBytes)
	if err != nil {
		return err
	}

	signURL, err := uc.save(repo, defaultMetaSign, signBytes)
	if err != nil {
		os.Remove(metaURL)
		return err
	}

	_, err = uc.save(repo, defaultPublicKey, pubBytes)
	if err != nil {
		os.Remove(metaURL)
		os.Remove(signURL)
		return err
	}

	return nil
}