package tmobile

import (
	"os"
	"testing"

	"github.com/google/uuid"
)

const mockPKCS8Fail = `-----BEGIN PRIVATE KEY-----
unparseable
`

const mockPKCS8 = `-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDBQ0Wrn3iBE+3y
sTGr+vxSX+wtq4hz1W9i6LqjV3O4DeA9hZ8Lj3PbFyLuIYXvlFsb6xXydCSHFg2j
b8x1g3sUlh7+hDMh2ryVCDFJ05npCyVoxD05Ya9VUsHw1mjKWUt9+x2/sPYjDVzs
zhqEwryacrkzmJlpCCpRnfmnfzL9PBPwr1tSkovPvzlzd+MC86Zu/4t7ZNR+UvFT
I/S7SLnTIRFaHV0lUf6XYut3HNUotIVX9qFNaF+OenEMk85dgYGam/vReW0xwkx5
QDMk/kE38lw9dB4DqhZGWnPw7NciH8+COOJ+JTmDls9WHCXiP9Fh/9ToHBmdD3LX
o2KvrMMPAgMBAAECggEBAII9GIVoyWeLC66idMvmLwZAOEQqtaEB87dfCO+sroIZ
b8Vl9+FtgfDibZq2orDqdF+jFD44wKj8VqKOY+XJfjdIV4jDhEXLR4zTYYvT+oOP
DF3G6U9zIhpI1AO+Kg47EOHMSab11VmX1siKuFpBdaJLr70ymCes5f/siuKymKUI
HMgz10exE9ypa0GPUzY1gtoIGRv2xsVoEy7wn29sJkMhhx4MMtfhtnSaLXjoKByW
twOew3rNP4BajdmtGIQe2Z3qz/3dG1LG1jLe5AVoCPKZo5JlrzzjaRDbgv1ZZoze
Ddi/RVqF0We4pPCNOdHSjhoWs61xDwdpCy24kUEooHECgYEA3002/N2zNQKgerjx
3lkX8GbmHx42n1Q/2ihjiygR8RklAIgFCCfJNpAlHqKLGe4ZW7+llIWQOdA7NHOR
DWS8StVlog7FwrgBA7dNh2zcmGVmEtP0vg29mhMBMM5IQ66Nsu+vXXhU4qy0CdWY
BeadiHTX7YYA/0NsoMfcfIK0iNcCgYEA3ZACBqSiD8dnNnFVj66ut/zZRHX9bSO6
dZ07htdOp9pttZlDzlOvA0QhH+qFxe+6h4NJ07Sf+Opu2PeRh90qYvmz0i2DKQHD
CmmpaP6iITvvgOa48/sr7XG1k8stlNLa4cBRG6f35/qwr7ZIcU0N7lvnZYTSguXc
+oohOatTuIkCgYEAgvYiHcNYauqTe+Yj1CekZpWyuOVbW65plGTDnMVvYFtC3EDp
0pKi66E2Y/UoZ5jAvpJzZdu/bmi1kFmG5LgDxk/JP3YyfbS0w50plxc9eRNe/gPZ
Me2VGVu0Aw+4ShmBeUQhMUx1XEu1e18Nvcg28+SzDtbcltjQSKtuoId3ohkCgYEA
n2CjBHJTHbSb2z7lhGjsx+77v1J8zZCA5XAITPP+YaIvfw1UCEyMPXY5ucKzHfJX
pHldlwt8c8r3l91mc2w1vGLQ5qTUj5/z1D6znZJlwDBkFb5iVydbrv831au3CzIu
P2xfK9zE6Ludc5hVPiNmnQrBRnaoE38UWakZQ2mp3LkCgYAO9IZijrLWGTX77W3l
ruIj2IHKEtb+27aFdZjoMfBpU+HOoWacnmBL3vL0gq8J7KwWuJa18cDLIaiIggHc
fFjuF3lTk62dLro94yGr01rIRQnhtoBW5evutX85ukUQQo7E0ieABZP7V1Fqjvg+
2oVGWBo1Wzar6CDovkH0yapPTA==
-----END PRIVATE KEY-----`

func TestAsFileIfText(t *testing.T) {
	f, err := AsFileIfText(mockPKCS8)
	if err != nil {
		t.Errorf("failed to save into file: %v", err)

		return
	}

	if f == nil {
		t.Errorf("file was nil")

		return
	}

	defer f.Close()
	defer os.Remove(f.Name())

	f2, err := AsFileIfText("/tmp/" + "definetly_not_a_file" + uuid.New().String())
	if err == nil {
		t.Errorf("failed to get error: %v", err)

		return
	}

	if f2 != nil {
		t.Errorf("file was not nil: %v", f2)

		return
	}

	p := f.Name()

	fp, err := AsFileIfText(p)
	if err != nil {
		t.Errorf("failed to stat file that exists: %v", err)

		return
	}

	if fp != nil {
		t.Errorf("file was not nil: %v", fp)

		fp.Close()
		os.Remove(fp.Name())

		return
	}
}

func Test_pkPathVal(t *testing.T) {
	f, err := os.CreateTemp("/tmp", "pk*.pkcs8")
	if err != nil {
		t.Errorf("failed to create temp file: %v", err)
	}

	_, err = f.WriteString(mockPKCS8)
	if err != nil {
		t.Errorf("failed to write to temp file: %v", err)
	}

	p := pkPathVal(f, "/tmp/somePath")
	if p != f.Name() {
		t.Errorf("pkPathVal returned wrong value: %v", p)
		f.Close()
		os.Remove(f.Name())
	}

	p = pkPathVal(nil, "/tmp/somePath")
	if p != "/tmp/somePath" {
		t.Errorf("pkPathVal returned wrong value: %v", p)
	}
}

func Test_tempFilCloseDelete(t *testing.T) {
	f, err := os.CreateTemp("/tmp", "pk*.pkcs8")
	if err != nil {
		t.Errorf("failed to create temp file: %v", err)
	}
	tempFilCloseDelete(f)
	_, err = os.Stat(f.Name())
	if err == nil {
		t.Errorf("file was not deleted: %v", err)
	}
}
