Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testability #2

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 6 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,35 @@ go get github.com/mileusna/srs
```go
func main() {
// setting up engine with mandatory params
srs := srs.SRS{
s := srs.SRS{
Secret: []byte("YourSecretKeyForHashingUniqueAndPermanentPerServer"),
Domain: "forwarding-domain.com",
}

// forwarding
// this code will produce something like this for fwd address
// SRS0=JvSE=IT=mailspot.com=milos@forwarding-domain.com
fwd, err := srs.Forward("milos@mailspot.com")
fwd, err := s.Forward("milos@mailspot.com")
if err != nil {
log.Error(err)
return
log.Fatal(err)
}

// reverse check when emails are bounced back to forwarding server
rvs, err := srs.Reverse("SRS0=JvSE=IT=mailspot.com=milos@forwarding-domain.com")
rvs, err := s.Reverse("SRS0=JvSE=IT=mailspot.com=milos@forwarding-domain.com")
if err != nil {
// email is not SRS, invalid hash, invalid timestamp, timestamp out of date, etc..
log.Error(err)
return
log.Fatal(err)
}

// rvs is normal email address
fmt.Println(rvs)
}
```

## Testing

Since SRS contains timestamp component it is difficult to test package against static expected results because SRS result will change over time.
That is the reasons why the tests actually connects to most popular SRS daemon for Postfix, [postsrsd](https://github.com/roehling/postsrsd), and checks the results. As long as you use the same domain name and same secret key, results should match, although there are some exceptions.

### Exceptions

There are some cases which postsrsd will accept, but I find them wrong and they won't be supported by this package.
I guess that postsrsd rely on mailserver to reject this type of email addresses so it doesn't check bad email formats.
I guess that postsrsd rely on mail server to reject this type of email addresses so it doesn't check bad email formats.

These are some examples which postsrsd will accept, but this go package will return an error due to bad email formatting:

Expand All @@ -70,16 +63,3 @@ These are some examples which postsrsd will accept, but this go package will ret
- milosmileusnic@domain,net // comma in domain name
- milos mileusnic@domain.net // space in user
- etc.

This types of emails are excluded from testing.

### Testing setup
- Install postsrsd from https://github.com/roehling/postsrsd or use repo
for your linux distribution (CentOS https://wiki.mailserver.guru/doku.php/centos:mailserver.guru)
- Start postsrsd
- Use the same domain and secret key in `srs_test.go` as postsrsd. Postsrsd key is located in
`/etc/postsrsd.secret`
- Add more test emails in `srs_test.go` for testing
- Run tests


43 changes: 43 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package srs_test

import (
"fmt"
"log"

"github.com/mileusna/srs"
)

func ExampleSRS_Forward() {
// setting up engine with mandatory params
s := srs.SRS{
Secret: []byte("YourSecretKeyForHashingUniqueAndPermanentPerServer"),
Domain: "forwarding-domain.com",
}

// forwarding
// this code will produce something like this for fwd address
// SRS0=JvSE=IT=mailspot.com=milos@forwarding-domain.com
fwd, err := s.Forward("milos@mailspot.com")
if err != nil {
log.Fatal(err)
}
fmt.Println(fwd)
}

func ExampleSRS_Reverse() {
// setting up engine with mandatory params
s := srs.SRS{
Secret: []byte("YourSecretKeyForHashingUniqueAndPermanentPerServer"),
Domain: "forwarding-domain.com",
}

// reverse check when emails are bounced back to forwarding server
rvs, err := s.Reverse("SRS0=JvSE=IT=mailspot.com=milos@forwarding-domain.com")
if err != nil {
// email is not SRS, invalid hash, invalid timestamp, timestamp out of date, etc..
log.Fatal(err)
}

// rvs is normal email address
fmt.Println(rvs)
}
Loading