Skip to content

Commit

Permalink
Remove unused cruft and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
randomthingsandstuff authored Oct 28, 2021
2 parents d5023c1 + e64644f commit a875e54
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ People probably use this!

## To start developing

You need a working [Go environment](https://golang.org/doc/install) (1.10 or newer).
You need a working [Go environment](https://golang.org/doc/install) (1.16 or newer).
This project also uses [Go Modules](https://github.com/golang/go/wiki/Modules).

```bash
Expand Down
23 changes: 23 additions & 0 deletions cmd/stayrtr/smalltest.rpki.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"metadata": {
"buildtime": "2021-07-27T18:56:02Z",
"vrps": 2
},

"roas": [
{
"asn": 13335,
"prefix": "1.0.0.0/24",
"maxLength": 24,
"ta": "apnic",
"expires": 1627568318
},
{
"asn": "AS9367",
"prefix": "2001:200:136::/48",
"maxLength": 48,
"ta": "apnic",
"expires": 1627575699
}
]
}
14 changes: 8 additions & 6 deletions cmd/stayrtr/stayrtr.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ func metricHTTP() {
log.Fatal(http.ListenAndServe(*MetricsAddr, nil))
}

func checkFile(data []byte) ([]byte, error) {
hsum := sha256.Sum256(data)
return hsum[:], nil
// newSHA256 will return the sha256 sum of the byte slice
// The return will be converted form a [32]byte to []byte
func newSHA256(data []byte) []byte {
hash := sha256.Sum256(data)
return hash[:]
}

func decodeJSON(data []byte) (*prefixfile.VRPList, error) {
Expand Down Expand Up @@ -251,7 +253,7 @@ func (e IdenticalFile) Error() string {

// Update the state based on the current slurm file and data.
func (s *state) updateFromNewState() error {
sessid, _ := s.server.GetSessionId(nil)
sessid := s.server.GetSessionId()

if s.checktime {
buildtime, err := time.Parse(time.RFC3339, s.lastdata.Metadata.Buildtime)
Expand Down Expand Up @@ -327,7 +329,7 @@ func (s *state) updateFile(file string) (bool, error) {
RefreshStatusCode.WithLabelValues(file, fmt.Sprintf("%d", code)).Inc()
}

hsum, _ := checkFile(data)
hsum := newSHA256(data)
if s.lasthash != nil {
cres := bytes.Compare(s.lasthash, hsum)
if cres == 0 {
Expand Down Expand Up @@ -603,7 +605,7 @@ func run() error {

if *Bind != "" {
go func() {
sessid, _ := server.GetSessionId(nil)
sessid := server.GetSessionId()
log.Infof("StayRTR Server started (sessionID:%d, refresh:%d, retry:%d, expire:%d)", sessid, sc.RefreshInterval, sc.RetryInterval, sc.ExpireInterval)
err := server.Start(*Bind)
if err != nil {
Expand Down
47 changes: 47 additions & 0 deletions cmd/stayrtr/stayrtr_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"net"
"os"
"testing"
Expand Down Expand Up @@ -133,3 +134,49 @@ func BenchmarkDecodeJSON(b *testing.B) {
decodeJSON(json)
}
}

func TestJson(t *testing.T) {
json, err := os.ReadFile("smalltest.rpki.json")
if err != nil {
panic(err)
}
got, err := decodeJSON(json)
if err != nil {
t.Errorf("Unable to decode json: %v", err)
}

want := (&prefixfile.VRPList{
Metadata: prefixfile.MetaData{
Counts: 2,
Buildtime: "2021-07-27T18:56:02Z",
},
Data: []prefixfile.VRPJson{
{Prefix: "1.0.0.0/24",
Length: 24,
ASN: float64(13335),
TA: "apnic",
Expires: 1627568318,
},
{
Prefix: "2001:200:136::/48",
Length: 48,
ASN: "AS9367",
TA: "apnic",
Expires: 1627575699,
},
},
})

if !cmp.Equal(got, want) {
t.Errorf("Got (%v), Wanted (%v)", got, want)
}

}

func TestNewSHA256(t *testing.T) {
want := "8eddd6897b244bb4d045ff811128b50b53ed85d19a9d1b756a0a400e82b23c2f"
got := fmt.Sprintf("%x", newSHA256([]byte("☘️")))
if got != want {
t.Errorf("Got (%s), Wanted (%s)", got, want)
}
}
8 changes: 4 additions & 4 deletions lib/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type RTREventHandler interface {

type VRPManager interface {
GetCurrentSerial(uint16) (uint32, bool)
GetSessionId(*Client) (uint16, error)
GetSessionId() uint16
GetCurrentVRPs() ([]VRP, bool)
GetVRPsSerialDiff(uint32) ([]VRP, bool)
}
Expand All @@ -51,7 +51,7 @@ func (e *DefaultRTREventHandler) RequestCache(c *Client) {
if e.Log != nil {
e.Log.Debugf("%v > Request Cache", c)
}
sessionId, _ := e.vrpManager.GetSessionId(c)
sessionId := e.vrpManager.GetSessionId()
serial, valid := e.vrpManager.GetCurrentSerial(sessionId)
if !valid {
c.SendNoDataError()
Expand Down Expand Up @@ -274,8 +274,8 @@ func (s *Server) SetManualSerial(v bool) {
s.manualserial = v
}

func (s *Server) GetSessionId(c *Client) (uint16, error) {
return s.sessId, nil
func (s *Server) GetSessionId() uint16 {
return s.sessId
}

func (s *Server) GetCurrentVRPs() ([]VRP, bool) {
Expand Down

0 comments on commit a875e54

Please sign in to comment.