Skip to content

Commit

Permalink
Merge pull request #9 from smlx/fix-timestamp
Browse files Browse the repository at this point in the history
fix: timestamp parsing
  • Loading branch information
smlx authored Nov 26, 2023
2 parents 4ec8892 + afd698b commit 1449901
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
3 changes: 1 addition & 2 deletions mitm/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ func handleTimeSyncRespPacket(
return fmt.Errorf("couldn't parse time sync response: %v", err)
}
log.Debug("inbound time sync response",
slog.Any("cleartext", cleartext),
slog.Time("response", tsResp.Timestamp.Time()))
slog.Time("responseTimestamp", tsResp.Timestamp.Time()))
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion mitm/mitm.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Timestamp [6]byte
func (t *Timestamp) Time() time.Time {
return time.Date(2000+int(t[0]), time.Month(t[1]),
int(t[2]), int(t[3]), int(t[4]), int(t[5]), 0,
time.FixedZone("+08", 8))
time.FixedZone("+08", 8*60*60))
}

// handlePacketFunc is the type implemented by handle(In|Out)boundPacket.
Expand Down
31 changes: 31 additions & 0 deletions mitm/mitm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package mitm

import (
"testing"
"time"

"github.com/alecthomas/assert/v2"
)

func TestTimestamp(t *testing.T) {
chinaStandardTime, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
t.Error(err)
}
var testCases = map[string]struct {
input [6]byte
expect time.Time
}{
"timestamp_1": {
input: [6]byte{0x17, 0x0b, 0x1a, 0x16, 0x04, 0x21},
expect: time.Date(2023, time.November, 26, 22, 04, 33, 0, chinaStandardTime),
},
}
for name, tc := range testCases {
t.Run(name, func(tt *testing.T) {
ts := Timestamp(tc.input)
assert.True(tt, tc.expect.Equal(ts.Time()),
"expected: %v, got %v", tc.expect, ts.Time())
})
}
}
2 changes: 1 addition & 1 deletion mitm/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func handleTimeSyncPacket(
log.Debug("couldn't parse time sync", slog.Any("cleartext", cleartext))
return fmt.Errorf("couldn't parse time sync: %v", err)
}
log.Debug("outbound time sync", slog.Any("cleartext", cleartext))
log.Debug("outbound time sync")
timeSyncPacketsTotal.With(prometheus.Labels{
"model": model,
"serial": string(envelope.DeviceSerial[:]),
Expand Down

0 comments on commit 1449901

Please sign in to comment.