helper_test.go 5.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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 (
Lukas Matt's avatar
Lukas Matt committed
21
  "crypto/rsa"
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  "testing"
)

var TEST_AUTHOR = `diaspora_2nd@localhost:3001`

var TEST_HCARD_LINK = "https://joindiaspora.com/hcard/users/db3d5a70c1ee01334cdd5215e5076e52"

var TEST_PRIV_KEY = []byte(`-----BEGIN PRIVATE KEY-----
MIIEpAIBAAKCAQEA5qryP/x8Wtht+VLH/TvLNofTF9B4aj78sYlctQxIuzEmuLc3
nU+EhpeExe9DpJEt6/l5V3NIHymkkNMA7Mu4fWmxdyVMEweY7utTl+wkwdJFFlBV
TI1H9dF91sUB2p5o7irZUdgW8zyZv2d2aHW5cOwR1mysh16IwDSNSRd8quqIkqPC
xYKcVc2cga0jYb5RIpv4mq1MlKrmFwxXws3OK+/5ZtErOJZCyVkz2lf0gqzMIyBD
JT9iU0x0HEq4A9LcFMCtjv/AuQj5y7AV2ehY/J7kxmw/m7sLr3R7YZ/H+pwdiW7n
LyQilUNXRuwLJQY2HmQ1rZaVivwviBqaqeFkpwIDAQABAoIBAHAkr/33zKWGD4Fl
g6FUDqoGQtSTH9fXo5bUx2OmAz4u2Tp4qOssG6wrwft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IVKPsH0Ct7G9TqEgkG7UaHOw4HBtybSUf+gDRw98tb0mk9gGK6m2A1vz
sK4ExAtclJCC8pc2MldVoeGbePUIYIsMMTGMZQ2O6jlINbYaOkFM/w==
-----END PRIVATE KEY-----`)

var TEST_PUB_KEY = []byte(`-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAxREQ/8yKpfxNTET2MIIY
1sIOktYl+6u1O3Ad9GJctzrRxWa/PbxWKX8QmGoWNb9S9tt1oLeRZ4ZGTV51t/ln
4ZOUUhR5Xpx5+jxP6DM5Jf2wqGjzXmQaSgKWsrJ2R0EyP9Ga+j5zk+uMpRXBBt0C
bh2kcPAF7mRZ7EBK+p1a+YKaKhebTJNF3YXSjxNlN6LXvTq+JUg6zTzUWg/6w1K0
jXGpXMVMOSjmRsdfbMjIYg8AXSH4xK2AvqUjWvIrQRPiXa6EjB9468sT0IABzcaY
4iNs3kzMB2aRCcdzMdZplAUH9kA+BOC3sU6VmOCfKCsQ3RyudeWqkbKxJzk/duoG
HaO+3nFgxhubRhO1VNj0EhpGPkVuQH6hhDpNh6sYH0Tq4Fs6gTxQynlwiERqoHW/
bp4TZc8mmSgrnDNuWklM6IeyoSse1lO4ivaSLDuvm8UbZTT1P09QaaCPLI5iLHVT
dP7gLEAYalirOXeZSyzwMAWzWF7NXkVbHjFn4JvJFYKyN+xoBpAwlIXdI1DMOK1H
kJaT7PEyjvar0M9oIqVqEV5hdGqFrlDnW4MvP+wQkmuK+9CygggE/0oefhKYIYc3
zQKne2ejiu9e5cDD5WyVusjeRstj/+9bDlOrQ8X4eh6vmjvd+98B/ZWFCCEkTH5m
DX3H15lu+GelrDpYLThXjnkCAwEAAQ==
-----END PUBLIC KEY-----`)

Lukas Matt's avatar
Lukas Matt committed
72 73 74 75 76
func TestParseRSAPublicKey(t *testing.T) {
  var (
    err error
    pub interface{}
  )
77

Lukas Matt's avatar
Lukas Matt committed
78 79 80 81 82 83 84 85 86 87 88 89
  pub, err = ParseRSAPublicKey(TEST_PUB_KEY)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }

  if data, ok := pub.(*rsa.PublicKey); !ok {
    t.Errorf("Expected to be '*rsa.PublicKey', got %v", data)
  }

  _, err = ParseRSAPublicKey([]byte(""))
  if err == nil {
    t.Errorf("Expected an error, got nil")
90 91
  }

Lukas Matt's avatar
Lukas Matt committed
92 93 94
  _, err = ParseRSAPublicKey(TEST_PRIV_KEY)
  if err == nil {
    t.Errorf("Expected an error, got nil")
95 96 97
  }
}

Lukas Matt's avatar
Lukas Matt committed
98 99 100 101 102
func TestParseRSAPrivateKey(t *testing.T) {
  var (
    err error
    priv interface{}
  )
103

Lukas Matt's avatar
Lukas Matt committed
104 105 106 107
  priv, err = ParseRSAPrivateKey(TEST_PRIV_KEY)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }
108

Lukas Matt's avatar
Lukas Matt committed
109 110
  if data, ok := priv.(*rsa.PrivateKey); !ok {
    t.Errorf("Expected to be '*rsa.PrivateKey', got %v", data)
111 112
  }

Lukas Matt's avatar
Lukas Matt committed
113 114 115 116 117 118 119 120
  _, err = ParseRSAPrivateKey([]byte(""))
  if err == nil {
    t.Errorf("Expected an error, got nil")
  }

  _, err = ParseRSAPrivateKey(TEST_PUB_KEY)
  if err == nil {
    t.Errorf("Expected an error, got nil")
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  }
}

func TestParseWebfingerHandle(t *testing.T) {
  expectedHandle := "diaspora_2nd"
  handle, err := ParseWebfingerHandle("acct:" + TEST_AUTHOR)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }

  if handle != expectedHandle {
    t.Errorf("Expected to be %s, got %s", expectedHandle, handle)
  }
}

func TestParseStringHelper(t *testing.T) {
  parts, err := parseStringHelper("abc", `^(\w{2})`, 1)
  if err != nil {
    t.Errorf("Some error occured while parsing: %v", err)
  }

  expected := "ab"
  if parts[1] != expected {
    t.Errorf("Expected to be %s, got %s", expected, parts[1])
  }

  parts, err = parseStringHelper("abc", `^\w{2}`, 1)
  if err == nil {
    t.Errorf("Expected an error, got nil")
  }
}