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

package libkb

import (
	"encoding/base64"
	require "github.com/stretchr/testify/require"
	"testing"
)

func TestKeithWinstein(t *testing.T) {

	var err error
	// Encode the key with spaces as a Base64-encoded string, since trailing
	// spaces don't survive our various text editors
	key64 := "[encoded data, 15464 characters]"
	key, err := base64.StdEncoding.DecodeString(key64)
	if err != nil {
		t.Fatal(err)
	} else if _, _, err = ReadOneKeyFromString(string(key)); err != nil {
		t.Fatal(err)
	}
}

func TestBug8612Prepass(t *testing.T) {

	whackyKey := `-----BEGIN PGP PUBLIC KEY BLOCK-----              [1/2]

mI0EWcknrwEEAK2X5lKA76pf6i5D1aVcApUAH6NnZ4NkFeSxKT92soiSWkFn+I/G
VKJfTvx2dzxOAB4rvyFjUzjgAwhK3FblCnfXwgPAh6/vukF/YBwynCVyNxOVAVHY
gCkw/+7zIM24RUxzI3V9wzJ6i/SpfNnWKkKqJXIt4Xzv3Rs/UXk5DMY5ABEBAAG0
I01heCBUZXN0IDc3NyA8dGhlbWF4Kzc3N0BnbWFpbC5jb20+iLgEEwECACIFAlnJ
J68CGwMGCwkIBwMCBhUIAgkKCwQWAgMBAh4BAheAAAoJEM5jclcHa4v8ssQD/RJA
JTrfydAkLzffGgZyKafs/aTraVDNtDv1sF+5iBtZvOB0FFjzivl3BGlplEqnPquB
bg8/OQuxFLYM/f8+WgP5MDqBce8s6h5kGZPj73zP4GQLZWojS5/H3J1sDBmNTSnd
ByELhvVxJmJiHdz2jMb74+thB/ZK4xf+zAkRF4WZuI0EWcknrwEEALk0Bsp/xJ+4
75hswySLZmvNh79CyIC0K1gzXiEPDwqGy6xT1PbXLJ5HcXna4HJdNAgXf1T/n+zR
19xJMAB6NSv2zigRaYAyy4It2p2cRPHseWWTcP7lMUpwqtQsnqNKJ14RuAaMOQtG
xSaQMgMGZx6lxFWNaK40SxSnqFRtfRs9ABEBAAGInwQYAQIACQUCWcknrwIbDAAK
CRDOY3JXB2uL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KMkuJv90hxxt1rqjN6
3GFiwBVdsSuyEb3uQJ/ytAyVozwwxjMQZ+gJTYfK8syPdf2T1W6cv7lfHp8E8g==
=sJQD
-----END PGP PUBLIC KEY BLOCK-----`

	require.Equal(t, regularKey, bug8612Prepass(regularKey))
	require.Equal(t, regularKey, bug8612Prepass(whackyKey))

	_, _, err := ReadOneKeyFromString(whackyKey)
	require.Error(t, err)
	_, _, err = ReadOneKeyFromStringLiberal(whackyKey)
	require.NoError(t, err)

}
