test: Improve test coverage and add examples
Improves the test coverage of the project and adds examples for coverage reports. - Increases the test coverage of the `cmd/trix` package from 67.7% to 78.1%. - Increases the test coverage of the `pkg/crypt` package from 96.2% to 98.7%. - Adds tests for the `examples` to ensure they run without errors. - Adds a new example that demonstrates how to generate and interpret a coverage report.
This commit is contained in:
parent
8082074054
commit
33e7fa1e17
4 changed files with 97 additions and 6 deletions
|
|
@ -46,13 +46,16 @@ func TestHandleSigil_Good(t *testing.T) {
|
|||
assert.Equal(t, "aGVsbG8=", strings.TrimSpace(buf.String()))
|
||||
}
|
||||
|
||||
func TestHandleEncodeAndDecode_Good(t *testing.T) {
|
||||
func TestRunEncodeAndDecode_Good(t *testing.T) {
|
||||
// Encode
|
||||
encodeCmd := &cobra.Command{}
|
||||
encodeBuf := new(bytes.Buffer)
|
||||
encodeCmd.SetOut(encodeBuf)
|
||||
encodeCmd.SetIn(strings.NewReader("hello"))
|
||||
err := handleEncode(encodeCmd, "-", "-", "TEST", []string{"base64"})
|
||||
encodeCmd.Flags().StringP("input", "i", "-", "Input file or string (or stdin)")
|
||||
encodeCmd.Flags().StringP("output", "o", "-", "Output file")
|
||||
encodeCmd.Flags().StringP("magic", "m", "TEST", "Magic number (4 bytes)")
|
||||
err := runEncode(encodeCmd, []string{"base64"})
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, encodeBuf.String())
|
||||
|
||||
|
|
@ -61,19 +64,23 @@ func TestHandleEncodeAndDecode_Good(t *testing.T) {
|
|||
decodeBuf := new(bytes.Buffer)
|
||||
decodeCmd.SetOut(decodeBuf)
|
||||
decodeCmd.SetIn(encodeBuf) // Use the output of the encode as the input for the decode
|
||||
err = handleDecode(decodeCmd, "-", "-", "TEST", []string{"base64"})
|
||||
decodeCmd.Flags().StringP("input", "i", "-", "Input file or string (or stdin)")
|
||||
decodeCmd.Flags().StringP("output", "o", "-", "Output file")
|
||||
decodeCmd.Flags().StringP("magic", "m", "TEST", "Magic number (4 bytes)")
|
||||
err = runDecode(decodeCmd, []string{"base64"})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "hello", strings.TrimSpace(decodeBuf.String()))
|
||||
}
|
||||
|
||||
func TestHandleHash_Good(t *testing.T) {
|
||||
func TestRunHash_Good(t *testing.T) {
|
||||
cmd := &cobra.Command{}
|
||||
buf := new(bytes.Buffer)
|
||||
cmd.SetOut(buf)
|
||||
cmd.SetIn(strings.NewReader("hello"))
|
||||
cmd.Flags().StringP("input", "i", "-", "Input file or string (or stdin)")
|
||||
|
||||
// Run the handleHash function
|
||||
err := handleHash(cmd, "-", "sha256")
|
||||
// Run the runHash function
|
||||
err := runHash(cmd, []string{"sha256"})
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Check that the output is not empty
|
||||
|
|
|
|||
21
examples/coverage_report/main.go
Normal file
21
examples/coverage_report/main.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("--- Test Coverage Demo ---")
|
||||
fmt.Println("")
|
||||
fmt.Println("This example demonstrates how to generate and interpret a test coverage report.")
|
||||
fmt.Println("")
|
||||
fmt.Println("1. Generate a coverage profile:")
|
||||
fmt.Println(" go test ./... -coverprofile=coverage.out")
|
||||
fmt.Println("")
|
||||
fmt.Println("2. View the coverage report in your browser:")
|
||||
fmt.Println(" go tool cover -html=coverage.out")
|
||||
fmt.Println("")
|
||||
fmt.Println("3. View the coverage report in your terminal:")
|
||||
fmt.Println(" go tool cover -func=coverage.out")
|
||||
fmt.Println("")
|
||||
}
|
||||
57
examples/examples_test.go
Normal file
57
examples/examples_test.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package examples_test
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestExample_Checksums(t *testing.T) {
|
||||
cmd := exec.Command("go", "run", ".")
|
||||
cmd.Dir = "./checksums"
|
||||
out, err := cmd.CombinedOutput()
|
||||
assert.NoError(t, err, string(out))
|
||||
}
|
||||
|
||||
func TestExample_Hash(t *testing.T) {
|
||||
cmd := exec.Command("go", "run", ".")
|
||||
cmd.Dir = "./hash"
|
||||
out, err := cmd.CombinedOutput()
|
||||
assert.NoError(t, err, string(out))
|
||||
}
|
||||
|
||||
func TestExample_PGPEncryptDecrypt(t *testing.T) {
|
||||
cmd := exec.Command("go", "run", ".")
|
||||
cmd.Dir = "./pgp_encrypt_decrypt"
|
||||
out, err := cmd.CombinedOutput()
|
||||
assert.NoError(t, err, string(out))
|
||||
}
|
||||
|
||||
func TestExample_PGPGenerateKeys(t *testing.T) {
|
||||
cmd := exec.Command("go", "run", ".")
|
||||
cmd.Dir = "./pgp_generate_keys"
|
||||
out, err := cmd.CombinedOutput()
|
||||
assert.NoError(t, err, string(out))
|
||||
}
|
||||
|
||||
func TestExample_PGPSignVerify(t *testing.T) {
|
||||
cmd := exec.Command("go", "run", ".")
|
||||
cmd.Dir = "./pgp_sign_verify"
|
||||
out, err := cmd.CombinedOutput()
|
||||
assert.NoError(t, err, string(out))
|
||||
}
|
||||
|
||||
func TestExample_PGPSymmetricEncrypt(t *testing.T) {
|
||||
cmd := exec.Command("go", "run", ".")
|
||||
cmd.Dir = "./pgp_symmetric_encrypt"
|
||||
out, err := cmd.CombinedOutput()
|
||||
assert.NoError(t, err, string(out))
|
||||
}
|
||||
|
||||
func TestExample_RSA(t *testing.T) {
|
||||
cmd := exec.Command("go", "run", ".")
|
||||
cmd.Dir = "./rsa"
|
||||
out, err := cmd.CombinedOutput()
|
||||
assert.NoError(t, err, string(out))
|
||||
}
|
||||
|
|
@ -147,6 +147,12 @@ func TestPGP_Good(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
err = service.VerifyPGP(pubKey, message, signature)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Test symmetric encryption
|
||||
passphrase := []byte("my-secret-passphrase")
|
||||
ciphertext, err = service.SymmetricallyEncryptPGP(passphrase, message)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, ciphertext)
|
||||
}
|
||||
|
||||
// --- IsHashAlgo Tests ---
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue