Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

4.6 KB
Raw Blame
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

//go:build rustport

// Test vectors for the Rust port, generated by whatsmeow's own unexported
// code. generate.sh copies the *_test.go files of this directory into a
// whatsmeow checkout (package whatsmeow) and runs them with the rustport
// tag; they write JSON files into $VECTORS_OUT. The tag keeps them out of
// the generator module's own builds.
package whatsmeow

import (
	"encoding/hex"
	"encoding/json"
	"maps"
	"os"
	"path/filepath"
	"testing"
	"time"

	waBinary "go.mau.fi/whatsmeow/binary"
	"go.mau.fi/whatsmeow/socket"
	"go.mau.fi/whatsmeow/store"
	"go.mau.fi/whatsmeow/types"
	"go.mau.fi/whatsmeow/util/keys"
	waLog "go.mau.fi/whatsmeow/util/log"
)

type vectors map[string]any

func writeVectors(t *testing.T, name string, values vectors) {
	t.Helper()
	out := os.Getenv("VECTORS_OUT")
	if out == "" {
		t.Skip("VECTORS_OUT not set")
	}
	data, err := json.MarshalIndent(values, "", "  ")
	if err != nil {
		t.Fatal(err)
	}
	if err = os.WriteFile(filepath.Join(out, name), append(data, '\n'), 0o644); err != nil {
		t.Fatal(err)
	}
}

func fixedKeyPair(start byte) *keys.KeyPair {
	var private [32]byte
	for i := range private {
		private[i] = start + byte(i)
	}
	return keys.NewKeyPairFromPrivateKey(private)
}

func marshalHex(t *testing.T, node waBinary.Node) string {
	t.Helper()
	data, err := waBinary.Marshal(node)
	if err != nil {
		t.Fatal(err)
	}
	return hex.EncodeToString(data)
}

func mustJID(t *testing.T, jid string) types.JID {
	t.Helper()
	parsed, err := types.ParseJID(jid)
	if err != nil {
		t.Fatal(err)
	}
	return parsed
}

// captureLogger records the node sendNodeAndGetData logs right before it
// hands the payload to the socket.
type captureLogger struct {
	node *waBinary.Node
}

func (l *captureLogger) Warnf(string, ...any)  {}
func (l *captureLogger) Errorf(string, ...any) {}
func (l *captureLogger) Infof(string, ...any)  {}
func (l *captureLogger) Debugf(_ string, args ...any) {
	if len(args) == 1 {
		if node, ok := args[0].(*waBinary.Node); ok {
			copied := *node
			l.node = &copied
		}
	}
}
func (l *captureLogger) Sub(string) waLog.Logger { return l }

// newCaptureClient returns a logged-in client (1234:[email protected],
// 9876:5@lid) with empty stores whose socket is a zero NoiseSocket: sending
// logs the node and then panics before anything leaves the process, and
// captureNode recovers.
func newCaptureClient(t *testing.T) (*Client, *captureLogger) {
	t.Helper()
	empty := &store.NoopStore{}
	id := mustJID(t, "1234:[email protected]")
	device := *store.NoopDevice
	device.ID = &id
	device.LID = mustJID(t, "9876:5@lid")
	device.PushName = "Tester"
	device.Identities = empty
	device.Sessions = empty
	device.PreKeys = empty
	device.SenderKeys = empty
	device.AppStateKeys = empty
	device.AppState = empty
	device.Contacts = empty
	device.ChatSettings = empty
	device.MsgSecrets = empty
	device.PrivacyTokens = empty
	device.NCTSalt = empty
	device.EventBuffer = empty
	device.LIDs = empty
	device.Container = empty
	logger := &captureLogger{}
	cli := NewClient(&device, waLog.Noop)
	cli.sendLog = logger
	cli.socket = &socket.NoiseSocket{}
	return cli, logger
}

// captureNode runs send and returns the node it sent, with the iq id and
// usync sid replaced by ID and SID (they come from a random prefix).
func captureNode(t *testing.T, logger *captureLogger, send func()) string {
	t.Helper()
	logger.node = nil
	func() {
		defer func() { _ = recover() }()
		send()
	}()
	if logger.node == nil {
		t.Fatal("nothing was sent")
	}
	node := *logger.node
	if node.Tag == "iq" {
		node.Attrs = maps.Clone(node.Attrs)
		node.Attrs["id"] = "ID"
		if children := node.GetChildren(); len(children) == 1 && children[0].Tag == "usync" {
			usync := children[0]
			usync.Attrs = maps.Clone(usync.Attrs)
			usync.Attrs["sid"] = "SID"
			node.Content = []waBinary.Node{usync}
		}
	}
	return marshalHex(t, node)
}

func jidString(jid types.JID) string {
	return jid.String()
}

func optionalJIDString(jid *types.JID) any {
	if jid == nil {
		return nil
	}
	return jid.String()
}

func jidStrings(jids []types.JID) []string {
	out := make([]string, 0, len(jids))
	for _, jid := range jids {
		out = append(out, jid.String())
	}
	return out
}

func unixOrZero(ts time.Time) int64 {
	if ts.IsZero() {
		return 0
	}
	return ts.Unix()
}

func lidPairStrings(pairs []store.LIDMapping) [][2]string {
	out := make([][2]string, 0, len(pairs))
	for _, pair := range pairs {
		out = append(out, [2]string{pair.LID.String(), pair.PN.String()})
	}
	return out
}