From 3238ee3c71915775c77198174cab2e72082e3e37 Mon Sep 17 00:00:00 2001 From: Jean Date: Tue, 10 Oct 2023 22:20:04 +0200 Subject: [PATCH] improve coverage --- .github/workflows/server_test.yml | 6 +- server/blockchain/calypso/calypso_test.go | 140 +++++++++++++++++++++- server/sonar-project.properties | 2 +- 3 files changed, 141 insertions(+), 7 deletions(-) diff --git a/.github/workflows/server_test.yml b/.github/workflows/server_test.yml index bcf69530..1681f2a2 100644 --- a/.github/workflows/server_test.yml +++ b/.github/workflows/server_test.yml @@ -2,7 +2,6 @@ name: Server test on: push: - branches: [ main ] pull_request: types: [opened, synchronize, reopened] @@ -27,8 +26,7 @@ jobs: - name: Set up Go 1.21 uses: actions/setup-go@v4 with: - go-version: "1.21" - cache: false + go-version: "=1.21" - name: Test without coverage if: matrix.platform == 'macos-latest' || matrix.platform == 'windows-latest' @@ -38,7 +36,7 @@ jobs: if: matrix.platform == 'ubuntu-latest' run: make coverage - - name: Run code analysis + - name: Sonarcloud scan if: matrix.platform == 'ubuntu-latest' uses: SonarSource/sonarcloud-github-action@master with: diff --git a/server/blockchain/calypso/calypso_test.go b/server/blockchain/calypso/calypso_test.go index dc14d27c..0fe718c6 100644 --- a/server/blockchain/calypso/calypso_test.go +++ b/server/blockchain/calypso/calypso_test.go @@ -17,7 +17,44 @@ import ( "go.dedis.ch/dela/testing/fake" ) -func TestExecute(t *testing.T) { +func Test_secretSet_addSecret(t *testing.T) { + s := secretSet{} + secret := "some secret" + + s.addSecret(secret) + + require.Equal(t, 1, len(s)) + require.Equal(t, struct{}{}, s[secret]) +} + +func Test_NewCreds(t *testing.T) { + creds := NewCreds() + + require.NotNilf(t, creds, "creds should not be nil") +} + +func Test_RegisterContract(_ *testing.T) { + RegisterContract(native.NewExecution(), Contract{}) +} + +func Test_NewContract(t *testing.T) { + contract := NewContract(fakeAccess{}) + + require.NotNilf(t, contract, "contract should not be nil") + require.NotNilf(t, contract.secrets, "secrets should not be nil") + require.NotNilf(t, contract.printer, "printer should not be nil") + require.NotNilf(t, contract.cmd, "cmd should not be nil") + require.NotNilf(t, contract.access, "access should not be nil") +} + +func TestContract_UID(t *testing.T) { + contract := NewContract(fakeAccess{}) + + require.NotNilf(t, contract, "contract should not be nil") + require.Equal(t, ContractUID, contract.UID()) +} + +func TestExecuteFailing(t *testing.T) { contract := NewContract(fakeAccess{err: fake.GetError()}) err := contract.Execute(fakeStore{}, makeStep(t)) @@ -39,12 +76,49 @@ func TestExecute(t *testing.T) { err = contract.Execute(fakeStore{}, makeStep(t, CmdArg, "LIST_SMC")) require.EqualError(t, err, fake.Err("failed to LIST_SMC")) + err = contract.Execute(fakeStore{}, makeStep(t, CmdArg, "CREATE_SECRET")) + require.EqualError(t, err, fake.Err("failed to CREATE_SECRET")) + + err = contract.Execute(fakeStore{}, makeStep(t, CmdArg, "LIST_SECRETS")) + require.EqualError(t, err, fake.Err("failed to LIST_SECRETS")) + + err = contract.Execute(fakeStore{}, makeStep(t, CmdArg, "REVEAL_SECRET")) + require.EqualError(t, err, fake.Err("failed to REVEAL_SECRET")) + + err = contract.Execute(fakeStore{}, makeStep(t, CmdArg, "LIST_AUDIT_LOG")) + require.EqualError(t, err, fake.Err("failed to LIST_AUDIT_LOG")) + err = contract.Execute(fakeStore{}, makeStep(t, CmdArg, "fake")) require.EqualError(t, err, "unknown command: fake") +} +func TestExecutePassing(t *testing.T) { + contract := NewContract(fakeAccess{}) contract.cmd = fakeCmd{} - err = contract.Execute(fakeStore{}, makeStep(t, CmdArg, "ADVERTISE_SMC")) + + err := contract.Execute(fakeStore{}, makeStep(t, CmdArg, "ADVERTISE_SMC")) + require.NoError(t, err) + + err = contract.Execute(fakeStore{}, makeStep(t, CmdArg, "DELETE_SMC")) + require.NoError(t, err) + + err = contract.Execute(fakeStore{}, makeStep(t, CmdArg, "LIST_SMC")) + require.NoError(t, err) + + err = contract.Execute(fakeStore{}, makeStep(t, CmdArg, "CREATE_SECRET")) + require.NoError(t, err) + + err = contract.Execute(fakeStore{}, makeStep(t, CmdArg, "LIST_SECRETS")) + require.NoError(t, err) + + err = contract.Execute(fakeStore{}, makeStep(t, CmdArg, "REVEAL_SECRET")) + require.NoError(t, err) + + err = contract.Execute(fakeStore{}, makeStep(t, CmdArg, "LIST_AUDIT_LOG")) require.NoError(t, err) + + err = contract.Execute(fakeStore{}, makeStep(t, CmdArg, "fake")) + require.EqualError(t, err, "unknown command: fake") } func TestCommand_AdvertiseSmc(t *testing.T) { @@ -524,6 +598,68 @@ func TestRegisterContract(_ *testing.T) { RegisterContract(native.NewExecution(), Contract{}) } +func Test_validateRosterUpdate(t *testing.T) { + type args struct { + oldRoster []byte + newRoster []byte + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "validateNew", + args: args{ + oldRoster: []byte(""), + newRoster: []byte("node:12345"), + }, + wantErr: true, + }, + { + name: "validateAdd", + args: args{ + oldRoster: []byte("node:12345"), + newRoster: []byte("node:12345,other:54321"), + }, + wantErr: false, + }, + { + name: "validateNoChange", + args: args{ + oldRoster: []byte("node:12345"), + newRoster: []byte("node:12345"), + }, + wantErr: false, + }, + { + name: "validateRemove", + args: args{ + oldRoster: []byte("node:12345,other:54321"), + newRoster: []byte("node:12345"), + }, + wantErr: true, + }, + { + name: "validateNoOverlap", + args: args{ + oldRoster: []byte("node:12345"), + newRoster: []byte("other:54321"), + }, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := validateRosterUpdate(tt.args.oldRoster, + tt.args.newRoster); (err != nil) != tt.wantErr { + t.Errorf("validateRosterUpdate() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + // ----------------------------------------------------------------------------- // Utility functions diff --git a/server/sonar-project.properties b/server/sonar-project.properties index cdde68c6..0aed59ee 100644 --- a/server/sonar-project.properties +++ b/server/sonar-project.properties @@ -3,7 +3,7 @@ sonar.projectKey=hbt-server # Path of the test and coverage reports sonar.go.tests.reportPaths=./report.json -sonar.go.coverage.reportPaths=./coverage.out +sonar.go.coverage.reportPaths=./profile.cov # Path patterns of the source files sonar.sources=.