// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.

package engine

import (
	"bytes"
	"testing"

	"github.com/keybase/client/go/libkb"
	keybase1 "github.com/keybase/client/go/protocol/keybase1"
	"github.com/stretchr/testify/require"
	"golang.org/x/net/context"

	"github.com/keybase/go-crypto/openpgp"
	"github.com/keybase/go-crypto/openpgp/armor"
)

// TestPGPSavePublicPush runs the PGPSave engine, pushing the
// public key to api server and checks that it runs without error.
func TestPGPImportAndExport(t *testing.T) {
	tc := SetupEngineTest(t, "pgpsave")
	defer tc.Cleanup()

	u := CreateAndSignupFakeUser(tc, "login")
	secui := &libkb.TestSecretUI{Passphrase: u.Passphrase}
	uis := libkb.UIs{LogUI: tc.G.UI.GetLogUI(), SecretUI: secui}

	// try all four permutations of push options:

	_, _, key := genPGPKeyAndArmor(t, tc, u.Email)
	eng, err := NewPGPKeyImportEngineFromBytes(tc.G, []byte(key), false)
	if err != nil {
		t.Fatal(err)
	}
	m := NewMetaContextForTest(tc).WithUIs(uis)
	if err = RunEngine2(m, eng); err != nil {
		t.Fatal(err)
	}

	fp, _, key := genPGPKeyAndArmor(t, tc, u.Email)
	eng, err = NewPGPKeyImportEngineFromBytes(tc.G, []byte(key), true)
	if err != nil {
		t.Fatal(err)
	}
	if err = RunEngine2(m, eng); err != nil {
		t.Fatal(err)
	}

	arg := keybase1.PGPExportArg{
		Options: keybase1.PGPQuery{
			Secret: true,
			Query:  fp.String(),
		},
	}

	xe := NewPGPKeyExportEngine(tc.G, arg)
	if err := RunEngine2(m, xe); err != nil {
		t.Fatal(err)
	}

	if len(xe.Results()) != 1 {
		t.Fatalf("Expected 1 key back out")
	}

	arg = keybase1.PGPExportArg{
		Options: keybase1.PGPQuery{
			Secret: true,
			Query:  fp.String()[0:10] + "aabb",
		},
	}

	xe = NewPGPKeyExportEngine(tc.G, arg)
	if err := RunEngine2(m, xe); err != nil {
		t.Fatal(err)
	}
	if len(xe.Results()) != 0 {
		t.Fatalf("num keys exported: %d, expected 0", len(xe.Results()))
	}

	arg = keybase1.PGPExportArg{
		Options: keybase1.PGPQuery{
			Secret: false,
		},
	}
	xe = NewPGPKeyExportEngine(tc.G, arg)
	if err := RunEngine2(m, xe); err != nil {
		t.Fatal(err)
	}
	if len(xe.Results()) != 2 {
		t.Fatalf("Expected two keys back out; got %d", len(xe.Results()))
	}
}

// TestPGPImportPrivImport - import the same key twice, only importing the
// private key the second time / on the second device (CORE-10562).
func TestPGPImportPrivImport(t *testing.T) {
	user, dev1, dev2, cleanup := SetupTwoDevices(t, "login")
	defer cleanup()

	secui := &libkb.TestSecretUI{Passphrase: user.Passphrase}
	uis := libkb.UIs{LogUI: dev1.G.UI.GetLogUI(), SecretUI: secui}

	// Import a private key into dev1
	fp, _, key := genPGPKeyAndArmor(t, dev1, user.Email)
	eng, err := NewPGPKeyImportEngineFromBytes(dev1.G, []byte(key), false)
	if err != nil {
		t.Fatal(err)
	}
	m := NewMetaContextForTest(dev1).WithUIs(uis)
	if err = RunEngine2(m, eng); err != nil {
		t.Fatal(err)
	}

	// Make sure that we've imported it successfully
	xe := NewPGPKeyExportEngine(dev1.G, keybase1.PGPExportArg{
		Options: keybase1.PGPQuery{
			Secret: true,
			Query:  fp.String(),
		},
	})
	if err := RunEngine2(m, xe); err != nil {
		t.Fatal(err)
	}
	if len(xe.Results()) != 1 {
		t.Fatalf("Expected 1 key back out")
	}

	// Make sure that we only have a public key on dev2
	uis = libkb.UIs{LogUI: dev2.G.UI.GetLogUI(), SecretUI: secui}
	m = NewMetaContextForTest(dev2).WithUIs(uis)

	xe = NewPGPKeyExportEngine(dev2.G, keybase1.PGPExportArg{
		Options: keybase1.PGPQuery{
			Secret: true,
			Query:  fp.String(),
		},
	})
	if err := RunEngine2(m, xe); err != nil {
		t.Fatal(err)
	}
	if len(xe.Results()) != 0 {
		t.Fatalf("Expected 0 keys back out")
	}
	xe = NewPGPKeyExportEngine(dev2.G, keybase1.PGPExportArg{
		Options: keybase1.PGPQuery{
			Secret: false,
			Query:  fp.String(),
		},
	})
	if err := RunEngine2(m, xe); err != nil {
		t.Fatal(err)
	}
	if len(xe.Results()) != 1 {
		t.Fatalf("Expected 1 key back out")
	}

	// Run import on dev2
	eng, err = NewPGPKeyImportEngineFromBytes(dev2.G, []byte(key), false)
	if err != nil {
		t.Fatal(err)
	}
	if err = RunEngine2(m, eng); err != nil {
		t.Fatal(err)
	}

	// Secret key should be present
	xe = NewPGPKeyExportEngine(dev1.G, keybase1.PGPExportArg{
		Options: keybase1.PGPQuery{
			Secret: true,
			Query:  fp.String(),
		},
	})
	if err := RunEngine2(m, xe); err != nil {
		t.Fatal(err)
	}
	if len(xe.Results()) != 1 {
		t.Fatalf("Expected 1 key back out")
	}
	xe = NewPGPKeyExportEngine(dev1.G, keybase1.PGPExportArg{
		Options: keybase1.PGPQuery{
			Secret: false,
			Query:  fp.String(),
		},
	})
	if err := RunEngine2(m, xe); err != nil {
		t.Fatal(err)
	}
	if len(xe.Results()) != 1 {
		t.Fatalf("Expected 1 key back out")
	}
}

func TestPGPImportLocalPrivateThenServer(t *testing.T) {
	tc := SetupEngineTest(t, "pgpsave")
	defer tc.Cleanup()

	user := CreateAndSignupFakeUser(tc, "login")
	secui := &libkb.TestSecretUI{Passphrase: user.Passphrase}
	uis := libkb.UIs{LogUI: tc.G.UI.GetLogUI(), SecretUI: secui}

	_, _, key := genPGPKeyAndArmor(t, tc, user.Email)

	// Import a private key locally first.
	eng, err := NewPGPKeyImportEngineFromBytes(tc.G, []byte(key), false /* pushSecret*/)
	require.NoError(t, err)
	mctx := NewMetaContextForTest(tc).WithUIs(uis)
	err = RunEngine2(mctx, eng)
	require.NoError(t, err)

	// Can we import locally twice?
	eng, err = NewPGPKeyImportEngineFromBytes(tc.G, []byte(key), false /* pushSecret*/)
	require.NoError(t, err)
	mctx = NewMetaContextForTest(tc).WithUIs(uis)
	err = RunEngine2(mctx, eng)
	require.NoError(t, err)

	kid := eng.GetKID()

	ss, err := mctx.ActiveDevice().SyncSecretsForce(mctx)
	require.NoError(t, err)
	_, ok := ss.FindPrivateKey(kid.String())
	require.False(t, ok)

	// Try to import with push secret afterwards - user also wants
	// to have this key available online.
	eng, err = NewPGPKeyImportEngineFromBytes(tc.G, []byte(key), true /* pushSecret*/)
	require.NoError(t, err)
	mctx = NewMetaContextForTest(tc).WithUIs(uis)
	err = RunEngine2(mctx, eng)
	require.NoError(t, err)

	ss, err = mctx.ActiveDevice().SyncSecretsForce(mctx)
	require.NoError(t, err)
	privKey, ok := ss.FindPrivateKey(kid.String())
	require.True(t, ok)
	require.NotEmpty(t, privKey.Bundle)
}

// Test for issue 325.
func TestPGPImportPublicKey(t *testing.T) {
	tc := SetupEngineTest(t, "pgpsave")
	defer tc.Cleanup()

	_, err := NewPGPKeyImportEngineFromBytes(tc.G, []byte(pubkeyIssue325), false)
	if err == nil {
		t.Fatal("import of public key didn't generate error")
	}
	if _, ok := err.(libkb.NoSecretKeyError); !ok {
		t.Error(err)
		t.Errorf("error returned for import of public key: %T, expected libkb.NoSecretKeyError", err)
	}
}

func TestPGPImportNotLoggedIn(t *testing.T) {
	tc := SetupEngineTest(t, "pgpnotloggedin")
	defer tc.Cleanup()

	u := CreateAndSignupFakeUser(tc, "login")
	secui := &libkb.TestSecretUI{Passphrase: u.Passphrase}
	uis := libkb.UIs{LogUI: tc.G.UI.GetLogUI(), SecretUI: secui}

	_, _, key := genPGPKeyAndArmor(t, tc, u.Email)
	eng, err := NewPGPKeyImportEngineFromBytes(tc.G, []byte(key), false)
	if err != nil {
		t.Fatal(err)
	}
	m := NewMetaContextForTest(tc).WithUIs(uis)
	Logout(tc)
	err = RunEngine2(m, eng)
	require.Error(t, err)
	require.Contains(t, err.Error(), "Login required")
}

func TestIssue454(t *testing.T) {
	testImportKey(t, "pgp454", keyIssue454, "test")
}

func TestIssue2147(t *testing.T) {
	testImportKey(t, "issue2147", keyIssue2147, keyIssue2147Passphrase)
}

func testImportKey(t *testing.T, which string, armor string, pp string) {
	tc := SetupEngineTest(t, which)
	defer tc.Cleanup()

	CreateAndSignupFakeUser(tc, "login")
	secui := &libkb.TestSecretUI{Passphrase: pp}
	uis := libkb.UIs{LogUI: tc.G.UI.GetLogUI(), SecretUI: secui}
	eng, err := NewPGPKeyImportEngineFromBytes(tc.G, []byte(armor), false)
	eng.arg.OnlySave = true
	if err != nil {
		t.Fatal(err)
	}
	m := NewMetaContextForTest(tc).WithUIs(uis)
	err = RunEngine2(m, eng)
	if err != nil {
		t.Fatal(err)
	}
}

// Issue CORE-2063: check that generated secret key is exported
// to user's GPG keyring.
func TestPGPImportGPGExport(t *testing.T) {
	tc := SetupEngineTest(t, "pgpexp")
	defer tc.Cleanup()

	u := CreateAndSignupFakeUser(tc, "pgp")
	uis := libkb.UIs{LogUI: tc.G.UI.GetLogUI(), SecretUI: u.NewSecretUI()}

	// before running, they should have no pgp keys in key family or in gpg
	me, err := libkb.LoadMe(libkb.NewLoadUserArg(tc.G))
	if err != nil {
		t.Fatal(err)
	}
	if len(me.GetActivePGPKeys(false)) != 0 {
		t.Fatalf("active pgp keys: %d, expected 0", len(me.GetActivePGPKeys(false)))
	}
	gpgPrivate, err := numPrivateGPGKeys(tc.G)
	if err != nil {
		t.Fatal(err)
	}
	if gpgPrivate != 0 {
		t.Fatalf("private gpg keys: %d, expected 0", gpgPrivate)
	}

	// this is similar to how cmd_pgp_gen works:
	genArg := &libkb.PGPGenArg{
		PrimaryBits: 1024,
		SubkeyBits:  1024,
	}
	if err := genArg.MakeAllIds(tc.G); err != nil {
		t.Fatal(err)
	}
	arg := PGPKeyImportEngineArg{
		Gen:        genArg,
		PushSecret: true,
		AllowMulti: true,
		DoExport:   true,
	}
	m := NewMetaContextForTest(tc).WithUIs(uis)
	eng := NewPGPKeyImportEngine(tc.G, arg)
	if err := RunEngine2(m, eng); err != nil {
		t.Fatal(err)
	}

	// after running, they should have one pgp keys in key family and in gpg
	me, err = libkb.LoadMe(libkb.NewLoadUserArg(tc.G))
	if err != nil {
		t.Fatal(err)
	}
	if len(me.GetActivePGPKeys(false)) != 1 {
		t.Errorf("active pgp keys: %d, expected 1", len(me.GetActivePGPKeys(false)))
	}
	gpgPrivate, err = numPrivateGPGKeys(tc.G)
	if err != nil {
		t.Fatal(err)
	}
	if gpgPrivate != 1 {
		t.Errorf("private gpg keys: %d, expected 1", gpgPrivate)
	}
}

// TestPGPImportPushSecretWithoutPassword - the engine should prevent nopw
// users from importing secret keys
func TestPGPImportPushSecretWithoutPassword(t *testing.T) {
	tc := SetupEngineTest(t, "pgpnopw")
	defer tc.Cleanup()

	user, _ := NewFakeUser("login")
	arg := MakeTestSignupEngineRunArg(user)
	arg.StoreSecret = true
	arg.GenerateRandomPassphrase = true
	arg.Passphrase = ""
	_, err := CreateAndSignupFakeUserSafeWithArg(tc.G, user, arg)
	require.NoError(t, err)

	secui := &libkb.TestSecretUI{}
	uis := libkb.UIs{LogUI: tc.G.UI.GetLogUI(), SecretUI: secui}
	_, _, key := genPGPKeyAndArmor(t, tc, user.Email)
	eng, err := NewPGPKeyImportEngineFromBytes(tc.G, []byte(key), true)
	require.Nil(t, err, "engine initialization should succeed")
	m := NewMetaContextForTest(tc).WithUIs(uis)
	err = RunEngine2(m, eng)
	require.NotNil(t, err, "import should fail")
	require.Contains(t, err.Error(), "You need to set your password first before uploading secret keys")
}

func numPrivateGPGKeys(g *libkb.GlobalContext) (int, error) {
	gpg := g.GetGpgClient()
	if err := gpg.Configure(libkb.NewMetaContext(context.Background(), g)); err != nil {
		return 0, err
	}

	index, _, err := gpg.Index(libkb.NewMetaContext(context.Background(), g), true, "")
	if err != nil {
		return 0, err
	}

	return index.Len(), nil
}

func encodeArmoredPrivatePGP(entity *openpgp.Entity) (buf bytes.Buffer, err error) {
	writer, err := armor.Encode(&buf, "PGP PRIVATE KEY BLOCK", nil)
	if err != nil {
		return buf, err
	}
	if err := entity.SerializePrivate(writer, nil); err != nil {
		return buf, err
	}
	if err := writer.Close(); err != nil {
		return buf, err
	}
	return buf, nil
}

func genPGPKeyAndArmor(t *testing.T, tc libkb.TestContext, email string) (libkb.PGPFingerprint, keybase1.KID, string) {
	bundle, err := tc.MakePGPKey(email)
	if err != nil {
		t.Fatal(err)
	}
	buf, err := encodeArmoredPrivatePGP(bundle.Entity)
	require.NoError(t, err)
	fp := *bundle.GetFingerprintP()
	kid := bundle.GetKID()
	return fp, kid, buf.String()
}

const pubkeyIssue325 = `-----BEGIN PGP PUBLIC KEY BLOCK-----
Comment: GPGTools - http://gpgtools.org

mQENBFKK2rkBCADbZJNgrtb5AoBb6DFlCoP1PPXniOGwewDdnty7RJ/2Ue3NO+b2
xcq9ZG2Ex9TsgED0QPUTpdpgZGYHdNQggUPV4LKLaaDoXQ28sjGChKDKe6k6edkT
pL0wxhPrPSJRtlskHylHtbX/0pYVxdgr4o1UwPOmavt8EXYZazOPfphW+bUw9rpk
P7VNnVRUDTANIJeaVuwI+iAyHv4PBDBx0Ffuqv4t/qufYGz1ajbn6itkfRSbrsm6
ruGkr9cxnGoH8ViO6U8ymFQpaXlALE8P5AWM8GSjWleFZJYvW0xlX2aSG+w8mEz0
+SxH6LJSs70z7DOCadzEfS0hXhNYRsLGkmOTABEBAAG0IVZNIFN0cmFrYSA8d2hv
aXNzdHJha2FAZ21haWwuY29tPokBOAQTAQIAIgUCUorauQIbLwYLCQgHAwIGFQgC
CQoLBBYCAwECHgECF4AACgkQnpQQj0+PXPkKpwf9H7e5x+ePbVWlcK7ItTmd8Y/y
M3Pyt7/w+xyJnTd8q1boc7YSioyH9cv+e3xSohSebmf3q1+INY2UvmPCsvI8jKa4
k1ELuWPoX8aMs9zimj03pDm8y4zg/uzCnzubB1ufH7wUA4R429zlkfVIVNFK6c7T
SF6KWQ7hWWhhOGk6A5pCqhSewwQDD+J2AhXCGg31kf7zdWV+w6qGE2UR6/sdU2Yk
sgKholqh9EqH+iyXLtHP81MO7gM5ZBtyjUSlEhM9ULPbzQVcqJGhOZxMNhWHlPf/
l37DX0KIAhthgHAkXCZepAMIFysEz715KRREPfO4R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fG0w9fddZe9ZYcwFs+Qe6bM+JSFJkB+o47AtEcv9dEhQ0g87oRH+DO5
/RRJzo7QQNKzG8juDwrYhKQJ6WrSFaT/a6IEGhvvgg==
=DDQ1
-----END PGP PUBLIC KEY BLOCK-----`

const keyIssue454 = `-----BEGIN PGP PRIVATE KEY BLOCK-----
Comment: GPGTools - http://gpgtools.org

lQc+BFVo5d8BEADByJSWkOrCQsjZcPurVIPIDSz6Fz3C7Pu+0/ZDbCDSAtZKINkN
OEh+YeFXENa5wrKWjXB3A3r9+X73wGztocnHRSJ688Sf6J03jCDneh2CutUELFRV
MXf3r63Fr3RoemF+D+AN+m5hgj7pVfw614ncBYITMQLLbOLjI2N90BLn6V2Txqg8
cInL2IIAoT8neySO+/0D86+89tq4OiIIbcBZwvUJS6i8ArZuw2aJC36u9/oAnPNS
H6K9AF7RJBMOLKExeBOiHSJBhnitlzqYYp24a85stpMX1XEi38pueVufs9lqzOgM
k6e4cfyNRLLElay2BPZ6IVeK59buj98N9F606EI6bS7nHpeYkW14F27/SuBWQ4gb
s4eEWdCX2/U401tK/3mma2t7Ybp37vn5x4ER+5g16DmXdhW+GSYCh69CnyJwXJGX
ZJRLS3mryhGHhZslUEpsJ+T/CY/wOa31T0+g4/9kEbXbYkmBP5eB+If5lvmkYmDR
I7JMG8OfPjHg9rhzecE217YahQlsMGIW0hTFAzKEpCqdDwCvHdej00DIGELnAxI9
hdWHtlmx4S7p0yhzE14pGGWm5ByrS21dE6ErF0EDTJPFxUb/q0XnaPiwO3JiQCQM
P9wqL0WEOW+rVZNW3rt6gPw1qnk7VYQa/ZCiAEq5uzLW4fDqUTytHDps2QARAQAB
/gMDApFjPLgXJipU4+fmRQXMiYulCTbNqYzj1eE5XVAHbekTfj6ym0fXRqe2rc8z
eZ3BzW48IFCvxYtxVX2jySabLQiZXhEPEPpyMtf4Oyg/lrmqM+XmlV0QZh5YEitw
FXgYZnpBh1+QkxJ4k64s5Ux551XE/NKMYDPG+pgjXdYRElrr9gC4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