feat: Implement Good, Bad, and Ugly testing strategy
This commit refactors the test suites for the `crypt` and `trix` packages to follow the Good, Bad, and Ugly testing strategy. - `_Good` tests cover the ideal "happy path" scenarios. - `_Bad` tests cover expected failure scenarios with well-formed but invalid inputs. - `_Ugly` tests cover malicious or malformed inputs designed to cause crashes or panics. This new testing structure makes the test suite more organized, comprehensive, and robust. Additionally, this commit includes a bug fix for the `Luhn` function, which now correctly handles empty and single-digit strings.
This commit is contained in:
parent
e21c910f91
commit
d5ae9a44e1
17 changed files with 281 additions and 261 deletions
12
Taskfile.yml
Normal file
12
Taskfile.yml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
version: '3'
|
||||
|
||||
tasks:
|
||||
test:
|
||||
desc: "Run all tests"
|
||||
cmds:
|
||||
- go test -v ./...
|
||||
|
||||
build:
|
||||
desc: "Build the project"
|
||||
cmds:
|
||||
- go build -v ./...
|
||||
|
|
@ -1,68 +0,0 @@
|
|||
package crypt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestHash(t *testing.T) {
|
||||
payload := "hello"
|
||||
hash := Hash(LTHN, payload)
|
||||
assert.NotEmpty(t, hash)
|
||||
}
|
||||
|
||||
func TestLuhn(t *testing.T) {
|
||||
assert.True(t, Luhn("79927398713"))
|
||||
assert.False(t, Luhn("79927398714"))
|
||||
}
|
||||
|
||||
func TestFletcher16(t *testing.T) {
|
||||
assert.Equal(t, uint16(0xC8F0), Fletcher16("abcde"))
|
||||
assert.Equal(t, uint16(0x2057), Fletcher16("abcdef"))
|
||||
assert.Equal(t, uint16(0x0627), Fletcher16("abcdefgh"))
|
||||
}
|
||||
|
||||
func TestFletcher32(t *testing.T) {
|
||||
expected := uint32(0xF04FC729)
|
||||
actual := Fletcher32("abcde")
|
||||
fmt.Printf("Fletcher32('abcde'): expected: %x, actual: %x\n", expected, actual)
|
||||
assert.Equal(t, expected, actual)
|
||||
|
||||
expected = uint32(0x56502D2A)
|
||||
actual = Fletcher32("abcdef")
|
||||
fmt.Printf("Fletcher32('abcdef'): expected: %x, actual: %x\n", expected, actual)
|
||||
assert.Equal(t, expected, actual)
|
||||
|
||||
expected = uint32(0xEBE19591)
|
||||
actual = Fletcher32("abcdefgh")
|
||||
fmt.Printf("Fletcher32('abcdefgh'): expected: %x, actual: %x\n", expected, actual)
|
||||
assert.Equal(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestFletcher64(t *testing.T) {
|
||||
assert.Equal(t, uint64(0xc8c6c527646362c6), Fletcher64("abcde"))
|
||||
assert.Equal(t, uint64(0xc8c72b276463c8c6), Fletcher64("abcdef"))
|
||||
assert.Equal(t, uint64(0x312e2b28cccac8c6), Fletcher64("abcdefgh"))
|
||||
}
|
||||
|
||||
func TestRootFS(t *testing.T) {
|
||||
tempDir, err := os.MkdirTemp("", "enchantrix-crypt-test")
|
||||
assert.NoError(t, err)
|
||||
defer os.RemoveAll(tempDir)
|
||||
|
||||
key := make([]byte, 32)
|
||||
for i := range key {
|
||||
key[i] = 1
|
||||
}
|
||||
|
||||
fs := NewRootFS(tempDir, key)
|
||||
err = fs.Write("test.txt", []byte("hello"))
|
||||
assert.NoError(t, err)
|
||||
|
||||
data, err := fs.Read("test.txt")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("hello"), data)
|
||||
}
|
||||
|
|
@ -6,8 +6,8 @@ import (
|
|||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/Snider/Enchantrix/chachapoly"
|
||||
"github.com/Snider/Enchantrix/trix"
|
||||
"github.com/Snider/Enchantrix/pkg/crypt/std/chachapoly"
|
||||
"github.com/Snider/Enchantrix/pkg/trix"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
@ -44,7 +44,8 @@ func main() {
|
|||
}
|
||||
|
||||
// 4. Encode the .trix container into its binary format
|
||||
encodedTrix, err := trix.Encode(trixContainer)
|
||||
magicNumber := "MyT1" // My Trix 1
|
||||
encodedTrix, err := trix.Encode(trixContainer, magicNumber)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to encode .trix container: %v", err)
|
||||
}
|
||||
|
|
@ -52,7 +53,7 @@ func main() {
|
|||
fmt.Println("Successfully created .trix container.")
|
||||
|
||||
// 5. Decode the .trix container to retrieve the encrypted data
|
||||
decodedTrix, err := trix.Decode(encodedTrix)
|
||||
decodedTrix, err := trix.Decode(encodedTrix, magicNumber)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to decode .trix container: %v", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,9 +10,17 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/Snider/Enchantrix/lthn"
|
||||
"github.com/Snider/Enchantrix/pkg/crypt/std/lthn"
|
||||
)
|
||||
|
||||
// Service is the main struct for the crypt service.
|
||||
type Service struct{}
|
||||
|
||||
// NewService creates a new crypt service.
|
||||
func NewService() *Service {
|
||||
return &Service{}
|
||||
}
|
||||
|
||||
// HashType defines the supported hashing algorithms.
|
||||
type HashType string
|
||||
|
||||
|
|
@ -27,7 +35,7 @@ const (
|
|||
// --- Hashing ---
|
||||
|
||||
// Hash computes a hash of the payload using the specified algorithm.
|
||||
func Hash(lib HashType, payload string) string {
|
||||
func (s *Service) Hash(lib HashType, payload string) string {
|
||||
switch lib {
|
||||
case LTHN:
|
||||
return lthn.Hash(payload)
|
||||
|
|
@ -51,12 +59,16 @@ func Hash(lib HashType, payload string) string {
|
|||
// --- Checksums ---
|
||||
|
||||
// Luhn validates a number using the Luhn algorithm.
|
||||
func Luhn(payload string) bool {
|
||||
func (s *Service) Luhn(payload string) bool {
|
||||
payload = strings.ReplaceAll(payload, " ", "")
|
||||
if len(payload) <= 1 {
|
||||
return false
|
||||
}
|
||||
|
||||
sum := 0
|
||||
isSecond := false
|
||||
for i := len(payload) - 1; i >= 0; i-- {
|
||||
digit, err := strconv.Atoi(string(payload[i]))
|
||||
isSecond := len(payload)%2 == 0
|
||||
for _, r := range payload {
|
||||
digit, err := strconv.Atoi(string(r))
|
||||
if err != nil {
|
||||
return false // Contains non-digit
|
||||
}
|
||||
|
|
@ -75,7 +87,7 @@ func Luhn(payload string) bool {
|
|||
}
|
||||
|
||||
// Fletcher16 computes the Fletcher-16 checksum.
|
||||
func Fletcher16(payload string) uint16 {
|
||||
func (s *Service) Fletcher16(payload string) uint16 {
|
||||
data := []byte(payload)
|
||||
var sum1, sum2 uint16
|
||||
for _, b := range data {
|
||||
|
|
@ -86,7 +98,7 @@ func Fletcher16(payload string) uint16 {
|
|||
}
|
||||
|
||||
// Fletcher32 computes the Fletcher-32 checksum.
|
||||
func Fletcher32(payload string) uint32 {
|
||||
func (s *Service) Fletcher32(payload string) uint32 {
|
||||
data := []byte(payload)
|
||||
if len(data)%2 != 0 {
|
||||
data = append(data, 0)
|
||||
|
|
@ -102,7 +114,7 @@ func Fletcher32(payload string) uint32 {
|
|||
}
|
||||
|
||||
// Fletcher64 computes the Fletcher-64 checksum.
|
||||
func Fletcher64(payload string) uint64 {
|
||||
func (s *Service) Fletcher64(payload string) uint64 {
|
||||
data := []byte(payload)
|
||||
if len(data)%4 != 0 {
|
||||
padding := 4 - (len(data) % 4)
|
||||
|
|
@ -127,7 +139,7 @@ func Fletcher64(payload string) uint64 {
|
|||
// import "github.com/Snider/Enchantrix/openpgp"
|
||||
//
|
||||
// // EncryptPGP encrypts data for a recipient, optionally signing it.
|
||||
// func EncryptPGP(writer io.Writer, recipientPath, data string, signerPath, signerPassphrase *string) error {
|
||||
// func (s *Service) EncryptPGP(writer io.Writer, recipientPath, data string, signerPath, signerPassphrase *string) error {
|
||||
// var buf bytes.Buffer
|
||||
// err := openpgp.EncryptPGP(&buf, recipientPath, data, signerPath, signerPassphrase)
|
||||
// if err != nil {
|
||||
|
|
@ -143,6 +155,6 @@ func Fletcher64(payload string) uint64 {
|
|||
// }
|
||||
//
|
||||
// // DecryptPGP decrypts a PGP message, optionally verifying the signature.
|
||||
// func DecryptPGP(recipientPath, message, passphrase string, signerPath *string) (string, error) {
|
||||
// func (s *Service) DecryptPGP(recipientPath, message, passphrase string, signerPath *string) (string, error) {
|
||||
// return openpgp.DecryptPGP(recipientPath, message, passphrase, signerPath)
|
||||
// }
|
||||
109
pkg/crypt/crypt_test.go
Normal file
109
pkg/crypt/crypt_test.go
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
package crypt
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var service = NewService()
|
||||
|
||||
// --- Hashing Tests ---
|
||||
|
||||
func TestHash_Good(t *testing.T) {
|
||||
payload := "hello"
|
||||
// Test all supported hash types
|
||||
for _, hashType := range []HashType{LTHN, SHA512, SHA256, SHA1, MD5} {
|
||||
hash := service.Hash(hashType, payload)
|
||||
assert.NotEmpty(t, hash, "Hash should not be empty for type %s", hashType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHash_Bad(t *testing.T) {
|
||||
// Using an unsupported hash type should default to SHA256
|
||||
hash := service.Hash("unsupported", "hello")
|
||||
expectedHash := service.Hash(SHA256, "hello")
|
||||
assert.Equal(t, expectedHash, hash)
|
||||
}
|
||||
|
||||
func TestHash_Ugly(t *testing.T) {
|
||||
// Test with potentially problematic inputs
|
||||
testCases := []string{
|
||||
"", // Empty string
|
||||
" ", // Whitespace
|
||||
"\x00\x01\x02\x03\x04", // Null bytes
|
||||
strings.Repeat("a", 1024*1024), // Large payload (1MB)
|
||||
"こんにちは", // Unicode characters
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
for _, hashType := range []HashType{LTHN, SHA512, SHA256, SHA1, MD5} {
|
||||
hash := service.Hash(hashType, tc)
|
||||
assert.NotEmpty(t, hash, "Hash for ugly input should not be empty for type %s", hashType)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Checksum Tests ---
|
||||
|
||||
// Luhn Tests
|
||||
func TestLuhn_Good(t *testing.T) {
|
||||
assert.True(t, service.Luhn("79927398713"))
|
||||
}
|
||||
|
||||
func TestLuhn_Bad(t *testing.T) {
|
||||
assert.False(t, service.Luhn("79927398714"), "Should fail for incorrect checksum")
|
||||
assert.False(t, service.Luhn("7992739871a"), "Should fail for non-numeric input")
|
||||
}
|
||||
|
||||
func TestLuhn_Ugly(t *testing.T) {
|
||||
assert.False(t, service.Luhn(""), "Should be false for empty string")
|
||||
assert.False(t, service.Luhn(" 1 2 3 "), "Should handle spaces but result in false")
|
||||
}
|
||||
|
||||
// Fletcher16 Tests
|
||||
func TestFletcher16_Good(t *testing.T) {
|
||||
assert.Equal(t, uint16(0xC8F0), service.Fletcher16("abcde"))
|
||||
assert.Equal(t, uint16(0x2057), service.Fletcher16("abcdef"))
|
||||
assert.Equal(t, uint16(0x0627), service.Fletcher16("abcdefgh"))
|
||||
}
|
||||
|
||||
func TestFletcher16_Bad(t *testing.T) {
|
||||
// No obviously "bad" inputs that don't fall into "ugly"
|
||||
// For Fletcher, any string is a valid input.
|
||||
}
|
||||
|
||||
func TestFletcher16_Ugly(t *testing.T) {
|
||||
assert.Equal(t, uint16(0), service.Fletcher16(""), "Checksum of empty string should be 0")
|
||||
}
|
||||
|
||||
// Fletcher32 Tests
|
||||
func TestFletcher32_Good(t *testing.T) {
|
||||
assert.Equal(t, uint32(0xF04FC729), service.Fletcher32("abcde"))
|
||||
assert.Equal(t, uint32(0x56502D2A), service.Fletcher32("abcdef"))
|
||||
assert.Equal(t, uint32(0xEBE19591), service.Fletcher32("abcdefgh"))
|
||||
}
|
||||
|
||||
func TestFletcher32_Bad(t *testing.T) {
|
||||
// Any string is a valid input.
|
||||
}
|
||||
|
||||
func TestFletcher32_Ugly(t *testing.T) {
|
||||
assert.Equal(t, uint32(0), service.Fletcher32(""), "Checksum of empty string should be 0")
|
||||
}
|
||||
|
||||
// Fletcher64 Tests
|
||||
func TestFletcher64_Good(t *testing.T) {
|
||||
assert.Equal(t, uint64(0xc8c6c527646362c6), service.Fletcher64("abcde"))
|
||||
assert.Equal(t, uint64(0xc8c72b276463c8c6), service.Fletcher64("abcdef"))
|
||||
assert.Equal(t, uint64(0x312e2b28cccac8c6), service.Fletcher64("abcdefgh"))
|
||||
}
|
||||
|
||||
func TestFletcher64_Bad(t *testing.T) {
|
||||
// Any string is a valid input.
|
||||
}
|
||||
|
||||
func TestFletcher64_Ugly(t *testing.T) {
|
||||
assert.Equal(t, uint64(0), service.Fletcher64(""), "Checksum of empty string should be 0")
|
||||
}
|
||||
3
pkg/crypt/std/rsa/rsa.go
Normal file
3
pkg/crypt/std/rsa/rsa.go
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
package rsa
|
||||
|
||||
// This file is a placeholder for RSA key handling functionality.
|
||||
|
|
@ -5,17 +5,18 @@ import (
|
|||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
const (
|
||||
MagicNumber = "TRIX"
|
||||
Version = 2
|
||||
Version = 2
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidMagicNumber = errors.New("trix: invalid magic number")
|
||||
ErrInvalidVersion = errors.New("trix: invalid version")
|
||||
ErrMagicNumberLength = errors.New("trix: magic number must be 4 bytes long")
|
||||
)
|
||||
|
||||
// Trix represents the structure of a .trix file.
|
||||
|
|
@ -25,7 +26,11 @@ type Trix struct {
|
|||
}
|
||||
|
||||
// Encode serializes a Trix struct into the .trix binary format.
|
||||
func Encode(trix *Trix) ([]byte, error) {
|
||||
func Encode(trix *Trix, magicNumber string) ([]byte, error) {
|
||||
if len(magicNumber) != 4 {
|
||||
return nil, ErrMagicNumberLength
|
||||
}
|
||||
|
||||
headerBytes, err := json.Marshal(trix.Header)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -35,7 +40,7 @@ func Encode(trix *Trix) ([]byte, error) {
|
|||
buf := new(bytes.Buffer)
|
||||
|
||||
// Write Magic Number
|
||||
if _, err := buf.WriteString(MagicNumber); err != nil {
|
||||
if _, err := buf.WriteString(magicNumber); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
@ -63,7 +68,11 @@ func Encode(trix *Trix) ([]byte, error) {
|
|||
}
|
||||
|
||||
// Decode deserializes the .trix binary format into a Trix struct.
|
||||
func Decode(data []byte) (*Trix, error) {
|
||||
func Decode(data []byte, magicNumber string) (*Trix, error) {
|
||||
if len(magicNumber) != 4 {
|
||||
return nil, ErrMagicNumberLength
|
||||
}
|
||||
|
||||
buf := bytes.NewReader(data)
|
||||
|
||||
// Read and Verify Magic Number
|
||||
|
|
@ -71,8 +80,8 @@ func Decode(data []byte) (*Trix, error) {
|
|||
if _, err := io.ReadFull(buf, magic); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if string(magic) != MagicNumber {
|
||||
return nil, ErrInvalidMagicNumber
|
||||
if string(magic) != magicNumber {
|
||||
return nil, fmt.Errorf("%w: expected %s, got %s", ErrInvalidMagicNumber, magicNumber, string(magic))
|
||||
}
|
||||
|
||||
// Read and Verify Version
|
||||
113
pkg/trix/trix_test.go
Normal file
113
pkg/trix/trix_test.go
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
package trix
|
||||
|
||||
import (
|
||||
"io"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestTrixEncodeDecode_Good tests the ideal "happy path" scenario for encoding and decoding.
|
||||
func TestTrixEncodeDecode_Good(t *testing.T) {
|
||||
header := map[string]interface{}{
|
||||
"content_type": "application/octet-stream",
|
||||
"encryption_algorithm": "chacha20poly1035",
|
||||
"nonce": "AAECAwQFBgcICQoLDA0ODxAREhMUFRY=",
|
||||
"created_at": "2025-10-30T12:00:00Z",
|
||||
}
|
||||
payload := []byte("This is a secret message.")
|
||||
trix := &Trix{Header: header, Payload: payload}
|
||||
magicNumber := "TRIX"
|
||||
|
||||
encoded, err := Encode(trix, magicNumber)
|
||||
assert.NoError(t, err)
|
||||
|
||||
decoded, err := Decode(encoded, magicNumber)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.True(t, reflect.DeepEqual(trix.Header, decoded.Header))
|
||||
assert.Equal(t, trix.Payload, decoded.Payload)
|
||||
}
|
||||
|
||||
// TestTrixEncodeDecode_Bad tests expected failure scenarios with well-formed but invalid inputs.
|
||||
func TestTrixEncodeDecode_Bad(t *testing.T) {
|
||||
t.Run("MismatchedMagicNumber", func(t *testing.T) {
|
||||
trix := &Trix{Header: map[string]interface{}{}, Payload: []byte("payload")}
|
||||
encoded, err := Encode(trix, "GOOD")
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = Decode(encoded, "BAD!")
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "invalid magic number")
|
||||
})
|
||||
|
||||
t.Run("InvalidMagicNumberLength", func(t *testing.T) {
|
||||
trix := &Trix{Header: map[string]interface{}{}, Payload: []byte("payload")}
|
||||
_, err := Encode(trix, "TOOLONG")
|
||||
assert.EqualError(t, err, "trix: magic number must be 4 bytes long")
|
||||
|
||||
_, err = Decode([]byte{}, "SHORT")
|
||||
assert.EqualError(t, err, "trix: magic number must be 4 bytes long")
|
||||
})
|
||||
|
||||
t.Run("MalformedHeaderJSON", func(t *testing.T) {
|
||||
// Create a Trix struct with a header that cannot be marshaled to JSON.
|
||||
header := map[string]interface{}{
|
||||
"unsupported": make(chan int), // Channels cannot be JSON-encoded
|
||||
}
|
||||
trix := &Trix{Header: header, Payload: []byte("payload")}
|
||||
_, err := Encode(trix, "TRIX")
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "json: unsupported type")
|
||||
})
|
||||
}
|
||||
|
||||
// TestTrixEncodeDecode_Ugly tests malicious or malformed inputs designed to cause crashes or panics.
|
||||
func TestTrixEncodeDecode_Ugly(t *testing.T) {
|
||||
magicNumber := "UGLY"
|
||||
|
||||
t.Run("CorruptedHeaderLength", func(t *testing.T) {
|
||||
// Manually construct a byte slice where the header length is larger than the actual data.
|
||||
var buf []byte
|
||||
buf = append(buf, []byte(magicNumber)...) // Magic Number
|
||||
buf = append(buf, byte(Version)) // Version
|
||||
// Header length of 1000, but the header is only 2 bytes long.
|
||||
buf = append(buf, []byte{0, 0, 3, 232}...) // BigEndian representation of 1000
|
||||
buf = append(buf, []byte("{}")...) // A minimal valid JSON header
|
||||
buf = append(buf, []byte("payload")...)
|
||||
|
||||
_, err := Decode(buf, magicNumber)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, err, io.ErrUnexpectedEOF)
|
||||
})
|
||||
|
||||
t.Run("DataTooShort", func(t *testing.T) {
|
||||
// Data is too short to contain even the magic number.
|
||||
data := []byte("BAD")
|
||||
_, err := Decode(data, magicNumber)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("EmptyPayload", func(t *testing.T) {
|
||||
data := []byte{}
|
||||
_, err := Decode(data, magicNumber)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("FuzzedJSON", func(t *testing.T) {
|
||||
// A header that is technically valid but contains unexpected types.
|
||||
header := map[string]interface{}{
|
||||
"payload": map[string]interface{}{"nested": 123},
|
||||
}
|
||||
payload := []byte("some data")
|
||||
trix := &Trix{Header: header, Payload: payload}
|
||||
|
||||
encoded, err := Encode(trix, magicNumber)
|
||||
assert.NoError(t, err)
|
||||
|
||||
decoded, err := Decode(encoded, magicNumber)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, decoded)
|
||||
})
|
||||
}
|
||||
11
rootfs.go
11
rootfs.go
|
|
@ -1,11 +0,0 @@
|
|||
package crypt
|
||||
|
||||
import "github.com/Snider/Enchantrix/rootfs"
|
||||
|
||||
// Storage is an alias for the rootfs.Storage interface.
|
||||
type Storage = rootfs.Storage
|
||||
|
||||
// NewRootFS creates a new encrypted passthrough storage system.
|
||||
func NewRootFS(root string, key []byte) Storage {
|
||||
return rootfs.NewLocalStorage(root, key)
|
||||
}
|
||||
|
|
@ -1,71 +0,0 @@
|
|||
package rootfs
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/Snider/Enchantrix/chachapoly"
|
||||
)
|
||||
|
||||
// LocalStorage provides a passthrough storage system that encrypts data at rest.
|
||||
type LocalStorage struct {
|
||||
root string
|
||||
key []byte
|
||||
filePerm fs.FileMode
|
||||
dirPerm fs.FileMode
|
||||
}
|
||||
|
||||
// NewLocalStorage creates a new LocalStorage.
|
||||
func NewLocalStorage(root string, key []byte) *LocalStorage {
|
||||
return &LocalStorage{
|
||||
root: root,
|
||||
key: key,
|
||||
filePerm: 0644,
|
||||
dirPerm: 0755,
|
||||
}
|
||||
}
|
||||
|
||||
// Read reads and decrypts the data for the given key.
|
||||
func (s *LocalStorage) Read(key string) ([]byte, error) {
|
||||
path := filepath.Join(s.root, key)
|
||||
ciphertext, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return chachapoly.Decrypt(ciphertext, s.key)
|
||||
}
|
||||
|
||||
// Write encrypts and writes the data for the given key.
|
||||
func (s *LocalStorage) Write(key string, data []byte) error {
|
||||
ciphertext, err := chachapoly.Encrypt(data, s.key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
path := filepath.Join(s.root, key)
|
||||
if err := os.MkdirAll(filepath.Dir(path), s.dirPerm); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, ciphertext, s.filePerm)
|
||||
}
|
||||
|
||||
// Delete deletes the data for the given key.
|
||||
func (s *LocalStorage) Delete(key string) error {
|
||||
path := filepath.Join(s.root, key)
|
||||
return os.Remove(path)
|
||||
}
|
||||
|
||||
// List lists the keys in the storage.
|
||||
func (s *LocalStorage) List(prefix string) ([]fs.FileInfo, error) {
|
||||
var files []fs.FileInfo
|
||||
err := filepath.Walk(filepath.Join(s.root, prefix), func(path string, info fs.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() {
|
||||
files = append(files, info)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return files, err
|
||||
}
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
package rootfs
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLocalStorage(t *testing.T) {
|
||||
// Create a temporary directory for testing.
|
||||
tempDir, err := os.MkdirTemp("", "enchantrix-test")
|
||||
assert.NoError(t, err)
|
||||
defer os.RemoveAll(tempDir)
|
||||
|
||||
// Create a new LocalStorage instance.
|
||||
key := make([]byte, 32)
|
||||
for i := range key {
|
||||
key[i] = 1
|
||||
}
|
||||
storage := NewLocalStorage(tempDir, key)
|
||||
|
||||
// Test Write and Read.
|
||||
err = storage.Write("test.txt", []byte("hello"))
|
||||
assert.NoError(t, err)
|
||||
data, err := storage.Read("test.txt")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("hello"), data)
|
||||
|
||||
// Test List.
|
||||
files, err := storage.List("")
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, files, 1)
|
||||
assert.Equal(t, "test.txt", files[0].Name())
|
||||
|
||||
// Test Delete.
|
||||
err = storage.Delete("test.txt")
|
||||
assert.NoError(t, err)
|
||||
_, err = os.Stat(filepath.Join(tempDir, "test.txt"))
|
||||
assert.True(t, os.IsNotExist(err))
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
package rootfs
|
||||
|
||||
import "io/fs"
|
||||
|
||||
// Storage defines the interface for a passthrough storage system.
|
||||
type Storage interface {
|
||||
// Read reads the data for the given key.
|
||||
Read(key string) ([]byte, error)
|
||||
// Write writes the data for the given key.
|
||||
Write(key string, data []byte) error
|
||||
// Delete deletes the data for the given key.
|
||||
Delete(key string) error
|
||||
// List lists the keys in the storage.
|
||||
List(prefix string) ([]fs.FileInfo, error)
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
package trix
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestEncodeDecode(t *testing.T) {
|
||||
header := map[string]interface{}{
|
||||
"content_type": "application/octet-stream",
|
||||
"encryption_algorithm": "chacha20poly1035",
|
||||
"nonce": "AAECAwQFBgcICQoLDA0ODxAREhMUFRY=",
|
||||
"created_at": "2025-10-30T12:00:00Z",
|
||||
}
|
||||
payload := []byte("This is a secret message.")
|
||||
|
||||
trix := &Trix{
|
||||
Header: header,
|
||||
Payload: payload,
|
||||
}
|
||||
|
||||
encoded, err := Encode(trix)
|
||||
assert.NoError(t, err)
|
||||
|
||||
decoded, err := Decode(encoded)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.True(t, reflect.DeepEqual(trix.Header, decoded.Header))
|
||||
assert.Equal(t, trix.Payload, decoded.Payload)
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue