| 1 | package smtpscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/ |
| 2 | |
| 3 | import ( |
| 4 | "errorsscip-go gomod github.com/golang/go/src . errors/" |
| 5 | "net/smtpscip-go gomod github.com/golang/go/src . `net/smtp`/" |
| 6 | "stringsscip-go gomod github.com/golang/go/src . strings/" |
| 7 | "testingscip-go gomod github.com/golang/go/src . testing/" |
| 8 | ) |
| 9 | |
| 10 | func TestLoginAuthStartscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/TestLoginAuthStart().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) { |
| 11 | t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().() |
| 12 | |
| 13 | tests := []struct { |
| 14 | name string |
| 15 | host string |
| 16 | server *smtpscip-go gomod github.com/golang/go/src . `net/smtp`/.ServerInfoscip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo# |
| 17 | wantErr error |
| 18 | }{ |
| 19 | { |
| 20 | name: "TLS and matching host", |
| 21 | host: "mail.example.com", |
| 22 | server: &smtpscip-go gomod github.com/golang/go/src . `net/smtp`/.ServerInfoscip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#{Namescip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#Name.: "mail.example.com", TLSscip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#TLS.: true}, |
| 23 | }, |
| 24 | { |
| 25 | name: "localhost without TLS", |
| 26 | host: "localhost", |
| 27 | server: &smtpscip-go gomod github.com/golang/go/src . `net/smtp`/.ServerInfoscip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#{Namescip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#Name.: "localhost", TLSscip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#TLS.: false}, |
| 28 | }, |
| 29 | { |
| 30 | name: "127.0.0.1 without TLS", |
| 31 | host: "127.0.0.1", |
| 32 | server: &smtpscip-go gomod github.com/golang/go/src . `net/smtp`/.ServerInfoscip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#{Namescip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#Name.: "127.0.0.1", TLSscip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#TLS.: false}, |
| 33 | }, |
| 34 | { |
| 35 | name: "unencrypted remote", |
| 36 | host: "mail.example.com", |
| 37 | server: &smtpscip-go gomod github.com/golang/go/src . `net/smtp`/.ServerInfoscip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#{Namescip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#Name.: "mail.example.com", TLSscip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#TLS.: false}, |
| 38 | wantErr: errUnencryptedConnectionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/errUnencryptedConnection., |
| 39 | }, |
| 40 | { |
| 41 | name: "wrong host", |
| 42 | host: "mail.example.com", |
| 43 | server: &smtpscip-go gomod github.com/golang/go/src . `net/smtp`/.ServerInfoscip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#{Namescip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#Name.: "evil.example.com", TLSscip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#TLS.: true}, |
| 44 | wantErr: errWrongHostNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/errWrongHostName., |
| 45 | }, |
| 46 | } |
| 47 | |
| 48 | for _, tt := range tests { |
| 49 | t.Runscip-go gomod github.com/golang/go/src . testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) { |
| 50 | t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().() |
| 51 | |
| 52 | auth := LoginAuthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/LoginAuth().("user", "pass", tt.host) |
| 53 | |
| 54 | mech, resp, err := auth.Startscip-go gomod github.com/golang/go/src . `net/smtp`/Auth#Start().(tt.server) |
| 55 | if !errorsscip-go gomod github.com/golang/go/src . errors/.Isscip-go gomod github.com/golang/go/src . errors/Is().(err, tt.wantErr) { |
| 56 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#Fatalf().("Start() error = %v, want %v", err, tt.wantErr) |
| 57 | } |
| 58 | |
| 59 | if tt.wantErr != nil { |
| 60 | return |
| 61 | } |
| 62 | |
| 63 | if mech != "LOGIN" { |
| 64 | t.Errorfscip-go gomod github.com/golang/go/src . testing/common#Errorf().("Start() mech = %q, want LOGIN", mech) |
| 65 | } |
| 66 | |
| 67 | if resp != nil { |
| 68 | t.Errorfscip-go gomod github.com/golang/go/src . testing/common#Errorf().("Start() resp = %v, want nil", resp) |
| 69 | } |
| 70 | }) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestLoginAuthNextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/TestLoginAuthNext().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) { |
| 75 | t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().() |
| 76 | |
| 77 | auth := LoginAuthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/LoginAuth().("alice", "s3cret", "mail.example.com").(*loginAuthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#) |
| 78 | |
| 79 | // Successful two-step exchange. Challenge text is ignored. |
| 80 | resp, err := auth.Nextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#Next().([]byte("User Name\x00"), true) |
| 81 | if err != nil { |
| 82 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#Fatalf().("step 0: unexpected error: %v", err) |
| 83 | } |
| 84 | |
| 85 | if string(resp) != "alice" { |
| 86 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#Fatalf().("step 0: resp = %q, want alice", resp) |
| 87 | } |
| 88 | |
| 89 | resp, err = auth.Nextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#Next().([]byte("password:"), true) |
| 90 | if err != nil { |
| 91 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#Fatalf().("step 1: unexpected error: %v", err) |
| 92 | } |
| 93 | |
| 94 | if string(resp) != "s3cret" { |
| 95 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#Fatalf().("step 1: resp = %q, want s3cret", resp) |
| 96 | } |
| 97 | |
| 98 | // An extra challenge must abort with a non-nil error, not a dummy response. |
| 99 | resp, err = auth.Nextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#Next().([]byte("Username:"), true) |
| 100 | if err == nil { |
| 101 | t.Fatalscip-go gomod github.com/golang/go/src . testing/common#Fatal().("step 2: expected error, got nil") |
| 102 | } |
| 103 | |
| 104 | if resp != nil { |
| 105 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#Fatalf().("step 2: resp = %v, want nil so net/smtp aborts", resp) |
| 106 | } |
| 107 | |
| 108 | if !errorsscip-go gomod github.com/golang/go/src . errors/.Isscip-go gomod github.com/golang/go/src . errors/Is().(err, errUnexpectedServerChallengescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/errUnexpectedServerChallenge.) { |
| 109 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#Fatalf().("step 2: error = %v, want %v", err, errUnexpectedServerChallengescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/errUnexpectedServerChallenge.) |
| 110 | } |
| 111 | |
| 112 | if got := err.Error(); !stringsscip-go gomod github.com/golang/go/src . strings/.Containsscip-go gomod github.com/golang/go/src . strings/Contains().(got, "Username:") { |
| 113 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#Fatalf().("step 2: error = %q, want challenge quoted in message", got) |
| 114 | } |
| 115 | |
| 116 | // more=false ends the exchange cleanly. |
| 117 | resp, err = auth.Nextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#Next().(nil, false) |
| 118 | if err != nil || resp != nil { |
| 119 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#Fatalf().("more=false: resp=%v err=%v, want nil,nil", resp, err) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestLoginAuthNextResetsWithStartscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/TestLoginAuthNextResetsWithStart().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) { |
| 124 | t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().() |
| 125 | |
| 126 | auth := LoginAuthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/LoginAuth().("u", "p", "localhost").(*loginAuthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#) |
| 127 | if _, err := auth.Nextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#Next().(nil, true); err != nil { |
| 128 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#Fatalf().("first Next: %v", err) |
| 129 | } |
| 130 | |
| 131 | if _, _, err := auth.Startscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#Start().(&smtpscip-go gomod github.com/golang/go/src . `net/smtp`/.ServerInfoscip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#{Namescip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#Name.: "localhost", TLSscip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#TLS.: false}); err != nil { |
| 132 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#Fatalf().("Start: %v", err) |
| 133 | } |
| 134 | |
| 135 | if auth.respStepscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#respStep. != 0 { |
| 136 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#Fatalf().("respStep after Start = %d, want 0", auth.respStepscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#respStep.) |
| 137 | } |
| 138 | |
| 139 | resp, err := auth.Nextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#Next().([]byte("Username:"), true) |
| 140 | if err != nil { |
| 141 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#Fatalf().("Next after Start: %v", err) |
| 142 | } |
| 143 | |
| 144 | if string(resp) != "u" { |
| 145 | t.Fatalfscip-go gomod github.com/golang/go/src . testing/common#Fatalf().("Next after Start: resp = %q, want u", resp) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestIsLocalhostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/TestIsLocalhost().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#) { |
| 150 | t.Parallelscip-go gomod github.com/golang/go/src . testing/T#Parallel().() |
| 151 | |
| 152 | for _, name := range []string{"localhost", "127.0.0.1", "::1"} { |
| 153 | if !isLocalhostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/isLocalhost().(name) { |
| 154 | t.Errorfscip-go gomod github.com/golang/go/src . testing/common#Errorf().("isLocalhost(%q) = false, want true", name) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if isLocalhostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/isLocalhost().("example.com") { |
| 159 | t.Errorscip-go gomod github.com/golang/go/src . testing/common#Error().("isLocalhost(example.com) = true, want false") |
| 160 | } |
| 161 | } |