Add sender_test.
This commit is contained in:
parent
74c7587870
commit
5269681c26
58
deliver/sender_test.go
Normal file
58
deliver/sender_test.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package deliver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/sha256"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/pem"
|
||||||
|
"github.com/Songmu/go-httpdate"
|
||||||
|
"github.com/go-fed/httpsig"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func generatePublicKeyPEMString(publicKey *rsa.PublicKey) string {
|
||||||
|
publicKeyByte := x509.MarshalPKCS1PublicKey(publicKey)
|
||||||
|
publicKeyPem := pem.EncodeToMemory(
|
||||||
|
&pem.Block{
|
||||||
|
Type: "RSA PUBLIC KEY",
|
||||||
|
Bytes: publicKeyByte,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return string(publicKeyPem)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAppendSignature(t *testing.T) {
|
||||||
|
file, _ := os.Open("../misc/test/create.json")
|
||||||
|
body, _ := ioutil.ReadAll(file)
|
||||||
|
req, _ := http.NewRequest("POST", "https://localhost", bytes.NewBuffer(body))
|
||||||
|
req.Header.Set("Content-Type", "application/activity+json")
|
||||||
|
req.Header.Set("Date", httpdate.Time2Str(time.Now()))
|
||||||
|
appendSignature(req, &body, "https://innocent.yukimochi.io/users/YUKIMOCHI#main-key", globalConfig.ActorKey())
|
||||||
|
|
||||||
|
// Verify HTTPSignature
|
||||||
|
verifier, err := httpsig.NewVerifier(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed - " + err.Error())
|
||||||
|
}
|
||||||
|
err = verifier.Verify(globalConfig.ActorKey().Public(), httpsig.RSA_SHA256)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed - " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify Digest
|
||||||
|
givenDigest := req.Header.Get("Digest")
|
||||||
|
hash := sha256.New()
|
||||||
|
hash.Write(body)
|
||||||
|
b := hash.Sum(nil)
|
||||||
|
calculatedDigest := "SHA-256=" + base64.StdEncoding.EncodeToString(b)
|
||||||
|
|
||||||
|
if givenDigest != calculatedDigest {
|
||||||
|
t.Fatalf("Failed - " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user