magic_test.go 2.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
package federation
//
// GangGo Diaspora Federation Library
// Copyright (C) 2017 Lukas Matt <lukas@zauberstuhl.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import (
  "testing"
  "bytes"
  "regexp"
)

var TEST_MAGIC_DATA = []byte(`<x></x>`)

Lukas Matt's avatar
Lukas Matt committed
28 29 30 31 32 33
var TEST_MAGIC_PAYLOAD = []byte(`<me:env xmlns:me="http://salmon-protocol.org/ns/magic-env">
  <me:data type="application/xml">PHg-PC94Pg==</me:data>
  <me:encoding>base64url</me:encoding>
  <me:alg>RSA-SHA256</me:alg>
  <me:sig key_id="ZGlhc3BvcmFfMm5kQGxvY2FsaG9zdDozMDAx">PIlS0XhUHGqSsoGKP2efeitDKv7uO0ekNoDQPm5lk844muzMPk7iK9t6T3ageqIsl14xmnInDGKlbrM49JiuYB4aFKEwqHAIEj2axCjdm6HRF5mv+2nhVjKISx+AcuKY1Rav9pKQoQqphRm8p9CQr632TK5mkFfBAeGpyJE8I3WNwguy9AozR+ym0b3MrbDhHxpzGxcRAvjyzbRMfvLhOlVKauaIEGDVN6nbBXVSY4hSBYu38+02PyGuyPjjlBJHNIPQXUL9dcSq/LXs/ElwQA2JBLwF6+opQvIBDbjUVkX4spKo/uRNEAlFuR4Ul+bi/Y7+ssoD3DrMHN4Fg2hx5w==</me:sig>
</me:env>`)
34 35

func TestMagicEnvelope(t *testing.T) {
Lukas Matt's avatar
Lukas Matt committed
36 37 38 39 40 41
  privKey, err := ParseRSAPrivateKey(TEST_PRIV_KEY)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }

  payload, err := MagicEnvelope(privKey, string(TEST_AUTHOR), TEST_MAGIC_DATA)
42 43 44 45 46 47 48 49 50 51 52
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }

  if bytes.Compare(payload, TEST_MAGIC_PAYLOAD) != 0 {
    t.Errorf("Expected to be %s, got %s",
      string(TEST_MAGIC_PAYLOAD), string(payload))
  }
}

func TestEncryptedMagicEnvelope(t *testing.T) {
Lukas Matt's avatar
Lukas Matt committed
53
  privKey, err := ParseRSAPrivateKey(TEST_PRIV_KEY)
54 55 56
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }
Lukas Matt's avatar
Lukas Matt committed
57
  pubKey, err := ParseRSAPublicKey(TEST_PUB_KEY)
58 59 60 61
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }

Lukas Matt's avatar
Lukas Matt committed
62 63 64 65
  payload, err := EncryptedMagicEnvelope(
    privKey, pubKey, TEST_AUTHOR, TEST_MAGIC_DATA)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
66 67
  }

Lukas Matt's avatar
Lukas Matt committed
68 69 70
  matched := regexp.MustCompile(`encrypted_magic_envelope`).Match(payload)
  if !matched {
    t.Errorf("Expected match for pattern 'encrypted_magic_envelope' got nothing")
71
  }
72
}