Skip to content

Commit 9d21b91

Browse files
authored
all: Refactor the protocol/model interface a bit (ref syncthing#8981) (syncthing#9007)
1 parent b806026 commit 9d21b91

21 files changed

+431
-359
lines changed

lib/api/api_test.go

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var (
5858

5959
func init() {
6060
dev1, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
61-
apiCfg.GUIReturns(config.GUIConfiguration{APIKey: testAPIKey})
61+
apiCfg.GUIReturns(config.GUIConfiguration{APIKey: testAPIKey, RawAddress: "127.0.0.1:0"})
6262
}
6363

6464
func TestMain(m *testing.M) {
@@ -496,7 +496,9 @@ func TestAPIServiceRequests(t *testing.T) {
496496
}
497497

498498
for _, tc := range cases {
499+
tc := tc
499500
t.Run(cases[0].URL, func(t *testing.T) {
501+
t.Parallel()
500502
testHTTPRequest(t, baseURL, tc, testAPIKey)
501503
})
502504
}
@@ -557,9 +559,10 @@ func TestHTTPLogin(t *testing.T) {
557559

558560
cfg := newMockedConfig()
559561
cfg.GUIReturns(config.GUIConfiguration{
560-
User: "üser",
561-
Password: "$2a$10$IdIZTxTg/dCNuNEGlmLynOjqg4B1FvDKuIV5e0BB3pnWVHNb8.GSq", // bcrypt of "räksmörgås" in UTF-8
562-
APIKey: testAPIKey,
562+
User: "üser",
563+
Password: "$2a$10$IdIZTxTg/dCNuNEGlmLynOjqg4B1FvDKuIV5e0BB3pnWVHNb8.GSq", // bcrypt of "räksmörgås" in UTF-8
564+
RawAddress: "127.0.0.1:0",
565+
APIKey: testAPIKey,
563566
})
564567
baseURL, cancel, err := startHTTP(cfg)
565568
if err != nil {
@@ -1050,30 +1053,31 @@ func TestHostCheck(t *testing.T) {
10501053
t.Error("Incorrect host header, check disabled: expected 200 OK, not", resp.Status)
10511054
}
10521055

1053-
// A server bound to a wildcard address also doesn't do the check
1056+
if !testing.Short() {
1057+
// A server bound to a wildcard address also doesn't do the check
10541058

1055-
cfg = newMockedConfig()
1056-
cfg.GUIReturns(config.GUIConfiguration{
1057-
RawAddress: "0.0.0.0:0",
1058-
InsecureSkipHostCheck: true,
1059-
})
1060-
baseURL, cancel, err = startHTTP(cfg)
1061-
if err != nil {
1062-
t.Fatal(err)
1063-
}
1064-
defer cancel()
1059+
cfg = newMockedConfig()
1060+
cfg.GUIReturns(config.GUIConfiguration{
1061+
RawAddress: "0.0.0.0:0",
1062+
})
1063+
baseURL, cancel, err = startHTTP(cfg)
1064+
if err != nil {
1065+
t.Fatal(err)
1066+
}
1067+
defer cancel()
10651068

1066-
// A request with a suspicious Host header should be allowed
1069+
// A request with a suspicious Host header should be allowed
10671070

1068-
req, _ = http.NewRequest("GET", baseURL, nil)
1069-
req.Host = "example.com"
1070-
resp, err = http.DefaultClient.Do(req)
1071-
if err != nil {
1072-
t.Fatal(err)
1073-
}
1074-
resp.Body.Close()
1075-
if resp.StatusCode != http.StatusOK {
1076-
t.Error("Incorrect host header, wildcard bound: expected 200 OK, not", resp.Status)
1071+
req, _ = http.NewRequest("GET", baseURL, nil)
1072+
req.Host = "example.com"
1073+
resp, err = http.DefaultClient.Do(req)
1074+
if err != nil {
1075+
t.Fatal(err)
1076+
}
1077+
resp.Body.Close()
1078+
if resp.StatusCode != http.StatusOK {
1079+
t.Error("Incorrect host header, wildcard bound: expected 200 OK, not", resp.Status)
1080+
}
10771081
}
10781082

10791083
// This should all work over IPv6 as well

lib/model/fakeconns_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ func newFakeConnection(id protocol.DeviceID, model Model) *fakeConnection {
3232
f.RequestCalls(func(ctx context.Context, folder, name string, blockNo int, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
3333
return f.fileData[name], nil
3434
})
35-
f.IDReturns(id)
35+
f.DeviceIDReturns(id)
3636
f.CloseCalls(func(err error) {
3737
f.closeOnce.Do(func() {
3838
close(f.closed)
3939
})
40-
model.Closed(id, err)
40+
model.Closed(f, err)
4141
f.ClosedReturns(f.closed)
4242
})
4343
return f
@@ -157,15 +157,15 @@ func (f *fakeConnection) sendIndexUpdate() {
157157
for i := range f.files {
158158
toSend[i] = prepareFileInfoForIndex(f.files[i])
159159
}
160-
f.model.IndexUpdate(f.id, f.folder, toSend)
160+
f.model.IndexUpdate(f, f.folder, toSend)
161161
}
162162

163163
func addFakeConn(m *testModel, dev protocol.DeviceID, folderID string) *fakeConnection {
164164
fc := newFakeConnection(dev, m)
165165
fc.folder = folderID
166166
m.AddConnection(fc, protocol.Hello{})
167167

168-
m.ClusterConfig(dev, protocol.ClusterConfig{
168+
m.ClusterConfig(fc, protocol.ClusterConfig{
169169
Folders: []protocol.Folder{
170170
{
171171
ID: folderID,

lib/model/folder_recvonly_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestRecvOnlyRevertDeletes(t *testing.T) {
3030
defer wcfgCancel()
3131
ffs := f.Filesystem(nil)
3232
defer cleanupModel(m)
33-
addFakeConn(m, device1, f.ID)
33+
conn := addFakeConn(m, device1, f.ID)
3434

3535
// Create some test data
3636

@@ -45,7 +45,7 @@ func TestRecvOnlyRevertDeletes(t *testing.T) {
4545

4646
// Send and index update for the known stuff
4747

48-
must(t, m.Index(device1, "ro", knownFiles))
48+
must(t, m.Index(conn, "ro", knownFiles))
4949
f.updateLocalsFromScanning(knownFiles)
5050

5151
size := globalSize(t, m, "ro")
@@ -112,7 +112,7 @@ func TestRecvOnlyRevertNeeds(t *testing.T) {
112112
defer wcfgCancel()
113113
ffs := f.Filesystem(nil)
114114
defer cleanupModel(m)
115-
addFakeConn(m, device1, f.ID)
115+
conn := addFakeConn(m, device1, f.ID)
116116

117117
// Create some test data
118118

@@ -122,7 +122,7 @@ func TestRecvOnlyRevertNeeds(t *testing.T) {
122122

123123
// Send and index update for the known stuff
124124

125-
must(t, m.Index(device1, "ro", knownFiles))
125+
must(t, m.Index(conn, "ro", knownFiles))
126126
f.updateLocalsFromScanning(knownFiles)
127127

128128
// Scan the folder.
@@ -202,7 +202,7 @@ func TestRecvOnlyUndoChanges(t *testing.T) {
202202
defer wcfgCancel()
203203
ffs := f.Filesystem(nil)
204204
defer cleanupModel(m)
205-
addFakeConn(m, device1, f.ID)
205+
conn := addFakeConn(m, device1, f.ID)
206206

207207
// Create some test data
208208

@@ -212,7 +212,7 @@ func TestRecvOnlyUndoChanges(t *testing.T) {
212212

213213
// Send an index update for the known stuff
214214

215-
must(t, m.Index(device1, "ro", knownFiles))
215+
must(t, m.Index(conn, "ro", knownFiles))
216216
f.updateLocalsFromScanning(knownFiles)
217217

218218
// Scan the folder.
@@ -272,7 +272,7 @@ func TestRecvOnlyDeletedRemoteDrop(t *testing.T) {
272272
defer wcfgCancel()
273273
ffs := f.Filesystem(nil)
274274
defer cleanupModel(m)
275-
addFakeConn(m, device1, f.ID)
275+
conn := addFakeConn(m, device1, f.ID)
276276

277277
// Create some test data
278278

@@ -282,7 +282,7 @@ func TestRecvOnlyDeletedRemoteDrop(t *testing.T) {
282282

283283
// Send an index update for the known stuff
284284

285-
must(t, m.Index(device1, "ro", knownFiles))
285+
must(t, m.Index(conn, "ro", knownFiles))
286286
f.updateLocalsFromScanning(knownFiles)
287287

288288
// Scan the folder.
@@ -337,7 +337,7 @@ func TestRecvOnlyRemoteUndoChanges(t *testing.T) {
337337
defer wcfgCancel()
338338
ffs := f.Filesystem(nil)
339339
defer cleanupModel(m)
340-
addFakeConn(m, device1, f.ID)
340+
conn := addFakeConn(m, device1, f.ID)
341341

342342
// Create some test data
343343

@@ -347,7 +347,7 @@ func TestRecvOnlyRemoteUndoChanges(t *testing.T) {
347347

348348
// Send an index update for the known stuff
349349

350-
must(t, m.Index(device1, "ro", knownFiles))
350+
must(t, m.Index(conn, "ro", knownFiles))
351351
f.updateLocalsFromScanning(knownFiles)
352352

353353
// Scan the folder.
@@ -402,7 +402,7 @@ func TestRecvOnlyRemoteUndoChanges(t *testing.T) {
402402
return true
403403
})
404404
snap.Release()
405-
must(t, m.IndexUpdate(device1, "ro", files))
405+
must(t, m.IndexUpdate(conn, "ro", files))
406406

407407
// Ensure the pull to resolve conflicts (content identical) happened
408408
must(t, f.doInSync(func() error {
@@ -427,7 +427,7 @@ func TestRecvOnlyRevertOwnID(t *testing.T) {
427427
defer wcfgCancel()
428428
ffs := f.Filesystem(nil)
429429
defer cleanupModel(m)
430-
addFakeConn(m, device1, f.ID)
430+
conn := addFakeConn(m, device1, f.ID)
431431

432432
// Create some test data
433433

@@ -470,7 +470,7 @@ func TestRecvOnlyRevertOwnID(t *testing.T) {
470470
}()
471471

472472
// Receive an index update with an older version, but valid and then revert
473-
must(t, m.Index(device1, f.ID, []protocol.FileInfo{fi}))
473+
must(t, m.Index(conn, f.ID, []protocol.FileInfo{fi}))
474474
f.Revert()
475475

476476
select {

lib/model/folder_sendrecv_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ func TestPullSymlinkOverExistingWindows(t *testing.T) {
12781278

12791279
m, f, wcfgCancel := setupSendReceiveFolder(t)
12801280
defer wcfgCancel()
1281-
addFakeConn(m, device1, f.ID)
1281+
conn := addFakeConn(m, device1, f.ID)
12821282

12831283
name := "foo"
12841284
if fd, err := f.mtimefs.Create(name); err != nil {
@@ -1296,7 +1296,7 @@ func TestPullSymlinkOverExistingWindows(t *testing.T) {
12961296
if !ok {
12971297
t.Fatal("file missing")
12981298
}
1299-
must(t, m.Index(device1, f.ID, []protocol.FileInfo{{Name: name, Type: protocol.FileInfoTypeSymlink, Version: file.Version.Update(device1.Short())}}))
1299+
must(t, m.Index(conn, f.ID, []protocol.FileInfo{{Name: name, Type: protocol.FileInfoTypeSymlink, Version: file.Version.Update(device1.Short())}}))
13001300

13011301
scanChan := make(chan string)
13021302

0 commit comments

Comments
 (0)