Skip to content

Commit

Permalink
Add Generic gateway test and bugfixes
Browse files Browse the repository at this point in the history
fix broken tests (random seed)
  • Loading branch information
karmanyaahm committed May 22, 2023
1 parent 0378039 commit f2b8305
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
12 changes: 10 additions & 2 deletions gateway/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func (m Generic) Get() []byte {

func (m Generic) Req(body []byte, req http.Request) ([]*http.Request, error) {
myurl := req.URL.EscapedPath()
encodedEndpoint := strings.SplitN(myurl, "/", 4)[2]
encodedEndpoint := ""
if encodedEndpoints := strings.SplitN(myurl, "/", 4); len(encodedEndpoints) >= 3 {
encodedEndpoint = encodedEndpoints[2]
}
endpointBytes, err := base64.RawURLEncoding.DecodeString(encodedEndpoint)
if err != nil {
return nil, fmt.Errorf("Encoded endpoint not valid base64: %w", err)
Expand Down Expand Up @@ -63,7 +66,12 @@ func (m Generic) Req(body []byte, req http.Request) ([]*http.Request, error) {
}

func (Generic) Resp(r []*http.Response, w http.ResponseWriter) {
w.WriteHeader(r[0].StatusCode)
if r[0] != nil {
w.WriteHeader(r[0].StatusCode)
} else {
w.WriteHeader(500)
}
w.Header().Add("TTL", "0")
}

func (m *Generic) Defaults() (failed bool) {
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var configFile = flag.String("c", "config.toml", "path to toml file for config")
// various translaters
var handlers = []Handler{}

func init() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
}

func main() {
flag.Parse()
err := ParseConf(*configFile)
Expand Down
28 changes: 27 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"bytes"
"encoding/base64"
"io"
"io/ioutil"
"math/rand"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -43,6 +45,7 @@ func (s *RewriteTests) SetupTest() {

u, _ := url.Parse(s.ts.URL)
config.Config.Gateway.AllowedHosts = []string{u.Host}
ForceBypassValidProviderCheck(s.ts.URL, "http://temp.test")

s.resetTest()
}
Expand All @@ -62,14 +65,15 @@ const goodFCMResponse = `{"Results": [{"Error":""}]}`

func (s *RewriteTests) TestFCM() {
fcm := rewrite.FCM{Key: "testkey", APIURL: s.ts.URL}
rand.Seed(0)

myFancyContent, myFancyContent64 := myFancyContentGenerate()
cases := [][]string{
{"EFCMD", "/?token=a&instance=b", `{"to":"a","data":{"body":"content","instance":"b"}}`, `content`},
{"FCMD", "/?token=a&app=a", `{"to":"a","data":{"app":"a","body":"content"}}`, `content`},
{"FCMv2", "/?token=a&instance=myinst&v2", `{"to":"a","data":{"b":"Y29udGVudA==","i":"myinst"}}`, `content`},
{"FCMv2-2", "/?v2&token=a&instance=myinst", `{"to":"a","data":{"b":"Y29udGVudA==","i":"myinst"}}`, `content`},
{"FCMv2-3", "/?v2&token=a&instance=myinst", `{"to":"a","data":{"b":"` + myFancyContent64[3000:] + `","i":"myinst","m":"5577006791947779411","s":"2"}}`, myFancyContent}, // this test only tests the second value because that's much easier than testing for the first one due to the architecture of this file. Someday I'll fix that TODO.
{"FCMv2-3", "/?v2&token=a&instance=myinst", `{"to":"a","data":{"b":"` + myFancyContent64[3000:] + `","i":"myinst","m":"8717895732742165506","s":"2"}}`, myFancyContent}, // this test only tests the second value because that's much easier than testing for the first one due to the architecture of this file. Someday I'll fix that TODO.
}

for _, i := range cases {
Expand Down Expand Up @@ -181,6 +185,28 @@ func (s *RewriteTests) TestMatrixResp() {
//TODO
}

func (s *RewriteTests) TestGenericGateway() {
gw := gateway.Generic{}

content := `this is
my msg`
request := httptest.NewRequest("POST", "/generic/"+base64.RawURLEncoding.EncodeToString([]byte(s.ts.URL)), bytes.NewBufferString(content))
request.Header.Add("cOntent-Encoding", "aesgcm")
request.Header.Add("cryPTo-KEY", `dh="BNoRDbb84JGm8g5Z5CFxurSqsXWJ11ItfXEWYVLE85Y7CYkDjXsIEc4aqxYaQ1G8BqkXCJ6DPpDrWtdWj_mugHU"`)
request.Header.Add("EncRYPTION", `Encryption: salt="lngarbyKfMoi9Z75xYXmkg"`)
handle(&gw)(s.Resp, request)

s.Equal(200, s.Resp.Result().StatusCode, "request should be valid")
s.Equal(`this is
my msg
dh="BNoRDbb84JGm8g5Z5CFxurSqsXWJ11ItfXEWYVLE85Y7CYkDjXsIEc4aqxYaQ1G8BqkXCJ6DPpDrWtdWj_mugHU"
Encryption: salt="lngarbyKfMoi9Z75xYXmkg"
aesgcm`, string(s.CallBody), "body should match")

}

func (s *RewriteTests) TestHealth() {
resp, err := http.Get(s.ts.URL + "/health")
s.Require().Nil(err)
Expand Down
6 changes: 6 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import (
"github.com/stretchr/testify/assert"
)

func ForceBypassValidProviderCheck(host ...string) {
for _, i := range host {
allowedProxies.Set(i, true, 1000*time.Minute)
}
}

func TestCheckRewriteProxy(t *testing.T) {
c := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
Expand Down

0 comments on commit f2b8305

Please sign in to comment.