parent
98ab794f13
commit
eccad4bbfd
11 changed files with 376 additions and 70 deletions
@ -0,0 +1,52 @@ |
||||
// Copyright (c) 2020 Proton Technologies AG
|
||||
//
|
||||
// This file is part of ProtonMail Bridge.
|
||||
//
|
||||
// ProtonMail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ProtonMail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
// +build !darwin
|
||||
|
||||
package versioner |
||||
|
||||
import ( |
||||
"io/ioutil" |
||||
"path/filepath" |
||||
"testing" |
||||
|
||||
"github.com/Masterminds/semver/v3" |
||||
"github.com/stretchr/testify/assert" |
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
// RemoveOldVersions is a noop on darwin; we don't test it there.
|
||||
|
||||
func TestRemoveOldVersions(t *testing.T) { |
||||
updates, err := ioutil.TempDir("", "updates") |
||||
require.NoError(t, err) |
||||
|
||||
v := newTestVersioner(t, "myCoolApp", updates, "2.3.4-beta", "2.3.4", "2.3.5", "2.4.0") |
||||
|
||||
allVersions, err := v.ListVersions() |
||||
require.NoError(t, err) |
||||
require.Len(t, allVersions, 4) |
||||
|
||||
assert.NoError(t, v.RemoveOldVersions()) |
||||
|
||||
cleanedVersions, err := v.ListVersions() |
||||
assert.NoError(t, err) |
||||
assert.Len(t, cleanedVersions, 1) |
||||
|
||||
assert.Equal(t, semver.MustParse("2.4.0"), cleanedVersions[0].version) |
||||
assert.Equal(t, filepath.Join(updates, "2.4.0"), cleanedVersions[0].path) |
||||
} |
||||
@ -0,0 +1,66 @@ |
||||
// Copyright (c) 2020 Proton Technologies AG
|
||||
//
|
||||
// This file is part of ProtonMail Bridge.
|
||||
//
|
||||
// ProtonMail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ProtonMail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package sum |
||||
|
||||
import ( |
||||
"crypto/sha512" |
||||
"io" |
||||
"os" |
||||
"path/filepath" |
||||
"strings" |
||||
) |
||||
|
||||
// RecursiveSum computes the sha512 sum of all files in the root directory and descendents.
|
||||
// If a skipFile is provided (e.g. the path of a checksum file relative to rootDir), it (and its signature) is ignored.
|
||||
func RecursiveSum(rootDir, skipFile string) ([]byte, error) { |
||||
hash := sha512.New() |
||||
|
||||
if err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error { |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
if info.IsDir() { |
||||
return nil |
||||
} |
||||
|
||||
// The hashfile itself isn't included in the hash.
|
||||
if path == filepath.Join(rootDir, skipFile) || path == filepath.Join(rootDir, skipFile+".sig") { |
||||
return nil |
||||
} |
||||
|
||||
if _, err := hash.Write([]byte(strings.TrimPrefix(path, rootDir))); err != nil { |
||||
return err |
||||
} |
||||
|
||||
f, err := os.Open(path) // nolint[gosec]
|
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
if _, err := io.Copy(hash, f); err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
}); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return hash.Sum([]byte{}), nil |
||||
} |
||||
@ -0,0 +1,112 @@ |
||||
// Copyright (c) 2020 Proton Technologies AG
|
||||
//
|
||||
// This file is part of ProtonMail Bridge.
|
||||
//
|
||||
// ProtonMail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ProtonMail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package sum |
||||
|
||||
import ( |
||||
"io/ioutil" |
||||
"os" |
||||
"path/filepath" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
func TestRecursiveSum(t *testing.T) { |
||||
tempDir, err := ioutil.TempDir("", "verify-test") |
||||
require.NoError(t, err) |
||||
|
||||
createFiles(t, tempDir, |
||||
filepath.Join("a", "1"), |
||||
filepath.Join("a", "2"), |
||||
filepath.Join("b", "3"), |
||||
filepath.Join("b", "4"), |
||||
filepath.Join("b", "c", "5"), |
||||
filepath.Join("b", "c", "6"), |
||||
) |
||||
|
||||
sumOriginal := sum(t, tempDir) |
||||
|
||||
// Renaming files should produce a different checksum.
|
||||
require.NoError(t, os.Rename(filepath.Join(tempDir, "a", "1"), filepath.Join(tempDir, "a", "11"))) |
||||
sumRenamed := sum(t, tempDir) |
||||
require.NotEqual(t, sumOriginal, sumRenamed) |
||||
|
||||
// Reverting to the original name should produce the same checksum again.
|
||||
require.NoError(t, os.Rename(filepath.Join(tempDir, "a", "11"), filepath.Join(tempDir, "a", "1"))) |
||||
require.Equal(t, sumOriginal, sum(t, tempDir)) |
||||
|
||||
// Moving files should produce a different checksum.
|
||||
require.NoError(t, os.Rename(filepath.Join(tempDir, "a", "1"), filepath.Join(tempDir, "1"))) |
||||
sumMoved := sum(t, tempDir) |
||||
require.NotEqual(t, sumOriginal, sumMoved) |
||||
|
||||
// Moving files back to their original location should produce the same checksum again.
|
||||
require.NoError(t, os.Rename(filepath.Join(tempDir, "1"), filepath.Join(tempDir, "a", "1"))) |
||||
require.Equal(t, sumOriginal, sum(t, tempDir)) |
||||
|
||||
// Changing file data should produce a different checksum.
|
||||
originalData := modifyFile(t, filepath.Join(tempDir, "a", "1"), []byte("something")) |
||||
require.NotEqual(t, sumOriginal, sum(t, tempDir)) |
||||
|
||||
// Reverting file data should produce the original checksum.
|
||||
modifyFile(t, filepath.Join(tempDir, "a", "1"), originalData) |
||||
require.Equal(t, sumOriginal, sum(t, tempDir)) |
||||
} |
||||
|
||||
func createFiles(t *testing.T, root string, paths ...string) { |
||||
for _, path := range paths { |
||||
makeFile(t, filepath.Join(root, path)) |
||||
} |
||||
} |
||||
|
||||
func makeFile(t *testing.T, path string) { |
||||
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0700)) |
||||
|
||||
f, err := os.Create(path) |
||||
require.NoError(t, err) |
||||
|
||||
_, err = f.WriteString(path) |
||||
require.NoError(t, err) |
||||
|
||||
require.NoError(t, f.Close()) |
||||
} |
||||
|
||||
func sum(t *testing.T, path string) []byte { |
||||
sum, err := RecursiveSum(path, "") |
||||
require.NoError(t, err) |
||||
|
||||
return sum |
||||
} |
||||
|
||||
func modifyFile(t *testing.T, path string, data []byte) []byte { |
||||
r, err := os.Open(path) |
||||
require.NoError(t, err) |
||||
|
||||
b, err := ioutil.ReadAll(r) |
||||
require.NoError(t, err) |
||||
require.NoError(t, r.Close()) |
||||
|
||||
f, err := os.Create(path) |
||||
require.NoError(t, err) |
||||
|
||||
_, err = f.Write(data) |
||||
require.NoError(t, err) |
||||
require.NoError(t, f.Close()) |
||||
|
||||
return b |
||||
} |
||||
@ -0,0 +1,62 @@ |
||||
// Copyright (c) 2020 Proton Technologies AG
|
||||
//
|
||||
// This file is part of ProtonMail Bridge.
|
||||
//
|
||||
// ProtonMail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ProtonMail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package main |
||||
|
||||
import ( |
||||
"os" |
||||
|
||||
"github.com/ProtonMail/proton-bridge/pkg/sum" |
||||
"github.com/sirupsen/logrus" |
||||
"github.com/urfave/cli/v2" |
||||
) |
||||
|
||||
func main() { |
||||
if err := createApp().Run(os.Args); err != nil { |
||||
logrus.Fatal(err) |
||||
} |
||||
} |
||||
|
||||
func createApp() *cli.App { // nolint[funlen]
|
||||
app := cli.NewApp() |
||||
|
||||
app.Name = "hasher" |
||||
app.Usage = "Generate the recursive hash of a directory" |
||||
app.Action = computeSum |
||||
app.Flags = []cli.Flag{ |
||||
&cli.StringFlag{ |
||||
Name: "root", |
||||
Usage: "The root directory from which to begin recursive hashing", |
||||
Required: true, |
||||
}, |
||||
} |
||||
|
||||
return app |
||||
} |
||||
|
||||
func computeSum(c *cli.Context) error { |
||||
b, err := sum.RecursiveSum(c.String("root"), "") |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
if _, err := c.App.Writer.Write(b); err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
Loading…
Reference in new issue