<commit_before>package sdp

import (
	"fmt"
	"net"
	"testing"
	"time"
)

func shouldDecode(tb testing.TB, s Session, name string) {
	buf := make([]byte, 0, 1024)
	tData := loadData(tb, name)
	buf = s.AppendTo(buf)
	if !byteSliceEqual(tData, buf) {
		fmt.Println(tData)
		fmt.Println(buf)
		fmt.Println(string(tData))
		fmt.Println(string(buf))
		tb.Errorf("not equal")
	}
	newSesssion, err := DecodeSession(buf, nil)
	if err != nil {
		tb.Errorf("decode error: %v", err)
	}
	if !newSesssion.Equal(s) {
		tb.Error("sessions does not equal")
	}
}

func shouldDecodeExpS(tb testing.TB, s Session, name string) {
	shouldDecode(tb, s, "spd_session_ex_"+name)
}

func TestSession_AddVersion(t *testing.T) {
	shouldDecode(t, new(Session).AddVersion(1337), "spd_session_ex2")
}

func TestSession_AddPhoneEmail(t *testing.T) {
	s := new(Session).AddPhone("+1 617 555-6011")
	s = s.AddEmail("<EMAIL> (<NAME>)")
	shouldDecode(t, s, "spd_session_ex3")
}

func TestSession_AddConnectionDataIP(t *testing.T) {
	s := new(Session).
		AddConnectionDataIP(net.ParseIP("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b")).
		AddConnectionData(ConnectionData{
			IP:  net.ParseIP("172.16.58.3"),
			TTL: 127}).
		AddConnectionData(ConnectionData{
			IP:          net.ParseIP("172.16.58.3"),
			AddressType: "IP4",
			NetworkType: "IN",
			TTL:         95,
			Addresses:   4,
		})
	shouldDecodeExpS(t, s, "ip")
}

func TestSession_AddOrigin(t *testing.T) {
	s := new(Session).AddOrigin(Origin{
		Username:       "jdoe",
		SessionID:      2890844526,
		SessionVersion: 2890842807,
		IP:             net.ParseIP("10.47.16.5"),
	})
	s = s.AddOrigin(Origin{
		Username:       "jdoe",
		SessionID:      2890844527,
		SessionVersion: 2890842807,
		IP:             net.ParseIP("fdf8:f53e:61e4::18"),
	})
	shouldDecodeExpS(t, s, "origin")
}

func TestSession_AddTiming(t *testing.T) {
	s := new(Session).
		AddTiming(time.Time{}, time.Time{}).
		AddTiming(time.Time{}, time.Unix(833473619, 0))
	shouldDecodeExpS(t, s, "timing")
}

func TestSession_AddAttribute(t *testing.T) {
	s := new(Session).
		AddFlag("recvonly").
		AddAttribute("anotherflag").
		AddAttribute("orient", "landscape").
		AddAttribute("rtpmap", "96", "L8/8000")
	shouldDecodeExpS(t, s, "attributes")
}

func TestSession_AddBandwidth(t *testing.T) {
	s := new(Session).
		AddBandwidth(BandwidthConferenceTotal, 154798).
		AddBandwidth(BandwidthApplicationSpecific, 66781)
	shouldDecodeExpS(t, s, "bandwidth")
}

func TestSession_AddSessionName(t *testing.T) {
	s := new(Session).AddSessionName("CyConf")
	shouldDecodeExpS(t, s, "name")
}

func TestSession_AddSessionInfo(t *testing.T) {
	s := new(Session).AddSessionInfo("Info goes here")
	shouldDecodeExpS(t, s, "info")
}

func TestSession_AddURI(t *testing.T) {
	s := new(Session).AddURI("http://cydev.ru")
	shouldDecodeExpS(t, s, "uri")
}

func TestSession_AddRepeatTimes(t *testing.T) {
	s := new(Session).
		AddRepeatTimes(
			time.Second*604800,
			time.Second*3600,
			0,
			time.Second*90000,
		)
	shouldDecodeExpS(t, s, "repeat")
}

func TestSession_AddMediaDescription(t *testing.T) {
	s := new(Session).AddMediaDescription(MediaDescription{
		Type:        "video",
		Port:        49170,
		PortsNumber: 2,
		Protocol:    "RTP/AVP",
		Format:      "31",
	}).AddMediaDescription(MediaDescription{
		Type:     "audio",
		Port:     49170,
		Protocol: "RTP/AVP",
		Format:   "555",
	})
	shouldDecodeExpS(t, s, "media")
}

func TestSession_AddEncryptionKey(t *testing.T) {
	s := new(Session).AddEncryptionKey("clear", "<KEY>").
		AddEncryptionMethod("prompt")
	shouldDecodeExpS(t, s, "keys")
}

func TestNTP(t *testing.T) {
	var ntpTable = []struct {
		in  uint64
		out time.Time
	}{
		{3549086042, time.Unix(1340097242, 0)},
		{0, time.Time{}},
	}
	for _, tt := range ntpTable {
		outReal := NTPToTime(tt.in)
		if tt.out != outReal {
			t.Errorf("%v != %v", tt.out, outReal)
		}
		outNTP := TimeToNTP(outReal)
		if outNTP != tt.in {
			t.Errorf("%d != %d", outNTP, tt.in)
		}
	}
}<commit_msg>session: add ex1 encoding test<commit_after>package sdp

import (
	"fmt"
	"net"
	"testing"
	"time"
)

func shouldDecode(tb testing.TB, s Session, name string) {
	buf := make([]byte, 0, 1024)
	tData := loadData(tb, name)
	buf = s.AppendTo(buf)
	if !byteSliceEqual(tData, buf) {
		fmt.Println(tData)
		fmt.Println(buf)
		fmt.Println(string(tData))
		fmt.Println(string(buf))
		tb.Errorf("not equal")
	}
	newSesssion, err := DecodeSession(buf, nil)
	if err != nil {
		tb.Errorf("decode error: %v", err)
	}
	if !newSesssion.Equal(s) {
		tb.Error("sessions does not equal")
	}
}

func shouldDecodeExpS(tb testing.TB, s Session, name string) {
	shouldDecode(tb, s, "spd_session_ex_"+name)
}

func TestSession_AddVersion(t *testing.T) {
	shouldDecode(t, new(Session).AddVersion(1337), "spd_session_ex2")
}

func TestSession_AddPhoneEmail(t *testing.T) {
	s := new(Session).AddPhone("+1 617 555-6011")
	s = s.AddEmail("<EMAIL> (<NAME>)")
	shouldDecode(t, s, "spd_session_ex3")
}

func TestSession_AddConnectionDataIP(t *testing.T) {
	s := new(Session).
		AddConnectionDataIP(net.ParseIP("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b")).
		AddConnectionData(ConnectionData{
			IP:  net.ParseIP("172.16.58.3"),
			TTL: 127}).
		AddConnectionData(ConnectionData{
			IP:          net.ParseIP("172.16.58.3"),
			AddressType: "IP4",
			NetworkType: "IN",
			TTL:         95,
			Addresses:   4,
		})
	shouldDecodeExpS(t, s, "ip")
}

func TestSession_AddOrigin(t *testing.T) {
	s := new(Session).AddOrigin(Origin{
		Username:       "jdoe",
		SessionID:      2890844526,
		SessionVersion: 2890842807,
		IP:             net.ParseIP("10.47.16.5"),
	})
	s = s.AddOrigin(Origin{
		Username:       "jdoe",
		SessionID:      2890844527,
		SessionVersion: 2890842807,
		IP:             net.ParseIP("fdf8:f53e:61e4::18"),
	})
	shouldDecodeExpS(t, s, "origin")
}

func TestSession_AddTiming(t *testing.T) {
	s := new(Session).
		AddTiming(time.Time{}, time.Time{}).
		AddTiming(time.Time{}, time.Unix(833473619, 0))
	shouldDecodeExpS(t, s, "timing")
}

func TestSession_AddAttribute(t *testing.T) {
	s := new(Session).
		AddFlag("recvonly").
		AddAttribute("anotherflag").
		AddAttribute("orient", "landscape").
		AddAttribute("rtpmap", "96", "L8/8000")
	shouldDecodeExpS(t, s, "attributes")
}

func TestSession_AddBandwidth(t *testing.T) {
	s := new(Session).
		AddBandwidth(BandwidthConferenceTotal, 154798).
		AddBandwidth(BandwidthApplicationSpecific, 66781)
	shouldDecodeExpS(t, s, "bandwidth")
}

func TestSession_AddSessionName(t *testing.T) {
	s := new(Session).AddSessionName("CyConf")
	shouldDecodeExpS(t, s, "name")
}

func TestSession_AddSessionInfo(t *testing.T) {
	s := new(Session).AddSessionInfo("Info goes here")
	shouldDecodeExpS(t, s, "info")
}

func TestSession_AddURI(t *testing.T) {
	s := new(Session).AddURI("http://cydev.ru")
	shouldDecodeExpS(t, s, "uri")
}

func TestSession_AddRepeatTimes(t *testing.T) {
	s := new(Session).
		AddRepeatTimes(
			time.Second*604800,
			time.Second*3600,
			0,
			time.Second*90000,
		)
	shouldDecodeExpS(t, s, "repeat")
}

func TestSession_AddMediaDescription(t *testing.T) {
	s := new(Session).AddMediaDescription(MediaDescription{
		Type:        "video",
		Port:        49170,
		PortsNumber: 2,
		Protocol:    "RTP/AVP",
		Format:      "31",
	}).AddMediaDescription(MediaDescription{
		Type:     "audio",
		Port:     49170,
		Protocol: "RTP/AVP",
		Format:   "555",
	})
	shouldDecodeExpS(t, s, "media")
}

func TestSession_AddEncryptionKey(t *testing.T) {
	s := new(Session).AddEncryptionKey("clear", "<KEY>").
		AddEncryptionMethod("prompt")
	shouldDecodeExpS(t, s, "keys")
}

func TestSession_EX1(t *testing.T) {
	/*
		v=0
		o=jdoe 2890844526 2890842807 IN IP4 10.47.16.5
		s=SDP Seminar
		i=A Seminar on the session description protocol
		u=http://www.example.com/seminars/sdp.pdf
		e=<EMAIL> (<NAME>)
		c=IN IP4 192.168.3.11/127
		t=2873397496 2873404696
		a=recvonly
		m=audio 49170 RTP/AVP 0
		m=video 51372 RTP/AVP 99
		a=rtpmap:99 h263-1998/90000
	*/

	s := new(Session).
		AddVersion(0).
		AddOrigin(Origin{
			Username:       "jdoe",
			SessionID:      2890844526,
			SessionVersion: 2890842807,
			IP:             net.ParseIP("10.47.16.5"),
		}).
		AddSessionName("SDP Seminar").
		AddSessionInfo("A Seminar on the session description protocol").
		AddURI("http://www.example.com/seminars/sdp.pdf").
		AddEmail("<EMAIL> (<NAME>)").
		AddConnectionData(ConnectionData{
			IP:  net.ParseIP("192.168.3.11"),
			TTL: 127,
		}).
		AddTiming(NTPToTime(2873397496), NTPToTime(2873404696)).
		AddFlag("recvonly").
		AddMediaDescription(MediaDescription{
			Type:     "audio",
			Port:     49170,
			Protocol: "RTP/AVP",
			Format:   "0",
		}).
		AddMediaDescription(MediaDescription{
			Type:     "video",
			Port:     51372,
			Protocol: "RTP/AVP",
			Format:   "99",
		}).
		AddAttribute("rtpmap", "99", "h263-1998/90000")
	shouldDecode(t, s, "sdp_session_ex1")
}

func TestNTP(t *testing.T) {
	var ntpTable = []struct {
		in  uint64
		out time.Time
	}{
		{3549086042, time.Unix(1340097242, 0)},
		{0, time.Time{}},
	}
	for _, tt := range ntpTable {
		outReal := NTPToTime(tt.in)
		if tt.out != outReal {
			t.Errorf("%v != %v", tt.out, outReal)
		}
		outNTP := TimeToNTP(outReal)
		if outNTP != tt.in {
			t.Errorf("%d != %d", outNTP, tt.in)
		}
	}
}
{'fraction_non_alphanumeric': 0.16556934700485698, 'fraction_numerical': 0.06314085267134377, 'mean_word_length': 4.102422907488987, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 10, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '55111', 'n_tokens_mistral': 4266, 'n_tokens_neox': 3708, 'n_words': 662}