Debian Codegraph @

httphandler_test.go

raw
1 package desync
2
3 import (
4 "net/http/httptest"
5 "net/url"
6 "testing"
7
8 "github.com/stretchr/testify/require"
9 )
10
11 func TestHTTPHandlerReadWrite(t *testing.T) {
12 store := t.TempDir()
13
14 upstream, err := NewLocalStore(store, StoreOptions{})
15 require.NoError(t, err)
16
17 // Start a read-write capable server and a read-only server
18 rw := httptest.NewServer(NewHTTPHandler(upstream, true, false, []converter{Compressor{}}, ""))
19 defer rw.Close()
20 ro := httptest.NewServer(NewHTTPHandler(upstream, false, false, []converter{Compressor{}}, ""))
21 defer ro.Close()
22
23 // Initialize HTTP chunks stores, one RW and the other RO
24 rwStoreURL, _ := url.Parse(rw.URL)
25 rwStore, err := NewRemoteHTTPStore(rwStoreURL, StoreOptions{})
26 require.NoError(t, err)
27
28 roStoreURL, _ := url.Parse(ro.URL)
29 roStore, err := NewRemoteHTTPStore(roStoreURL, StoreOptions{})
30 require.NoError(t, err)
31
32 // Make up some data and store it in the RW store
33 dataIn := []byte("some data")
34 chunkIn := NewChunk(dataIn)
35 id := chunkIn.ID()
36
37 // Write a chunk
38 err = rwStore.StoreChunk(chunkIn)
39 require.NoError(t, err)
40
41 // Check it's in the store
42 hasChunk, err := rwStore.HasChunk(id)
43 require.NoError(t, err)
44 require.True(t, hasChunk)
45
46 // Let's try to send some data to the RO store, that should fail
47 err = roStore.StoreChunk(chunkIn)
48 require.Error(t, err, "expected error writing to read-only chunkstore")
49 }
50
51 func TestHTTPHandlerCompression(t *testing.T) {
52 store := t.TempDir()
53
54 upstream, err := NewLocalStore(store, StoreOptions{})
55 require.NoError(t, err)
56
57 // Start a server that uses compression, and one that serves uncompressed chunks
58 co := httptest.NewServer(NewHTTPHandler(upstream, true, false, []converter{Compressor{}}, ""))
59 defer co.Close()
60 un := httptest.NewServer(NewHTTPHandler(upstream, true, false, nil, ""))
61 defer un.Close()
62
63 // Initialize HTTP chunks stores, one RW and the other RO. Also make one that's
64 // trying to get compressed data from a HTTP store that serves only uncompressed.
65 coStoreURL, _ := url.Parse(co.URL)
66 coStore, err := NewRemoteHTTPStore(coStoreURL, StoreOptions{})
67 require.NoError(t, err)
68
69 unStoreURL, _ := url.Parse(un.URL)
70 unStore, err := NewRemoteHTTPStore(unStoreURL, StoreOptions{Uncompressed: true})
71 require.NoError(t, err)
72
73 invalidStore, err := NewRemoteHTTPStore(unStoreURL, StoreOptions{})
74 require.NoError(t, err)
75
76 // Make up some data and store it in the RW store
77 dataIn := []byte("some data")
78 chunkIn := NewChunk(dataIn)
79 id := chunkIn.ID()
80
81 // Try to get compressed chunks from a store that only serves uncompressed chunks
82 _, err = invalidStore.GetChunk(id)
83 require.Error(t, err, "expected failure trying to get compressed chunks from uncompressed http store")
84
85 err = coStore.StoreChunk(chunkIn)
86 require.NoError(t, err)
87
88 // Check it's in the store when looking for compressed chunks
89 coExists, err := coStore.HasChunk(id)
90 require.NoError(t, err)
91 require.True(t, coExists)
92
93 // It's also visible when looking for uncompressed data
94 unExists, err := unStore.HasChunk(id)
95 require.NoError(t, err)
96 require.True(t, unExists)
97
98 // Send it uncompressed
99 err = unStore.StoreChunk(chunkIn)
100 require.NoError(t, err)
101
102 // Try to get the uncompressed chunk
103 _, err = unStore.GetChunk(id)
104 require.NoError(t, err)
105 }
106
107 func TestHTTPHandlerEncryption(t *testing.T) {
108 // Prep a local store (no encryption)
109 store := t.TempDir()
110 upstream, err := NewLocalStore(store, StoreOptions{})
111 require.NoError(t, err)
112
113 // Start a read-write capable server with Encryption, no Compression
114 enc, err := NewXChaCha20Poly1305(testKey(t, testEncryptionKey))
115 require.NoError(t, err)
116 server := httptest.NewServer(NewHTTPHandler(upstream, true, false, []converter{enc}, ""))
117 defer server.Close()
118
119 // Initialize HTTP chunks store (client)
120 httpStoreURL, _ := url.Parse(server.URL)
121 httpStore, err := NewRemoteHTTPStore(httpStoreURL, StoreOptions{
122 Uncompressed: true,
123 Encryption: true,
124 EncryptionKey: testEncryptionKey,
125 })
126 require.NoError(t, err)
127
128 // Make up some data and store it in the RW store
129 dataIn := []byte("some data")
130 chunkIn := NewChunk(dataIn)
131 id := chunkIn.ID()
132
133 // Write a chunk via HTTP
134 err = httpStore.StoreChunk(chunkIn)
135 require.NoError(t, err)
136
137 // Read it back via HTTP and compare to the original
138 chunkOut, err := httpStore.GetChunk(id)
139 require.NoError(t, err)
140 dataOut, err := chunkOut.Data()
141 require.NoError(t, err)
142 require.Equal(t, dataIn, dataOut)
143 }
144
145 func TestHTTPHandlerExtensionMismatch(t *testing.T) {
146 chunkID := NewChunk([]byte("some data")).ID()
147 id := chunkID.String()
148
149 // A server without compression or encryption has no chunk file extension.
150 // Asking it for a compressed chunk has to fail with an error that points
151 // at the mismatch, not with an ID parse error.
152 h := HTTPHandler{}
153 _, err := h.idFromPath("/" + id[0:4] + "/" + id + ".cacnk")
154 require.ErrorContains(t, err, "compression and encryption settings")
155
156 // The same path without the extension is accepted
157 _, err = h.idFromPath("/" + id[0:4] + "/" + id)
158 require.NoError(t, err)
159 }