1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
package ics
import (
"github.com/stretchr/testify/assert"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
"unicode/utf8"
)
func TestTimeParsing(t *testing.T) {
calFile, err := os.OpenFile("./testdata/timeparsing.ics", os.O_RDONLY, 0400)
if err != nil {
t.Errorf("read file: %v", err)
}
cal, err := ParseCalendar(calFile)
if err != nil {
t.Errorf("parse calendar: %v", err)
}
cphLoc, locErr := time.LoadLocation("Europe/Copenhagen")
if locErr != nil {
t.Errorf("could not load location")
}
var tests = []struct {
name string
uid string
start time.Time
end time.Time
allDayStart time.Time
allDayEnd time.Time
}{
{
"FORM 1",
"be7c9690-d42a-40ef-b82f-1634dc5033b4",
time.Date(1998, 1, 18, 23, 0, 0, 0, time.Local),
time.Date(1998, 1, 19, 23, 0, 0, 0, time.Local),
time.Date(1998, 1, 18, 0, 0, 0, 0, time.Local),
time.Date(1998, 1, 19, 0, 0, 0, 0, time.Local),
},
{
"FORM 2",
"53634aed-1b7d-4d85-aa38-ede76a2e4fe3",
time.Date(2022, 1, 22, 17, 0, 0, 0, time.UTC),
time.Date(2022, 1, 22, 20, 0, 0, 0, time.UTC),
time.Date(2022, 1, 22, 0, 0, 0, 0, time.UTC),
time.Date(2022, 1, 22, 0, 0, 0, 0, time.UTC),
},
{
"FORM 3",
"269cf715-4e14-4a10-8753-f2feeb9d060e",
time.Date(2021, 12, 7, 14, 0, 0, 0, cphLoc),
time.Date(2021, 12, 7, 15, 0, 0, 0, cphLoc),
time.Date(2021, 12, 7, 0, 0, 0, 0, cphLoc),
time.Date(2021, 12, 7, 0, 0, 0, 0, cphLoc),
},
{
"Unknown local date, with 'VALUE'",
"fb54680e-7f69-46d3-9632-00aed2469f7b",
time.Date(2021, 6, 27, 0, 0, 0, 0, time.Local),
time.Date(2021, 6, 28, 0, 0, 0, 0, time.Local),
time.Date(2021, 6, 27, 0, 0, 0, 0, time.Local),
time.Date(2021, 6, 28, 0, 0, 0, 0, time.Local),
},
{
"Unknown UTC date",
"62475ad0-a76c-4fab-8e68-f99209afcca6",
time.Date(2021, 5, 27, 0, 0, 0, 0, time.UTC),
time.Date(2021, 5, 28, 0, 0, 0, 0, time.UTC),
time.Date(2021, 5, 27, 0, 0, 0, 0, time.UTC),
time.Date(2021, 5, 28, 0, 0, 0, 0, time.UTC),
},
}
assertTime := func(evtUid string, exp time.Time, timeFunc func() (given time.Time, err error)) {
given, err := timeFunc()
if err == nil {
if !exp.Equal(given) {
t.Errorf("no match on '%s', expected=%v != given=%v", evtUid, exp, given)
}
} else {
t.Errorf("get time on uid '%s', %v", evtUid, err)
}
}
eventMap := map[string]*VEvent{}
for _, e := range cal.Events() {
eventMap[e.Id()] = e
}
for _, tt := range tests {
t.Run(tt.uid, func(t *testing.T) {
evt, ok := eventMap[tt.uid]
if !ok {
t.Errorf("Test %#v, event UID not found, %s", tt.name, tt.uid)
return
}
assertTime(tt.uid, tt.start, evt.GetStartAt)
assertTime(tt.uid, tt.end, evt.GetEndAt)
assertTime(tt.uid, tt.allDayStart, evt.GetAllDayStartAt)
assertTime(tt.uid, tt.allDayEnd, evt.GetAllDayEndAt)
})
}
}
func TestCalendarStream(t *testing.T) {
i := `
ATTENDEE;RSVP=TRUE;ROLE=REQ-PARTICIPANT;CUTYPE=GROUP:
mailto:employee-A@example.com
DESCRIPTION:Project XYZ Review Meeting
CATEGORIES:MEETING
CLASS:PUBLIC
`
expected := []ContentLine{
ContentLine("ATTENDEE;RSVP=TRUE;ROLE=REQ-PARTICIPANT;CUTYPE=GROUP:mailto:employee-A@example.com"),
ContentLine("DESCRIPTION:Project XYZ Review Meeting"),
ContentLine("CATEGORIES:MEETING"),
ContentLine("CLASS:PUBLIC"),
}
c := NewCalendarStream(strings.NewReader(i))
cont := true
for i := 0; cont; i++ {
l, err := c.ReadLine()
if err != nil {
switch err {
case io.EOF:
cont = false
default:
t.Logf("Unknown error; %v", err)
t.Fail()
return
}
}
if l == nil {
if err == io.EOF && i == len(expected) {
cont = false
} else {
t.Logf("Nil response...")
t.Fail()
return
}
}
if i < len(expected) {
if string(*l) != string(expected[i]) {
t.Logf("Got %s expected %s", string(*l), string(expected[i]))
t.Fail()
}
} else if l != nil {
t.Logf("Larger than expected")
t.Fail()
return
}
}
}
func TestRfc5545Sec4Examples(t *testing.T) {
rnReplace := regexp.MustCompile("\r?\n")
err := filepath.Walk("./testdata/rfc5545sec4/", func(path string, info os.FileInfo, _ error) error {
if info.IsDir() {
return nil
}
inputBytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}
input := rnReplace.ReplaceAllString(string(inputBytes), "\r\n")
structure, err := ParseCalendar(strings.NewReader(input))
if assert.Nil(t, err, path) {
// This should fail as the sample data doesn't conform to https://tools.ietf.org/html/rfc5545#page-45
// Probably due to RFC width guides
assert.NotNil(t, structure)
output := structure.Serialize()
assert.NotEqual(t, input, output)
}
return nil
})
if err != nil {
t.Fatalf("cannot read test directory: %v", err)
}
}
func TestLineFolding(t *testing.T) {
testCases := []struct {
name string
input string
output string
}{
{
name: "fold lines at nearest space",
input: "some really long line with spaces to fold on and the line should fold",
output: `BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//arran4//Golang ICS Library
DESCRIPTION:some really long line with spaces to fold on and the line
should fold
END:VCALENDAR
`,
},
{
name: "fold lines if no space",
input: "somereallylonglinewithnospacestofoldonandthelineshouldfoldtothenextline",
output: `BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//arran4//Golang ICS Library
DESCRIPTION:somereallylonglinewithnospacestofoldonandthelineshouldfoldtothe
nextline
END:VCALENDAR
`,
},
{
name: "fold lines at nearest space",
input: "some really long line with spaces howeverthelastpartofthelineisactuallytoolongtofitonsowehavetofoldpartwaythrough",
output: `BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//arran4//Golang ICS Library
DESCRIPTION:some really long line with spaces
howeverthelastpartofthelineisactuallytoolongtofitonsowehavetofoldpartwayt
hrough
END:VCALENDAR
`,
},
{
name: "75 chars line should not fold",
input: " this line is exactly 75 characters long with the property name",
output: `BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//arran4//Golang ICS Library
DESCRIPTION: this line is exactly 75 characters long with the property name
END:VCALENDAR
`,
},
{
name: "runes should not be split",
// the 75 bytes mark is in the middle of a rune
input: "éé界世界世界世界世界世界世界世界世界世界世界世界世界",
output: `BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//arran4//Golang ICS Library
DESCRIPTION:éé界世界世界世界世界世界世界世界世界世界
世界世界世界
END:VCALENDAR
`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
c := NewCalendar()
c.SetDescription(tc.input)
// we're not testing for encoding here so lets make the actual output line breaks == expected line breaks
text := strings.Replace(c.Serialize(), "\r\n", "\n", -1)
assert.Equal(t, tc.output, text)
assert.True(t, utf8.ValidString(text), "Serialized .ics calendar isn't valid UTF-8 string")
})
}
}
func TestParseCalendar(t *testing.T) {
testCases := []struct {
name string
input string
output string
}{
{
name: "test custom fields in calendar",
input: `BEGIN:VCALENDAR
VERSION:2.0
X-CUSTOM-FIELD:test
PRODID:-//arran4//Golang ICS Library
DESCRIPTION:test
END:VCALENDAR
`,
output: `BEGIN:VCALENDAR
VERSION:2.0
X-CUSTOM-FIELD:test
PRODID:-//arran4//Golang ICS Library
DESCRIPTION:test
END:VCALENDAR
`,
},
{
name: "test multiline description - multiple custom fields suppress",
input: `BEGIN:VCALENDAR
VERSION:2.0
X-CUSTOM-FIELD:test
PRODID:-//arran4//Golang ICS Library
DESCRIPTION:test
BEGIN:VEVENT
DESCRIPTION:blablablablablablablablablablablablablablablabl
testtesttest
CLASS:PUBLIC
END:VEVENT
END:VCALENDAR
`,
output: `BEGIN:VCALENDAR
VERSION:2.0
X-CUSTOM-FIELD:test
PRODID:-//arran4//Golang ICS Library
DESCRIPTION:test
BEGIN:VEVENT
DESCRIPTION:blablablablablablablablablablablablablablablabltesttesttest
CLASS:PUBLIC
END:VEVENT
END:VCALENDAR
`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
c, err := ParseCalendar(strings.NewReader(tc.input))
if !assert.NoError(t, err) {
return
}
// we're not testing for encoding here so lets make the actual output line breaks == expected line breaks
text := strings.Replace(c.Serialize(), "\r\n", "\n", -1)
if !assert.Equal(t, tc.output, text) {
return
}
})
}
}
func TestIssue52(t *testing.T) {
err := filepath.Walk("./testdata/issue52/", func(path string, info os.FileInfo, _ error) error {
if info.IsDir() {
return nil
}
_, fn := filepath.Split(path)
t.Run(fn, func(t *testing.T) {
f, err := os.Open(path)
if err != nil {
t.Fatalf("Error reading file: %s", err)
}
defer f.Close()
if _, err := ParseCalendar(f); err != nil {
t.Fatalf("Error parsing file: %s", err)
}
})
return nil
})
if err != nil {
t.Fatalf("cannot read test directory: %v", err)
}
}
|