50 lines
1.6 KiB
Go
50 lines
1.6 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
agentv1 "git.ipao.vip/rogee/go-sip/gen/agent/v1"
|
|
)
|
|
|
|
type uploadTransportFunc func(*http.Request) (*http.Response, error)
|
|
|
|
func (f uploadTransportFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
|
|
|
|
func TestUploadDoesNotReportPreReadDigestForChangedBytes(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "recording")
|
|
if err := os.WriteFile(path, []byte("original"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sum := sha256.Sum256([]byte("original"))
|
|
attempts := 0
|
|
client := &http.Client{Transport: uploadTransportFunc(func(r *http.Request) (*http.Response, error) {
|
|
attempts++
|
|
if err := os.WriteFile(path, []byte("modified"), 0600); err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := io.Copy(io.Discard, r.Body); err != nil {
|
|
return nil, err
|
|
}
|
|
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(""))}, nil
|
|
})}
|
|
grant := &agentv1.UploadGrant{UploadId: "upload-a", ObjectKey: "recording", TargetUrl: "https://oss.invalid/object", MaxBytes: 8, RequiredChecksumSha256: hex.EncodeToString(sum[:]), ExpiresAtUnixMs: time.Now().Add(time.Minute).UnixMilli()}
|
|
if _, err := (UploadClient{HTTPClient: client}).UploadFile(context.Background(), grant, path); err == nil {
|
|
t.Fatal("reported original checksum after sending changed bytes")
|
|
}
|
|
if attempts != 1 {
|
|
t.Fatal("integrity failure retried PUT")
|
|
}
|
|
if _, err := os.Stat(path); err != nil {
|
|
t.Fatal("source file removed")
|
|
}
|
|
}
|