// Copyright (c) 2025 H0llyW00dzZ All rights reserved. // // By accessing or using this software, you agree to be bound by the terms // of the License Agreement, which you can find at LICENSE files. package client_test import ( "crypto/tls" "log" "net" "testing" "git.b0zal.io/H0llyW00dzZ/imap/client" "github.com/emersion/go-imap/backend/memory" "github.com/emersion/go-imap/server" "github.com/stretchr/testify/assert" ) func setupTestServer() (net.Listener, *server.Server) { // Create a memory backend be := memory.New() // Create a new IMAP server with TLS configuration s := server.New(be) s.TLSConfig = &tls.Config{ InsecureSkipVerify: true, // For testing purposes Certificates: []tls.Certificate{loadTLSCertificate()}, } // Listen on a random port listener, err := tls.Listen("tcp", "localhost:0", s.TLSConfig) if err != nil { log.Fatal(err) } // Start the server go func() { if err := s.Serve(listener); err != nil { log.Println("Server stopped:", err) } }() return listener, s } func loadTLSCertificate() tls.Certificate { cert, err := tls.LoadX509KeyPair("cert_test.pem", "key_test.pem") if err != nil { log.Fatal("Failed to load TLS certificate:", err) } return cert } func TestIMAPClient(t *testing.T) { listener, srv := setupTestServer() defer listener.Close() defer srv.Close() // Ensure the server is closed properly // Use the existing user in the memory backend config := &client.Config{ Username: "username", // Use the predefined username Password: "password", // Use the predefined password Server: listener.Addr().String(), InsecureSkipVerify: true, } imapClient := client.NewIMAP(config) // Test connection err := imapClient.Connect() assert.NoError(t, err, "Failed to connect") // Test disconnection err = imapClient.Disconnect() assert.NoError(t, err, "Failed to disconnect") } func TestListMessages(t *testing.T) { listener, srv := setupTestServer() defer listener.Close() defer srv.Close() // Ensure the server is closed properly // Use the existing user in the memory backend config := &client.Config{ Username: "username", // Use the predefined username Password: "password", // Use the predefined password Server: listener.Addr().String(), InsecureSkipVerify: true, } imapClient := client.NewIMAP(config) // Connect to the server err := imapClient.Connect() assert.NoError(t, err, "Failed to connect") defer imapClient.Disconnect() // Define the message config to grab specific parts, including body messageConfig := client.MessageConfig{ GrabID: true, GrabFrom: true, GrabSubject: true, GrabBody: true, // Grab the full body } // List only one message from the INBOX messages, err := imapClient.ListMessages("INBOX", 1, messageConfig) assert.NoError(t, err, "Failed to list messages") // Validate that only one message is returned assert.Len(t, messages, 1, "Expected 1 message") // Validate the content of the message expectedBody := "From: contact@example.org\r\n" + "To: contact@example.org\r\n" + "Subject: A little message, just for you\r\n" + "Date: Wed, 11 May 2016 14:31:59 +0000\r\n" + "Message-ID: <0000000@localhost/>\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "Hi there :)" assert.Equal(t, expectedBody, messages[0][client.KeyBody], "Unexpected body content") } func TestListMailboxes(t *testing.T) { listener, srv := setupTestServer() defer listener.Close() defer srv.Close() // Ensure the server is closed properly // Use the existing user in the memory backend config := &client.Config{ Username: "username", Password: "password", Server: listener.Addr().String(), InsecureSkipVerify: true, } imapClient := client.NewIMAP(config) // Connect to the server err := imapClient.Connect() assert.NoError(t, err, "Failed to connect") defer imapClient.Disconnect() // List mailboxes mailboxes, err := imapClient.ListMailboxes() assert.NoError(t, err, "Failed to list mailboxes") // Validate the mailboxes expectedMailboxes := []string{"INBOX"} assert.ElementsMatch(t, expectedMailboxes, mailboxes, "Unexpected mailboxes") }