mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-06-23 04:10:25 +00:00
c59a413765
CI / lint (push) Has been cancelled
CI / test (agent) (push) Has been cancelled
CI / test (cli) (push) Has been cancelled
CI / test (cmd) (push) Has been cancelled
CI / test (internal) (push) Has been cancelled
CI / test (manager, true) (push) Has been cancelled
CI / test (pkg) (push) Has been cancelled
CI / upload-coverage (push) Has been cancelled
* feat: implement extensible resource downloader framework with support for S3, GCS, and OCI sources Signed-off-by: SammyOina <sammyoina@gmail.com> * refactor: improve resource URL parsing and add support for bare OCI image references Signed-off-by: Sammy Oina <sammyoina@gmail.com> * fix: add empty string check and slash requirement for OCI image inference, and update python unit tests with event mock expectations Signed-off-by: Sammy Oina <sammyoina@gmail.com> * refactor: introduce OCIClient interface, add test coverage for decryption, and improve resource download error handling Signed-off-by: Sammy Oina <sammyoina@gmail.com> * chore: remove trailing whitespace in OCI downloader and HTTP tests Signed-off-by: Sammy Oina <sammyoina@gmail.com> --------- Signed-off-by: SammyOina <sammyoina@gmail.com> Signed-off-by: Sammy Oina <sammyoina@gmail.com>
81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package resource
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/rand"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestDecryptFile(t *testing.T) {
|
|
key := make([]byte, 32)
|
|
_, err := io.ReadFull(rand.Reader, key)
|
|
require.NoError(t, err)
|
|
|
|
plaintext := []byte("hello world")
|
|
|
|
// Encrypt data
|
|
block, err := aes.NewCipher(key)
|
|
require.NoError(t, err)
|
|
|
|
gcm, err := cipher.NewGCM(block)
|
|
require.NoError(t, err)
|
|
|
|
nonce := make([]byte, gcm.NonceSize())
|
|
_, err = io.ReadFull(rand.Reader, nonce)
|
|
require.NoError(t, err)
|
|
|
|
ciphertext := gcm.Seal(nonce, nonce, plaintext, nil)
|
|
|
|
tmpDir := t.TempDir()
|
|
encryptedPath := filepath.Join(tmpDir, "encrypted.bin")
|
|
err = os.WriteFile(encryptedPath, ciphertext, 0o644)
|
|
require.NoError(t, err)
|
|
|
|
t.Run("Successful decryption", func(t *testing.T) {
|
|
decrypted, err := DecryptFile(encryptedPath, key)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, plaintext, decrypted)
|
|
})
|
|
|
|
t.Run("Invalid key size", func(t *testing.T) {
|
|
_, err := DecryptFile(encryptedPath, key[:16])
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "invalid key size")
|
|
})
|
|
|
|
t.Run("File not found", func(t *testing.T) {
|
|
_, err := DecryptFile(filepath.Join(tmpDir, "nonexistent"), key)
|
|
assert.Error(t, err)
|
|
})
|
|
|
|
t.Run("Ciphertext too short", func(t *testing.T) {
|
|
shortPath := filepath.Join(tmpDir, "short.bin")
|
|
err = os.WriteFile(shortPath, []byte("short"), 0o644)
|
|
require.NoError(t, err)
|
|
|
|
_, err = DecryptFile(shortPath, key)
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "ciphertext too short")
|
|
})
|
|
|
|
t.Run("Decryption failed (auth error)", func(t *testing.T) {
|
|
wrongKey := make([]byte, 32)
|
|
_, err := io.ReadFull(rand.Reader, wrongKey)
|
|
require.NoError(t, err)
|
|
|
|
_, err = DecryptFile(encryptedPath, wrongKey)
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "decryption failed")
|
|
})
|
|
}
|