magic_test.go 2.78 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 28 29 30 31 32 33 34 35 36 37 38 39 40
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>`)

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>`)

func TestMagicEnvelope(t *testing.T) {
  payload, err := MagicEnvelope(
    string(TEST_PRIV_KEY), string(TEST_AUTHOR), TEST_MAGIC_DATA)
  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))
  }
41 42 43 44 45

  _, err = MagicEnvelope("", string(TEST_AUTHOR), TEST_MAGIC_DATA)
  if err == nil {
    t.Errorf("Expected to be an error, got nil")
  }
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
}

func TestEncryptedMagicEnvelope(t *testing.T) {
  payload, err := EncryptedMagicEnvelope(
    string(TEST_PRIV_KEY), string(TEST_PUB_KEY),
    TEST_AUTHOR, TEST_MAGIC_DATA)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }

  pattern := `aes_key.+?encrypted_magic_envelope`
  matched, err := regexp.Match(pattern, payload)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }

  if !matched {
    t.Errorf("Expected match for pattern '%s', got nothing", pattern)
  }
65 66 67 68 69 70 71 72 73 74 75 76 77 78

  _, err = EncryptedMagicEnvelope(
    "", string(TEST_PUB_KEY),
    TEST_AUTHOR, TEST_MAGIC_DATA)
  if err == nil {
    t.Errorf("Expected to be an error, got nil")
  }

  _, err = EncryptedMagicEnvelope(
    string(TEST_PRIV_KEY), "",
    TEST_AUTHOR, TEST_MAGIC_DATA)
  if err == nil {
    t.Errorf("Expected to be an error, got nil")
  }
79
}