222 lines
7.0 KiB
Go
222 lines
7.0 KiB
Go
package drop
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
"time"
|
|
)
|
|
|
|
func TestSessionAndUploadAPIs(t *testing.T) {
|
|
app := NewApp(Config{Addr: ":0", TempDir: t.TempDir(), MaxFileSize: 1024, FileTTL: time.Hour}, nil)
|
|
server := httptest.NewServer(app.Routes())
|
|
defer server.Close()
|
|
|
|
body := bytes.NewBufferString(`{"code":"","name":"Alice"}`)
|
|
resp, err := http.Post(server.URL+"/api/session", "application/json", body)
|
|
if err != nil {
|
|
t.Fatalf("session request: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("session status = %d", resp.StatusCode)
|
|
}
|
|
var session sessionResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&session); err != nil {
|
|
t.Fatalf("decode session: %v", err)
|
|
}
|
|
if len(session.User.Code) != 6 || session.SessionID == "" {
|
|
t.Fatalf("bad session response: %#v", session)
|
|
}
|
|
|
|
uploadBody := bytes.NewBufferString("hello")
|
|
req, err := http.NewRequest(http.MethodPost, server.URL+"/api/uploads?sender_id="+session.User.ID+"&receiver_id=receiver-1&filename=hello.txt", uploadBody)
|
|
if err != nil {
|
|
t.Fatalf("new upload request: %v", err)
|
|
}
|
|
req.Header.Set("Content-Type", "text/plain")
|
|
resp, err = http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatalf("upload request: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("upload status = %d", resp.StatusCode)
|
|
}
|
|
var uploaded uploadResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&uploaded); err != nil {
|
|
t.Fatalf("decode upload: %v", err)
|
|
}
|
|
if uploaded.Transfer.ID == "" || !strings.HasPrefix(uploaded.DownloadURL, "/api/downloads/") {
|
|
t.Fatalf("bad upload response: %#v", uploaded)
|
|
}
|
|
}
|
|
|
|
func TestServerInfoAPIShowsCurrentURLAndPort(t *testing.T) {
|
|
app := NewApp(Config{Addr: ":10051", TempDir: t.TempDir(), MaxFileSize: 1024, FileTTL: time.Hour}, nil)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/server-info", nil)
|
|
req.Host = "drop.test:10051"
|
|
rec := httptest.NewRecorder()
|
|
|
|
app.Routes().ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
|
}
|
|
var info serverInfoResponse
|
|
if err := json.NewDecoder(rec.Body).Decode(&info); err != nil {
|
|
t.Fatalf("decode server info: %v", err)
|
|
}
|
|
if info.Port != "10051" {
|
|
t.Fatalf("port = %q, want 10051", info.Port)
|
|
}
|
|
if info.CurrentURL != "http://drop.test:10051" {
|
|
t.Fatalf("current url = %q", info.CurrentURL)
|
|
}
|
|
for _, url := range info.URLs {
|
|
if strings.Contains(url, "0.0.0.0") || strings.Contains(url, "127.0.0.1") {
|
|
t.Fatalf("urls = %#v, should only contain LAN IP candidates", info.URLs)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestServerInfoAPIPrioritizesConfiguredPublicURLs(t *testing.T) {
|
|
app := NewApp(
|
|
Config{
|
|
Addr: ":10051",
|
|
TempDir: t.TempDir(),
|
|
MaxFileSize: 1024,
|
|
FileTTL: time.Hour,
|
|
PublicURLs: []string{"http://10.20.8.138:10051", "https://drop.company.lan"},
|
|
},
|
|
nil,
|
|
)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/server-info", nil)
|
|
rec := httptest.NewRecorder()
|
|
|
|
app.Routes().ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
|
}
|
|
var info serverInfoResponse
|
|
if err := json.NewDecoder(rec.Body).Decode(&info); err != nil {
|
|
t.Fatalf("decode server info: %v", err)
|
|
}
|
|
if len(info.URLs) < 2 {
|
|
t.Fatalf("urls = %#v, want configured public urls", info.URLs)
|
|
}
|
|
want := []string{"http://10.20.8.138:10051", "https://drop.company.lan"}
|
|
for i := range want {
|
|
if info.URLs[i] != want[i] {
|
|
t.Fatalf("urls = %#v, want configured public urls first %#v", info.URLs, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStaticAssetsAreServedWithoutBrowserCaching(t *testing.T) {
|
|
app := NewApp(
|
|
Config{Addr: ":10051", TempDir: t.TempDir(), MaxFileSize: 1024, FileTTL: time.Hour},
|
|
http.FS(fstest.MapFS{"app.js": {Data: []byte("console.log('ok')")}}),
|
|
)
|
|
req := httptest.NewRequest(http.MethodGet, "/app.js", nil)
|
|
rec := httptest.NewRecorder()
|
|
|
|
app.Routes().ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
|
}
|
|
if got := rec.Header().Get("Cache-Control"); got != "no-store, max-age=0" {
|
|
t.Fatalf("Cache-Control = %q, want no-store, max-age=0", got)
|
|
}
|
|
if got := rec.Header().Get("Pragma"); got != "no-cache" {
|
|
t.Fatalf("Pragma = %q, want no-cache", got)
|
|
}
|
|
if got := rec.Header().Get("Expires"); got != "0" {
|
|
t.Fatalf("Expires = %q, want 0", got)
|
|
}
|
|
}
|
|
|
|
func TestPrivateLANIPv4Filter(t *testing.T) {
|
|
cases := map[string]bool{
|
|
"10.0.1.5": true,
|
|
"172.16.8.9": true,
|
|
"172.31.255.10": true,
|
|
"192.168.1.20": true,
|
|
"0.0.0.0": false,
|
|
"127.0.0.1": false,
|
|
"172.32.0.1": false,
|
|
"169.254.1.2": false,
|
|
"8.8.8.8": false,
|
|
}
|
|
for ip, want := range cases {
|
|
if got := isPrivateLANIPv4(net.ParseIP(ip)); got != want {
|
|
t.Fatalf("isPrivateLANIPv4(%s) = %v, want %v", ip, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWebSocketFramesRoundTripServerAndClientMessages(t *testing.T) {
|
|
payload := []byte(`{"type":"text.offer","text":"hi"}`)
|
|
frame := encodeServerFrame(payload)
|
|
got, err := readFrame(bytes.NewReader(frame))
|
|
if err != nil {
|
|
t.Fatalf("read server frame: %v", err)
|
|
}
|
|
if string(got) != string(payload) {
|
|
t.Fatalf("payload = %q, want %q", got, payload)
|
|
}
|
|
|
|
clientFrame := encodeClientFrame(payload, []byte{1, 2, 3, 4})
|
|
got, err = readFrame(bytes.NewReader(clientFrame))
|
|
if err != nil {
|
|
t.Fatalf("read client frame: %v", err)
|
|
}
|
|
if string(got) != string(payload) {
|
|
t.Fatalf("client payload = %q, want %q", got, payload)
|
|
}
|
|
}
|
|
|
|
func TestCompletedFileTransferDeletesCachedFile(t *testing.T) {
|
|
app := NewApp(Config{Addr: ":0", TempDir: t.TempDir(), MaxFileSize: 1024, FileTTL: time.Hour}, nil)
|
|
alice, _ := app.store.CreateOrRestoreSession("", "Alice")
|
|
bob, _ := app.store.CreateOrRestoreSession("", "Bob")
|
|
transfer := app.store.CreateFileTransfer(alice.User.ID, bob.User.ID, "done.txt", 4, "text/plain", time.Hour)
|
|
if err := app.storage.Save(transfer.ID, []byte("done")); err != nil {
|
|
t.Fatalf("save transfer file: %v", err)
|
|
}
|
|
if _, err := os.Stat(app.storage.Path(transfer.ID)); err != nil {
|
|
t.Fatalf("saved transfer file missing: %v", err)
|
|
}
|
|
|
|
app.forwardTransferStatus(&Client{ID: "receiver", UserID: bob.User.ID}, transfer.ID, TransferCompleted, "transfer.completed")
|
|
|
|
if _, err := os.Stat(app.storage.Path(transfer.ID)); !os.IsNotExist(err) {
|
|
t.Fatalf("completed transfer file still exists: %v", err)
|
|
}
|
|
updated, ok := app.store.Transfer(transfer.ID)
|
|
if !ok || updated.Status != TransferCompleted {
|
|
t.Fatalf("completed transfer status = %#v, exists %v", updated, ok)
|
|
}
|
|
}
|
|
|
|
func TestAllowedCIDRsRejectsRequestsOutsideConfiguredNetworks(t *testing.T) {
|
|
app := NewApp(Config{Addr: ":0", TempDir: t.TempDir(), MaxFileSize: 1024, FileTTL: time.Hour, AllowedCIDRs: []string{"10.0.0.0/8"}}, nil)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/health", nil)
|
|
req.RemoteAddr = "192.168.1.10:4567"
|
|
rec := httptest.NewRecorder()
|
|
|
|
app.Routes().ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusForbidden {
|
|
t.Fatalf("status = %d, want %d", rec.Code, http.StatusForbidden)
|
|
}
|
|
}
|