From 35da3edcab413691df5d96fb3583fed90c3215f8 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 2 Apr 2026 10:15:00 +0100 Subject: [PATCH] ax(node): rename bundle_test.go functions to TestFilename_Function_{Good,Bad,Ugly} convention All 10 test functions in bundle_test.go lacked the AX Principle 10 naming convention. Renamed to TestBundle_* with _Good/_Bad/_Ugly suffixes and converted inlined Bad/Ugly subtests into top-level functions where needed. Added usage-example comments per AX Principle 2. Co-Authored-By: Charon --- pkg/node/bundle_test.go | 220 ++++++++++++++++++++++------------------ 1 file changed, 123 insertions(+), 97 deletions(-) diff --git a/pkg/node/bundle_test.go b/pkg/node/bundle_test.go index 4bc8f26..79453ac 100644 --- a/pkg/node/bundle_test.go +++ b/pkg/node/bundle_test.go @@ -7,7 +7,9 @@ import ( "testing" ) -func TestCreateProfileBundleUnencrypted(t *testing.T) { +// TestBundle_CreateProfileBundleUnencrypted_Good verifies happy-path unencrypted bundle creation. +// bundle, err := CreateProfileBundleUnencrypted(profileJSON, "main") +func TestBundle_CreateProfileBundleUnencrypted_Good(t *testing.T) { profileJSON := []byte(`{"name":"test-profile","minerType":"xmrig","config":{}}`) bundle, err := CreateProfileBundleUnencrypted(profileJSON, "test-profile") @@ -32,35 +34,41 @@ func TestCreateProfileBundleUnencrypted(t *testing.T) { } } -func TestVerifyBundle(t *testing.T) { - t.Run("ValidChecksum", func(t *testing.T) { - bundle, _ := CreateProfileBundleUnencrypted([]byte(`{"test":"data"}`), "test") +// TestBundle_VerifyBundle_Good verifies that a correctly constructed bundle passes verification. +// if !node.VerifyBundle(bundle) { return fmt.Errorf("bundle corrupted or tampered") } +func TestBundle_VerifyBundle_Good(t *testing.T) { + bundle, _ := CreateProfileBundleUnencrypted([]byte(`{"test":"data"}`), "test") - if !VerifyBundle(bundle) { - t.Error("valid bundle should verify") - } - }) - - t.Run("InvalidChecksum", func(t *testing.T) { - bundle, _ := CreateProfileBundleUnencrypted([]byte(`{"test":"data"}`), "test") - bundle.Checksum = "invalid-checksum" - - if VerifyBundle(bundle) { - t.Error("bundle with invalid checksum should not verify") - } - }) - - t.Run("ModifiedData", func(t *testing.T) { - bundle, _ := CreateProfileBundleUnencrypted([]byte(`{"test":"data"}`), "test") - bundle.Data = []byte(`{"test":"modified"}`) - - if VerifyBundle(bundle) { - t.Error("bundle with modified data should not verify") - } - }) + if !VerifyBundle(bundle) { + t.Error("valid bundle should verify") + } } -func TestCreateProfileBundle(t *testing.T) { +// TestBundle_VerifyBundle_Bad verifies that a bundle with a wrong checksum fails verification. +// if !node.VerifyBundle(bundle) { return fmt.Errorf("bundle corrupted or tampered") } +func TestBundle_VerifyBundle_Bad(t *testing.T) { + bundle, _ := CreateProfileBundleUnencrypted([]byte(`{"test":"data"}`), "test") + bundle.Checksum = "invalid-checksum" + + if VerifyBundle(bundle) { + t.Error("bundle with invalid checksum should not verify") + } +} + +// TestBundle_VerifyBundle_Ugly verifies that a bundle with modified data fails verification. +// if !node.VerifyBundle(bundle) { return fmt.Errorf("bundle corrupted or tampered") } +func TestBundle_VerifyBundle_Ugly(t *testing.T) { + bundle, _ := CreateProfileBundleUnencrypted([]byte(`{"test":"data"}`), "test") + bundle.Data = []byte(`{"test":"modified"}`) + + if VerifyBundle(bundle) { + t.Error("bundle with modified data should not verify") + } +} + +// TestBundle_CreateProfileBundle_Good verifies encrypted bundle round-trip. +// bundle, err := CreateProfileBundle(profileJSON, "main", password) +func TestBundle_CreateProfileBundle_Good(t *testing.T) { profileJSON := []byte(`{"name":"encrypted-profile","minerType":"xmrig"}`) password := "test-password-123" @@ -89,7 +97,9 @@ func TestCreateProfileBundle(t *testing.T) { } } -func TestExtractProfileBundle(t *testing.T) { +// TestBundle_ExtractProfileBundle_Good verifies extraction of both plain and encrypted bundles. +// profileJSON, err := ExtractProfileBundle(bundle, password) +func TestBundle_ExtractProfileBundle_Good(t *testing.T) { t.Run("UnencryptedBundle", func(t *testing.T) { originalJSON := []byte(`{"name":"plain","config":{}}`) bundle, _ := CreateProfileBundleUnencrypted(originalJSON, "plain") @@ -119,76 +129,84 @@ func TestExtractProfileBundle(t *testing.T) { t.Error("extracted data should match original") } }) - - t.Run("WrongPassword", func(t *testing.T) { - originalJSON := []byte(`{"name":"secret"}`) - bundle, _ := CreateProfileBundle(originalJSON, "secret", "correct-password") - - _, err := ExtractProfileBundle(bundle, "wrong-password") - if err == nil { - t.Error("should fail with wrong password") - } - }) - - t.Run("CorruptedChecksum", func(t *testing.T) { - bundle, _ := CreateProfileBundleUnencrypted([]byte(`{}`), "test") - bundle.Checksum = "corrupted" - - _, err := ExtractProfileBundle(bundle, "") - if err == nil { - t.Error("should fail with corrupted checksum") - } - }) } -func TestTarballFunctions(t *testing.T) { - t.Run("CreateAndExtractTarball", func(t *testing.T) { - files := map[string][]byte{ - "file1.txt": []byte("content of file 1"), - "dir/file2.json": []byte(`{"key":"value"}`), - "miners/xmrig": []byte("binary content"), - } +// TestBundle_ExtractProfileBundle_Bad verifies that a wrong password fails decryption. +// profileJSON, err := ExtractProfileBundle(bundle, password) +func TestBundle_ExtractProfileBundle_Bad(t *testing.T) { + originalJSON := []byte(`{"name":"secret"}`) + bundle, _ := CreateProfileBundle(originalJSON, "secret", "correct-password") - tarData, err := createTarball(files) - if err != nil { - t.Fatalf("failed to create tarball: %v", err) - } - - if len(tarData) == 0 { - t.Error("tarball should not be empty") - } - - // Extract to temp directory - tmpDir, _ := os.MkdirTemp("", "tarball-test") - defer os.RemoveAll(tmpDir) - - firstExec, err := extractTarball(tarData, tmpDir) - if err != nil { - t.Fatalf("failed to extract tarball: %v", err) - } - - // Check files exist - for name, content := range files { - path := filepath.Join(tmpDir, name) - data, err := os.ReadFile(path) - if err != nil { - t.Errorf("failed to read extracted file %s: %v", name, err) - continue - } - - if !bytes.Equal(data, content) { - t.Errorf("content mismatch for %s", name) - } - } - - // Check first executable is the miner - if firstExec == "" { - t.Error("should find an executable") - } - }) + _, err := ExtractProfileBundle(bundle, "wrong-password") + if err == nil { + t.Error("should fail with wrong password") + } } -func TestStreamAndReadBundle(t *testing.T) { +// TestBundle_ExtractProfileBundle_Ugly verifies that a corrupted checksum is rejected before decryption. +// profileJSON, err := ExtractProfileBundle(bundle, password) +func TestBundle_ExtractProfileBundle_Ugly(t *testing.T) { + bundle, _ := CreateProfileBundleUnencrypted([]byte(`{}`), "test") + bundle.Checksum = "corrupted" + + _, err := ExtractProfileBundle(bundle, "") + if err == nil { + t.Error("should fail with corrupted checksum") + } +} + +// TestBundle_TarballFunctions_Good verifies createTarball and extractTarball round-trip. +// tarData, err := createTarball(files) +// firstExec, err := extractTarball(tarData, destDir) +func TestBundle_TarballFunctions_Good(t *testing.T) { + files := map[string][]byte{ + "file1.txt": []byte("content of file 1"), + "dir/file2.json": []byte(`{"key":"value"}`), + "miners/xmrig": []byte("binary content"), + } + + tarData, err := createTarball(files) + if err != nil { + t.Fatalf("failed to create tarball: %v", err) + } + + if len(tarData) == 0 { + t.Error("tarball should not be empty") + } + + // Extract to temp directory + tmpDir, _ := os.MkdirTemp("", "tarball-test") + defer os.RemoveAll(tmpDir) + + firstExec, err := extractTarball(tarData, tmpDir) + if err != nil { + t.Fatalf("failed to extract tarball: %v", err) + } + + // Check files exist + for name, content := range files { + path := filepath.Join(tmpDir, name) + data, err := os.ReadFile(path) + if err != nil { + t.Errorf("failed to read extracted file %s: %v", name, err) + continue + } + + if !bytes.Equal(data, content) { + t.Errorf("content mismatch for %s", name) + } + } + + // Check first executable is the miner + if firstExec == "" { + t.Error("should find an executable") + } +} + +// TestBundle_StreamAndReadBundle_Good verifies streaming encode/decode round-trip. +// err := StreamBundle(bundle, &buf) +// restored, err := ReadBundle(&buf) +func TestBundle_StreamAndReadBundle_Good(t *testing.T) { original, _ := CreateProfileBundleUnencrypted([]byte(`{"streaming":"test"}`), "stream-test") // Stream to buffer @@ -217,7 +235,9 @@ func TestStreamAndReadBundle(t *testing.T) { } } -func TestCalculateChecksum(t *testing.T) { +// TestBundle_CalculateChecksum_Good verifies determinism and hex format of SHA-256 checksums. +// checksum := calculateChecksum(stimData) +func TestBundle_CalculateChecksum_Good(t *testing.T) { t.Run("Deterministic", func(t *testing.T) { data := []byte("test data for checksum") @@ -255,7 +275,9 @@ func TestCalculateChecksum(t *testing.T) { }) } -func TestIsJSON(t *testing.T) { +// TestBundle_IsJSON_Good verifies JSON detection for both valid and invalid byte slices. +// if isJSON(bundle.Data) { return bundle.Data, nil } // skip decrypt for plain profiles +func TestBundle_IsJSON_Good(t *testing.T) { tests := []struct { data []byte expected bool @@ -278,7 +300,9 @@ func TestIsJSON(t *testing.T) { } } -func TestBundleTypes(t *testing.T) { +// TestBundle_BundleTypes_Good verifies that BundleType constants encode to their expected string values. +// bundle := &Bundle{Type: BundleProfile, ...} +func TestBundle_BundleTypes_Good(t *testing.T) { types := []BundleType{ BundleProfile, BundleMiner, @@ -294,7 +318,9 @@ func TestBundleTypes(t *testing.T) { } } -func TestCreateMinerBundle(t *testing.T) { +// TestBundle_CreateMinerBundle_Good verifies miner bundle creation and extraction round-trip. +// bundle, err := CreateMinerBundle("/usr/bin/xmrig", profileJSON, "xmrig-lthn", password) +func TestBundle_CreateMinerBundle_Good(t *testing.T) { // Create a temp "miner binary" tmpDir, _ := os.MkdirTemp("", "miner-bundle-test") defer os.RemoveAll(tmpDir)