aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/browser_pet_owner_protocol_test.go (3336B)


      1 package main
      2 
      3 import (
      4 	"ao/server/assets"
      5 	"context"
      6 	"crypto/sha256"
      7 	"encoding/binary"
      8 	"fmt"
      9 	"net/http"
     10 	"net/http/httptest"
     11 	"os"
     12 	"os/exec"
     13 	"path/filepath"
     14 	"runtime"
     15 	"testing"
     16 	"time"
     17 )
     18 
     19 func TestBrowserPetOwnerProtocol(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for the original pet owner-list protocol")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/pet-owner-protocol", time.Now().Format("20060102-150405"))
     26 	data := testNPCData()
     27 	data.Playfields[0].ID = 705
     28 	group := &data.Playfields[0].Districts[0].Groups[0]
     29 	group.Name, group.MonsterData, group.AppearanceStatus = "Anger Manifestation", 96195, "inferred_species"
     30 	group.Levels, group.Positions = [2]uint16{1, 1}, [][3]float32{{960, 16.5, 514}}
     31 	useNPCData(t, data)
     32 	useResources(t, assets.Files)
     33 	world := newTestServer(t)
     34 	for index := range 2 {
     35 		player := world.newCharacter()
     36 		player.name = fmt.Sprint("Petowner", index)
     37 		player.profession = 12
     38 		player.stats = testCharacterStats()
     39 		player.stats.NanoAiding.MatterCreation, player.stats.NanoAiding.TimeAndSpace = 6, 6
     40 		player.playfield, player.position = 705, [3]float32{960 + float32(index*2), 16.5, 511}
     41 		player.rotation = [4]float32{0, 0, 0, 1}
     42 		player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("pet-owner-", index))))
     43 		if err := world.saveUser(player); err != nil {
     44 			t.Fatal(err)
     45 		}
     46 	}
     47 	actors := world.characters
     48 	shutdown := startTestService(t, world)
     49 	mux := http.NewServeMux()
     50 	mux.Handle("/", world.httpHandler())
     51 	for _, action := range []struct {
     52 		path string
     53 		code uint32
     54 	}{{"link", 0x194e4f76}, {"unlink", 0x58742a0f}} {
     55 		mux.HandleFunc("POST /test-"+action.path, testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     56 			body := binary.BigEndian.AppendUint32(nil, 50000)
     57 			body = binary.BigEndian.AppendUint32(body, 1000000)
     58 			for _, client := range world.clients {
     59 				if err := client.send(n3Packet(action.code, 50000, 1, body)); err != nil {
     60 					t.Error(err)
     61 					http.Error(w, err.Error(), http.StatusInternalServerError)
     62 					return
     63 				}
     64 			}
     65 		}))
     66 	}
     67 	mux.HandleFunc("POST /test-counter", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     68 		body := binary.BigEndian.AppendUint32(nil, 251)
     69 		body = binary.BigEndian.AppendUint32(body, 1)
     70 		for _, client := range world.clients {
     71 			if err := client.send(n3Packet(0x2b333d6e, 50000, 1, body)); err != nil {
     72 				t.Error(err)
     73 				http.Error(w, err.Error(), http.StatusInternalServerError)
     74 				return
     75 			}
     76 		}
     77 	}))
     78 	host := httptest.NewServer(mux)
     79 	t.Cleanup(host.Close)
     80 	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     81 	defer cancel()
     82 
     83 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_pet_owner_protocol.mjs"), host.URL, output)
     84 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     85 	err := command.Run()
     86 	host.Close()
     87 	if err := shutdown(); err != nil {
     88 		t.Fatal(err)
     89 	}
     90 
     91 	t.Logf("Browser artifacts: %s", output)
     92 	if err != nil {
     93 		t.Fatal(err)
     94 	}
     95 	if newTestInventoryServer(t).nextItem != 0 || len(actors[1].inventory.Items) != 0 || len(actors[2].inventory.Items) != 0 {
     96 		t.Fatal("protocol fixture changed physical inventory")
     97 	}
     98 }