Commit 6f5c4296 authored by Lukas Matt's avatar Lukas Matt Committed by GitHub

Merge pull request #2 from ganggo/add_http_and_logger_tests

Add more test coverage
parents a4a68c80 3cb7708a
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 (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"encoding/xml"
)
type Test struct {
XMLName xml.Name `xml:"AB";json:"-"`
A string `xml:"A";json:"A"`
B string `xml:"B";json:"B"`
}
func TestPushToPrivate(t *testing.T) {
var guid = "1234"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.URL.Path != "/receive/users/" + guid {
t.Errorf("%s", r.URL.Path)
}
}))
defer ts.Close()
err := PushToPrivate(ts.URL[7:], guid, nil)
if err != nil {
t.Errorf("Some error occured while sending: %v", err)
}
err = PushToPrivate("", guid, nil)
if err == nil {
t.Errorf("Expected an error, got nil")
}
}
func TestPushToPublic(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.URL.Path != "/receive/public" {
t.Errorf("%s", r.URL.Path)
}
}))
defer ts.Close()
err := PushToPublic(ts.URL[7:], nil)
if err != nil {
t.Errorf("Some error occured while sending: %v", err)
}
err = PushToPublic("", nil)
if err == nil {
t.Errorf("Expected an error, got nil")
}
}
func TestFetchJson(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{"A":"a","B":"b"}`)
}))
defer ts.Close()
var res Test
err := FetchJson("GET", ts.URL, nil, &res)
if err != nil {
t.Errorf("Some error occured while sending: %v", err)
}
if res.A != "a" || res.B != "b" {
t.Errorf("Expected to be a and b, got %s and %s", res.A, res.B)
}
}
func TestFetchXml(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `<AB><A>a</A><B>b</B></AB>`)
}))
defer ts.Close()
var res Test
err := FetchXml("GET", ts.URL, nil, &res)
if err != nil {
t.Errorf("Some error occured while sending: %v", err)
}
if res.A != "a" || res.B != "b" {
t.Errorf("Expected to be a and b, got %s and %s", res.A, res.B)
}
}
......@@ -38,6 +38,11 @@ func TestMagicEnvelope(t *testing.T) {
t.Errorf("Expected to be %s, got %s",
string(TEST_MAGIC_PAYLOAD), string(payload))
}
_, err = MagicEnvelope("", string(TEST_AUTHOR), TEST_MAGIC_DATA)
if err == nil {
t.Errorf("Expected to be an error, got nil")
}
}
func TestEncryptedMagicEnvelope(t *testing.T) {
......@@ -57,4 +62,18 @@ func TestEncryptedMagicEnvelope(t *testing.T) {
if !matched {
t.Errorf("Expected match for pattern '%s', got nothing", pattern)
}
_, 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")
}
}
......@@ -22,7 +22,17 @@ 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-ZGlhc3BvcmFfMm5kQG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`)
message, err := ParseDecryptedRequest(xml)
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)
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
......@@ -36,12 +46,31 @@ func TestParseEncryptedRequest(t *testing.T) {
MagicEnvelope: `YLoRPK39sEWfAESJ5IknDfAyZXms4c9Us9K5zoW+8Z4HvR2MUpZOfP5TDFqZDgORp/dY4T6AlSFmu6VXAQsW1ajV7YDAdzhagId/c63kkDgeC1kDu1Ny1xFX23W97hwNizIHIK5uGpw44KgASgy3tXCLIe/JTCB0ykaqSJf0lJ2RO4PnXnz4m3z52WxmsDacBl8Cg0NfHwOgJAX+NGy9so9ECJckCGjCHsJrmVVt/Hp3/MftDFFNFKQ1COaBMFIa2l3qop4TW4yVsBhG5nVBM81+uurr3UZmdN38pmWWgWyTOoHsSK91WuaUbDDRMVb9G+adAIbcb6LXz8qoX68DnQE+7jh8eH7tuyog3+n67JLW3x34iNEpqr/fVdg/DQuujCeXI/OyHH8b6dmVnNMUzOwd4WPeGy6SclDS7s5bjYxRdWoH63d4QQMqrHWmH0RQMBwm8Yj7cRsFKSvQzqQAND+Hfk9XO75QLuBVa2DW/18r3qBiNIaMw1+6VU6XeDoH2vQJU01vK6uR4vXuOXRF0ZawaWS2AHMvDmhMEtGBfQaZpBXDMWTTbyq99vS04tL1AZXs+U2UyxovUSRgLUgqK5qya+MNDn4VcdAdj5tcQ3kciFZyxXDfZ7p0ir7XeTPZHeI9PnjqJEwOuVADmdUTUCx4k5T49YG1op7W14lDuw1DNC3d0KjNYwEFhyMwrgMq9x4Wv5Tsp3avrggdXuHdfJjiCgXKXor9qIqZAG1lD6lyOUV/BBvmEEv6x/hYMmUOmL/rlTNlv11HeynLGyFq6T+6A94Ea9Th+XVU+eYKIKoXs5bJ154s3o1J94zItmMArVV52BUOK8Kpg0OaotEjaMHTT3fOmHzErZTLc63Tb7h21A+DpiLERC+zFEdYs9ifkPDvCyB+TOO1AqRISSGaltaxxIfvo/XQPjP6yjWFcJgkJVy4Lg+nAvfCzZeTAMI8otzDpG6fCIfpg3BJK/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==`,
}
message, err := ParseEncryptedRequest(wrapper, TEST_PRIV_KEY)
message, err := ParseEncryptedRequest(wrapper, []byte(""))
if err == nil {
t.Errorf("Expected to be an error, got nil")
}
message, err = ParseEncryptedRequest(wrapper, TEST_PRIV_KEY)
if err != nil {
t.Errorf("Some error occured while parsing: %v", err)
}
parseRequest(t, message)
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")
}
}
func parseRequest(t *testing.T, message Message) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment