package smtp_server

import (
	"bytes"
	log "github.com/sirupsen/logrus"
	"io/fs"
	"net"
	"net/netip"
	"os"
	"path/filepath"
	"pmail/config"
	"pmail/db"
	parsemail2 "pmail/dto/parsemail"
	"pmail/session"
	"testing"
	"time"
)

func testInit() {
	// 设置日志格式为json格式
	//log.SetFormatter(&log.JSONFormatter{})

	log.SetReportCaller(true)
	log.SetFormatter(&log.TextFormatter{
		//以下设置只是为了使输出更美观
		DisableColors:   true,
		TimestampFormat: "2006-01-02 15:03:04",
	})

	// 设置将日志输出到标准输出（默认的输出为stderr,标准错误）
	// 日志消息输出可以是任意的io.writer类型
	log.SetOutput(os.Stdout)

	// 设置日志级别为warn以上
	log.SetLevel(log.TraceLevel)

	var cst, _ = time.LoadLocation("Asia/Shanghai")
	time.Local = cst

	config.Init()
	parsemail2.Init()
	db.Init()
	session.Init()

}

func TestNuisanace(t *testing.T) {
	testInit()

	s := Session{
		RemoteAddress: net.TCPAddrFromAddrPort(netip.AddrPortFrom(netip.AddrFrom4([4]byte{}), 25)),
	}

	data, _ := os.ReadFile("../docs/nuisance/demo.txt")
	s.Data(bytes.NewReader(data))

}

func TestSession_Data(t *testing.T) {
	testInit()
	s := Session{
		RemoteAddress: net.TCPAddrFromAddrPort(netip.AddrPortFrom(netip.AddrFrom4([4]byte{}), 25)),
	}

	filepath.WalkDir("docs", func(path string, d fs.DirEntry, err error) error {
		if !d.IsDir() {
			data, _ := os.ReadFile(path)
			s.Data(bytes.NewReader(data))
		}
		return nil
	})

}

func TestSession_DataGmail(t *testing.T) {
	testInit()
	s := Session{
		RemoteAddress: net.TCPAddrFromAddrPort(netip.AddrPortFrom(netip.AddrFrom4([4]byte{}), 25)),
	}

	data, _ := os.ReadFile("docs/gmail/带附件带图片.txt")
	s.Data(bytes.NewReader(data))

}

func TestPmailEmail(t *testing.T) {
	testInit()
	emailData := `DKIM-Signature: a=rsa-sha256; bh=x7Rh+N2y2K9exccEAyKCTAGDgYKfnLZpMWc25ug5Ny4=;
 c=simple/simple; d=domain.com;
 h=Content-Type:Mime-Version:Subject:To:From:Date; s=default; t=1693831868;
 v=1;
 b=1PZEupYvSMtGyYx42b4G65YbdnRj4y2QFo9kS7GXiTVhUM5EYzJhZzknwRMN5RL5aFY26W4E
 DmzJ85XvPPvrDtnU/B4jkc5xthE+KEsb1Go8HcL8WQqwvsE9brepeA0t0RiPnA/x7dbTo3u72SG
 WqtviWbJH5lPFc9PkSbEPFtc=
Content-Type: multipart/mixed;
 boundary=3c13260efb7bd8bad8315c21215489fe283f36cdf82813674f6e11215f6c
Mime-Version: 1.0
Subject: =?utf-8?q?=E6=8F=92=E4=BB=B6=E6=B5=8B=E8=AF=95?=
To: =?utf-8?q?=E5=90=8D?= <ok@jinnrry.com>
From: =?utf-8?q?=E5=8F=91=E9=80=81=E4=BA=BA?= <j@jinnrry.com>
Date: Mon, 04 Sep 2023 20:51:08 +0800

--3c13260efb7bd8bad8315c21215489fe283f36cdf82813674f6e11215f6c
Content-Type: multipart/alternative;
 boundary=9ebf2f3c4f97c51dd9a285ae28a54d2d0d84aa6d0ad28b76547e2096bb66

--9ebf2f3c4f97c51dd9a285ae28a54d2d0d84aa6d0ad28b76547e2096bb66
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/plain

=E8=BF=99=E6=98=AFText
--9ebf2f3c4f97c51dd9a285ae28a54d2d0d84aa6d0ad28b76547e2096bb66
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/html

<div>=E8=BF=99=E6=98=AFHtml</div>
--9ebf2f3c4f97c51dd9a285ae28a54d2d0d84aa6d0ad28b76547e2096bb66--

--3c13260efb7bd8bad8315c21215489fe283f36cdf82813674f6e11215f6c--
`
	s := Session{
		RemoteAddress: net.TCPAddrFromAddrPort(netip.AddrPortFrom(netip.AddrFrom4([4]byte{}), 25)),
	}

	s.Data(bytes.NewReader([]byte(emailData)))

}

func TestRuleForward(t *testing.T) {
	testInit()

	forwardEmail := `DKIM-Signature: a=rsa-sha256; bh=bpOshF+iimuqAQijVxqkH6gPpWf8A+Ih30/tMjgEgS0=;
 c=simple/simple; d=jinnrry.com;
 h=Content-Type:Mime-Version:Subject:To:From:Date; s=default; t=1693992640;
 v=1;
 b=XiOgYL9iGrkuYzXBAf7DSO0sRbFr6aPOE4VikmselNKEF1UTjMPdiqpeHyx/i6BOQlJWWZEC
 PzceHTDFIStcZE6a5Sc1nh8Fis+gRkrheBO/zK/P5P/euK+0Fj5+0T82keNTSCgo1ZtEIubaNR0
 JvkwJ2ZC9g8xV6Yiq+ZhRriT8lZ6zeI55PPEFJIzFgZ7xDshDgx5E7J1xRXQqcEMV1rgVq04d3c
 6wjU+LLtghmgtUToRp3ASn6DhVO+Bbc4QkmcQ/StQH3681+1GVMHvQSBhSSymSRA71SikE2u3a1
 JnvbOP9fThP7h+6oFEIRuF7MwDb3JWY5BXiFFKCkecdFg==
Content-Type: multipart/mixed;
 boundary=8e9d5abb6bdac11b8d7d6e13280af1a87d12b904a59368d6e852b0a4ce3e
Mime-Version: 1.0
Subject: forward
To: <t@jiangwei.one>
From: "i" <i@jinnrry.com>
Date: Wed, 06 Sep 2023 17:30:40 +0800

--8e9d5abb6bdac11b8d7d6e13280af1a87d12b904a59368d6e852b0a4ce3e
Content-Type: multipart/alternative;
 boundary=a62ae91c159ea22e8196d57d344626eb00d1ddfa9c5064a39b01588aa992

--a62ae91c159ea22e8196d57d344626eb00d1ddfa9c5064a39b01588aa992
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/plain

hello pls Forward the email.
--a62ae91c159ea22e8196d57d344626eb00d1ddfa9c5064a39b01588aa992
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/html

<p>hello pls Forward the email.</p>
--a62ae91c159ea22e8196d57d344626eb00d1ddfa9c5064a39b01588aa992--

--8e9d5abb6bdac11b8d7d6e13280af1a87d12b904a59368d6e852b0a4ce3e--`

	readEmail := `DKIM-Signature: a=rsa-sha256; bh=JcCDj6edb1bAwRbcFZ63plFZOeB5AdGWLE/PQ2FQ1Tc=;
 c=simple/simple; d=jinnrry.com;
 h=Content-Type:Mime-Version:Subject:To:From:Date; s=default; t=1693992600;
 v=1;
 b=rwlqSkDFKYH42pA1jsajemaw+4YdeLHPeqV4mLQrRdihgma1VSvXl5CEOur/KuwQuUarr2cu
 SntWrHE6+RnDaQcPEHbkgoMjEJw5+VPwkIvE6VSlMIB7jg93mGzvN2yjheWTePZ+cVPjOaIrgir
 wiT24hkrTHp+ONT8XoS0sDuY+ieyBZp/GCv/YvgE4t0JEkNozMAVWotrXxaICDzZoWP3NNmKLqg
 6He6zwWAl51r3W5R5weGBi6A/FqlHgHZGroXnNi+wolDuN6pQiVAJ7MZ6hboPCbCCRrBQDTdor5
 wEI2+MwlJ/d2f17wxoGmluCewbeYttuVcpUOVwACJKw3g==
Content-Type: multipart/mixed;
 boundary=9e33a130a8a976102a93e296d6408d228e151f7841ca9ee0d777234fd6f3
Mime-Version: 1.0
Subject: read
To: <t@jiangwei.one>
From: "i" <i@jinnrry.com>
Date: Wed, 06 Sep 2023 17:30:00 +0800

--9e33a130a8a976102a93e296d6408d228e151f7841ca9ee0d777234fd6f3
Content-Type: multipart/alternative;
 boundary=54a95f3429f3cdb342383db10293780bed341f8dc20d2f876eb0853e3884

--54a95f3429f3cdb342383db10293780bed341f8dc20d2f876eb0853e3884
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/plain

12 aRead 1sadf
--54a95f3429f3cdb342383db10293780bed341f8dc20d2f876eb0853e3884
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/html

<p>12 aRead 1sadf</p>
--54a95f3429f3cdb342383db10293780bed341f8dc20d2f876eb0853e3884--

--9e33a130a8a976102a93e296d6408d228e151f7841ca9ee0d777234fd6f3--`

	moveEmail := `DKIM-Signature: a=rsa-sha256; bh=YQfG/wlHGhky6FNmpIwgDYDOc/uyivdBv+9S02Z04xY=;
 c=simple/simple; d=jinnrry.com;
 h=Content-Type:Mime-Version:Subject:To:From:Date; s=default; t=1693992542;
 v=1;
 b=IhxswOCq8I7CmCas1EMp+n8loR7illqlF0IJC6eN1+OLjI/E5BPzpP4HWkyqaAkd0Vn9i+Bn
 MVb5kNHZ2S7qt0rqAAc6Atc0i9WpLEI3Cng+VDn+difcMZlJSAkhLLn2sUsS4Fzqqo3Cbw62qSO
 TgnWRmlj9aM+5xfGcl/76WOvQQpahJbGg6Go51kFMeHVom/VeGKIgFBCeMe37T/LS03c3pAV8gA
 i6Zy3GYE57W/qU3oCzaGeS3n5zom/i74H4VipiVIMX/OBNYhdHWrP8vyjvzLFpJlXp6RvzcRl0P
 ytyiCZfE8G7fAFntp20LW70Y5Xgqqczk1jR578UDczVoA==
Content-Type: multipart/mixed;
 boundary=c84d60b253aa6caee345c73e717ad59b1975448bbdfad7a23ac4d76e022d
Mime-Version: 1.0
Subject: Move
To: <t@jiangwei.one>
From: "i" <i@jinnrry.com>
Date: Wed, 06 Sep 2023 17:29:02 +0800

--c84d60b253aa6caee345c73e717ad59b1975448bbdfad7a23ac4d76e022d
Content-Type: multipart/alternative;
 boundary=a69985ebcf3c1c44d6e69e5a29c1044743cd9e44d4bc9bb6886f83a73966

--a69985ebcf3c1c44d6e69e5a29c1044743cd9e44d4bc9bb6886f83a73966
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/plain

MOVE move Move
--a69985ebcf3c1c44d6e69e5a29c1044743cd9e44d4bc9bb6886f83a73966
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/html

<p>MOVE move Move</p>
--a69985ebcf3c1c44d6e69e5a29c1044743cd9e44d4bc9bb6886f83a73966--

--c84d60b253aa6caee345c73e717ad59b1975448bbdfad7a23ac4d76e022d--`

	deleteEmail := `DKIM-Signature: a=rsa-sha256; bh=dNtHGqd1NbRj0WSwrJmPsqAcAy3h/4kZK2HFQ0Asld8=;
 c=simple/simple; d=jinnrry.com;
 h=Content-Type:Mime-Version:Subject:To:From:Date; s=default; t=1693992495;
 v=1;
 b=QllU8lqGdoOMaGYp8d13oWytb7+RebqKjq4y8Rs/kOeQxoE8dSEVliK3eBiXidsNTdDtkTqf
 eiwjyRBK92NVCYprdJqLbu9qZ39BC2lk3NXttTSJ1+1ZZ/bGtIW5JIYn2pToED0MqVVkxGFUtl+
 qFmc4mWo5a4Mbij7xaAB3uJtHpBDt7q4Ovr2hiMetQv7YrhZvCt/xrH8Q9YzZ6xzFUL5ekW40eH
 oWElU1GyVBHWCKh31aweyhA+1XLPYojjREQYd4svRqTbSFSsBqFwFIUGdnyJh2WgmF8eucmttAw
 oRhgzyZkHL1jAskKFBpO10SDReyk50Cvc+0kSLj+QcUpg==
Content-Type: multipart/mixed;
 boundary=bdfa9bf94e22e218105281e06bd59bd6df3ce70e71367bf49fbe73301af3
Mime-Version: 1.0
Subject: test
To: <t@jiangwei.one>
From: "i" <i@jinnrry.com>
Date: Wed, 06 Sep 2023 17:28:15 +0800

--bdfa9bf94e22e218105281e06bd59bd6df3ce70e71367bf49fbe73301af3
Content-Type: multipart/alternative;
 boundary=7352524eaae801790245f6bf095460fd1f4e01f5748b4dba48635bf59b04

--7352524eaae801790245f6bf095460fd1f4e01f5748b4dba48635bf59b04
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/plain

Delete
--7352524eaae801790245f6bf095460fd1f4e01f5748b4dba48635bf59b04
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/html

<p>Delete</p>
--7352524eaae801790245f6bf095460fd1f4e01f5748b4dba48635bf59b04--

--bdfa9bf94e22e218105281e06bd59bd6df3ce70e71367bf49fbe73301af3--`

	s := Session{
		RemoteAddress: net.TCPAddrFromAddrPort(netip.AddrPortFrom(netip.AddrFrom4([4]byte{}), 25)),
	}

	s.Data(bytes.NewReader([]byte(deleteEmail)))
	s.Data(bytes.NewReader([]byte(readEmail)))
	s.Data(bytes.NewReader([]byte(forwardEmail)))
	s.Data(bytes.NewReader([]byte(moveEmail)))
}

func TestRuleRead(t *testing.T) {
	testInit()

	readEmail := `DKIM-Signature: a=rsa-sha256; bh=JcCDj6edb1bAwRbcFZ63plFZOeB5AdGWLE/PQ2FQ1Tc=;
 c=simple/simple; d=jinnrry.com;
 h=Content-Type:Mime-Version:Subject:To:From:Date; s=default; t=1693992600;
 v=1;
 b=rwlqSkDFKYH42pA1jsajemaw+4YdeLHPeqV4mLQrRdihgma1VSvXl5CEOur/KuwQuUarr2cu
 SntWrHE6+RnDaQcPEHbkgoMjEJw5+VPwkIvE6VSlMIB7jg93mGzvN2yjheWTePZ+cVPjOaIrgir
 wiT24hkrTHp+ONT8XoS0sDuY+ieyBZp/GCv/YvgE4t0JEkNozMAVWotrXxaICDzZoWP3NNmKLqg
 6He6zwWAl51r3W5R5weGBi6A/FqlHgHZGroXnNi+wolDuN6pQiVAJ7MZ6hboPCbCCRrBQDTdor5
 wEI2+MwlJ/d2f17wxoGmluCewbeYttuVcpUOVwACJKw3g==
Content-Type: multipart/mixed;
 boundary=9e33a130a8a976102a93e296d6408d228e151f7841ca9ee0d777234fd6f3
Mime-Version: 1.0
Subject: read
To: <t@jiangwei.one>
From: "i" <i@jinnrry.com>
Date: Wed, 06 Sep 2023 17:30:00 +0800

--9e33a130a8a976102a93e296d6408d228e151f7841ca9ee0d777234fd6f3
Content-Type: multipart/alternative;
 boundary=54a95f3429f3cdb342383db10293780bed341f8dc20d2f876eb0853e3884

--54a95f3429f3cdb342383db10293780bed341f8dc20d2f876eb0853e3884
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/plain

12 aRead 1sadf
--54a95f3429f3cdb342383db10293780bed341f8dc20d2f876eb0853e3884
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/html

<p>12 aRead 1sadf</p>
--54a95f3429f3cdb342383db10293780bed341f8dc20d2f876eb0853e3884--

--9e33a130a8a976102a93e296d6408d228e151f7841ca9ee0d777234fd6f3--`

	s := Session{
		RemoteAddress: net.TCPAddrFromAddrPort(netip.AddrPortFrom(netip.AddrFrom4([4]byte{}), 25)),
	}

	s.Data(bytes.NewReader([]byte(readEmail)))

}

func TestRuleDelete(t *testing.T) {
	testInit()

	deleteEmail := `DKIM-Signature: a=rsa-sha256; bh=dNtHGqd1NbRj0WSwrJmPsqAcAy3h/4kZK2HFQ0Asld8=;
 c=simple/simple; d=jinnrry.com;
 h=Content-Type:Mime-Version:Subject:To:From:Date; s=default; t=1693992495;
 v=1;
 b=QllU8lqGdoOMaGYp8d13oWytb7+RebqKjq4y8Rs/kOeQxoE8dSEVliK3eBiXidsNTdDtkTqf
 eiwjyRBK92NVCYprdJqLbu9qZ39BC2lk3NXttTSJ1+1ZZ/bGtIW5JIYn2pToED0MqVVkxGFUtl+
 qFmc4mWo5a4Mbij7xaAB3uJtHpBDt7q4Ovr2hiMetQv7YrhZvCt/xrH8Q9YzZ6xzFUL5ekW40eH
 oWElU1GyVBHWCKh31aweyhA+1XLPYojjREQYd4svRqTbSFSsBqFwFIUGdnyJh2WgmF8eucmttAw
 oRhgzyZkHL1jAskKFBpO10SDReyk50Cvc+0kSLj+QcUpg==
Content-Type: multipart/mixed;
 boundary=bdfa9bf94e22e218105281e06bd59bd6df3ce70e71367bf49fbe73301af3
Mime-Version: 1.0
Subject: test
To: <t@jiangwei.one>
From: "i" <i@jinnrry.com>
Date: Wed, 06 Sep 2023 17:28:15 +0800

--bdfa9bf94e22e218105281e06bd59bd6df3ce70e71367bf49fbe73301af3
Content-Type: multipart/alternative;
 boundary=7352524eaae801790245f6bf095460fd1f4e01f5748b4dba48635bf59b04

--7352524eaae801790245f6bf095460fd1f4e01f5748b4dba48635bf59b04
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/plain

Delete
--7352524eaae801790245f6bf095460fd1f4e01f5748b4dba48635bf59b04
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/html

<p>Delete</p>
--7352524eaae801790245f6bf095460fd1f4e01f5748b4dba48635bf59b04--

--bdfa9bf94e22e218105281e06bd59bd6df3ce70e71367bf49fbe73301af3--`

	s := Session{
		RemoteAddress: net.TCPAddrFromAddrPort(netip.AddrPortFrom(netip.AddrFrom4([4]byte{}), 25)),
	}

	s.Data(bytes.NewReader([]byte(deleteEmail)))

}

func TestRuleMove(t *testing.T) {
	testInit()

	moveEmail := `DKIM-Signature: a=rsa-sha256; bh=YQfG/wlHGhky6FNmpIwgDYDOc/uyivdBv+9S02Z04xY=;
 c=simple/simple; d=jinnrry.com;
 h=Content-Type:Mime-Version:Subject:To:From:Date; s=default; t=1693992542;
 v=1;
 b=IhxswOCq8I7CmCas1EMp+n8loR7illqlF0IJC6eN1+OLjI/E5BPzpP4HWkyqaAkd0Vn9i+Bn
 MVb5kNHZ2S7qt0rqAAc6Atc0i9WpLEI3Cng+VDn+difcMZlJSAkhLLn2sUsS4Fzqqo3Cbw62qSO
 TgnWRmlj9aM+5xfGcl/76WOvQQpahJbGg6Go51kFMeHVom/VeGKIgFBCeMe37T/LS03c3pAV8gA
 i6Zy3GYE57W/qU3oCzaGeS3n5zom/i74H4VipiVIMX/OBNYhdHWrP8vyjvzLFpJlXp6RvzcRl0P
 ytyiCZfE8G7fAFntp20LW70Y5Xgqqczk1jR578UDczVoA==
Content-Type: multipart/mixed;
 boundary=c84d60b253aa6caee345c73e717ad59b1975448bbdfad7a23ac4d76e022d
Mime-Version: 1.0
Subject: Move
To: <t@jiangwei.one>
From: "i" <i@jinnrry.com>
Date: Wed, 06 Sep 2023 17:29:02 +0800

--c84d60b253aa6caee345c73e717ad59b1975448bbdfad7a23ac4d76e022d
Content-Type: multipart/alternative;
 boundary=a69985ebcf3c1c44d6e69e5a29c1044743cd9e44d4bc9bb6886f83a73966

--a69985ebcf3c1c44d6e69e5a29c1044743cd9e44d4bc9bb6886f83a73966
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/plain

MOVE move Move
--a69985ebcf3c1c44d6e69e5a29c1044743cd9e44d4bc9bb6886f83a73966
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Content-Type: text/html

<p>MOVE move Move</p>
--a69985ebcf3c1c44d6e69e5a29c1044743cd9e44d4bc9bb6886f83a73966--

--c84d60b253aa6caee345c73e717ad59b1975448bbdfad7a23ac4d76e022d--`

	s := Session{
		RemoteAddress: net.TCPAddrFromAddrPort(netip.AddrPortFrom(netip.AddrFrom4([4]byte{}), 25)),
	}

	s.Data(bytes.NewReader([]byte(moveEmail)))
}
