// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package plugin

import (
	"context"
	"crypto/tls"
	"crypto/x509"
	"errors"
	"fmt"
	"io"
	"log"
	"net/rpc"
	"os"
	"os/exec"
	"testing"
	"time"

	"github.com/golang/protobuf/ptypes/empty"
	"github.com/hashicorp/go-hclog"
	grpctest "github.com/hashicorp/go-plugin/test/grpc"
	"google.golang.org/grpc"
)

// Test that NetRPCUnsupportedPlugin implements the correct interfaces.
var _ Plugin = new(NetRPCUnsupportedPlugin)

var testHandshake = HandshakeConfig{
	ProtocolVersion:  1,
	MagicCookieKey:   "TEST_MAGIC_COOKIE",
	MagicCookieValue: "test",
}

var testVersionedHandshake = HandshakeConfig{
	MagicCookieKey:   "TEST_MAGIC_COOKIE",
	MagicCookieValue: "test",
}

// testInterface is the test interface we use for plugins.
type testInterface interface {
	Double(int) int
	PrintKV(string, interface{})
	Bidirectional() error
	PrintStdio(stdout, stderr []byte)
	Panic(msg string) error
}

// testStreamer is used to test the grpc streaming interface
type testStreamer interface {
	Stream(int32, int32) ([]int32, error)
}

// testInterfacePlugin implements the Plugin interface
var _ Plugin = (*testInterfacePlugin)(nil)

// testInterfacePlugin is the implementation of Plugin to create
// RPC client/server implementations for testInterface.
type testInterfacePlugin struct {
	Impl testInterface
}

func (p *testInterfacePlugin) Server(b *MuxBroker) (interface{}, error) {
	return &testInterfaceServer{Impl: p.impl()}, nil
}

func (p *testInterfacePlugin) Client(b *MuxBroker, c *rpc.Client) (interface{}, error) {
	return &testInterfaceClient{Client: c}, nil
}

func (p *testInterfacePlugin) impl() testInterface {
	if p.Impl != nil {
		return p.Impl
	}

	return &testInterfaceImpl{
		logger: hclog.New(&hclog.LoggerOptions{
			Level:      hclog.Trace,
			Output:     os.Stderr,
			JSONFormat: true,
		}),
	}
}

// testGRPCInterfacePlugin implements both Plugin and GRPCPlugin interfaces
var _ Plugin = (*testGRPCInterfacePlugin)(nil)
var _ GRPCPlugin = (*testGRPCInterfacePlugin)(nil)

// testGRPCInterfacePlugin is a test implementation of the GRPCPlugin interface
type testGRPCInterfacePlugin struct {
	Impl testInterface
}

func (p *testGRPCInterfacePlugin) Server(b *MuxBroker) (interface{}, error) {
	return &testInterfaceServer{Impl: p.impl()}, nil
}

func (p *testGRPCInterfacePlugin) Client(b *MuxBroker, c *rpc.Client) (interface{}, error) {
	return &testInterfaceClient{Client: c}, nil
}

func (p *testGRPCInterfacePlugin) GRPCServer(b *GRPCBroker, s *grpc.Server) error {
	grpctest.RegisterTestServer(s, &testGRPCServer{broker: b, Impl: p.impl()})
	return nil
}

func (p *testGRPCInterfacePlugin) GRPCClient(doneCtx context.Context, b *GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
	return &testGRPCClient{broker: b, Client: grpctest.NewTestClient(c)}, nil
}

func (p *testGRPCInterfacePlugin) impl() testInterface {
	if p.Impl != nil {
		return p.Impl
	}

	return &testInterfaceImpl{
		logger: hclog.New(&hclog.LoggerOptions{
			Level:      hclog.Trace,
			Output:     os.Stderr,
			JSONFormat: true,
		}),
	}
}

// testInterfaceImpl implements testInterface concretely
var _ testInterface = (*testInterfaceImpl)(nil)

type testInterfaceImpl struct {
	logger hclog.Logger
}

func (i *testInterfaceImpl) Double(v int) int { return v * 2 }

func (i *testInterfaceImpl) PrintKV(key string, value interface{}) {
	i.logger.Info("PrintKV called", key, value)
}

func (i *testInterfaceImpl) Bidirectional() error {
	return nil
}

func (i *testInterfaceImpl) PrintStdio(stdout, stderr []byte) {
	if len(stdout) > 0 {
		_, _ = fmt.Fprint(os.Stdout, string(stdout))
		_ = os.Stdout.Sync()
	}

	if len(stderr) > 0 {
		_, _ = fmt.Fprint(os.Stderr, string(stderr))
		_ = os.Stderr.Sync()
	}
}

func (i *testInterfaceImpl) Panic(msg string) error {
	panic(msg)
}

// testInterfaceClient implements testInterface to communicate over RPC
type testInterfaceClient struct {
	Client *rpc.Client
}

func (impl *testInterfaceClient) Double(v int) int {
	var resp int
	err := impl.Client.Call("Plugin.Double", v, &resp)
	if err != nil {
		panic(err)
	}

	return resp
}

func (impl *testInterfaceClient) PrintKV(key string, value interface{}) {
	err := impl.Client.Call("Plugin.PrintKV", map[string]interface{}{
		"key":   key,
		"value": value,
	}, &struct{}{})
	if err != nil {
		panic(err)
	}
}

func (impl *testInterfaceClient) Bidirectional() error {
	return nil
}

func (impl *testInterfaceClient) PrintStdio(stdout, stderr []byte) {
	// We don't implement this because we test stream syncing another
	// way (see rpc_client_test.go). We probably should test this way
	// but very few people use the net/rpc protocol nowadays so we didn'
	// put in the effort.
}

func (impl *testInterfaceClient) Panic(msg string) error {
	return nil
}

// testInterfaceServer is the RPC server for testInterfaceClient
type testInterfaceServer struct {
	Broker *MuxBroker
	Impl   testInterface
}

func (s *testInterfaceServer) Double(arg int, resp *int) error {
	*resp = s.Impl.Double(arg)
	return nil
}

func (s *testInterfaceServer) PrintKV(args map[string]interface{}, _ *struct{}) error {
	s.Impl.PrintKV(args["key"].(string), args["value"])
	return nil
}

// testPluginMap can be used for tests as a plugin map
var testPluginMap = map[string]Plugin{
	"test": new(testInterfacePlugin),
}

// testGRPCPluginMap can be used for tests as that need a GRPC plugin
var testGRPCPluginMap = map[string]Plugin{
	"test": new(testGRPCInterfacePlugin),
}

// testGRPCServer is the implementation of our GRPC service.
type testGRPCServer struct {
	grpctest.UnimplementedTestServer
	Impl   testInterface
	broker *GRPCBroker
}

func (s *testGRPCServer) Double(
	ctx context.Context,
	req *grpctest.TestRequest) (*grpctest.TestResponse, error) {
	return &grpctest.TestResponse{
		Output: int32(s.Impl.Double(int(req.Input))),
	}, nil
}

func (s *testGRPCServer) PrintKV(
	ctx context.Context,
	req *grpctest.PrintKVRequest) (*grpctest.PrintKVResponse, error) {
	var v interface{}
	switch rv := req.Value.(type) {
	case *grpctest.PrintKVRequest_ValueString:
		v = rv.ValueString

	case *grpctest.PrintKVRequest_ValueInt:
		v = rv.ValueInt

	default:
		panic(fmt.Sprintf("unknown value: %#v", req.Value))
	}

	s.Impl.PrintKV(req.Key, v)
	return &grpctest.PrintKVResponse{}, nil
}

func (s *testGRPCServer) Bidirectional(ctx context.Context, req *grpctest.BidirectionalRequest) (*grpctest.BidirectionalResponse, error) {
	conn, err := s.broker.Dial(req.Id)
	if err != nil {
		return nil, fmt.Errorf("server dial error: %w", err)
	}

	pingPongClient := grpctest.NewPingPongClient(conn)
	resp, err := pingPongClient.Ping(ctx, &grpctest.PingRequest{})
	if err != nil {
		return nil, fmt.Errorf("server Ping() error: %w", err)
	}
	if resp.Msg != "pong" {
		return nil, errors.New("server Bad PingPong")
	}

	nextID := s.broker.NextId()
	go s.broker.AcceptAndServe(nextID, func(opts []grpc.ServerOption) *grpc.Server {
		s := grpc.NewServer(opts...)
		grpctest.RegisterPingPongServer(s, &pingPongServer{})
		return s
	})

	return &grpctest.BidirectionalResponse{
		Id: nextID,
	}, nil
}

func (s *testGRPCServer) PrintStdio(
	ctx context.Context,
	req *grpctest.PrintStdioRequest,
) (*empty.Empty, error) {
	s.Impl.PrintStdio(req.Stdout, req.Stderr)
	return &empty.Empty{}, nil
}

func (s *testGRPCServer) Panic(ctx context.Context, req *grpctest.PanicRequest) (*empty.Empty, error) {
	err := s.Impl.Panic(req.Message)
	return &empty.Empty{}, err
}

type pingPongServer struct {
	grpctest.UnimplementedPingPongServer
}

func (p *pingPongServer) Ping(ctx context.Context, req *grpctest.PingRequest) (*grpctest.PongResponse, error) {
	return &grpctest.PongResponse{
		Msg: "pong",
	}, nil
}

func (s testGRPCServer) Stream(stream grpctest.Test_StreamServer) error {
	for {
		req, err := stream.Recv()
		if err != nil {
			if err != io.EOF {
				return err
			}
			return nil
		}

		if err := stream.Send(&grpctest.TestResponse{Output: req.Input}); err != nil {
			return err
		}
	}
}

// testGRPCClient is an implementation of TestInterface that communicates
// over gRPC.
type testGRPCClient struct {
	Client grpctest.TestClient
	broker *GRPCBroker
}

func (c *testGRPCClient) Double(v int) int {
	resp, err := c.Client.Double(context.Background(), &grpctest.TestRequest{
		Input: int32(v),
	})
	if err != nil {
		panic(err)
	}

	return int(resp.Output)
}

func (c *testGRPCClient) PrintKV(key string, value interface{}) {
	req := &grpctest.PrintKVRequest{Key: key}
	switch v := value.(type) {
	case string:
		req.Value = &grpctest.PrintKVRequest_ValueString{
			ValueString: v,
		}

	case int:
		req.Value = &grpctest.PrintKVRequest_ValueInt{
			ValueInt: int32(v),
		}

	default:
		panic(fmt.Sprintf("unknown type: %T", value))
	}

	_, err := c.Client.PrintKV(context.Background(), req)
	if err != nil {
		panic(err)
	}
}

func (c *testGRPCClient) Bidirectional() error {
	nextID := c.broker.NextId()
	go c.broker.AcceptAndServe(nextID, func(opts []grpc.ServerOption) *grpc.Server {
		s := grpc.NewServer(opts...)
		grpctest.RegisterPingPongServer(s, &pingPongServer{})
		return s
	})

	resp, err := c.Client.Bidirectional(context.Background(), &grpctest.BidirectionalRequest{
		Id: nextID,
	})
	if err != nil {
		return fmt.Errorf("client Bidirectional() error: %w", err)
	}

	conn, err := c.broker.Dial(resp.Id)
	if err != nil {
		return fmt.Errorf("client dial error: %w", err)
	}

	pingPongClient := grpctest.NewPingPongClient(conn)
	pResp, err := pingPongClient.Ping(context.Background(), &grpctest.PingRequest{})
	if err != nil {
		return fmt.Errorf("client Ping() error: %w", err)
	}
	if pResp.Msg != "pong" {
		return errors.New("client Bad PingPong")
	}
	return nil
}

// Stream sends a series of requests from [start, stop) using a bidirectional
// streaming service, and returns the streamed responses.
func (impl *testGRPCClient) Stream(start, stop int32) ([]int32, error) {
	if stop <= start {
		return nil, fmt.Errorf("invalid range [%d, %d)", start, stop)
	}
	streamClient, err := impl.Client.Stream(context.Background())
	if err != nil {
		return nil, err
	}

	var resp []int32
	for i := start; i < stop; i++ {
		if err := streamClient.Send(&grpctest.TestRequest{Input: i}); err != nil {
			return resp, err
		}

		out, err := streamClient.Recv()
		if err != nil {
			return resp, err
		}

		resp = append(resp, out.Output)
	}

	_ = streamClient.CloseSend()

	return resp, nil
}

func (c *testGRPCClient) PrintStdio(stdout, stderr []byte) {
	_, err := c.Client.PrintStdio(context.Background(), &grpctest.PrintStdioRequest{
		Stdout: stdout,
		Stderr: stderr,
	})
	if err != nil {
		panic(err)
	}
}

func (c *testGRPCClient) Panic(msg string) error {
	_, err := c.Client.Panic(context.Background(), &grpctest.PanicRequest{Message: msg})
	return err
}

func helperProcess(s ...string) *exec.Cmd {
	cs := []string{"-test.run=TestHelperProcess", "--"}
	cs = append(cs, s...)
	env := []string{
		"GO_WANT_HELPER_PROCESS=1",
	}

	cmd := exec.Command(os.Args[0], cs...)
	cmd.Env = append(env, os.Environ()...)
	return cmd
}

// This is not a real test. This is just a helper process kicked off by
// tests.
func TestHelperProcess(*testing.T) {
	if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
		return
	}

	defer os.Exit(0)

	args := os.Args
	for len(args) > 0 {
		if args[0] == "--" {
			args = args[1:]
			break
		}

		args = args[1:]
	}

	if len(args) == 0 {
		fmt.Fprintf(os.Stderr, "No command\n")
		os.Exit(2)
	}

	// override testPluginMap with one that uses
	// hclog logger on its implementation
	pluginLogger := hclog.New(&hclog.LoggerOptions{
		Level:      hclog.Trace,
		Output:     os.Stderr,
		JSONFormat: true,
	})

	testPlugin := &testInterfaceImpl{
		logger: pluginLogger,
	}

	testPluginMap := map[string]Plugin{
		"test": &testInterfacePlugin{Impl: testPlugin},
	}

	testGRPCPluginMap := map[string]Plugin{
		"test": &testGRPCInterfacePlugin{Impl: testPlugin},
	}

	cmd, args := args[0], args[1:]
	switch cmd {
	case "bad-version":
		// If we have an arg, we write there on start
		if len(args) > 0 {
			path := args[0]
			err := os.WriteFile(path, []byte("foo"), 0644)
			if err != nil {
				panic(err)
			}
		}

		fmt.Printf("%d|%d1|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
		<-make(chan int)
	case "invalid-rpc-address":
		fmt.Println("lolinvalid")
	case "mock":
		fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
		<-make(chan int)
	case "start-timeout":
		time.Sleep(1 * time.Minute)
		os.Exit(1)
	case "stderr":
		fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
		_, _ = os.Stderr.WriteString("HELLO\n")
		_, _ = os.Stderr.WriteString("WORLD\n")
	case "stderr-json":
		// write values that might be JSON, but aren't KVs
		_, _ = fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
		_, _ = os.Stderr.WriteString("[\"HELLO\"]\n")
		_, _ = os.Stderr.WriteString("12345\n")
		_, _ = os.Stderr.WriteString("{\"a\":1}\n")
	case "level-warn-text":
		// write values that might be JSON, but aren't KVs
		_, _ = fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
		_, _ = os.Stderr.WriteString("[WARN] test line 98765\n")
	case "stdin":
		_, _ = fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
		data := make([]byte, 5)
		if _, err := os.Stdin.Read(data); err != nil {
			log.Printf("stdin read error: %s", err)
			os.Exit(100)
		}

		if string(data) == "hello" {
			os.Exit(0)
		}

		os.Exit(1)
	case "cleanup":
		// Create a defer to write the file. This tests that we get cleaned
		// up properly versus just calling os.Exit
		path := args[0]
		defer func() {
			err := os.WriteFile(path, []byte("foo"), 0644)
			if err != nil {
				panic(err)
			}
		}()

		Serve(&ServeConfig{
			HandshakeConfig: testHandshake,
			Plugins:         testPluginMap,
		})

		// Exit
		return
	case "test-grpc":
		Serve(&ServeConfig{
			HandshakeConfig: testHandshake,
			Plugins:         testGRPCPluginMap,
			GRPCServer:      DefaultGRPCServer,
		})

		// Shouldn't reach here but make sure we exit anyways
		os.Exit(0)
	case "test-grpc-tls":
		// Serve!
		Serve(&ServeConfig{
			HandshakeConfig: testHandshake,
			Plugins:         testGRPCPluginMap,
			GRPCServer:      DefaultGRPCServer,
			TLSProvider:     helperTLSProvider,
		})

		// Shouldn't reach here but make sure we exit anyways
		os.Exit(0)
	case "test-interface":
		Serve(&ServeConfig{
			HandshakeConfig: testHandshake,
			Plugins:         testPluginMap,
		})

		// Shouldn't reach here but make sure we exit anyways
		os.Exit(0)
	case "test-interface-logger-netrpc":
		Serve(&ServeConfig{
			HandshakeConfig: testHandshake,
			Plugins:         testPluginMap,
		})
		// Shouldn't reach here but make sure we exit anyways
		os.Exit(0)
	case "test-interface-logger-grpc":
		Serve(&ServeConfig{
			HandshakeConfig: testHandshake,
			Plugins:         testGRPCPluginMap,
			GRPCServer:      DefaultGRPCServer,
		})
		// Shouldn't reach here but make sure we exit anyways
		os.Exit(0)
	case "test-interface-daemon":
		// Serve!
		Serve(&ServeConfig{
			HandshakeConfig: testHandshake,
			Plugins:         testPluginMap,
		})

		// Shouldn't reach here but make sure we exit anyways
		os.Exit(0)
	case "test-interface-tls":
		// Serve!
		Serve(&ServeConfig{
			HandshakeConfig: testHandshake,
			Plugins:         testPluginMap,
			TLSProvider:     helperTLSProvider,
		})

		// Shouldn't reach here but make sure we exit anyways
		os.Exit(0)

	case "test-interface-mtls":
		// Serve!
		Serve(&ServeConfig{
			HandshakeConfig: testVersionedHandshake,
			Plugins:         testPluginMap,
		})

		// Shouldn't reach here but make sure we exit anyways
		os.Exit(0)

	case "test-versioned-plugins":
		// Serve!
		Serve(&ServeConfig{
			HandshakeConfig: testVersionedHandshake,
			VersionedPlugins: map[int]PluginSet{
				2: testGRPCPluginMap,
			},
			GRPCServer:  DefaultGRPCServer,
			TLSProvider: helperTLSProvider,
		})

		// Shouldn't reach here but make sure we exit anyways
		os.Exit(0)
	case "test-proto-upgraded-plugin":
		// Serve 2 plugins over different protocols
		Serve(&ServeConfig{
			HandshakeConfig: testVersionedHandshake,
			VersionedPlugins: map[int]PluginSet{
				1: PluginSet{
					"old": &testInterfacePlugin{Impl: testPlugin},
				},
				2: testGRPCPluginMap,
			},
			GRPCServer:  DefaultGRPCServer,
			TLSProvider: helperTLSProvider,
		})

		// Shouldn't reach here but make sure we exit anyways
		os.Exit(0)
	case "test-proto-upgraded-client":
		// Serve 2 plugins over different protocols
		Serve(&ServeConfig{
			HandshakeConfig: HandshakeConfig{
				ProtocolVersion:  2,
				MagicCookieKey:   "TEST_MAGIC_COOKIE",
				MagicCookieValue: "test",
			},
			Plugins:     testGRPCPluginMap,
			GRPCServer:  DefaultGRPCServer,
			TLSProvider: helperTLSProvider,
		})

		// Shouldn't reach here but make sure we exit anyways
		os.Exit(0)
	case "test-mtls":
		// Serve 2 plugins over different protocols
		Serve(&ServeConfig{
			HandshakeConfig: HandshakeConfig{
				ProtocolVersion:  2,
				MagicCookieKey:   "TEST_MAGIC_COOKIE",
				MagicCookieValue: "test",
			},
			Plugins:    testGRPCPluginMap,
			GRPCServer: DefaultGRPCServer,
		})

		// Shouldn't reach here but make sure we exit anyways
		os.Exit(0)
	case "test-skip-host-env-true":
		fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
		if os.Getenv("PLUGIN_TEST_SKIP_HOST_ENV") == "" {
			os.Exit(0)
		}

		os.Exit(1)
	case "test-skip-host-env-false":
		fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
		if os.Getenv("PLUGIN_TEST_SKIP_HOST_ENV") != "" {
			os.Exit(0)
		}

		os.Exit(1)
	case "mux-grpc-with-old-plugin":
		// gRPC broker multiplexing requested, but plugin has no awareness of the
		// feature, so just prints 6 segments in the protocol negotiation line.
		// Client should fail with a helpful error.
		if os.Getenv(envMultiplexGRPC) == "" {
			fmt.Println("failed precondition for mux test")
			os.Exit(1)
		}
		fmt.Printf("%d|%d|tcp|:1234|%s|\n", CoreProtocolVersion, testHandshake.ProtocolVersion, ProtocolGRPC)
		<-make(chan int)
	case "mux-grpc-with-unsupported-plugin":
		// gRPC broker multiplexing requested, but plugin explicitly does not
		// support it. Client should fail with a helpful error.
		if os.Getenv(envMultiplexGRPC) == "" {
			fmt.Println("failed precondition for mux test")
			os.Exit(1)
		}
		fmt.Printf("%d|%d|tcp|:1234|%s||false\n", CoreProtocolVersion, testHandshake.ProtocolVersion, ProtocolGRPC)
		<-make(chan int)
	default:
		fmt.Fprintf(os.Stderr, "Unknown command: %q\n", cmd)
		os.Exit(2)
	}
}

func helperTLSProvider() (*tls.Config, error) {
	serverCert, err := tls.X509KeyPair([]byte(TestClusterServerCert), []byte(TestClusterServerKey))
	if err != nil {
		return nil, err
	}

	rootCAs := x509.NewCertPool()
	rootCAs.AppendCertsFromPEM([]byte(TestClusterCACert))
	tlsConfig := &tls.Config{
		Certificates: []tls.Certificate{serverCert},
		RootCAs:      rootCAs,
		ClientCAs:    rootCAs,
		ClientAuth:   tls.VerifyClientCertIfGiven,
		ServerName:   "127.0.0.1",
	}

	return tlsConfig, nil
}

const (
	TestClusterCACert = `-----BEGIN CERTIFICATE-----
MIIDPjCCAiagAwIBAgIUfIKsF2VPT7sdFcKOHJH2Ii6K4MwwDQYJKoZIhvcNAQEL
BQAwFjEUMBIGA1UEAxMLbXl2YXVsdC5jb20wIBcNMTYwNTAyMTYwNTQyWhgPMjA2
NjA0MjAxNjA2MTJaMBYxFDASBgNVBAMTC215dmF1bHQuY29tMIIBIjANBgkqhkiG
9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuOimEXawD2qBoLCFP3Skq5zi1XzzcMAJlfdS
xz9hfymuJb+cN8rB91HOdU9wQCwVKnkUtGWxUnMp0tT0uAZj5NzhNfyinf0JGAbP
67HDzVZhGBHlHTjPX0638yaiUx90cTnucX0N20SgCYct29dMSgcPl+W78D3Jw3xE
JsHQPYS9ASe2eONxG09F/qNw7w/RO5/6WYoV2EmdarMMxq52pPe2chtNMQdSyOUb
cCcIZyk4QVFZ1ZLl6jTnUPb+JoCx1uMxXvMek4NF/5IL0Wr9dw2gKXKVKoHDr6SY
WrCONRw61A5Zwx1V+kn73YX3USRlkufQv/ih6/xThYDAXDC9cwIDAQABo4GBMH8w
DgYDVR0PAQH/BAQDAgEGM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