Commit 1943f266 authored by zauberstuhl's avatar zauberstuhl Committed by GitHub

Merge pull request #1 from ganggo/implement_tests_and_coverage

Implement tests and coverage
parents d159c2fa d66a5a75
language: go
sudo: false
go:
- 1.8.x
before_install:
- go get -t -v ./...
script: |
go test -v -race -coverprofile=p.out -covermode=atomic
[ -f p.out ] && cat p.out >> coverage.txt || echo "" > coverage.txt
after_success:
- bash <(curl -s https://codecov.io/bash)
......@@ -104,18 +104,20 @@ func (a Aes) Decrypt() (ciphertext []byte, err error) {
return ciphertext, err
}
headerText, fail := base64.URLEncoding.DecodeString(a.Data)
if fail == nil {
info("header aes decryption detected")
a.Data = string(headerText)
ciphertext, err = base64.StdEncoding.DecodeString(a.Data)
if err != nil {
return ciphertext, err
}
ciphertext, err = base64.StdEncoding.DecodeString(a.Data)
block, err := aes.NewCipher(key)
if err != nil {
return ciphertext, err
}
return decryptAES(key, iv, ciphertext)
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(ciphertext, ciphertext)
return ciphertext, nil
}
func (w AesWrapper) Decrypt(serializedKey []byte) (entityXML []byte, err error) {
......@@ -147,15 +149,3 @@ func (w AesWrapper) Decrypt(serializedKey []byte) (entityXML []byte, err error)
}
return
}
func decryptAES(key, iv, ciphertext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return ciphertext, err
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(ciphertext, ciphertext)
return ciphertext, nil
}
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"
"encoding/base64"
"strings"
)
func TestAes(t *testing.T) {
var (
aes Aes
expected = "Hello World"
)
err := aes.Generate()
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
key := aes.Key
iv := aes.Iv
aes.Key = ""
err = aes.Encrypt([]byte(expected))
if err == nil {
t.Errorf("Expected to be an error, got nil")
}
aes.Key = base64.StdEncoding.EncodeToString([]byte("wrongkey"))
err = aes.Encrypt([]byte(expected))
if err == nil {
t.Errorf("Expected to be an error, got nil")
}
aes.Key = key
// XXX will panic and break the test
//aes.Iv = ""
//err = aes.Encrypt([]byte(expected))
//if err == nil {
// t.Errorf("Expected to be an error, got nil")
//}
aes.Iv = iv
err = aes.Encrypt([]byte(expected))
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
aes.Key = ""
_, err = aes.Decrypt()
if err == nil {
t.Errorf("Expected to be an error, got nil")
}
aes.Key = base64.StdEncoding.EncodeToString([]byte("wrongkey"))
_, err = aes.Decrypt()
if err == nil {
t.Errorf("Expected to be an error, got nil")
}
// XXX will panic and break the test
aes.Key = key
//aes.Iv = ""
//_, err = aes.Decrypt()
//if err == nil {
// t.Errorf("Expected to be an error, got nil")
//}
aes.Iv = iv
decrypted, err := aes.Decrypt()
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
if strings.Compare(expected, string(decrypted)) == 0 {
t.Errorf("Expected to be '%s', got '%s'", expected, string(decrypted))
}
}
......@@ -62,7 +62,7 @@ func ParseRSAPrivKey(decodedKey []byte) (privkey *rsa.PrivateKey, err error) {
return
}
func AuthorSignature(data interface{}, order, privKey string) (string, error) {
func AuthorSignature(data interface{}, order string, privKey []byte) (string, error) {
var text string
var r = reflect.TypeOf(data)
var v = reflect.ValueOf(data)
......@@ -115,13 +115,13 @@ func (envelope *MagicEnvelopeMarshal) Sign(privKey string) (err error) {
text := envelope.Data.Data + "." + type64 +
"." + encoding64 + "." + alg64
(*envelope).Sig.Sig, err = Sign(text, privKey)
(*envelope).Sig.Sig, err = Sign(text, []byte(privKey))
return
}
func Sign(text, privKey string) (sig string, err error) {
privkey, err := ParseRSAPrivKey([]byte(privKey))
func Sign(text string, privKey []byte) (sig string, err error) {
privkey, err := ParseRSAPrivKey(privKey)
if err != nil {
return "", err
}
......
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 (
"crypto/rsa"
"testing"
)
func TestParseRSAPubKey(t *testing.T) {
var (
err error
pub interface{}
)
pub, err = ParseRSAPubKey(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)
}
pub, err = ParseRSAPubKey([]byte("INVALID"))
if err == nil {
t.Errorf("Expected an error, got nil")
}
}
func TestParseRSAPrivKey(t *testing.T) {
var (
err error
priv interface{}
)
priv, err = ParseRSAPrivKey(TEST_PRIV_KEY)
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
if data, ok := priv.(*rsa.PrivateKey); !ok {
t.Errorf("Expected to be '*rsa.PrivateKey', got %v", data)
}
priv, err = ParseRSAPrivKey([]byte("INVALID"))
if err == nil {
t.Errorf("Expected an error, got nil")
}
}
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"
"encoding/xml"
)
func TestEntitiesUnmarshalXML(t *testing.T) {
var entity Entity
var retractionRaw = []byte(`<retraction></retraction>`)
var profileRaw = []byte(`<profile></profile>`)
var statusMessageRaw = []byte(`<status_message></status_message>`)
//var reshareRaw = []byte(`<reshare></reshare>`)
var commentRaw = []byte(`<comment></comment>`)
var likeRaw = []byte(`<like></like>`)
var contactRaw = []byte(`<contact></contact>`)
var notSupportedRaw = []byte(`<notsupported></notsupported>`)
err := xml.Unmarshal(retractionRaw, &entity)
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
if data, ok := entity.Data.(EntityRetraction); !ok {
t.Errorf("Expected to be 'like', got %v", data)
}
err = xml.Unmarshal(profileRaw, &entity)
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
if data, ok := entity.Data.(EntityProfile); !ok {
t.Errorf("Expected to be 'profile', got %v", data)
}
err = xml.Unmarshal(statusMessageRaw, &entity)
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
if data, ok := entity.Data.(EntityStatusMessage); !ok {
t.Errorf("Expected to be 'status_message', got %v", data)
}
//err = xml.Unmarshal(reshareRaw, &entity)
//if err != nil {
// t.Errorf("Some error occured while parsing: %v", err)
//}
//if data, ok := entity.Data.(EntityStatusMessage); !ok {
// t.Errorf("Expected to be 'reshare', got %v", data)
//}
err = xml.Unmarshal(commentRaw, &entity)
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
if data, ok := entity.Data.(EntityComment); !ok {
t.Errorf("Expected to be 'comment', got %v", data)
}
err = xml.Unmarshal(likeRaw, &entity)
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
if data, ok := entity.Data.(EntityLike); !ok {
t.Errorf("Expected to be 'like', got %v", data)
}
err = xml.Unmarshal(contactRaw, &entity)
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
if data, ok := entity.Data.(EntityContact); !ok {
t.Errorf("Expected to be 'contact', got %v", data)
}
err = xml.Unmarshal(notSupportedRaw, &entity)
if err == nil {
t.Errorf("Expected an error, got nil")
}
}
......@@ -37,7 +37,7 @@ func (e *EntityComment) SignatureOrder() string {
return "author created_at guid parent_guid text"
}
func (e *EntityComment) AppendSignature(privKey, order string, typ int) error {
func (e *EntityComment) AppendSignature(privKey []byte, order string, typ int) error {
signature, err := AuthorSignature(*e, order, privKey)
if err != nil {
return err
......
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 TestCommentSignatureOrder(t *testing.T) {
var comment EntityComment
expected := "author created_at guid parent_guid text"
if expected != comment.SignatureOrder() {
t.Errorf("Expected to be %s, got %s", expected, comment.SignatureOrder())
}
}
func TestCommentAppendSignature(t *testing.T) {
comment := EntityComment{
Author: "author@localhost",
CreatedAt: "01.01.1970",
Guid: "1234",
ParentGuid: "4321",
Text: "hello world",
}
if comment.AuthorSignature != "" {
t.Errorf("Expected to be empty, got %s", comment.AuthorSignature)
}
if comment.ParentAuthorSignature != "" {
t.Errorf("Expected to be empty, got %s", comment.AuthorSignature)
}
err := comment.AppendSignature(TEST_PRIV_KEY,
comment.SignatureOrder(), AuthorSignatureType)
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
if comment.AuthorSignature == "" {
t.Errorf("Expected signature, was empty")
}
err = comment.AppendSignature(TEST_PRIV_KEY,
comment.SignatureOrder(), ParentAuthorSignatureType)
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
if comment.ParentAuthorSignature == "" {
t.Errorf("Expected signature, was empty")
}
}
......@@ -37,8 +37,8 @@ func (e *EntityLike) SignatureOrder() string {
return "positive guid parent_guid target_type author"
}
func (e *EntityLike) AppendSignature(privKey, order string, typ int) error {
signature, err := AuthorSignature(*e, privKey, order)
func (e *EntityLike) AppendSignature(privKey []byte, order string, typ int) error {
signature, err := AuthorSignature(*e, order, privKey)
if err != nil {
return err
}
......
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 TestLikeSignatureOrder(t *testing.T) {
var like EntityLike
expected := "positive guid parent_guid target_type author"
if expected != like.SignatureOrder() {
t.Errorf("Expected to be %s, got %s", expected, like.SignatureOrder())
}
}
func TestLikeAppendSignature(t *testing.T) {
like := EntityLike{
Positive: true,
Guid: "1234",
ParentGuid: "4321",
TargetType: "Post",
Author: "author@localhost",
}
if like.AuthorSignature != "" {
t.Errorf("Expected to be empty, got %s", like.AuthorSignature)
}
if like.ParentAuthorSignature != "" {
t.Errorf("Expected to be empty, got %s", like.AuthorSignature)
}
err := like.AppendSignature(TEST_PRIV_KEY,
like.SignatureOrder(), AuthorSignatureType)
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
if like.AuthorSignature == "" {
t.Errorf("Expected signature, was empty")
}
err = like.AppendSignature(TEST_PRIV_KEY,
like.SignatureOrder(), ParentAuthorSignatureType)
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
if like.ParentAuthorSignature == "" {
t.Errorf("Expected signature, was empty")
}
}
......@@ -36,7 +36,6 @@ type Hcard struct {
PhotoSmall string
}
func (h *Hcard) Fetch(endpoint string) error {
resp, err := FetchHtml("GET", endpoint, nil)
if err != nil {
......
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"
"regexp"
)
func TestHcard(t *testing.T) {
var hcard Hcard
err := hcard.Fetch(TEST_HCARD_LINK)
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
matched, err := regexp.MatchString(`^[\w\d]+?$`, hcard.Guid)
if err != nil || !matched {
t.Errorf("Expected nil and match, got %v and %v", err, matched)
}
nickname := "podmin"
if hcard.Nickname != nickname {
t.Errorf("Expected to be %s, got %s", nickname, hcard.Nickname)
}
matched, err = regexp.MatchString(`^-----BEGIN.+?KEY-----`, hcard.PublicKey)
if err != nil || !matched {
t.Errorf("Expected nil and match, got %v and '%s'", err, hcard.PublicKey)
}
url := "https://joindiaspora.com/"
if hcard.Url != url {
t.Errorf("Expected to be %s, got %s", url, hcard.Url)
}
photo := url + "uploads/images/thumb_"
matched, err = regexp.MatchString(photo + `large_[\w\d]+?\.png`, hcard.Photo)
if err != nil || !matched {
t.Errorf("Expected nil and match, got %v and %s", err, hcard.Photo)
}
matched, err = regexp.MatchString(photo + `medium_[\w\d]+?\.png`, hcard.PhotoMedium)
if err != nil || !matched {
t.Errorf("Expected nil and match, got %v and %s", err, hcard.PhotoMedium)
}
matched, err = regexp.MatchString(photo + `small_[\w\d]+?\.png`, hcard.PhotoSmall)
if err != nil || !matched {
t.Errorf("Expected nil and match, got %v and %s", err, hcard.PhotoSmall)
}
}
......@@ -94,7 +94,9 @@ func SortByEntityOrder(order string, entity []byte) (sorted []byte) {
for _, o := range orderArr {
re := regexp.MustCompile("<"+o+">(.+?)</"+o+">")
elements := re.FindAllStringSubmatch(string(entity), 1)
sortedXmlElements += elements[0][0]
if len(elements) > 0 && len(elements[0]) > 0 {
sortedXmlElements += elements[0][0]
}
}
// replace only the elements we have to sort
......@@ -131,7 +133,8 @@ func ParseWebfingerHandle(handle string) (string, error) {
func parseStringHelper(line, regex string, max int) (parts []string, err error) {
r := regexp.MustCompile(regex)
parts = r.FindStringSubmatch(line)
if len(parts) < max {
if (len(parts) - 1) < max {
err = errors.New("Cannot extract " + regex + " from " + line)
return
}
......
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"
)
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
TI1H9dF91sUB2p5o7irZUdgW8zyZv2d2aHW5cOwR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