salmon_test.go 7.1 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
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"

func TestParseDecryptedRequest(t *testing.T) {
  var xml = []byte(`<?xml version="1.0" encoding="UTF-8"?><me:env xmlns:me="http://salmon-protocol.org/ns/magic-env"><me:data type="application/xml">PHN0YXR1c19tZXNzYWdlPgogIDxhdXRob3I-ZGlhc3BvcmFfMm5kQGxvY2FsaG9zdDozMDAxPC9hdXRob3I-CiAgPGd1aWQ-ZmUyZDJhODA1MzQ4MDEzNWQwOGY1Mjk2ZjJlNzQ0N2I8L2d1aWQ-CiAgPGNyZWF0ZWRfYXQ-MjAxNy0wNy0yNVQwOToyNDozM1o8L2NyZWF0ZWRfYXQ-CiAgPHByb3ZpZGVyX2Rpc3BsYXlfbmFtZS8-CiAgPHRleHQ-cGluZzwvdGV4dD4KICA8cHVibGljPmZhbHNlPC9wdWJsaWM-Cjwvc3RhdHVzX21lc3NhZ2U-</me:data><me:encoding>base64url</me:encoding><me:alg>RSA-SHA256</me:alg><me:sig key_id="ZGlhc3BvcmFfMm5kQGxvY2FsaG9zdDozMDAx">NbuD4kERZzXPFRORH4NOcr7EAij-dWKTCG0eBBGZObN3Aic0lMAZ_rLU7o6PLOH9Q6p6dyneYjUjSu07vtI5Jy_N2XQpKUni3fUWxfDNgfMo26XKmxdJ5S2Gp1ux1ToO3FY9RByTZw5HZRpOBAfRSgttTgiY5_Yu5D-BLcEm_94R6FMWRniQXrMAt8hU9qCNSuVQlUKtuuy8qJXu6Z21VhI9lAT7wIALlR9UwIgz0e6UG9S9sU95f_38co0ibD1KbQpBd8c_lu5vCVIqlEe_Fa_xYZupMLaU8De-wzoBpBgqR65mRtUQTu2jP-Qxa3aXrANHxweIbnYfpZ5QcNA50hfyVJJSolczDSlDljTunEmHmWNaS3J7waEQsIDFATPFy6H5leRPpSzebXYca4T-EiapPP-mn41Vs3VKIdUXOHus_HcTPWRVT-Vr-yt7byFYEanb5b5lQ_IHcI0oyqX7RrVJid6UsBtwxwkX0FSc1cZgLhBQUgxBsUh5MNte-WZJv_6c9rHyNsH3rn9YEZp431P9GCe8gNdLY9bFQ1pYS9BxOAS2enu3yVpWpWRechiR7D__HC4-Hw2MHfSSmBQTxq5oO01_efEHB8XxWF85XYNT6_icXf3ZsTxkURT9HlHapkFwL7TlO5gPUZZVJt9f6kn9HoGQ56MX2n46KdKKid8=</me:sig></me:env>`)

Lukas Matt's avatar
Lukas Matt committed
25 26 27 28 29 30 31 32 33 34 35
  message, err := ParseDecryptedRequest([]byte(""))
  if err == nil {
    t.Errorf("Expected to be an error, got nil")
  }

  message, err = ParseDecryptedRequest([]byte("<broken></broken"))
  if err == nil {
    t.Errorf("Expected to be an error, got nil")
  }

  message, err = ParseDecryptedRequest(xml)
36 37 38 39 40 41 42 43 44 45 46 47 48
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }

  parseRequest(t, message)
}

func TestParseEncryptedRequest(t *testing.T) {
  var wrapper = AesWrapper{
    AesKey: `SOcOINIGdXX5QslLmPKXRllOXgBb3HhcUS78BNsCn0hGff0WdobvmgITvd6v+FiQOqIx5RUU2EH7Woh1KEc1yOn0AamyaJrbgDt9wl4az3hxacJsBd+xpcUca8niIGzCZuoJdtnENlAAUU1mKpT7R0Pikdd0/3bjoS6FN2dX1frBx2hlMvHsnADazgfckmK+53ftWWBJ7cAWNJtYOj3MphnipInuOZ3JH43rjpc6QLYRx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8otzDpG6fCIfpg3BJK/5MObAR6rLwT9EvvfWTHNKZR3SNKyIxYjNJr1dwhgB9jGWFRHA+bgDnV2yQPB+Vp+YE2E60jJlnTeSwEfTxPhz5ueQ/rsbPoI2BuqgwZKYkOQ6vjuLfo81EIhQeUfrid5oCCAGDOFjerRaaLVM6iqKeWaAyVujTzGdYOB2tCkFhB9rju6hAHP5ycHp/utOQssDO0LDThosH98fVyVlmC2L+ZIHZ2B+n3OiFz/E1hJ7EOJI2P+jQhwb2uwKVkHgywGIdRTGdzZ1DzXLDcwF5+lGS1wEikmnh1nMEovNNATCFp7qMLK01EywgrLFuF75T00jHld2eU/K/6KhUYi0SJSGDjCx5DxR48xVBRKrn8dCMBC+kbyQ/1pMtM5vg05uPZ963gzWw4uMr/StowT347H/WuuyKIeOSM4RPi+vB1QN2oINVecq3ZKU8U1xKWvuV2M1j/V2OYVcz5NP9Z9nf0fkNqpmek0D4epU4/6bwAuw1YAa6eEvIt1yGrrmh81wumBOHkk614bcyljVut1JAdreJsAj9n7FBIO4UvFO9zmPF2PIRZ+dxt6uNvIItlR659PpIBtoQiLW803SsLcwMqQ+Opg+eFgAB+qvUjZ4F9ZhFHHMRbMJlfu/ezatg7mL9VidKwLavHbCvgmz6ckRdU6m6aQsnRIdWKHU43sjCoPRnSwgMz/D4vNh5F8A43o4RICOUUUT2jADphblbpos61tZyuhR+1uTLEmVrpGfSigj1A11ByHIPtlFxpN0/D2iJkO3OuQ==`,
  }

Lukas Matt's avatar
Lukas Matt committed
49 50 51 52 53 54
  message, err := ParseEncryptedRequest(wrapper, []byte(""))
  if err == nil {
    t.Errorf("Expected to be an error, got nil")
  }

  message, err = ParseEncryptedRequest(wrapper, TEST_PRIV_KEY)
55 56 57 58 59
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }

  parseRequest(t, message)
Lukas Matt's avatar
Lukas Matt committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73

  w := wrapper
  w.AesKey = ""
  message, err = ParseEncryptedRequest(w, TEST_PRIV_KEY)
  if err == nil {
    t.Errorf("Expected to be an error, got nil")
  }

  w = wrapper
  w.MagicEnvelope = ""
  message, err = ParseEncryptedRequest(w, TEST_PRIV_KEY)
  if err == nil {
    t.Errorf("Expected to be an error, got nil")
  }
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
}

func parseRequest(t *testing.T, message Message) {
  var data = `PHN0YXR1c19tZXNzYWdlPgogIDxhdXRob3I-ZGlhc3BvcmFfMm5kQGxvY2FsaG9zdDozMDAxPC9hdXRob3I-CiAgPGd1aWQ-ZmUyZDJhODA1MzQ4MDEzNWQwOGY1Mjk2ZjJlNzQ0N2I8L2d1aWQ-CiAgPGNyZWF0ZWRfYXQ-MjAxNy0wNy0yNVQwOToyNDozM1o8L2NyZWF0ZWRfYXQ-CiAgPHByb3ZpZGVyX2Rpc3BsYXlfbmFtZS8-CiAgPHRleHQ-cGluZzwvdGV4dD4KICA8cHVibGljPmZhbHNlPC9wdWJsaWM-Cjwvc3RhdHVzX21lc3NhZ2U-`

  var sig = `NbuD4kERZzXPFRORH4NOcr7EAij-dWKTCG0eBBGZObN3Aic0lMAZ_rLU7o6PLOH9Q6p6dyneYjUjSu07vtI5Jy_N2XQpKUni3fUWxfDNgfMo26XKmxdJ5S2Gp1ux1ToO3FY9RByTZw5HZRpOBAfRSgttTgiY5_Yu5D-BLcEm_94R6FMWRniQXrMAt8hU9qCNSuVQlUKtuuy8qJXu6Z21VhI9lAT7wIALlR9UwIgz0e6UG9S9sU95f_38co0ibD1KbQpBd8c_lu5vCVIqlEe_Fa_xYZupMLaU8De-wzoBpBgqR65mRtUQTu2jP-Qxa3aXrANHxweIbnYfpZ5QcNA50hfyVJJSolczDSlDljTunEmHmWNaS3J7waEQsIDFATPFy6H5leRPpSzebXYca4T-EiapPP-mn41Vs3VKIdUXOHus_HcTPWRVT-Vr-yt7byFYEanb5b5lQ_IHcI0oyqX7RrVJid6UsBtwxwkX0FSc1cZgLhBQUgxBsUh5MNte-WZJv_6c9rHyNsH3rn9YEZp431P9GCe8gNdLY9bFQ1pYS9BxOAS2enu3yVpWpWRechiR7D__HC4-Hw2MHfSSmBQTxq5oO01_efEHB8XxWF85XYNT6_icXf3ZsTxkURT9HlHapkFwL7TlO5gPUZZVJt9f6kn9HoGQ56MX2n46KdKKid8=`

  if message.Me != XMLNS_ME {
    t.Errorf("Expected to be %s, got %s", XMLNS_ME, message.Me)
  }

  if message.Encoding != BASE64_URL {
    t.Errorf("Expected to be %s, got %s", BASE64_URL, message.Encoding)
  }

  if message.Alg != RSA_SHA256 {
    t.Errorf("Expected to be %s, got %s", RSA_SHA256, message.Alg)
  }

  if message.Data.Type != APPLICATION_XML {
    t.Errorf("Expected to be %s, got %s", APPLICATION_XML, message.Data.Type)
  }

  if message.Data.Data != data {
    t.Errorf("Expected to be %s, got %s", data, message.Data.Data)
  }

  if message.Sig.Sig != sig {
    t.Errorf("Expected to be %s, got %s", sig, message.Sig.Sig)
  }

  if message.Sig.KeyId != TEST_AUTHOR {
    t.Errorf("Expected to be %s, got %s", TEST_AUTHOR, message.Sig.KeyId)
  }
}