Skip to content

Commit

Permalink
Merge 7d00875 into e54288d
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavosbarreto committed Nov 29, 2017
2 parents e54288d + 7d00875 commit fb4c973
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 47 deletions.
4 changes: 2 additions & 2 deletions testsmocks/statesmock/sha256checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Sha256CheckerMock struct {
mock.Mock
}

func (scm *Sha256CheckerMock) CheckDownloadedObjectSha256sum(fsBackend afero.Fs, downloadDir string, expectedSha256sum string) error {
func (scm *Sha256CheckerMock) CheckDownloadedObjectSha256sum(fsBackend afero.Fs, downloadDir string, expectedSha256sum string) (bool, error) {
args := scm.Called(fsBackend, downloadDir, expectedSha256sum)
return args.Error(0)
return args.Bool(0), args.Error(1)
}
6 changes: 4 additions & 2 deletions testsmocks/statesmock/sha256checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import (

func TestCheckDownloadedObjectSha256sum(t *testing.T) {
fs := afero.NewMemMapFs()
expectedResult := true
expectedError := fmt.Errorf("some error")

scm := &Sha256CheckerMock{}
scm.On("CheckDownloadedObjectSha256sum", fs, "downloaddir", "sha256sum").Return(expectedError)
scm.On("CheckDownloadedObjectSha256sum", fs, "downloaddir", "sha256sum").Return(expectedResult, expectedError)

err := scm.CheckDownloadedObjectSha256sum(fs, "downloaddir", "sha256sum")
ok, err := scm.CheckDownloadedObjectSha256sum(fs, "downloaddir", "sha256sum")

assert.Equal(t, expectedResult, ok)
assert.Equal(t, expectedError, err)

scm.AssertExpectations(t)
Expand Down
5 changes: 5 additions & 0 deletions updatehub/downloading_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func (state *DownloadingState) Handle(uh *UpdateHub) (State, bool) {
continue
}

if err == ErrSha256sum {
log.Warn("sha256sum's don't match after download update")
continue
}

break
}

Expand Down
38 changes: 38 additions & 0 deletions updatehub/downloading_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,44 @@ func TestStateDownloadingTimeout(t *testing.T) {
assert.Equal(t, expectedState, nextState)
}

func TestStateDownloadingSha256Error(t *testing.T) {
om := &objectmock.ObjectMock{}

mode := installmodes.RegisterInstallMode(installmodes.InstallMode{
Name: "test",
CheckRequirements: func() error { return nil },
GetObject: func() interface{} { return om },
})
defer mode.Unregister()

m, err := metadata.NewUpdateMetadata([]byte(validJSONMetadata))
assert.NoError(t, err)

memFs := afero.NewMemMapFs()

apiClient := client.NewApiClient("localhost")

s := NewDownloadingState(apiClient, m, nil)

uh, err := newTestUpdateHub(s, nil)
assert.NoError(t, err)

uh.Store = memFs

cm := &controllermock.ControllerMock{}
cm.On("DownloadUpdate", apiClient, m, mock.Anything, mock.AnythingOfType("chan<- int")).Return(ErrSha256sum).Once()
cm.On("DownloadUpdate", apiClient, m, mock.Anything, mock.AnythingOfType("chan<- int")).Return(nil).Once()

uh.Controller = cm

nextState, _ := s.Handle(uh)

expectedState := NewDownloadedState(apiClient, m)
assert.Equal(t, expectedState, nextState)

cm.AssertExpectations(t)
}

func TestStateDownloadingToMap(t *testing.T) {
ptm := &progresstrackermock.ProgressTrackerMock{}

Expand Down
36 changes: 22 additions & 14 deletions updatehub/updatehub.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/OSSystems/pkg/log"
"github.com/anacrolix/missinggo/httptoo"
"github.com/pkg/errors"
"github.com/spf13/afero"

"github.com/updatehub/updatehub/activeinactive"
Expand All @@ -29,6 +30,8 @@ import (
"github.com/updatehub/updatehub/utils"
)

var ErrSha256sum = errors.New("sha256sum's don't match")

// GetIndexOfObjectToBeInstalled selects which object will be installed from the update metadata
func GetIndexOfObjectToBeInstalled(aii activeinactive.Interface, um *metadata.UpdateMetadata) (int, error) {
if len(um.Objects) < 1 || len(um.Objects) > 2 {
Expand All @@ -53,25 +56,19 @@ func GetIndexOfObjectToBeInstalled(aii activeinactive.Interface, um *metadata.Up
}

type Sha256Checker interface {
CheckDownloadedObjectSha256sum(fsBackend afero.Fs, downloadDir string, expectedSha256sum string) error
CheckDownloadedObjectSha256sum(fsBackend afero.Fs, downloadDir string, expectedSha256sum string) (bool, error)
}

type Sha256CheckerImpl struct {
}

func (s *Sha256CheckerImpl) CheckDownloadedObjectSha256sum(fsBackend afero.Fs, downloadDir string, expectedSha256sum string) error {
func (s *Sha256CheckerImpl) CheckDownloadedObjectSha256sum(fsBackend afero.Fs, downloadDir string, expectedSha256sum string) (bool, error) {
calculatedSha256sum, err := utils.FileSha256sum(fsBackend, path.Join(downloadDir, expectedSha256sum))
if err != nil {
return err
}

if calculatedSha256sum != expectedSha256sum {
err = fmt.Errorf("sha256sum's don't match. Expected: %s / Calculated: %s", expectedSha256sum, calculatedSha256sum)
log.Error(err)
return err
return false, err
}

return nil
return calculatedSha256sum == expectedSha256sum, nil
}

type UpdateHub struct {
Expand Down Expand Up @@ -389,6 +386,12 @@ func (uh *UpdateHub) DownloadUpdate(apiClient *client.ApiClient, updateMetadata
}
}

ok, err := uh.CheckDownloadedObjectSha256sum(uh.Store, uh.Settings.DownloadDir, obj.GetObjectMetadata().Sha256sum)
if !ok {
uh.Store.Remove(path.Join(uh.Settings.DownloadDir, obj.GetObjectMetadata().Sha256sum))
return ErrSha256sum
}

log.Info("object ", objectUID, " downloaded successfully")

step := 100 / len(updateMetadata.Objects[indexToInstall])
Expand Down Expand Up @@ -431,11 +434,16 @@ func (uh *UpdateHub) InstallUpdate(updateMetadata *metadata.UpdateMetadata, prog
progress := 0

for _, obj := range updateMetadata.Objects[indexToInstall] {
err := uh.CheckDownloadedObjectSha256sum(uh.Store, uh.Settings.DownloadDir, obj.GetObjectMetadata().Sha256sum)
if err != nil {
return err
}
ok, err := uh.CheckDownloadedObjectSha256sum(uh.Store, uh.Settings.DownloadDir, obj.GetObjectMetadata().Sha256sum)
if !ok {
if err != nil {
return err
}

log.Error(ErrSha256sum)

return ErrSha256sum
}
log.Info(fmt.Sprintf("installing object: %s (mode: %s)", obj.GetObjectMetadata().Sha256sum, obj.GetObjectMetadata().Mode))

err = obj.Setup()
Expand Down
Loading

0 comments on commit fb4c973

Please sign in to comment.