From 1e91e2b9f7bad8dd1e87262a6c2abf61e2331691 Mon Sep 17 00:00:00 2001 From: Daniel <dhaavi@users.noreply.github.com> Date: Fri, 1 Oct 2021 13:58:26 +0200 Subject: [PATCH] Fix test script and linter errors --- .golangci.yml | 7 ++++ cmd/cmd-configure.go | 64 +++++++++++++++++----------------- core.go | 4 --- core_test.go | 11 +++--- defaults.go | 2 +- hashtools/tools.go | 2 +- hashtools/tools_test.go | 1 - letter-file.go | 6 ++-- letter-wire.go | 9 +++-- letter.go | 2 +- lhash/labeledhash_test.go | 1 - password.go | 1 - requirements.go | 2 +- requirements_test.go | 1 - session-wire.go | 4 +-- session.go | 2 +- signet.go | 4 +-- suite.go | 2 +- suites_test.go | 1 - supply/supply_test.go | 1 - test | 73 +++++++++++++++++++-------------------- tools/ecdh/nist.go | 12 +++---- tools/gostdlib/aes-ctr.go | 1 - tools/gostdlib/aes-gcm.go | 1 - tools/tool.go | 4 +-- tools_test.go | 3 -- truststore.go | 4 +-- truststores/dir.go | 2 +- 28 files changed, 105 insertions(+), 122 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index e314a4c..ba5ffe0 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -7,6 +7,13 @@ linters: - funlen - whitespace - wsl + - gomnd + - goerr113 + - testpackage + - nestif + - gocognit + - noctx + - gocyclo linters-settings: godox: diff --git a/cmd/cmd-configure.go b/cmd/cmd-configure.go index a1d03a8..6828c4d 100644 --- a/cmd/cmd-configure.go +++ b/cmd/cmd-configure.go @@ -14,41 +14,39 @@ func init() { rootCmd.AddCommand(configureCmd) } -var ( - configureCmd = &cobra.Command{ - Use: "configure <envelope name>", - Short: "configure (and create) envelope", - DisableFlagsInUseLine: true, - Args: cobra.MaximumNArgs(1), - PreRunE: requireTrustStore, - RunE: func(cmd *cobra.Command, args []string) (err error) { - // check envelope name existence - if len(args) == 0 { - return errors.New("please specify an envelope name") - } - envelopeName := args[0] +var configureCmd = &cobra.Command{ + Use: "configure <envelope name>", + Short: "configure (and create) envelope", + DisableFlagsInUseLine: true, + Args: cobra.MaximumNArgs(1), + PreRunE: requireTrustStore, + RunE: func(cmd *cobra.Command, args []string) (err error) { + // check envelope name existence + if len(args) == 0 { + return errors.New("please specify an envelope name") + } + envelopeName := args[0] - // check envelope name - if !truststores.NamePlaysNiceWithFS(envelopeName) { - return errors.New("please only use alphanumeric characters and `- ._+@` for best compatibility with various systems") - } + // check envelope name + if !truststores.NamePlaysNiceWithFS(envelopeName) { + return errors.New("please only use alphanumeric characters and `- ._+@` for best compatibility with various systems") + } - // get envelope from trust store - envelope, err := trustStore.GetEnvelope(envelopeName) - if err != nil && err != jess.ErrEnvelopeNotFound { + // get envelope from trust store + envelope, err := trustStore.GetEnvelope(envelopeName) + if err != nil && err != jess.ErrEnvelopeNotFound { + return err + } + + // create + if envelope == nil { + envelope, err = newEnvelope(envelopeName) + if err != nil { return err } + } - // create - if envelope == nil { - envelope, err = newEnvelope(envelopeName) - if err != nil { - return err - } - } - - // edit (and save) - return editEnvelope(envelope) - }, - } -) + // edit (and save) + return editEnvelope(envelope) + }, +} diff --git a/core.go b/core.go index aeb1f9d..ad3acb6 100644 --- a/core.go +++ b/core.go @@ -142,7 +142,6 @@ func (s *Session) Close(data []byte) (*Letter, error) { //nolint:gocognit for _, tool := range s.signers { //nolint:scopelint // function is executed immediately within loop err = s.envelope.LoopSenders(tool.Info().Name, func(signet *Signet) error { - sig, err := tool.Sign(data, associatedSigningData, signet) if err != nil { return fmt.Errorf("failed to sign with %s: %s", tool.Info().Name, err) @@ -218,7 +217,6 @@ func (s *Session) Open(letter *Letter) ([]byte, error) { //nolint:gocognit,gocyc for _, tool := range s.signers { //nolint:scopelint // function is executed immediately within loop err = s.envelope.LoopSenders(tool.Info().Name, func(signet *Signet) error { - err := tool.Verify(data, associatedSigningData, letter.Signatures[sigIndex].Value, signet) if err != nil { return fmt.Errorf("failed to verify signature (%s) with ID %s: %s", tool.Info().Name, letter.Signatures[sigIndex].ID, err) @@ -323,7 +321,6 @@ func (s *Session) Open(letter *Letter) ([]byte, error) { //nolint:gocognit,gocyc // Verify verifies signatures of the given letter. func (s *Session) Verify(letter *Letter) error { - // debugging: /* fmt.Printf("opening: %+v\n", letter) @@ -374,7 +371,6 @@ func (s *Session) Verify(letter *Letter) error { for _, tool := range s.signers { //nolint:scopelint // function is executed immediately within loop err = s.envelope.LoopSenders(tool.Info().Name, func(signet *Signet) error { - err := tool.Verify(data, associatedSigningData, letter.Signatures[sigIndex].Value, signet) if err != nil { return fmt.Errorf("failed to verify signature (%s) with ID %s: %s", tool.Info().Name, letter.Signatures[sigIndex].ID, err) diff --git a/core_test.go b/core_test.go index 85c17a4..6c86b2e 100644 --- a/core_test.go +++ b/core_test.go @@ -111,10 +111,10 @@ func init() { defaultSecurityLevel = 128 // init special test config - if RunComprehensiveTests == "true" { //nolint:goconst + if RunComprehensiveTests == "true" { runComprehensiveTestsActive = true } - if RunTestsInDebugStyle == "true" { //nolint:goconst + if RunTestsInDebugStyle == "true" { runTestsInDebugStyleActive = true } } @@ -456,7 +456,6 @@ func testInvalidToolset(e *Envelope, whyInvalid string) error { } func getOrMakeSignet(t *testing.T, tool tools.ToolLogic, recipient bool, signetID string) (*Signet, error) { - // check if signet already exists signet, err := testTrustStore.GetSignet(signetID, recipient) if err == nil { @@ -503,9 +502,9 @@ func getOrMakeSignet(t *testing.T, tool tools.ToolLogic, recipient bool, signetI } // generateCombinations returns all possible combinations of the given []string slice. -// Forked from https://github.com/mxschmitt/golang-combinations/blob/a887187146560effd2677e987b069262f356297f/combinations.go -// Copyright (c) 2018 Max Schmitt -// MIT License +// Forked from https://github.com/mxschmitt/golang-combinations/blob/a887187146560effd2677e987b069262f356297f/combinations.go +// Copyright (c) 2018 Max Schmitt, +// MIT License. func generateCombinations(set []string) (subsets [][]string) { length := uint(len(set)) diff --git a/defaults.go b/defaults.go index baba152..796432e 100644 --- a/defaults.go +++ b/defaults.go @@ -10,7 +10,7 @@ var ( minimumSymmetricKeySize = 0 ) -// Currently recommended toolsets +// Currently recommended toolsets. var ( RecommendedNetwork = []string{"ECDH-X25519", "HKDF(SHA2-256)", "CHACHA20-POLY1305"} RecommendedStoragePassword = []string{"PBKDF2-SHA2-256", "HKDF(SHA2-256)", "CHACHA20-POLY1305"} diff --git a/hashtools/tools.go b/hashtools/tools.go index e34523d..079710c 100644 --- a/hashtools/tools.go +++ b/hashtools/tools.go @@ -31,7 +31,7 @@ func Get(name string) (*HashTool, error) { return hashTool, nil } -// New returns a new hash.Hash with the given Name +// New returns a new hash.Hash with the given name. func New(name string) (hash.Hash, error) { hashTool, err := Get(name) if err != nil { diff --git a/hashtools/tools_test.go b/hashtools/tools_test.go index 9090fb4..a4086b5 100644 --- a/hashtools/tools_test.go +++ b/hashtools/tools_test.go @@ -3,7 +3,6 @@ package hashtools import "testing" func TestAll(t *testing.T) { - testData := []byte("The quick brown fox jumps over the lazy dog. ") all := AsList() diff --git a/letter-file.go b/letter-file.go index 82abc47..a2078eb 100644 --- a/letter-file.go +++ b/letter-file.go @@ -16,10 +16,8 @@ import ( - Data: byte block */ -var ( - // ErrIncompatibleFileFormatVersion is returned when an incompatible wire format is encountered. - ErrIncompatibleFileFormatVersion = errors.New("incompatible file format version") -) +// ErrIncompatibleFileFormatVersion is returned when an incompatible wire format is encountered. +var ErrIncompatibleFileFormatVersion = errors.New("incompatible file format version") // ToFileFormat serializes the letter for storing it as a file. func (letter *Letter) ToFileFormat() (*container.Container, error) { diff --git a/letter-wire.go b/letter-wire.go index 839488e..c456985 100644 --- a/letter-wire.go +++ b/letter-wire.go @@ -24,10 +24,8 @@ import ( - MAC: byte block */ -var ( - // ErrIncompatibleWireFormatVersion is returned when an incompatible wire format is encountered. - ErrIncompatibleWireFormatVersion = errors.New("incompatible wire format version") -) +// ErrIncompatibleWireFormatVersion is returned when an incompatible wire format is encountered. +var ErrIncompatibleWireFormatVersion = errors.New("incompatible wire format version") // ToWire serializes to letter for sending it over a network connection. func (letter *Letter) ToWire() (*container.Container, error) { @@ -87,7 +85,8 @@ func (letter *Letter) ToWire() (*container.Container, error) { } // LetterFromWireData is a relay to LetterFromWire to quickly fix import issues of godep. -// DEPRECATED +// +// Deprecated: Please use LetterFromWire with a fresh container directly. func LetterFromWireData(data []byte) (*Letter, error) { return LetterFromWire(container.New(data)) } diff --git a/letter.go b/letter.go index d4a2e1a..2c43374 100644 --- a/letter.go +++ b/letter.go @@ -150,7 +150,7 @@ func (letter *Letter) ToJSON() ([]byte, error) { return json.Marshal(letter) } -// LetterFromJSON loads a json-serialized letter +// LetterFromJSON loads a json-serialized letter. func LetterFromJSON(data []byte) (*Letter, error) { letter := &Letter{} diff --git a/lhash/labeledhash_test.go b/lhash/labeledhash_test.go index 493f021..fc52606 100644 --- a/lhash/labeledhash_test.go +++ b/lhash/labeledhash_test.go @@ -15,7 +15,6 @@ var ( ) func testAlgorithm(t *testing.T, alg Algorithm, emptyHex, foxHex string) { - // setup emptyBytes, err := hex.DecodeString(emptyHex) if err != nil { diff --git a/password.go b/password.go index 7f87c5e..7f06587 100644 --- a/password.go +++ b/password.go @@ -38,7 +38,6 @@ func SetPasswordCallbacks( // CalculatePasswordSecurityLevel calculates the security level of the given password and iterations of the pbkdf algorithm. func CalculatePasswordSecurityLevel(password string, iterations int) int { - // TODO: this calculation is pretty conservative and errs on the safe side // maybe soften this up a litte, but couldn't find any scientific foundation for that diff --git a/requirements.go b/requirements.go index 4533d7f..05a8a28 100644 --- a/requirements.go +++ b/requirements.go @@ -5,7 +5,7 @@ import ( "strings" ) -// Security requirements of a letter +// Security requirements of a letter. const ( Confidentiality uint8 = iota Integrity diff --git a/requirements_test.go b/requirements_test.go index e5a52b4..ad70bae 100644 --- a/requirements_test.go +++ b/requirements_test.go @@ -10,7 +10,6 @@ func checkNoSpec(t *testing.T, a *Requirements, expectedNoSpec string) { } func TestRequirements(t *testing.T) { - a := NewRequirements() checkNoSpec(t, a, "") diff --git a/session-wire.go b/session-wire.go index fc33ea5..ad80cb6 100644 --- a/session-wire.go +++ b/session-wire.go @@ -40,14 +40,14 @@ type WireSession struct { //nolint:maligned // TODO newKeyMaterial [][]byte } -// kxPair is key exchange pair +// kxPair is key exchange pair. type kxPair struct { tool tools.ToolLogic signet *Signet peer *Signet } -// kePair is key encapsulation "pair" +// kePair is key encapsulation "pair". type kePair struct { tool tools.ToolLogic signet *Signet diff --git a/session.go b/session.go index 3a0a44a..932cb11 100644 --- a/session.go +++ b/session.go @@ -48,7 +48,7 @@ type managedHasher struct { hash hash.Hash } -// Sum returns the hash sum of the managed hasher +// Sum returns the hash sum of the managed hasher. func (sh *managedHasher) Sum() ([]byte, error) { if sh == nil || sh.hash == nil { return nil, errors.New("managed hasher is broken") diff --git a/signet.go b/signet.go index 5c3e7d7..7946316 100644 --- a/signet.go +++ b/signet.go @@ -12,7 +12,7 @@ import ( uuid "github.com/satori/go.uuid" ) -// Special signet types +// Special signet types. const ( SignetSchemePassword = "pw" SignetSchemeKey = "key" @@ -176,7 +176,7 @@ func (signet *Signet) LoadKey() error { return signet.tool.StaticLogic.LoadKey(signet) } -// Tool returns the tool of the signet +// Tool returns the tool of the signet. func (signet *Signet) Tool() (*tools.Tool, error) { // load tool err := signet.loadTool() diff --git a/suite.go b/suite.go index 232e95a..e730e00 100644 --- a/suite.go +++ b/suite.go @@ -1,6 +1,6 @@ package jess -// Suite status options +// Suite status options. const ( SuiteStatusDeprecated uint8 = 0 SuiteStatusPermitted uint8 = 1 diff --git a/suites_test.go b/suites_test.go index 86a37c8..18373dc 100644 --- a/suites_test.go +++ b/suites_test.go @@ -91,7 +91,6 @@ func TestSuites(t *testing.T) { } } - } func suiteBullshitCheck(suite *Suite) error { //nolint:gocognit,gocyclo diff --git a/supply/supply_test.go b/supply/supply_test.go index 666cd23..57407fa 100644 --- a/supply/supply_test.go +++ b/supply/supply_test.go @@ -7,7 +7,6 @@ import ( ) func TestSupply(t *testing.T) { - total := 10 supply := NewSignetSupply(total) scheme := "ECDH-X25519" diff --git a/test b/test index 003aa41..b8917ef 100755 --- a/test +++ b/test @@ -4,21 +4,21 @@ warnings=0 errors=0 scripted=0 goUp="\\e[1A" -all=0 fullTestFlags="-short" install=0 +testonly=0 function help { echo "usage: $0 [command] [options]" echo "" echo "commands:" echo " <none> run baseline tests" - echo " all run all tests" - echo " install install deps for running baseline tests" - echo " install all install deps for running all tests" + echo " full run full tests (ie. not short)" + echo " install install deps for running tests" echo "" echo "options:" echo " --scripted dont jump console lines (still use colors)" + echo " --test-only don run linters only tests" echo " [package] run tests only on this package" } @@ -94,12 +94,15 @@ while true; do goUp="" shift 1 ;; + "--test-only") + testonly=1 + shift 1 + ;; "install") install=1 shift 1 ;; - "all") - all=1 + "full") fullTestFlags="" shift 1 ;; @@ -119,10 +122,9 @@ if [[ $install -eq 1 ]]; then echo "installing dependencies..." echo "$ go get -u golang.org/x/lint/golint" go get -u golang.org/x/lint/golint - if [[ $all -eq 1 ]]; then - echo "$ go get -u github.com/golangci/golangci-lint/cmd/golangci-lint" - go get -u github.com/golangci/golangci-lint/cmd/golangci-lint - fi + # TODO: update golangci-lint version regularly + echo "$ curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.24.0" + curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.24.0 exit 0 fi @@ -131,24 +133,24 @@ if [[ $(which go) == "" ]]; then echo "go command not found" exit 1 fi -if [[ $(which gofmt) == "" ]]; then - echo "gofmt command not found" - exit 1 -fi -if [[ $(which golint) == "" ]]; then - echo "golint command not found" - echo "install with: go get -u golang.org/x/lint/golint" - echo "or run: ./test install" - exit 1 -fi -if [[ $all -eq 1 ]]; then +if [[ $testonly -eq 0 ]]; then + if [[ $(which gofmt) == "" ]]; then + echo "gofmt command not found" + exit 1 + fi + if [[ $(which golint) == "" ]]; then + echo "golint command not found" + echo "install with: go get -u golang.org/x/lint/golint" + echo "or run: ./test install" + exit 1 + fi if [[ $(which golangci-lint) == "" ]]; then echo "golangci-lint command not found" - echo "install locally with: go get -u github.com/golangci/golangci-lint/cmd/golangci-lint" - echo "or run: ./test install all" - echo "" - echo "hint: install for CI with: curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin vX.Y.Z" + echo "install with: curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin vX.Y.Z" echo "don't forget to specify the version you want" + echo "or run: ./test install" + echo "" + echo "alternatively, install the current dev version with: go get -u github.com/golangci/golangci-lint/cmd/golangci-lint" exit 1 fi fi @@ -156,15 +158,10 @@ fi # target selection if [[ "$1" == "" ]]; then # get all packages - packages=$(go list ./...) + packages=$(go list -e ./...) else # single package testing - packages=$(go list)/$1 - if [[ ! -d "$GOPATH/src/$packages" ]]; then - echo "go package $packages does not exist" - help - exit 1 - fi + packages=$(go list -e)/$1 echo "note: only running tests for package $packages" fi @@ -174,13 +171,15 @@ echo "running tests for ${platformInfo//$'\n'/ }:" # run vet/test on packages for package in $packages; do + packagename=${package#github.com/safing/jess} #TODO: could be queried with `go list .` + packagename=${packagename#/} echo "" echo $package - checkformat $package - run golint -set_exit_status -min_confidence 1.0 $package - run go vet $package - if [[ $all -eq 1 ]]; then - run golangci-lint run $GOPATH/src/$package + if [[ $testonly -eq 0 ]]; then + checkformat $package + run golint -set_exit_status -min_confidence 1.0 $package + run go vet $package + run golangci-lint run $packagename fi run go test -cover $fullTestFlags $package done diff --git a/tools/ecdh/nist.go b/tools/ecdh/nist.go index 84c3550..937dc55 100644 --- a/tools/ecdh/nist.go +++ b/tools/ecdh/nist.go @@ -11,13 +11,11 @@ import ( "github.com/aead/ecdh" ) -var ( - nistCurveInfo = &tools.ToolInfo{ - Purpose: tools.PurposeKeyExchange, - Comment: "FIPS 186", - Author: "NIST, 2009", - } -) +var nistCurveInfo = &tools.ToolInfo{ + Purpose: tools.PurposeKeyExchange, + Comment: "FIPS 186", + Author: "NIST, 2009", +} func init() { tools.Register(&tools.Tool{ diff --git a/tools/gostdlib/aes-ctr.go b/tools/gostdlib/aes-ctr.go index a3c0402..a65477f 100644 --- a/tools/gostdlib/aes-ctr.go +++ b/tools/gostdlib/aes-ctr.go @@ -7,7 +7,6 @@ import ( "github.com/safing/jess/tools" ) -//nolint:dupl func init() { aesCtrInfo := &tools.ToolInfo{ Purpose: tools.PurposeCipher, diff --git a/tools/gostdlib/aes-gcm.go b/tools/gostdlib/aes-gcm.go index 84e664d..d13343c 100644 --- a/tools/gostdlib/aes-gcm.go +++ b/tools/gostdlib/aes-gcm.go @@ -7,7 +7,6 @@ import ( "github.com/safing/jess/tools" ) -//nolint:dupl func init() { aesGcmInfo := &tools.ToolInfo{ Purpose: tools.PurposeIntegratedCipher, diff --git a/tools/tool.go b/tools/tool.go index 043883b..9abdb24 100644 --- a/tools/tool.go +++ b/tools/tool.go @@ -30,7 +30,7 @@ type ToolInfo struct { Author string } -// Tool Purpose +// Tool Purposes. const ( // Key Management and Creation, as well as Authenticity @@ -71,7 +71,7 @@ const ( PurposeMAC ) -// Tool Options +// Tool Options. const ( // Operation diff --git a/tools_test.go b/tools_test.go index b3eccac..3390885 100644 --- a/tools_test.go +++ b/tools_test.go @@ -15,7 +15,6 @@ import ( ) func TestConformity(t *testing.T) { - // Test that every tool only provides one primary feature, as this enables to automatically assign a distinct role to every tool. for _, tool := range tools.AsList() { @@ -34,7 +33,6 @@ func TestConformity(t *testing.T) { } } - } func TestPasswordHashingSpeed(t *testing.T) { @@ -249,5 +247,4 @@ func TestSignetHandling(t *testing.T) { } } } - } diff --git a/truststore.go b/truststore.go index 53f3e0c..15f5514 100644 --- a/truststore.go +++ b/truststore.go @@ -6,14 +6,14 @@ import ( "sync" ) -// TrustStore filter options +// TrustStore filter options. const ( FilterAny uint8 = iota FilterSignetOnly FilterRecipientOnly ) -// TrustStore errors +// TrustStore errors. var ( ErrSignetNotFound = errors.New("could not find signet") ErrEnvelopeNotFound = errors.New("could not find envelope") diff --git a/truststores/dir.go b/truststores/dir.go index 5e340c1..25ab646 100644 --- a/truststores/dir.go +++ b/truststores/dir.go @@ -18,7 +18,7 @@ const ( permittedCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ._+@" ) -// TrustStore errors +// TrustStore errors. var ( errInvalidSignetIDChars = fmt.Errorf("this trust store only allows these characters in signet IDs: %s", permittedCharacters) errInvalidEnvelopeNameChars = fmt.Errorf("this trust store only allows these characters in envelope names: %s", permittedCharacters)