server/browser_startup_test.go (3801B)
1 package main 2 3 import ( 4 "bytes" 5 "crypto/sha256" 6 "encoding/binary" 7 "hash" 8 "net/http" 9 "net/http/httptest" 10 "os" 11 "strconv" 12 "strings" 13 "testing" 14 ) 15 16 // Hash the streamed body without retaining the entire startup bundle in the test. 17 type startupResponse struct { 18 header http.Header 19 hash hash.Hash 20 size int 21 } 22 23 func (response *startupResponse) Header() http.Header { return response.header } 24 func (response *startupResponse) WriteHeader(status int) {} 25 func (response *startupResponse) Write(data []byte) (int, error) { 26 response.size += len(data) 27 return response.hash.Write(data) 28 } 29 30 func TestBrowserStartupStream(t *testing.T) { 31 t.Chdir(t.TempDir()) 32 useDiskResources(t) 33 if err := os.MkdirAll("anarchy-assets-14.9.1/cd_image/data/db", 0755); err != nil { 34 t.Fatal(err) 35 } 36 file, err := os.Create("anarchy-assets-14.9.1/cd_image/data/db/ResourceDatabase.dat") 37 if err != nil { 38 t.Fatal(err) 39 } 40 defer file.Close() 41 if err := file.Truncate(555 * 1048576); err != nil { 42 t.Fatal(err) 43 } 44 45 // Mark every database chunk, including gaps, to catch wrong seeks and ordering. 46 marker := make([]byte, 8) 47 for chunk := int64(0); chunk < 555; chunk++ { 48 binary.LittleEndian.PutUint64(marker, uint64(chunk)) 49 if _, err := file.WriteAt(marker, chunk*1048576); err != nil { 50 t.Fatal(err) 51 } 52 } 53 response := &startupResponse{header: make(http.Header), hash: sha256.New()} 54 browserStartup(response, httptest.NewRequest("GET", "/browser-startup", nil)) 55 56 if response.size != 193986560 || response.Header().Get("Content-Length") != "193986560" { 57 t.Fatalf("bundle size: %d, headers: %v", response.size, response.Header()) 58 } 59 indices := strings.Split(response.Header().Get("X-AO-Chunks"), ",") 60 if len(indices) != 185 || indices[0] != "0" || indices[184] != "554" { 61 t.Fatalf("startup indices: %v", indices) 62 } 63 expected := sha256.New() 64 chunkBytes := make([]byte, 1048576) 65 previous := -1 66 for _, value := range indices { 67 index, err := strconv.Atoi(value) 68 if err != nil || index <= previous { 69 t.Fatalf("unsorted chunk index: %q after %d", value, previous) 70 } 71 previous = index 72 binary.LittleEndian.PutUint64(chunkBytes, uint64(index)) 73 expected.Write(chunkBytes) 74 } 75 if !bytes.Equal(response.hash.Sum(nil), expected.Sum(nil)) { 76 t.Fatal("streamed ranges do not match the advertised chunks") 77 } 78 79 // The cache must accept these bytes as the same revision as ordinary range requests. 80 asset := httptest.NewRecorder() 81 request := httptest.NewRequest("HEAD", "/browser-assets/cd_image/data/db/ResourceDatabase.dat", nil) 82 request.SetPathValue("path", "cd_image/data/db/ResourceDatabase.dat") 83 browserAsset(asset, request) 84 if response.Header().Get("X-AO-Asset-Version") != asset.Header().Get("ETag") { 85 t.Fatalf("startup and range versions differ: %v, %v", response.Header(), asset.Header()) 86 } 87 head := httptest.NewRecorder() 88 browserStartup(head, httptest.NewRequest("HEAD", "/browser-startup", nil)) 89 if head.Code != http.StatusOK || head.Body.Len() != 0 || head.Header().Get("X-AO-Chunks") != response.Header().Get("X-AO-Chunks") { 90 t.Fatalf("HEAD response: %d %v (%d bytes)", head.Code, head.Header(), head.Body.Len()) 91 } 92 } 93 94 func TestBrowserStartupRejectsIncompleteDatabase(t *testing.T) { 95 t.Chdir(t.TempDir()) 96 useDiskResources(t) 97 if err := os.MkdirAll("anarchy-assets-14.9.1/cd_image/data/db", 0755); err != nil { 98 t.Fatal(err) 99 } 100 if err := os.WriteFile("anarchy-assets-14.9.1/cd_image/data/db/ResourceDatabase.dat", []byte("short"), 0644); err != nil { 101 t.Fatal(err) 102 } 103 response := httptest.NewRecorder() 104 browserStartup(response, httptest.NewRequest("GET", "/browser-startup", nil)) 105 if response.Code != http.StatusInternalServerError || response.Header().Get("X-AO-Chunks") != "" { 106 t.Fatalf("incomplete database response: %d %v", response.Code, response.Header()) 107 } 108 }