filetime_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package mstypes
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "github.com/stretchr/testify/assert"
  6. "gopkg.in/jcmturner/rpc.v1/ndr"
  7. "testing"
  8. "time"
  9. )
  10. const TestNDRHeader = "01100800cccccccca00400000000000000000200"
  11. func TestFileTime(t *testing.T) {
  12. t.Parallel()
  13. //2007-02-22 17:00:01.6382155
  14. tt := time.Date(2007, 2, 22, 17, 0, 1, 638215500, time.UTC)
  15. ft := GetFileTime(tt)
  16. assert.Equal(t, tt.Unix(), ft.Unix(), "Unix epoch time not as expected")
  17. assert.Equal(t, int64(128166372016382155), ft.MSEpoch(), "MSEpoch not as expected")
  18. assert.Equal(t, tt, ft.Time(), "Golang time object returned from FileTime not as expected")
  19. }
  20. func TestDecodeFileTime(t *testing.T) {
  21. var tests = []struct {
  22. Hex string
  23. UnixNano int64
  24. }{
  25. {"d186660f656ac601", 1146188570925640100},
  26. {"17d439fe784ac601", 1142678694837147900},
  27. {"1794a328424bc601", 1142765094837147900},
  28. {"175424977a81c601", 1148726694837147900},
  29. {"058e4fdd80c6d201", 1494085991825766900},
  30. {"cc27969c39c6d201", 1494055388968750000},
  31. {"cce7ffc602c7d201", 1494141788968750000},
  32. {"c30bcc79e444d301", 1507982621052409900},
  33. {"c764125a0842d301", 1507668176220282300},
  34. {"c7247c84d142d301", 1507754576220282300},
  35. }
  36. for i, test := range tests {
  37. a := new(FileTime)
  38. hexStr := TestNDRHeader + test.Hex
  39. b, _ := hex.DecodeString(hexStr)
  40. dec := ndr.NewDecoder(bytes.NewReader(b))
  41. err := dec.Decode(a)
  42. if err != nil {
  43. t.Fatalf("test %d: %v", i+1, err)
  44. }
  45. assert.Equal(t, test.UnixNano, a.Time().UnixNano(), "Time value not as expected for test: %d", i+1)
  46. }
  47. }