Skip to content

Commit 63b61d4

Browse files
committed
feat: unit testing for server and indexer
1 parent 27f5959 commit 63b61d4

File tree

4 files changed

+532
-0
lines changed

4 files changed

+532
-0
lines changed

indexer/routes/indexer_test.go

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package routes
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io"
7+
"net/http"
8+
"testing"
9+
"time"
10+
)
11+
12+
type GenericResponse struct {
13+
Data string `json:"data"`
14+
Success bool `json:"success"`
15+
}
16+
17+
type DumpIndexersResponse struct {
18+
Data []string `json:"data"`
19+
Success bool `json:"success"`
20+
}
21+
22+
type HashItem struct {
23+
FileName string
24+
Size int64
25+
IsDirectory bool
26+
ModTime time.Time
27+
Hash string
28+
SubFiles map[string]HashItem
29+
}
30+
31+
type IndexerServer struct {
32+
Online bool
33+
Hashes int
34+
Files HashItem
35+
LastSync time.Time
36+
}
37+
38+
type GetAllFilesResponse struct {
39+
Data map[string]IndexerServer `json:"data"`
40+
Success bool `json:"success"`
41+
}
42+
43+
type SearchFileResponse struct {
44+
Data []FileTarget `json:"data"`
45+
Success bool `json:"success"`
46+
}
47+
48+
type GetFileResponse struct {
49+
Data struct {
50+
FileName string `json:"FileName"`
51+
Size int `json:"Size"`
52+
IsDirectory bool `json:"IsDirectory"`
53+
ModTime string `json:"ModTime"`
54+
Hash string `json:"Hash"`
55+
SubFiles struct {
56+
} `json:"SubFiles"`
57+
} `json:"data"`
58+
Success bool `json:"success"`
59+
}
60+
61+
func SendGetRequest(t *testing.T, url string) []byte {
62+
res, err := http.Get(url)
63+
if err != nil {
64+
t.Errorf("Failed to get send request: %s", err)
65+
}
66+
67+
defer res.Body.Close()
68+
body, err := io.ReadAll(res.Body)
69+
if err != nil {
70+
t.Errorf("Failed to read response: %s", err)
71+
}
72+
73+
return body
74+
}
75+
76+
func SendPostRequest(t *testing.T, url string, reqBody interface{}) []byte {
77+
json_data, err := json.Marshal(reqBody)
78+
if err != nil {
79+
t.Errorf("Failed to marshal POST request: %s", err)
80+
}
81+
82+
res, err := http.Post(url, "application/json", bytes.NewBuffer(json_data))
83+
if err != nil {
84+
t.Errorf("Failed to get send request: %s", err)
85+
}
86+
87+
defer res.Body.Close()
88+
body, err := io.ReadAll(res.Body)
89+
if err != nil {
90+
t.Errorf("Failed to read response: %s", err)
91+
}
92+
93+
return body
94+
}
95+
96+
// this tests both files/read and files/download
97+
func TestDumpIndexers(t *testing.T) {
98+
// Run download tests
99+
response := SendGetRequest(t, "http://localhost:8081/indexer/getAll/indexers")
100+
var result DumpIndexersResponse
101+
if err := json.Unmarshal(response, &result); err != nil {
102+
t.Errorf("Failed to unmarshal JSON: %s", err)
103+
}
104+
105+
if result.Success {
106+
if len(result.Data) == 1 && result.Data[0] == "http://localhost:8000" {
107+
t.Log("Dump test passed")
108+
}
109+
} else {
110+
t.Errorf("Dump test failed")
111+
}
112+
}
113+
114+
func TestGetAllFiles(t *testing.T) {
115+
// Test if hello.txt exists (lightweight Exists route)
116+
response := SendGetRequest(t, "http://localhost:8081/indexer/getAll/files")
117+
var result GetAllFilesResponse
118+
if err := json.Unmarshal(response, &result); err != nil {
119+
t.Errorf("Failed to unmarshal JSON: %s", err)
120+
}
121+
122+
if result.Success {
123+
if result.Data["http://localhost:8000"].Online == true && result.Data["http://localhost:8000"].Hashes == 3 {
124+
t.Log("GetAllFiles test passed")
125+
} else {
126+
t.Errorf("GetAllFiles test failed (2)")
127+
}
128+
} else {
129+
t.Errorf("GetAllFiles test failed")
130+
}
131+
}
132+
133+
func TestGetFile(t *testing.T) {
134+
// Test if ReadDir in main dir is functioning
135+
response := SendGetRequest(t, "http://localhost:8081/indexer/get?server=http://localhost:8000&hash=1a9ef7e5588a84b048a7f60468c270888dada51adfd1f9276662d4a275e73198")
136+
var result GetFileResponse
137+
if err := json.Unmarshal(response, &result); err != nil {
138+
t.Errorf("Failed to unmarshal JSON: %s", err)
139+
}
140+
141+
if result.Success {
142+
if result.Data.FileName == "hello.txt" && result.Data.Size == 15 {
143+
t.Log("GetFile test passed")
144+
} else {
145+
t.Errorf("GetFile test failed (wrong size of element, is indexer running default config?)")
146+
}
147+
} else {
148+
t.Errorf("GetFile test failed")
149+
}
150+
}
151+
152+
func TestSearchFile(t *testing.T) {
153+
// Test if StatFile in main dir is functioning
154+
response := SendGetRequest(t, "http://localhost:8081/indexer/search?server=http://localhost:8000&query=hello.txt")
155+
var result SearchFileResponse
156+
if err := json.Unmarshal(response, &result); err != nil {
157+
t.Errorf("Failed to unmarshal JSON: %s", err)
158+
}
159+
160+
if result.Success {
161+
if result.Data[0].PercentMatched == 1 && result.Data[0].Hash == "1a9ef7e5588a84b048a7f60468c270888dada51adfd1f9276662d4a275e73198" { // hash of Hello DistriFS!
162+
t.Log("SearchFile test passed")
163+
} else {
164+
t.Error("SearchFile test failed (wrong hash or % matched, is server running default config?)")
165+
}
166+
} else {
167+
t.Errorf("SearchFile test failed")
168+
}
169+
}
170+
171+
func TestPutFile(t *testing.T) {
172+
response := SendGetRequest(t, "http://localhost:8081/indexer/suggest?server=http://localhost:8000")
173+
var result GenericResponse
174+
if err := json.Unmarshal(response, &result); err != nil {
175+
t.Errorf("Failed to unmarshal JSON: %s", err)
176+
}
177+
178+
if result.Success || result.Data == "server_already_exists" {
179+
t.Log("PutFile test passed")
180+
} else {
181+
t.Errorf("PutFile test failed")
182+
}
183+
}

indexer/routes/suggestServer.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ func PutFileRoute(c *gin.Context) {
7070
server.Online = true
7171
server.Hashes = 0
7272
server.Files = globals.HashItem{}
73+
74+
c.JSON(200, gin.H{
75+
"success": true,
76+
"data": "added_server",
77+
})
78+
} else {
79+
c.JSON(200, gin.H{
80+
"success": false,
81+
"data": "server_already_exists",
82+
})
7383
}
7484
}
7585
}

server/routes/files/files_test.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package files
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io"
7+
"net/http"
8+
"testing"
9+
)
10+
11+
type GenericResponse struct {
12+
Data string `json:"data"`
13+
Success bool `json:"success"`
14+
}
15+
16+
type SimpleGenericResponse struct {
17+
Success bool `json:"success"`
18+
}
19+
20+
type ReadDirResponse struct {
21+
Data []struct {
22+
Name string `json:"Name"`
23+
IsDirectory bool `json:"IsDirectory"`
24+
} `json:"data"`
25+
Success bool `json:"success"`
26+
}
27+
28+
type StatFileResponse struct {
29+
Hash string `json:"hash"`
30+
IsDirectory bool `json:"isDirectory"`
31+
LastModified string `json:"lastModified"`
32+
Name string `json:"name"`
33+
Size int `json:"size"`
34+
Success bool `json:"success"`
35+
}
36+
37+
func SendGetRequest(t *testing.T, url string) []byte {
38+
res, err := http.Get(url)
39+
if err != nil {
40+
t.Errorf("Failed to get send request: %s", err)
41+
}
42+
43+
defer res.Body.Close()
44+
body, err := io.ReadAll(res.Body)
45+
if err != nil {
46+
t.Errorf("Failed to read response: %s", err)
47+
}
48+
49+
return body
50+
}
51+
52+
func SendPostRequest(t *testing.T, url string, reqBody interface{}) []byte {
53+
json_data, err := json.Marshal(reqBody)
54+
if err != nil {
55+
t.Errorf("Failed to marshal POST request: %s", err)
56+
}
57+
58+
res, err := http.Post(url, "application/json", bytes.NewBuffer(json_data))
59+
if err != nil {
60+
t.Errorf("Failed to get send request: %s", err)
61+
}
62+
63+
defer res.Body.Close()
64+
body, err := io.ReadAll(res.Body)
65+
if err != nil {
66+
t.Errorf("Failed to read response: %s", err)
67+
}
68+
69+
return body
70+
}
71+
72+
// this tests both files/read and files/download
73+
func TestDownload(t *testing.T) {
74+
// Run download tests
75+
response := SendGetRequest(t, "http://localhost:8000/files/read?filename=hello.txt")
76+
var result GenericResponse
77+
if err := json.Unmarshal(response, &result); err != nil {
78+
t.Errorf("Failed to unmarshal JSON: %s", err)
79+
}
80+
81+
response = SendGetRequest(t, "http://localhost:8000/files/download?id="+result.Data)
82+
t.Log(string(response))
83+
84+
if string(response) == "Hello DistriFS!" {
85+
t.Log("Download test passed")
86+
} else {
87+
t.Errorf("Download test failed")
88+
}
89+
}
90+
91+
func TestExists(t *testing.T) {
92+
// Test if hello.txt exists (lightweight Exists route)
93+
response := SendGetRequest(t, "http://localhost:8000/files/exists?filename=hello.txt")
94+
var result SimpleGenericResponse
95+
if err := json.Unmarshal(response, &result); err != nil {
96+
t.Errorf("Failed to unmarshal JSON: %s", err)
97+
}
98+
99+
if result.Success {
100+
t.Log("Exists test passed")
101+
} else {
102+
t.Errorf("Exists test failed")
103+
}
104+
}
105+
106+
func TestReadDir(t *testing.T) {
107+
// Test if ReadDir in main dir is functioning
108+
response := SendGetRequest(t, "http://localhost:8000/files/dir?dirname=.")
109+
var result ReadDirResponse
110+
if err := json.Unmarshal(response, &result); err != nil {
111+
t.Errorf("Failed to unmarshal JSON: %s", err)
112+
}
113+
114+
if result.Success {
115+
if len(result.Data) == 3 && result.Data[0].Name == "hello.txt" {
116+
t.Log("ReadDir test passed")
117+
} else {
118+
t.Errorf("ReadDir test failed (wrong len of elements or wrong element 0, is server running default config?)")
119+
}
120+
} else {
121+
t.Errorf("ReadDir test failed")
122+
}
123+
}
124+
125+
func TestStatFile(t *testing.T) {
126+
// Test if StatFile in main dir is functioning
127+
response := SendGetRequest(t, "http://localhost:8000/files/stat?filename=hello.txt")
128+
var result StatFileResponse
129+
if err := json.Unmarshal(response, &result); err != nil {
130+
t.Errorf("Failed to unmarshal JSON: %s", err)
131+
}
132+
133+
if result.Success {
134+
if result.Hash == "1a9ef7e5588a84b048a7f60468c270888dada51adfd1f9276662d4a275e73198" { // hash of Hello DistriFS!
135+
t.Log("StatFile test passed")
136+
} else {
137+
t.Errorf("StatFile test failed (wrong hash, is server running default config?)")
138+
}
139+
} else {
140+
t.Errorf("StatFile test failed")
141+
}
142+
}

0 commit comments

Comments
 (0)