1// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2016 The Go Authors.  All rights reserved.
4// https://github.com/golang/protobuf
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10//     * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12//     * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16//     * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32package ptypes
33
34import (
35	"math"
36	"testing"
37	"time"
38
39	"github.com/golang/protobuf/proto"
40	tspb "github.com/golang/protobuf/ptypes/timestamp"
41)
42
43var tests = []struct {
44	ts    *tspb.Timestamp
45	valid bool
46	t     time.Time
47}{
48	// The timestamp representing the Unix epoch date.
49	{&tspb.Timestamp{Seconds: 0, Nanos: 0}, true, utcDate(1970, 1, 1)},
50	// The smallest representable timestamp.
51	{&tspb.Timestamp{Seconds: math.MinInt64, Nanos: math.MinInt32}, false,
52		time.Unix(math.MinInt64, math.MinInt32).UTC()},
53	// The smallest representable timestamp with non-negative nanos.
54	{&tspb.Timestamp{Seconds: math.MinInt64, Nanos: 0}, false, time.Unix(math.MinInt64, 0).UTC()},
55	// The earliest valid timestamp.
56	{&tspb.Timestamp{Seconds: minValidSeconds, Nanos: 0}, true, utcDate(1, 1, 1)},
57	//"0001-01-01T00:00:00Z"},
58	// The largest representable timestamp.
59	{&tspb.Timestamp{Seconds: math.MaxInt64, Nanos: math.MaxInt32}, false,
60		time.Unix(math.MaxInt64, math.MaxInt32).UTC()},
61	// The largest representable timestamp with nanos in range.
62	{&tspb.Timestamp{Seconds: math.MaxInt64, Nanos: 1e9 - 1}, false,
63		time.Unix(math.MaxInt64, 1e9-1).UTC()},
64	// The largest valid timestamp.
65	{&tspb.Timestamp{Seconds: maxValidSeconds - 1, Nanos: 1e9 - 1}, true,
66		time.Date(9999, 12, 31, 23, 59, 59, 1e9-1, time.UTC)},
67	// The smallest invalid timestamp that is larger than the valid range.
68	{&tspb.Timestamp{Seconds: maxValidSeconds, Nanos: 0}, false, time.Unix(maxValidSeconds, 0).UTC()},
69	// A date before the epoch.
70	{&tspb.Timestamp{Seconds: -281836800, Nanos: 0}, true, utcDate(1961, 1, 26)},
71	// A date after the epoch.
72	{&tspb.Timestamp{Seconds: 1296000000, Nanos: 0}, true, utcDate(2011, 1, 26)},
73	// A date after the epoch, in the middle of the day.
74	{&tspb.Timestamp{Seconds: 1296012345, Nanos: 940483}, true,
75		time.Date(2011, 1, 26, 3, 25, 45, 940483, time.UTC)},
76}
77
78func TestValidateTimestamp(t *testing.T) {
79	for _, s := range tests {
80		got := validateTimestamp(s.ts)
81		if (got == nil) != s.valid {
82			t.Errorf("validateTimestamp(%v) = %v, want %v", s.ts, got, s.valid)
83		}
84	}
85}
86
87func TestTimestamp(t *testing.T) {
88	for _, s := range tests {
89		got, err := Timestamp(s.ts)
90		if (err == nil) != s.valid {
91			t.Errorf("Timestamp(%v) error = %v, but valid = %t", s.ts, err, s.valid)
92		} else if s.valid && got != s.t {
93			t.Errorf("Timestamp(%v) = %v, want %v", s.ts, got, s.t)
94		}
95	}
96	// Special case: a nil Timestamp is an error, but returns the 0 Unix time.
97	got, err := Timestamp(nil)
98	want := time.Unix(0, 0).UTC()
99	if got != want {
100		t.Errorf("Timestamp(nil) = %v, want %v", got, want)
101	}
102	if err == nil {
103		t.Errorf("Timestamp(nil) error = nil, expected error")
104	}
105}
106
107func TestTimestampProto(t *testing.T) {
108	for _, s := range tests {
109		got, err := TimestampProto(s.t)
110		if (err == nil) != s.valid {
111			t.Errorf("TimestampProto(%v) error = %v, but valid = %t", s.t, err, s.valid)
112		} else if s.valid && !proto.Equal(got, s.ts) {
113			t.Errorf("TimestampProto(%v) = %v, want %v", s.t, got, s.ts)
114		}
115	}
116	// No corresponding special case here: no time.Time results in a nil Timestamp.
117}
118
119func TestTimestampString(t *testing.T) {
120	for _, test := range []struct {
121		ts   *tspb.Timestamp
122		want string
123	}{
124		// Not much testing needed because presumably time.Format is
125		// well-tested.
126		{&tspb.Timestamp{Seconds: 0, Nanos: 0}, "1970-01-01T00:00:00Z"},
127		{&tspb.Timestamp{Seconds: minValidSeconds - 1, Nanos: 0}, "(timestamp: seconds:-62135596801  before 0001-01-01)"},
128	} {
129		got := TimestampString(test.ts)
130		if got != test.want {
131			t.Errorf("TimestampString(%v) = %q, want %q", test.ts, got, test.want)
132		}
133	}
134}
135
136func utcDate(year, month, day int) time.Time {
137	return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
138}
139
140func TestTimestampNow(t *testing.T) {
141	// Bracket the expected time.
142	before := time.Now()
143	ts := TimestampNow()
144	after := time.Now()
145
146	tm, err := Timestamp(ts)
147	if err != nil {
148		t.Errorf("between %v and %v\nTimestampNow() = %v\nwhich is invalid (%v)", before, after, ts, err)
149	}
150	if tm.Before(before) || tm.After(after) {
151		t.Errorf("between %v and %v\nTimestamp(TimestampNow()) = %v", before, after, tm)
152	}
153}
154