package pkcs7

import (
	"encoding/base64"
	"testing"

	"github.com/stretchr/testify/require"
)

// TestSCEPMsgWithCompoundOctetString tests parsing and verifying a PKCS7 SCEP message
// where the content is a compound octet string (i.e. multiple octet strings concatenated together).
// BouncyCastle, the library that jSCEP uses, produces such messages.
func TestSCEPMsgWithCompoundOctetString(t *testing.T) {
	fullMsgB64 := "MIAGCSqGSIb3DQEHAqCAMIACAQExDTALBglghkgBZQMEAgMwgAYJKoZIhvcNAQcBoIAkgASCA+gwgAYJKoZIhvcNAQcDoIAwgAIBADGCAU8wggFLAgEAMDMwGzEZMBcGA1UEAxMQcm9vdC1leGFtcGxlLmNvbQIUC9NyFBP0p6IlKHLhVWtEkxBlPXMwDQYJKoZIhvcNAQEBBQAEggEAT15jWqZPLFMse6ECY5k0v0u2QXp9g7wIS1l62g+aEB8ZrlQW9pVkD9gGxBPvloV8kuOWuQukebwusKlA1KgIDKUQgCIOtizmdDGsi7Wyi5RapKZ30BprMcSALuvQ+zJWrQxOQDYLOlTZzAbGXLhgYNHXFn877AJZ0arvU4qzzbRYrY5Q0L8R4srRFA+itansF16yPyAZQG/MJIUZ/Ze1IfGuJ4JMtCaIWUTAKpb40/s33iQwEspE7ucbIWnvMmSfxbTWdPul1YW0YsjnrZLMWubPEbj7HDU5taix+756qEFpAv72ajnImTFn+SzbO6DJxQaHFxnHaTEkxWCnOQFThzCABgkqhkiG9w0BBwEwHQYJYIZIAWUDBAECBBD0IztfStsOHkBt54Y0h24/oIAEggMAc/3F6wOYQ7QplE7Y0a/luJJBjgJrWEpajtLSNoOP/n6hPmErlhAs/ba3mXzP1fiq6NuhuxbhLrJ2PNz6PHcrvUMgue0gi57CiTSCOcG+jSd5CgZbkxNaOUIlUeSevCYScLbXagYsHNJbuWe4cT9Y9Z9qG1wTHWJWdA0PPAGWws/jWCTkr2E1higFh7t3HaEGfirVfa5GSzuRCEL6KWstPUDLysDuer7NMFy0XLKsTcSHTuDWmi5FaqjVBAwWO9ohPfzoJY5gLJsjU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ERlcGFydG1lbnQxEDAOBgNVBAcMB1Rvcm9udG8xEDAOBgNVBAgMB09udGFyaW8xCzAJBgNVBAYTAkNBAgEBMAsGCWCGSAFlAwQCA6CCASYwEgYKYIZIAYb4RQEJAjEEEwIxOTAYBgkqhkiG9w0BCQMxCwYJKoZIhvcNAQcBMBwGCSqGSIb3DQEJBTEPFw0yNjAxMjcxNDUzMjRaMCAGCmCGSAGG+EUBCQUxEgQQT6zNQSAcU/9IWbiFb6j2ITArBgkqhkiG9w0BCTQxHjAcMAsGCWCGSAFlAwQCA6ENBgkqhkiG9w0BAQ0FADA4BgpghkgBhvhFAQkHMSoTKDU5YWRlMzExYTAxNTExYzlhYzQ5YTIwOTgyZmE3OTA1MzBkZDZkODAwTwYJKoZIhvcNAQkEMUIEQPpzRDA+KhOiD/662wtVIufmDbFNreRAMkbHMFKHK543NpphD5gV08VI0fPvsBuDx6rFbgGsG/n0l7c5LQ/vz4MwDQYJKoZIhvcNAQENBQAEggEAfQ2EqR+TFmKdwqLEEzRFONOoukq9yfO9kfx0SEepYZYjqbKesk/jv0MENJSvKZ6yVRqfDswy0kWRY2KEWDqOZGPj6/5qUtSy60eqAqPbAILHKLEeB3y361JJyqBuDJ3keidkUSqlwLYxr5BkhTPvD2totGLrjxbFvX0xC1hCgNRNJ0htgfJAB9h8gmtDjiVabT5zke8G+1Qk0Hz3KxF6ZMOpOIaNE7L3D+oNuS4nSjJEY8CDBq1+lPfIZVzC38gYUEfDxOVjl2L5ZF7W951v2AvJaBXnkAIvkKG2K9rtyV79FqCKNFdFz6CdpUqrhz5ALWtKIYwhqU6dsqoxlYxrSwAAAAAAAA=="
	fullMsg, err := base64.StdEncoding.DecodeString(fullMsgB64)
	require.NoError(t, err, "failed decoding base64 fullMsg")

	myPkcs7Msg, err := Parse(fullMsg)
	require.NoError(t, err, "failed parsing fullMsg")
	// The content is a compound octet string, so ensure we read all parts
	// if we only read the first part, the length would be 1000
	require.Equal(t, 1187, len(myPkcs7Msg.Content), "The length of the content was short read")

	err = myPkcs7Msg.Verify()
	require.NoError(t, err, "failed verifying pkcs7 message")

	_, err = Parse(myPkcs7Msg.Content)
	require.NoError(t, err, "failed parsing inner cert pkcs7")
}
