// Copyright © 2016 Asteris, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package keystore

import (
	"bytes"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
)

func TestStoreTrustedKey(t *testing.T) {
	ks, ksPath, err := NewTestKeystore()
	if err != nil {
		t.Errorf("unexpected error %v", err)
	}
	defer os.RemoveAll(ksPath)

	output, err := ks.StoreTrustedKey(publicKey)
	if err != nil {
		t.Fatalf("unexpected error %v", err)
	}
	if filepath.Base(output) != fingerprint {
		t.Errorf("expected finger print %s, got %v", fingerprint, filepath.Base(output))
	}
	if err = ks.DeleteTrustedKey(fingerprint); err != nil {
		t.Errorf("unexpected error %v", err)
	}
	if _, err = os.Lstat(output); !os.IsNotExist(err) {
		t.Errorf("unexpected error %v", err)
	}

	output, err = ks.MaskTrustedSystemKey(fingerprint)
	if err != nil {
		t.Errorf("unexpected error %v", err)
	}
	fileInfo, err := os.Lstat(output)
	if err != nil {
		t.Errorf("unexpected error %v", err)
	}
	if fileInfo.Size() != 0 {
		t.Errorf("expected empty file")
	}
}

func TestCheckSignature(t *testing.T) {
	ks, ksPath, err := NewTestKeystore()
	if err != nil {
		t.Fatalf("unexpected error %v", err)
	}
	defer os.RemoveAll(ksPath)

	_, err = ks.StoreTrustedKey(publicKey)
	if err != nil {
		t.Fatalf("unexpected error %v", err)
	}

	err = ks.CheckSignature(bytes.NewReader(module), bytes.NewReader(signature))
	if err != nil {
		t.Fatalf("unexpected error %v", err)
	}
}

// NewTestKeystore creates a new KeyStore backed by a temp directory.
func NewTestKeystore() (*Keystore, string, error) {
	dir, err := ioutil.TempDir("", "keystore-test")
	if err != nil {
		return nil, "", err
	}
	localDir := filepath.Join(dir, "trustedkeys")
	userDir := filepath.Join(dir, "/home/converge/trustedkeys")
	systemDir := filepath.Join(dir, "/etc/converge/trustedkeys")
	for _, path := range []string{localDir, userDir, systemDir} {
		if err := os.MkdirAll(path, 0755); err != nil {
			return nil, "", err
		}
	}
	return New(localDir, userDir, systemDir), dir, nil
}

const fingerprint = "74fdf669f18d59f92b0aaccd720351ff475cc928"

var module = []byte("param \"message\" {\n  default = \"Hello, World!\"\n}\n\nparam \"filename\" {\n  default = \"test.txt\"\n}\n\ntask \"render\" {\n  check = \"cat {{param `filename`}} | tee /dev/stderr | grep -q '{{param `message`}}'\"\n  apply = \"echo '{{param `message`}}' > {{param `filename`}}\"\n}\n")

var signature = []byte(`
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQEcBAABAgAGBQJXp4W5AAoJEB+82b4TJ9ic970H/jMcsicc2dDoISSJ0JZ8qxVg
gvoqrmbA2Vn0nKpGIL83cf4M6n2f9DWelNNiq5N6Cq/i6+TIUeKFam/4+O7CXmmQ
BhKcxyQg6QDfLG12ivZMyFkKMFkp12NV9qojJwAaDQgeBKZRS4/cKecN/DXuV5H8
SW015eBtPboQZgktftFeq1DtDmj62nKUMMoc0Z0FkXJWGoG+BUk+JHZtFe+D8EDs
yqiIC49EGKvu9rln0gp1iEKKbFzGHwxqERc6a/IbBbRz9wcmcqio0RPcN6DCxEFX
/4zlzOg2GnpiICBPcYpUH7Q08l9OWhk/3DC7evztTnBQav0thH/pfREr590dGJU=
=K1Ms
-----END PGP SIGNATURE-----
`)

var publicKey = []byte(`
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1

mQENBFengzoBCADLJvQwXJtUfY3+vgCSj7x+X7yvgg/YT/5BfG5XhS+aR5foaTqM
DUEQb2gDY3pcWTi1TbGYJ9bedleTyn5F0Vh92nEdIIVG/y9RrV5vRsr5zRVGUj69
Gk2lOviZ5wWth9JI6rBy0aTtpeoDlQNofWWu4ml77LqxySu/uxZFC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