package protocol import ( "bytes" "encoding/binary" "strings" "testing" ) // buildLoginInfo lays out a synthetic LOGIN_INFOR packet. The C++ struct is // fixed-width and NUL-padded; fields not touched here stay zero. func buildLoginInfo(moduleVersion, clientType string, pcName []byte) []byte { b := make([]byte, OffsetReserved+512) b[0] = TokenLogin copy(b[OffsetModuleVersion:], moduleVersion) copy(b[OffsetPCName:OffsetPCName+240], pcName) copy(b[OffsetReserved:], clientType) return b } // A Chinese Windows host name arrives as ANSI (CP936/GBK) bytes: the client // fills LOGIN_INFOR.szPCName from gethostname(), a narrow Win32 API, whatever // CLIENT_CAP_UTF8 says (client/LoginServer.cpp:380). The MFC server converts // this field with CP_ACP, so it renders correctly; the Go server used to feed // it to the UTF-8 decoder for every modern client, turning each GBK byte into // U+FFFD ("����") in the web console. func TestLoginInfoGBKHostNameFromUTF8CapableClient(t *testing.T) { name := "我的电脑/研发组" raw := buildLoginInfo("Oct 20 2025-0006", "EXE", UTF8ToGBK(name)) info, err := ParseLoginInfo(raw) if err != nil { t.Fatalf("ParseLoginInfo: %v", err) } if info.PCName != name { t.Fatalf("PCName = %q, want %q", info.PCName, name) } } func TestLoginInfoGBKHostNameFromLegacyClient(t *testing.T) { name := "张三的电脑" raw := buildLoginInfo("Dec 19 2024", "EXE", UTF8ToGBK(name)) info, err := ParseLoginInfo(raw) if err != nil { t.Fatalf("ParseLoginInfo: %v", err) } if info.PCName != name { t.Fatalf("PCName = %q, want %q", info.PCName, name) } } // POSIX clients build szPCName from gethostname() on a UTF-8 system, so their // non-ASCII names must not be run through the GBK decoder. func TestLoginInfoUTF8HostNameFromLinuxClient(t *testing.T) { name := "Nürnberg-PC" raw := buildLoginInfo("Oct 20 2025-0006", "LNX", []byte(name)) info, err := ParseLoginInfo(raw) if err != nil { t.Fatalf("ParseLoginInfo: %v", err) } if info.PCName != name { t.Fatalf("PCName = %q, want %q", info.PCName, name) } } // The APK client sends the Android device model as UTF-8. func TestLoginInfoUTF8HostNameFromAndroidClient(t *testing.T) { name := "小米14" raw := buildLoginInfo("Oct 20 2025-8007", "APK", []byte(name)) info, err := ParseLoginInfo(raw) if err != nil { t.Fatalf("ParseLoginInfo: %v", err) } if info.PCName != name { t.Fatalf("PCName = %q, want %q", info.PCName, name) } } // Pre-1.0 macOS builds report the client type as "macOS" (current builds send // "MAC"); their host names are UTF-8 too and must not be GBK-decoded. func TestLoginInfoUTF8HostNameFromLegacyMacClient(t *testing.T) { name := "München-Mac" raw := buildLoginInfo("Dec 19 2024", "macOS", []byte(name)) info, err := ParseLoginInfo(raw) if err != nil { t.Fatalf("ParseLoginInfo: %v", err) } if info.PCName != name { t.Fatalf("PCName = %q, want %q", info.PCName, name) } } // Reserved must keep following CLIENT_CAP_UTF8: RES_FILE_PATH is UTF-8 on // modern clients (client/LoginServer.cpp:411) while legacy clients sent // CP_ACP, and the location field is UTF-8 for modern clients too. func TestLoginInfoReservedStillHonorsCapability(t *testing.T) { path := `C:\Users\张三\Desktop\样本.exe` reserved := strings.Join([]string{ "EXE", // 0 client type "64", // 1 system bits "8", // 2 cpu "16", // 3 memory path, // 4 file path (UTF-8 on capable clients) "?", // 5 "2025-01-02", // 6 install time "?", // 7 "64", // 8 program bits "", // 9 "北京/朝阳", // 10 location (UTF-8 on capable clients) "1.2.3.4", // 11 public ip "v1.0", // 12 version "user", // 13 username "0", // 14 admin }, "|") raw := buildLoginInfo("Oct 20 2025-0006", "", UTF8ToGBK("PC")) copy(raw[OffsetReserved:], reserved) // clientType itself lives in field 0 of the very same Reserved bytes. if !bytes.HasPrefix(raw[OffsetReserved:], []byte("EXE|")) { t.Fatal("test fixture broken") } info, err := ParseLoginInfo(raw) if err != nil { t.Fatalf("ParseLoginInfo: %v", err) } if got := info.GetReservedField(ResFieldFilePath); got != path { t.Fatalf("file path = %q, want %q", got, path) } if got := info.GetReservedField(ResFieldClientLoc); got != "北京/朝阳" { t.Fatalf("location = %q, want %q", got, "北京/朝阳") } } func TestLoginInfoBinaryFieldsUnaffected(t *testing.T) { raw := buildLoginInfo("Oct 20 2025-0006", "EXE", UTF8ToGBK("PC")) binary.LittleEndian.PutUint32(raw[OffsetCPUMHz:], 3600) raw[OffsetWebCamExist] = 1 binary.LittleEndian.PutUint32(raw[OffsetSpeed:], 123) info, err := ParseLoginInfo(raw) if err != nil { t.Fatalf("ParseLoginInfo: %v", err) } if info.CPUMHz != 3600 || !info.WebCamExist || info.Speed != 123 { t.Fatalf("binary fields = %d/%v/%d", info.CPUMHz, info.WebCamExist, info.Speed) } if info.PCName != "PC" { t.Fatalf("PCName = %q", info.PCName) } }