|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package integration |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "net" |
| 9 | + "net/smtp" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "code.gitea.io/gitea/modules/setting" |
| 14 | + "code.gitea.io/gitea/services/mailer/sender" |
| 15 | + "code.gitea.io/gitea/tests" |
| 16 | + |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | +) |
| 19 | + |
| 20 | +func TestSMTPNetImplementation(t *testing.T) { |
| 21 | + defer tests.PrepareTestEnv(t)() |
| 22 | + |
| 23 | + t.Run("UsesNetSMTP", func(t *testing.T) { |
| 24 | + // PR #36055 switches from gomail's SMTP to net/smtp |
| 25 | + // Test that the implementation uses net/smtp correctly |
| 26 | + |
| 27 | + // Configure SMTP settings |
| 28 | + oldHost := setting.MailService.Host |
| 29 | + oldPort := setting.MailService.Port |
| 30 | + |
| 31 | + setting.MailService.Host = "localhost" |
| 32 | + setting.MailService.Port = 25 |
| 33 | + |
| 34 | + defer func() { |
| 35 | + setting.MailService.Host = oldHost |
| 36 | + setting.MailService.Port = oldPort |
| 37 | + }() |
| 38 | + |
| 39 | + // Create SMTP sender |
| 40 | + // Should use net/smtp implementation |
| 41 | + assert.NotPanics(t, func() { |
| 42 | + _, err := sender.NewSMTPSender() |
| 43 | + // May error if no SMTP server, but shouldn't panic |
| 44 | + _ = err |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("SMTPConnectionHandling", func(t *testing.T) { |
| 49 | + // Test SMTP connection establishment |
| 50 | + // net/smtp should handle connections properly |
| 51 | + |
| 52 | + setting.MailService.Host = "smtp.example.com" |
| 53 | + setting.MailService.Port = 587 |
| 54 | + |
| 55 | + // Connection attempt should be handled gracefully |
| 56 | + assert.NotPanics(t, func() { |
| 57 | + _, err := sender.NewSMTPSender() |
| 58 | + _ = err // May fail to connect, but shouldn't crash |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("SMTPAuthMethods", func(t *testing.T) { |
| 63 | + // Test different SMTP authentication methods |
| 64 | + // net/smtp supports various auth methods |
| 65 | + |
| 66 | + authMethods := []string{"PLAIN", "LOGIN", "CRAM-MD5"} |
| 67 | + |
| 68 | + for _, method := range authMethods { |
| 69 | + t.Run(method, func(t *testing.T) { |
| 70 | + setting.MailService.SMTPAuth = method |
| 71 | + |
| 72 | + // Should handle different auth methods |
| 73 | + assert.NotPanics(t, func() { |
| 74 | + _, err := sender.NewSMTPSender() |
| 75 | + _ = err |
| 76 | + }) |
| 77 | + }) |
| 78 | + } |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +func TestSMTPSenderFunctionality(t *testing.T) { |
| 83 | + defer tests.PrepareTestEnv(t)() |
| 84 | + |
| 85 | + t.Run("SendEmailWithNetSMTP", func(t *testing.T) { |
| 86 | + // Test sending email using net/smtp implementation |
| 87 | + |
| 88 | + // Mock SMTP server would be needed for full test |
| 89 | + // Here we test that the sender can be created |
| 90 | + |
| 91 | + setting.MailService.Host = "localhost" |
| 92 | + setting.MailService.Port = 25 |
| 93 | + |
| 94 | + s, err := sender.NewSMTPSender() |
| 95 | + if err == nil { |
| 96 | + assert.NotNil(t, s, "Sender should be created") |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + t.Run("SMTPTLSSupport", func(t *testing.T) { |
| 101 | + // Test TLS/SSL support with net/smtp |
| 102 | + |
| 103 | + setting.MailService.UseTLS = true |
| 104 | + setting.MailService.Host = "smtp.gmail.com" |
| 105 | + setting.MailService.Port = 465 |
| 106 | + |
| 107 | + // Should support TLS connections |
| 108 | + assert.NotPanics(t, func() { |
| 109 | + _, err := sender.NewSMTPSender() |
| 110 | + _ = err |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | + t.Run("SMTPSTARTTLSSupport", func(t *testing.T) { |
| 115 | + // Test STARTTLS support |
| 116 | + |
| 117 | + setting.MailService.UseSTARTTLS = true |
| 118 | + setting.MailService.Host = "smtp.gmail.com" |
| 119 | + setting.MailService.Port = 587 |
| 120 | + |
| 121 | + // Should support STARTTLS |
| 122 | + assert.NotPanics(t, func() { |
| 123 | + _, err := sender.NewSMTPSender() |
| 124 | + _ = err |
| 125 | + }) |
| 126 | + }) |
| 127 | +} |
| 128 | + |
| 129 | +func TestSMTPErrorHandling(t *testing.T) { |
| 130 | + defer tests.PrepareTestEnv(t)() |
| 131 | + |
| 132 | + t.Run("InvalidHostHandling", func(t *testing.T) { |
| 133 | + // Test error handling for invalid host |
| 134 | + |
| 135 | + setting.MailService.Host = "invalid.host.that.does.not.exist" |
| 136 | + setting.MailService.Port = 25 |
| 137 | + |
| 138 | + _, err := sender.NewSMTPSender() |
| 139 | + if err != nil { |
| 140 | + // Should return clear error |
| 141 | + assert.NotEmpty(t, err.Error()) |
| 142 | + assert.Contains(t, err.Error(), "invalid.host") |
| 143 | + } |
| 144 | + }) |
| 145 | + |
| 146 | + t.Run("InvalidPortHandling", func(t *testing.T) { |
| 147 | + // Test error handling for invalid port |
| 148 | + |
| 149 | + setting.MailService.Host = "localhost" |
| 150 | + setting.MailService.Port = 99999 // Invalid port |
| 151 | + |
| 152 | + _, err := sender.NewSMTPSender() |
| 153 | + // Should handle invalid port gracefully |
| 154 | + _ = err |
| 155 | + }) |
| 156 | + |
| 157 | + t.Run("ConnectionTimeoutHandling", func(t *testing.T) { |
| 158 | + // Test connection timeout handling |
| 159 | + |
| 160 | + // Use a non-routable IP to trigger timeout |
| 161 | + setting.MailService.Host = "10.255.255.1" |
| 162 | + setting.MailService.Port = 25 |
| 163 | + |
| 164 | + _, err := sender.NewSMTPSender() |
| 165 | + if err != nil { |
| 166 | + // Should timeout gracefully |
| 167 | + assert.NotEmpty(t, err.Error()) |
| 168 | + } |
| 169 | + }) |
| 170 | + |
| 171 | + t.Run("AuthenticationFailureHandling", func(t *testing.T) { |
| 172 | + // Test auth failure handling |
| 173 | + |
| 174 | + setting.MailService.Host = "localhost" |
| 175 | + setting.MailService.Port = 25 |
| 176 | + setting.MailService.User = "invalid_user" |
| 177 | + setting.MailService.Passwd = "wrong_password" |
| 178 | + |
| 179 | + _, err := sender.NewSMTPSender() |
| 180 | + // Should handle auth failure gracefully |
| 181 | + _ = err |
| 182 | + }) |
| 183 | +} |
| 184 | + |
| 185 | +func TestSMTPMessageFormatting(t *testing.T) { |
| 186 | + defer tests.PrepareTestEnv(t)() |
| 187 | + |
| 188 | + t.Run("RFC5321Compliance", func(t *testing.T) { |
| 189 | + // net/smtp should follow RFC 5321 |
| 190 | + // Test that message formatting is correct |
| 191 | + |
| 192 | + setting.MailService.Host = "localhost" |
| 193 | + setting.MailService.Port = 25 |
| 194 | + |
| 195 | + s, err := sender.NewSMTPSender() |
| 196 | + if err == nil { |
| 197 | + // Sender should format messages according to standards |
| 198 | + assert.NotNil(t, s) |
| 199 | + } |
| 200 | + }) |
| 201 | + |
| 202 | + t.Run("HeaderFormatting", func(t *testing.T) { |
| 203 | + // Test email header formatting |
| 204 | + // net/smtp should handle headers correctly |
| 205 | + |
| 206 | + setting.MailService.Host = "localhost" |
| 207 | + setting.MailService.Port = 25 |
| 208 | + |
| 209 | + s, err := sender.NewSMTPSender() |
| 210 | + if err == nil { |
| 211 | + // Headers should be formatted properly |
| 212 | + assert.NotNil(t, s) |
| 213 | + } |
| 214 | + }) |
| 215 | + |
| 216 | + t.Run("MultipartMessages", func(t *testing.T) { |
| 217 | + // Test multipart message support |
| 218 | + // Should work with both plain text and HTML |
| 219 | + |
| 220 | + setting.MailService.Host = "localhost" |
| 221 | + setting.MailService.Port = 25 |
| 222 | + |
| 223 | + s, err := sender.NewSMTPSender() |
| 224 | + if err == nil { |
| 225 | + // Should support multipart messages |
| 226 | + assert.NotNil(t, s) |
| 227 | + } |
| 228 | + }) |
| 229 | +} |
| 230 | + |
| 231 | +func TestSMTPPerformance(t *testing.T) { |
| 232 | + defer tests.PrepareTestEnv(t)() |
| 233 | + |
| 234 | + t.Run("ConnectionPooling", func(t *testing.T) { |
| 235 | + // net/smtp should handle connections efficiently |
| 236 | + |
| 237 | + setting.MailService.Host = "localhost" |
| 238 | + setting.MailService.Port = 25 |
| 239 | + |
| 240 | + // Multiple sender creations should be efficient |
| 241 | + for i := 0; i < 10; i++ { |
| 242 | + _, err := sender.NewSMTPSender() |
| 243 | + _ = err |
| 244 | + } |
| 245 | + |
| 246 | + // Should complete without issues |
| 247 | + assert.True(t, true) |
| 248 | + }) |
| 249 | + |
| 250 | + t.Run("ConcurrentConnections", func(t *testing.T) { |
| 251 | + // Test concurrent SMTP connections |
| 252 | + |
| 253 | + setting.MailService.Host = "localhost" |
| 254 | + setting.MailService.Port = 25 |
| 255 | + |
| 256 | + done := make(chan bool, 5) |
| 257 | + |
| 258 | + for i := 0; i < 5; i++ { |
| 259 | + go func() { |
| 260 | + defer func() { done <- true }() |
| 261 | + _, err := sender.NewSMTPSender() |
| 262 | + _ = err |
| 263 | + }() |
| 264 | + } |
| 265 | + |
| 266 | + // Wait for all goroutines |
| 267 | + for i := 0; i < 5; i++ { |
| 268 | + <-done |
| 269 | + } |
| 270 | + |
| 271 | + assert.True(t, true, "Concurrent connections should work") |
| 272 | + }) |
| 273 | +} |
| 274 | + |
| 275 | +func TestSMTPCompatibility(t *testing.T) { |
| 276 | + defer tests.PrepareTestEnv(t)() |
| 277 | + |
| 278 | + t.Run("GoMailReplacement", func(t *testing.T) { |
| 279 | + // PR #36055 replaces gomail with net/smtp |
| 280 | + // Test that net/smtp provides same functionality |
| 281 | + |
| 282 | + setting.MailService.Host = "localhost" |
| 283 | + setting.MailService.Port = 25 |
| 284 | + |
| 285 | + // Should work as replacement for gomail |
| 286 | + s, err := sender.NewSMTPSender() |
| 287 | + if err == nil { |
| 288 | + assert.NotNil(t, s) |
| 289 | + } |
| 290 | + }) |
| 291 | + |
| 292 | + t.Run("BackwardCompatibility", func(t *testing.T) { |
| 293 | + // Existing configurations should still work |
| 294 | + |
| 295 | + oldConfigs := []struct { |
| 296 | + host string |
| 297 | + port int |
| 298 | + }{ |
| 299 | + {"smtp.gmail.com", 587}, |
| 300 | + {"smtp.office365.com", 587}, |
| 301 | + {"localhost", 25}, |
| 302 | + } |
| 303 | + |
| 304 | + for _, config := range oldConfigs { |
| 305 | + t.Run(fmt.Sprintf("%s:%d", config.host, config.port), func(t *testing.T) { |
| 306 | + setting.MailService.Host = config.host |
| 307 | + setting.MailService.Port = config.port |
| 308 | + |
| 309 | + // Should work with common SMTP servers |
| 310 | + _, err := sender.NewSMTPSender() |
| 311 | + // Connection may fail, but shouldn't crash |
| 312 | + _ = err |
| 313 | + }) |
| 314 | + } |
| 315 | + }) |
| 316 | + |
| 317 | + t.Run("MessageBodyGeneration", func(t *testing.T) { |
| 318 | + // go-mail is still used for message body generation |
| 319 | + // Only SMTP sending uses net/smtp |
| 320 | + |
| 321 | + // This validates the hybrid approach |
| 322 | + setting.MailService.Host = "localhost" |
| 323 | + setting.MailService.Port = 25 |
| 324 | + |
| 325 | + s, err := sender.NewSMTPSender() |
| 326 | + if err == nil { |
| 327 | + // Sender should work with go-mail generated messages |
| 328 | + assert.NotNil(t, s) |
| 329 | + } |
| 330 | + }) |
| 331 | +} |
| 332 | + |
| 333 | +func TestSMTPSecurityFeatures(t *testing.T) { |
| 334 | + defer tests.PrepareTestEnv(t)() |
| 335 | + |
| 336 | + t.Run("TLSVersionSupport", func(t *testing.T) { |
| 337 | + // Should support modern TLS versions |
| 338 | + |
| 339 | + setting.MailService.UseTLS = true |
| 340 | + setting.MailService.Host = "localhost" |
| 341 | + setting.MailService.Port = 465 |
| 342 | + |
| 343 | + // Should use secure TLS |
| 344 | + assert.NotPanics(t, func() { |
| 345 | + _, err := sender.NewSMTPSender() |
| 346 | + _ = err |
| 347 | + }) |
| 348 | + }) |
| 349 | + |
| 350 | + t.Run("CertificateValidation", func(t *testing.T) { |
| 351 | + // Should validate server certificates |
| 352 | + |
| 353 | + setting.MailService.UseTLS = true |
| 354 | + setting.MailService.SkipVerify = false |
| 355 | + |
| 356 | + // Should check certificates by default |
| 357 | + assert.NotPanics(t, func() { |
| 358 | + setting.MailService.Host = "localhost" |
| 359 | + setting.MailService.Port = 465 |
| 360 | + _, err := sender.NewSMTPSender() |
| 361 | + _ = err |
| 362 | + }) |
| 363 | + }) |
| 364 | + |
| 365 | + t.Run("InsecureSkipVerify", func(t *testing.T) { |
| 366 | + // Should support skipping verification when needed |
| 367 | + |
| 368 | + setting.MailService.UseTLS = true |
| 369 | + setting.MailService.SkipVerify = true |
| 370 | + |
| 371 | + assert.NotPanics(t, func() { |
| 372 | + setting.MailService.Host = "localhost" |
| 373 | + setting.MailService.Port = 465 |
| 374 | + _, err := sender.NewSMTPSender() |
| 375 | + _ = err |
| 376 | + }) |
| 377 | + }) |
| 378 | +} |
0 commit comments