package camel_oil_api

import (
	"testing"
)

func TestDecryptWithPrivateThenEncryptWithPublic(t *testing.T) {
	base64PublicKey := "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDpwypBPN8r3Zwv3T0XUh1Ka2m2hUe3KBgIyH4fHfN/T1jsBWnbwotKEQdZfRva7mRYiz9YrTHoH/eUAuv+WYqPMubaiqpWOu0l+BzEX1kPGA98qRC06IF2Tk4Z5xAmQ8p8u3O5jxohYFkO2XlDvPU+W9SDZgSEBTe8p80LExgo6wIDAQAB"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      // 公钥 base64 或带 PEM
	base64PrivateKey := "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIHSkp+Z0Lu+lZWr/wKcMT3EcWEIihKTg/jEOyKaczqG9hWL9UULJ1dFtIQNlpWRySsVZcJLoGTFdGam557lVzpY/tbN73KG9iVMBaKALLF52cgmyg0DRve4atc0OnkhTjv7Rf8B85UokdHCAM/5MgNcjXwqBGHohJ2LGC9yN2erAgMBAAECgYATfTeqww4daTaOkhQF4cnYonl83inQMRoSSe8wuiwLQMCHqounEk4VIW9AlcOh75FaKOuuV+kbx7K6SFskNPy7nGYfS22t2aM9E9Rt+JH+caEniYi5qAfb3gCIgsGExUNI6iuSM2p3/R542EDGc2FyfPPqyht+jR4CjLOLoXHfoQJBALwvF6uIOSW0Lxh7Lo/JsKpWJ1qffDvXWYag605L9JAyP0yO64woF60Tn+mGRzcaEhNDSEjinKQqPEJnxDUGYaECQQCwm1mQKD95MaeKWBiOVJZsdzL5aJsW42xyiu0ZwA7bZUgJyUskzXG0ubeIHK/czlJbev9ODubbMNJFcngX4N3LAkAJaxH0M80oZew1fXTHHYEKBWXS00iUdiK06jjcolCLJvikDEMdsKP+tYy7U00dJODitetYOn88eCCr8iWPwdIBAkBtUjzGt6NS6iHDyXSp5kKXMdIkAVS/flgLL2RFpFWOCcvmAuy5A1N3g97QKrHSBQWGC0UulJri4/3Fb25XmaKxAkBifs9dbUifeqZRNVh2Omck4xedb1FyQPLDicUycjYug3Vca0T/LRr80aX/NhbhtpSdwzF1ukiZ6W46O9DmGuNy" // 私钥 base64 或带 PEM
	token := "M20UkqDpKp7u3wO1wxSGlBvFWS4AAVjcglRw5Feh6ezvxW4NtoX0V8dGajhCK1+9Gf+Yqb+dzrQ0evYKe7kpqyeXtqg5xobQ6Sx1MQsf6eV9kUBU1u/qS71wlIgDYJhtMk81qfF+Ojx1FCFzV+89jKLr3jeXf2QeRv/ZqMXIqb0="                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            // 待私钥解密的密文 base64
	result, _ := DecryptWithPrivateThenEncryptWithPublic(base64PrivateKey, base64PublicKey, token)
	t.Log(result)
}

func TestEnc(t *testing.T) {
	pubkey := "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCkc6Xr/JhWEx/WPxG2q3VHLQ+FYk/oCmQ1y14B5j4xOJY+mAWoDOOti3sAXg0Kk662gWjWET1nLI6YED4wb9HWon1NAZn47lgc5ohIpEdU91Jao85X/kgkD3NvTTvhFicttepUOsrYUZN8rAQCE7AhzwGgKnCiIRY/kE8jOCCeZQIDAQAB"
	jsonString := "{\"openId\":\"app2511152349246268055\",\"phone\":\"13349946900\",\"goodId\":\"202511051701\",\"goodNum\":1,\"bindPhone\":\"13349946900\",\"payType\":\"appAli\",\"paramY\":26.996671,\"paramX\":77.450347,\"yanqian\":true,\"mobileOperatingPlatform\":\"iOS\",\"sysVersion\":\"iOS 15.7\",\"platformType\":\"iPhone X\",\"netWork\":\"wifi\",\"platform\":\"iOS\",\"brand\":\"Apple\",\"deviceId\":\"A6F2C1C0-8E65-4E4A-9A1B-3D8C9879A41E\"}"
	result, _ := EncryptWithPublicKey(pubkey, jsonString)
	t.Log(result)
}

func TestSign(t *testing.T) {
	// 测试 Sign 函数
	openId := "app2511181557205741495"
	result := Sign(openId)
	t.Logf("Sign result: %s", result)

	// Java 中的签名结果（根据 demo.java）
	// 不需要匹配具体值，简单验证应输出 MD5 值
	if result == "" {
		t.Fatalf("Sign result is empty")
	}
	t.Logf("Test passed! OpenId: %s, Signature: %s", openId, result)
}

func TestDec(t *testing.T) {
	// 使用 Java demo.java 中的源数据进行测试
	// 这是用来加密的公钥
	prikey := "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKRzpev8mFYTH9Y/EbardUctD4ViT+gKZDXLXgHmPjE4lj6YBagM462LewBeDQqTrraBaNYRPWcsjpgQPjBv0daifU0BmfjuWBzmiEikR1T3Ulqjzlf+SCQPc29NO+EWJy216lQ6ythRk3ysBAITsCHPAaAqcKIhFj+QTyM4IJ5lAgMBAAECgYEAg/vlMI7b3EkhBhw8JTVavLMnf8+1fe/JGXuMiU22oF5gBwCPmZ4upLwLDfJt2Q1J7WPTNetEMqgKEXUH1GwKJkFGm2tSEMHSHdTmUTQ3w6bS1C0peZghyhmlWRXUlpKk5tDOQ24sWO268YrwZyueXnVGKJ4s0hY0KOiZIU2trUECQQDxA+lzq/t/L09M/bUybjsiP6eb/HBeZeu+2+JnHekb8z9BMXTOKTHqAI0Cs9UvE6BDT3aU9IJbWHbRogIMypT9AkEArq0fccphwWtAIyS0+fns4Hqs4On7yTfXSXWiAbSVif1LxP60b5n5Xm8lo12oHkdwOvKaesvgDpnIGUM9xjFfiQJASTZrABxKNYRljnmzRTJ+/BRiEdxJNiO3zS52Q+SuHzNxD5i6ZrXU18R7EUsXg0lu8YN9/hmYT687yMpx3Pjc8QJAZBs1lSouQgIsPLfRvB1+otvLbg7KzPPivufak+2hcfanUNvEHt14a6V5RZnsOoYojK/y1oM3AkchxVCi+43aOQJAC0gI6qsZ3VaPu9QDddrHPJ1dCHTXyfcNJ0op3srCVF92HoBWX54pzeagj+9g/Z4oUT9IhaO0Q3YE07N03HuVrQ=="
	encryptedBody := "k5X+iTJUwgXDdt8fFg2RsD0KRGTkfI4RPJX9sCL2VAgQWHb9A3gMEksER5JXez0zXS3qACOILhw4UfavwMfsld9P9OuqMNlkJBxX5+vlMPL6St4DZdY194vKl+47UJ2nu18olgu0GXdnR3qFGTBLyAaz+ECVbCo7YhvucfKfSvpDeUTPsPFr41TeMHWaO8toAIvMyW3sjlfnrEhhuau38KiXgr8++GkLl8xJC8pBO4yjpq53oPe9II1vc6C8ScDX0Afg+O9XKn/RBoKJTK+BDro4657MlEJWVcKmZ3QlI70BuCGOLjMGIf6YvQHyYPCSNtdIsMoNGoSFoRJ0zRb3rp+hrefpHwCdfxyP5dcaLuoWc13tHEE2FHoM0c2wZqGRYPrPA7LnI7sSf1Zrt85oj3cClyRFiIfp0DzegpO59UbUus7cGIF18Q81oaXjK4tbbrBF3IPX59gsElXqriLHzBnVrydkb8m0dUzswBqsnPaNdxk6igIweecqBFPHZPlMDtouh9NRDD2pOZSEqSL/dJ8yPVTs+w/SGkELX29hvXgY6R8OAQEZUvFbmEzTe7QDqOuZ+jes6ployEAUeA8xSzcSdsFrKSn8ZAb7IU0hEna+w0WzJkavoDRinZoPfcojb9a1vkfGnfowx2zSkbND8EvjuI25FFC8fQXEofPTeNU="
	decrypted, err := DecryptWithPrivateKey(prikey, encryptedBody)
	if err != nil {
		t.Fatalf("Dec failed: %v", err)
	}
	t.Logf("Decrypted: %s", decrypted)
}
