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 durpb "github.com/golang/protobuf/ptypes/duration" 41) 42 43const ( 44 minGoSeconds = math.MinInt64 / int64(1e9) 45 maxGoSeconds = math.MaxInt64 / int64(1e9) 46) 47 48var durationTests = []struct { 49 proto *durpb.Duration 50 isValid bool 51 inRange bool 52 dur time.Duration 53}{ 54 // The zero duration. 55 {&durpb.Duration{Seconds: 0, Nanos: 0}, true, true, 0}, 56 // Some ordinary non-zero durations. 57 {&durpb.Duration{Seconds: 100, Nanos: 0}, true, true, 100 * time.Second}, 58 {&durpb.Duration{Seconds: -100, Nanos: 0}, true, true, -100 * time.Second}, 59 {&durpb.Duration{Seconds: 100, Nanos: 987}, true, true, 100*time.Second + 987}, 60 {&durpb.Duration{Seconds: -100, Nanos: -987}, true, true, -(100*time.Second + 987)}, 61 // The largest duration representable in Go. 62 {&durpb.Duration{Seconds: maxGoSeconds, Nanos: int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, true, math.MaxInt64}, 63 // The smallest duration representable in Go. 64 {&durpb.Duration{Seconds: minGoSeconds, Nanos: int32(math.MinInt64 - 1e9*minGoSeconds)}, true, true, math.MinInt64}, 65 {nil, false, false, 0}, 66 {&durpb.Duration{Seconds: -100, Nanos: 987}, false, false, 0}, 67 {&durpb.Duration{Seconds: 100, Nanos: -987}, false, false, 0}, 68 {&durpb.Duration{Seconds: math.MinInt64, Nanos: 0}, false, false, 0}, 69 {&durpb.Duration{Seconds: math.MaxInt64, Nanos: 0}, false, false, 0}, 70 // The largest valid duration. 71 {&durpb.Duration{Seconds: maxSeconds, Nanos: 1e9 - 1}, true, false, 0}, 72 // The smallest valid duration. 73 {&durpb.Duration{Seconds: minSeconds, Nanos: -(1e9 - 1)}, true, false, 0}, 74 // The smallest invalid duration above the valid range. 75 {&durpb.Duration{Seconds: maxSeconds + 1, Nanos: 0}, false, false, 0}, 76 // The largest invalid duration below the valid range. 77 {&durpb.Duration{Seconds: minSeconds - 1, Nanos: -(1e9 - 1)}, false, false, 0}, 78 // One nanosecond past the largest duration representable in Go. 79 {&durpb.Duration{Seconds: maxGoSeconds, Nanos: int32(math.MaxInt64-1e9*maxGoSeconds) + 1}, true, false, 0}, 80 // One nanosecond past the smallest duration representable in Go. 81 {&durpb.Duration{Seconds: minGoSeconds, Nanos: int32(math.MinInt64-1e9*minGoSeconds) - 1}, true, false, 0}, 82 // One second past the largest duration representable in Go. 83 {&durpb.Duration{Seconds: maxGoSeconds + 1, Nanos: int32(math.MaxInt64 - 1e9*maxGoSeconds)}, true, false, 0}, 84 // One second past the smallest duration representable in Go. 85 {&durpb.Duration{Seconds: minGoSeconds - 1, Nanos: int32(math.MinInt64 - 1e9*minGoSeconds)}, true, false, 0}, 86} 87 88func TestValidateDuration(t *testing.T) { 89 for _, test := range durationTests { 90 err := validateDuration(test.proto) 91 gotValid := (err == nil) 92 if gotValid != test.isValid { 93 t.Errorf("validateDuration(%v) = %t, want %t", test.proto, gotValid, test.isValid) 94 } 95 } 96} 97 98func TestDuration(t *testing.T) { 99 for _, test := range durationTests { 100 got, err := Duration(test.proto) 101 gotOK := (err == nil) 102 wantOK := test.isValid && test.inRange 103 if gotOK != wantOK { 104 t.Errorf("Duration(%v) ok = %t, want %t", test.proto, gotOK, wantOK) 105 } 106 if err == nil && got != test.dur { 107 t.Errorf("Duration(%v) = %v, want %v", test.proto, got, test.dur) 108 } 109 } 110} 111 112func TestDurationProto(t *testing.T) { 113 for _, test := range durationTests { 114 if test.isValid && test.inRange { 115 got := DurationProto(test.dur) 116 if !proto.Equal(got, test.proto) { 117 t.Errorf("DurationProto(%v) = %v, want %v", test.dur, got, test.proto) 118 } 119 } 120 } 121} 122