Files
upstream-seaweedfs/weed/s3api/s3_sse_ctr_test.go
upstream-mirror 89f5be005d
Some checks failed
go: build dev binaries / cleanup (push) Successful in 4s
Codespell / Check for spelling errors (push) Failing after 3s
docker: build dev containers / build-rust-binaries (amd64, x86_64-unknown-linux-musl) (push) Failing after 3s
docker: build dev containers / build-dev-containers (push) Has been skipped
docker: build dev containers / build-rust-binaries (arm64, true, aarch64-unknown-linux-musl) (push) Failing after 3s
End to End / FUSE Mount (push) Failing after 2s
EC Integration Tests / EC Integration Tests (push) Failing after 30s
FUSE DLM Integration Tests / FUSE DLM Integration Tests (push) Failing after 3s
FUSE Volume Server Failover Tests / FUSE Volume Server Failover (push) Failing after 3s
FUSE Integration Tests / FUSE Integration Testing (push) Failing after 2s
FUSE P2P Peer Chunk Sharing Integration Tests / FUSE P2P Peer Chunk Sharing (push) Failing after 3s
go: build binary / Go Vet (push) Failing after 2s
go: build binary / Go Vet 32-bit (push) Failing after 3s
go: build binary / Build (push) Failing after 4s
go: build binary / Build cross-platform (freebsd/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (darwin/arm64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/arm64) (push) Failing after 3s
go: build binary / Test (push) Failing after 2s
go: build binary / Test 32-bit (push) Failing after 3s
helm: lint and test charts / lint-test (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (11) (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (17) (push) Failing after 3s
go: build dev binaries / build_dev_linux_windows (amd64, linux) (push) Failing after 2s
go: build dev binaries / build_dev_linux_windows (amd64, windows) (push) Failing after 3s
go: build dev binaries / build_dev_darwin (amd64, darwin) (push) Has been skipped
go: build dev binaries / build_dev_darwin (arm64, darwin) (push) Has been skipped
Java Client Unit Tests / Java Unit Tests (11) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (17) (push) Failing after 2s
Java Client Unit Tests / Java Unit Tests (21) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (8) (push) Failing after 3s
Kafka Quick Test (Load Test with Schema Registry) / Kafka Client Load Test (Quick) (push) Failing after 3s
Kafka Gateway Tests / Kafka Unit Tests (unit-tests-1) (push) Failing after 11s
Kafka Gateway Tests / Kafka Integration Tests (Critical) (integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka End-to-End Tests (with SMQ) (e2e-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Consumer Group Tests (Highly Isolated) (consumer-group-1) (push) Failing after 2s
Kafka Gateway Tests / Kafka Client Compatibility (with SMQ) (client-compat-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka SMQ Integration Tests (Full Stack) (smq-integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Protocol Tests (Isolated) (protocol-1) (push) Failing after 3s
KMS Tests / KMS Provider Integration Tests (push) Failing after 2s
KMS Tests / S3 KMS End-to-End Tests (push) Failing after 3s
Master Cold Start Tests / Master Cold Start Tests (push) Failing after 4s
Metadata Subscribe Integration Tests / Metadata Subscribe Integration Tests (push) Failing after 3s
Multi-Master Tests / Multi-Master Failover Tests (push) Failing after 3s
Performance / CPU and Heap Profile (push) Failing after 2s
Performance / S3 Read/Write Benchmark (rust volume) (push) Failing after 3s
pjdfstest POSIX Compliance / pjdfstest (push) Failing after 2s
Plugin Worker Integration Tests / Plugin Worker: erasure_coding (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: vacuum (push) Failing after 3s
Rust Volume Server Tests / Rust Integration Tests (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 1) (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 1) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 3) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Distributed Tests (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Performance Tests (push) Failing after 3s
Performance / Throughput Benchmark (go volume) (push) Failing after 3s
Performance / Throughput Benchmark (rust volume) (push) Failing after 2s
Performance / S3 Read/Write Benchmark (go volume) (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: volume_balance (push) Failing after 3s
PostgreSQL Gateway Tests / PostgreSQL Basic Tests (push) Failing after 3s
Rust Volume Server Tests / Rust Unit Tests (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 3) (push) Failing after 3s
Rust Plugin Worker Tests / Rust Plugin Worker Build and Unit Tests (push) Failing after 3s
rust: build dev volume server binaries / cleanup (push) Successful in 3s
S3 ETag and ACL Tests / S3 ETag + ACL Integration Tests (push) Failing after 3s
S3 IAM Integration Tests / IAM Unit Tests (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (advanced) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (basic) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (group) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (policy-enforcement) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (sts) (push) Failing after 4s
S3 Keycloak Integration Tests / S3 Keycloak Integration Tests (push) Failing after 3s
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.11) (push) Failing after 3s
S3 PyArrow Parquet Tests / Go Unit Tests (Implicit Directory) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.12) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-amd64, x86_64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-arm64, aarch64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-linux (linux-amd64, x86_64-unknown-linux-gnu) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.9) (push) Has been cancelled
S3 Proxy Signature Tests / S3 Proxy Signature Verification Tests (push) Has been cancelled
S3 SDK V2 Route Disambiguation Tests / S3 SDK V2 Routing Tests (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (quick) (push) Has been cancelled
S3 SSE Tests / S3 SSE Compatibility Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Metadata Persistence Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Copy Operations Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Multipart Upload Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Performance Test (push) Has been cancelled
S3 SSE Tests / S3 Volume Encryption Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (comprehensive) (push) Has been cancelled
Spark Integration Tests / Spark Integration Tests (push) Failing after 2s
Telemetry Integration Tests / telemetry-integration-test (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 1) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 1) (push) Failing after 3s
Ceph S3 tests / Basic S3 tests (KV store) (push) Has been cancelled
Ceph S3 tests / S3 Versioning & Object Lock tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Unit Tests (push) Failing after 3s
Ceph S3 tests / S3 CORS tests (push) Has been cancelled
Ceph S3 tests / SeaweedFS Custom S3 Copy tests (push) Has been cancelled
Ceph S3 tests / Basic S3 tests (SQL store) (push) Has been cancelled
test s3 over https using aws-cli / awscli-tests (push) Has been cancelled
TLS Rotation Integration Tests / TLS Rotation Integration Tests (push) Has been cancelled
Vacuum Integration Tests / Vacuum Integration Tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Variables Integration Tests (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (advanced-policy) (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (basic-policy) (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (contextual-policy) (push) Failing after 4s
S3 Policy Integration Tests / Trusted Proxy Detection Tests (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Shell Integration Tests (push) Failing after 3s
Samba on FUSE Integration / samba-integration (push) Failing after 2s
SFTP Integration Tests / SFTP Integration Testing (push) Failing after 3s
terraform: validate and test modules / fmt, validate, plan-level tests (push) Failing after 3s
terraform: validate and test modules / local cluster smoke test (real weed) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 3) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 2) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 2) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 3) (push) Failing after 5s
mount: windows conformance / WinFsp conformance (push) Has been cancelled
mount: windows / Mount on Windows (push) Has been cancelled
snapshot of https://github.com/seaweedfs/seaweedfs.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 03:23:42 +05:00

308 lines
9.4 KiB
Go

package s3api
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
"testing"
)
// TestCalculateIVWithOffset tests the calculateIVWithOffset function
func TestCalculateIVWithOffset(t *testing.T) {
baseIV := make([]byte, 16)
rand.Read(baseIV)
tests := []struct {
name string
offset int64
expectedSkip int
expectedBlock int64
}{
{"BlockAligned_0", 0, 0, 0},
{"BlockAligned_16", 16, 0, 1},
{"BlockAligned_32", 32, 0, 2},
{"BlockAligned_48", 48, 0, 3},
{"NonAligned_1", 1, 1, 0},
{"NonAligned_5", 5, 5, 0},
{"NonAligned_10", 10, 10, 0},
{"NonAligned_15", 15, 15, 0},
{"NonAligned_17", 17, 1, 1},
{"NonAligned_21", 21, 5, 1},
{"NonAligned_33", 33, 1, 2},
{"NonAligned_47", 47, 15, 2},
{"LargeOffset", 1000, 1000 % 16, 1000 / 16},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
adjustedIV, skip := calculateIVWithOffset(baseIV, tt.offset)
// Verify skip is correct
if skip != tt.expectedSkip {
t.Errorf("calculateIVWithOffset(%d) skip = %d, want %d", tt.offset, skip, tt.expectedSkip)
}
// Verify IV length is preserved
if len(adjustedIV) != 16 {
t.Errorf("calculateIVWithOffset(%d) IV length = %d, want 16", tt.offset, len(adjustedIV))
}
// Verify IV was adjusted correctly (last 8 bytes incremented by blockOffset)
if tt.expectedBlock == 0 {
if !bytes.Equal(adjustedIV, baseIV) {
t.Errorf("calculateIVWithOffset(%d) IV changed when blockOffset=0", tt.offset)
}
} else {
// IV should be different for non-zero block offsets
if bytes.Equal(adjustedIV, baseIV) {
t.Errorf("calculateIVWithOffset(%d) IV not changed when blockOffset=%d", tt.offset, tt.expectedBlock)
}
}
})
}
}
// TestCTRDecryptionWithNonBlockAlignedOffset tests that CTR decryption works correctly
// for non-block-aligned offsets (the critical bug fix)
func TestCTRDecryptionWithNonBlockAlignedOffset(t *testing.T) {
// Generate test data
plaintext := make([]byte, 1024)
for i := range plaintext {
plaintext[i] = byte(i % 256)
}
// Generate random key and IV
key := make([]byte, 32) // AES-256
iv := make([]byte, 16)
rand.Read(key)
rand.Read(iv)
// Encrypt the entire plaintext
block, err := aes.NewCipher(key)
if err != nil {
t.Fatalf("Failed to create cipher: %v", err)
}
ciphertext := make([]byte, len(plaintext))
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(ciphertext, plaintext)
// Test various offsets (both block-aligned and non-block-aligned)
testOffsets := []int64{0, 1, 5, 10, 15, 16, 17, 21, 32, 33, 47, 48, 100, 500}
for _, offset := range testOffsets {
t.Run(string(rune('A'+offset)), func(t *testing.T) {
// Calculate adjusted IV and skip
adjustedIV, skip := calculateIVWithOffset(iv, offset)
// CRITICAL: Start from the block-aligned offset, not the user offset
// CTR mode works on 16-byte blocks, so we need to decrypt from the block start
blockAlignedOffset := offset - int64(skip)
// Decrypt from the block-aligned offset
decryptBlock, err := aes.NewCipher(key)
if err != nil {
t.Fatalf("Failed to create decrypt cipher: %v", err)
}
decryptStream := cipher.NewCTR(decryptBlock, adjustedIV)
// Create a reader for the ciphertext starting at block-aligned offset
ciphertextFromBlockStart := ciphertext[blockAlignedOffset:]
decryptedFromBlockStart := make([]byte, len(ciphertextFromBlockStart))
decryptStream.XORKeyStream(decryptedFromBlockStart, ciphertextFromBlockStart)
// CRITICAL: Skip the intra-block bytes to get to the user-requested offset
if skip > 0 {
if skip > len(decryptedFromBlockStart) {
t.Fatalf("Skip %d exceeds decrypted data length %d", skip, len(decryptedFromBlockStart))
}
decryptedFromBlockStart = decryptedFromBlockStart[skip:]
}
// Rename for consistency
decryptedFromOffset := decryptedFromBlockStart
// Verify decrypted data matches original plaintext
expectedPlaintext := plaintext[offset:]
if !bytes.Equal(decryptedFromOffset, expectedPlaintext) {
t.Errorf("Decryption mismatch at offset %d (skip=%d)", offset, skip)
previewLen := 32
if len(expectedPlaintext) < previewLen {
previewLen = len(expectedPlaintext)
}
t.Errorf(" Expected first 32 bytes: %x", expectedPlaintext[:previewLen])
previewLen2 := 32
if len(decryptedFromOffset) < previewLen2 {
previewLen2 = len(decryptedFromOffset)
}
t.Errorf(" Got first 32 bytes: %x", decryptedFromOffset[:previewLen2])
// Find first mismatch
for i := 0; i < len(expectedPlaintext) && i < len(decryptedFromOffset); i++ {
if expectedPlaintext[i] != decryptedFromOffset[i] {
t.Errorf(" First mismatch at byte %d: expected %02x, got %02x", i, expectedPlaintext[i], decryptedFromOffset[i])
break
}
}
}
})
}
}
// TestCTRRangeRequestSimulation simulates a real-world S3 range request scenario
func TestCTRRangeRequestSimulation(t *testing.T) {
// Simulate uploading a 5MB object
objectSize := 5 * 1024 * 1024
plaintext := make([]byte, objectSize)
for i := range plaintext {
plaintext[i] = byte(i % 256)
}
// Encrypt the object
key := make([]byte, 32)
iv := make([]byte, 16)
rand.Read(key)
rand.Read(iv)
block, err := aes.NewCipher(key)
if err != nil {
t.Fatalf("Failed to create cipher: %v", err)
}
ciphertext := make([]byte, len(plaintext))
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(ciphertext, plaintext)
// Simulate various S3 range requests
rangeTests := []struct {
name string
start int64
end int64
}{
{"First byte", 0, 0},
{"First 100 bytes", 0, 99},
{"Mid-block range", 5, 100}, // Critical: starts at non-aligned offset
{"Single mid-block byte", 17, 17}, // Critical: single byte at offset 17
{"Cross-block range", 10, 50}, // Spans multiple blocks
{"Large range", 1000, 10000},
{"Tail range", int64(objectSize - 1000), int64(objectSize - 1)},
}
for _, rt := range rangeTests {
t.Run(rt.name, func(t *testing.T) {
rangeSize := rt.end - rt.start + 1
// Calculate adjusted IV and skip for the range start
adjustedIV, skip := calculateIVWithOffset(iv, rt.start)
// CRITICAL: Start decryption from block-aligned offset
blockAlignedStart := rt.start - int64(skip)
// Create decryption stream
decryptBlock, err := aes.NewCipher(key)
if err != nil {
t.Fatalf("Failed to create decrypt cipher: %v", err)
}
decryptStream := cipher.NewCTR(decryptBlock, adjustedIV)
// Decrypt from block-aligned start through the end of range
ciphertextFromBlock := ciphertext[blockAlignedStart : rt.end+1]
decryptedFromBlock := make([]byte, len(ciphertextFromBlock))
decryptStream.XORKeyStream(decryptedFromBlock, ciphertextFromBlock)
// CRITICAL: Skip intra-block bytes to get to user-requested start
if skip > 0 {
decryptedFromBlock = decryptedFromBlock[skip:]
}
decryptedRange := decryptedFromBlock
// Verify decrypted range matches original plaintext
expectedPlaintext := plaintext[rt.start : rt.end+1]
if !bytes.Equal(decryptedRange, expectedPlaintext) {
t.Errorf("Range decryption mismatch for %s (offset=%d, size=%d, skip=%d)",
rt.name, rt.start, rangeSize, skip)
previewLen := 64
if len(expectedPlaintext) < previewLen {
previewLen = len(expectedPlaintext)
}
t.Errorf(" Expected: %x", expectedPlaintext[:previewLen])
previewLen2 := previewLen
if len(decryptedRange) < previewLen2 {
previewLen2 = len(decryptedRange)
}
t.Errorf(" Got: %x", decryptedRange[:previewLen2])
}
})
}
}
// TestCTRDecryptionWithIOReader tests the integration with io.Reader
func TestCTRDecryptionWithIOReader(t *testing.T) {
plaintext := []byte("Hello, World! This is a test of CTR mode decryption with non-aligned offsets.")
key := make([]byte, 32)
iv := make([]byte, 16)
rand.Read(key)
rand.Read(iv)
// Encrypt
block, err := aes.NewCipher(key)
if err != nil {
t.Fatalf("Failed to create cipher: %v", err)
}
ciphertext := make([]byte, len(plaintext))
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(ciphertext, plaintext)
// Test reading from various offsets using io.Reader
testOffsets := []int64{0, 5, 10, 16, 17, 30}
for _, offset := range testOffsets {
t.Run(string(rune('A'+offset)), func(t *testing.T) {
// Calculate adjusted IV and skip
adjustedIV, skip := calculateIVWithOffset(iv, offset)
// CRITICAL: Start reading from block-aligned offset in ciphertext
blockAlignedOffset := offset - int64(skip)
// Create decrypted reader
decryptBlock, err := aes.NewCipher(key)
if err != nil {
t.Fatalf("Failed to create decrypt cipher: %v", err)
}
decryptStream := cipher.NewCTR(decryptBlock, adjustedIV)
ciphertextReader := bytes.NewReader(ciphertext[blockAlignedOffset:])
decryptedReader := &cipher.StreamReader{S: decryptStream, R: ciphertextReader}
// Skip intra-block bytes to get to user-requested offset
if skip > 0 {
_, err := io.CopyN(io.Discard, decryptedReader, int64(skip))
if err != nil {
t.Fatalf("Failed to skip %d bytes: %v", skip, err)
}
}
// Read decrypted data
decryptedData, err := io.ReadAll(decryptedReader)
if err != nil {
t.Fatalf("Failed to read decrypted data: %v", err)
}
// Verify
expectedPlaintext := plaintext[offset:]
if !bytes.Equal(decryptedData, expectedPlaintext) {
t.Errorf("Decryption mismatch at offset %d (skip=%d)", offset, skip)
t.Errorf(" Expected: %q", expectedPlaintext)
t.Errorf(" Got: %q", decryptedData)
}
})
}
}