Commit d66a5a75 authored by zauberstuhl's avatar zauberstuhl
Browse files

Add travis test file with coverage check

including unit tests for:

* salmon
* webfinger
* helper
* magic
* hcard
* entities
* encryption
* aes
parent d159c2fa
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/>.