Skip to content

Commit

Permalink
Unify saml_test.go and saml2_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
russellhaering committed May 4, 2018
1 parent 4b1b265 commit 8f5c91f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 75 deletions.
70 changes: 0 additions & 70 deletions saml2_test.go

This file was deleted.

70 changes: 65 additions & 5 deletions saml_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,81 @@
package saml2

import (
"bytes"
"compress/flate"
"crypto"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/pem"
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"testing"

"bytes"
"compress/flate"

"github.com/beevik/etree"
"github.com/russellhaering/gosaml2/types"
"github.com/russellhaering/goxmldsig"
require "github.com/stretchr/testify/require"
dsig "github.com/russellhaering/goxmldsig"
"github.com/stretchr/testify/require"
)

var cert tls.Certificate
var pk crypto.PrivateKey

func init() {
var err error
pfx := "./testdata/test"
cert, err = tls.LoadX509KeyPair(fmt.Sprintf("%s.crt", pfx), fmt.Sprintf("%s.key", pfx))
if err != nil {
log.Fatal(err)
}
pk = cert.PrivateKey
}

func TestDecode(t *testing.T) {
f, err := ioutil.ReadFile("./testdata/saml.post")
if err != nil {
t.Fatalf("could not open test file: %v\n", err)
}
decoded := make([]byte, len(f))

base64.StdEncoding.Decode(decoded, f)
response := &types.Response{}

err = xml.Unmarshal(decoded, response)
if err != nil {
t.Fatalf("error decoding test saml: %v", err)
}

ea := response.EncryptedAssertions[0]

k, err := ea.EncryptedKey.DecryptSymmetricKey(&cert)
if err != nil {
t.Fatalf("could not get symmetric key: %v\n", err)
}

if k == nil {
t.Fatalf("no symmetric key")
}

assertion, err := ea.Decrypt(&cert)
if err != nil {
t.Fatalf("error decrypting saml data: %v\n", err)
}

f2, err := ioutil.ReadFile("./testdata/saml.xml")
if err != nil {
t.Fatalf("could not read expected output")
}

expected := &types.Assertion{}
err = xml.Unmarshal(f2, expected)

require.EqualValues(t, expected, assertion, "decrypted assertion did not match expectation")
}

func signResponse(t *testing.T, resp string, sp *SAMLServiceProvider) string {
doc := etree.NewDocument()
err := doc.ReadFromBytes([]byte(resp))
Expand Down

0 comments on commit 8f5c91f

Please sign in to comment.