github.com/coreos/mantle@v0.13.0/network/ntp/protocol_test.go (about) 1 // Copyright 2015 CoreOS, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ntp 16 17 import ( 18 "testing" 19 "time" 20 21 "github.com/kylelemons/godebug/pretty" 22 ) 23 24 type testHeader struct { 25 Header 26 Raw []byte 27 } 28 29 var testData = []testHeader{ 30 {Header{ 31 LeapIndicator: LEAP_NOSYNC, 32 VersionNumber: NTPv4, 33 Mode: MODE_CLIENT, 34 Poll: 3, 35 Precision: -6, 36 RootDelay: Short{Seconds: 1}, 37 RootDispersion: Short{Seconds: 1}, 38 TransmitTimestamp: Timestamp{ 39 Seconds: 0xd90b4fca, Fraction: 0x77ce2d4e}, 40 }, []byte{ 41 0xe3, 0x00, 0x03, 0xfa, 0x00, 0x01, 0x00, 0x00, 42 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 46 0xd9, 0x0b, 0x4f, 0xca, 0x77, 0xce, 0x2d, 0x4e, 47 }}, 48 {Header{ 49 LeapIndicator: LEAP_NONE, 50 VersionNumber: NTPv4, 51 Mode: MODE_SERVER, 52 Stratum: 1, 53 Poll: 3, 54 Precision: -29, 55 ReferenceId: [4]byte{'A', 'C', 'T', 'S'}, 56 ReferenceTimestamp: Timestamp{ 57 Seconds: 0xd90b4fbc, Fraction: 0x94e375b0}, 58 OriginTimestamp: Timestamp{ 59 Seconds: 0xd90b4fca, Fraction: 0x77ce2d4e}, 60 ReceiveTimestamp: Timestamp{ 61 Seconds: 0xd90b4fca, Fraction: 0x7e403121}, 62 TransmitTimestamp: Timestamp{ 63 Seconds: 0xd90b4fca, Fraction: 0x7e41298d}, 64 }, []byte{ 65 0x24, 0x01, 0x03, 0xe3, 0x00, 0x00, 0x00, 0x00, 66 0x00, 0x00, 0x00, 0x00, 0x41, 0x43, 0x54, 0x53, 67 0xd9, 0x0b, 0x4f, 0xbc, 0x94, 0xe3, 0x75, 0xb0, 68 0xd9, 0x0b, 0x4f, 0xca, 0x77, 0xce, 0x2d, 0x4e, 69 0xd9, 0x0b, 0x4f, 0xca, 0x7e, 0x40, 0x31, 0x21, 70 0xd9, 0x0b, 0x4f, 0xca, 0x7e, 0x41, 0x29, 0x8d, 71 }}, 72 } 73 74 func TestHeaderMarshal(t *testing.T) { 75 for i, d := range testData { 76 b, err := d.Header.MarshalBinary() 77 if err != nil { 78 t.Errorf("testData[%d] marshal failed: %v", i, err) 79 continue 80 } 81 if diff := pretty.Compare(d.Raw, b); diff != "" { 82 t.Errorf("testData[%d] failed: %v", i, diff) 83 } 84 } 85 } 86 87 func BenchmarkHeaderMarshal(b *testing.B) { 88 h := testData[0].Header 89 for i := 0; i < b.N; i++ { 90 h.MarshalBinary() 91 } 92 } 93 94 func TestHeaderUnmarshal(t *testing.T) { 95 for i, d := range testData { 96 if len(d.Raw) != headerSize { 97 t.Errorf("testData[%d].Raw is invalid", i) 98 } 99 h := Header{} 100 h.UnmarshalBinary(d.Raw) 101 if diff := pretty.Compare(d.Header, h); diff != "" { 102 t.Errorf("testData[%d] failed: %v", i, diff) 103 } 104 } 105 } 106 107 func BenchmarkHeaderUnmarshal(b *testing.B) { 108 h := Header{} 109 d := testData[0].Raw 110 for i := 0; i < b.N; i++ { 111 h.UnmarshalBinary(d) 112 } 113 } 114 115 func TestNow(t *testing.T) { 116 timestamp := Now() 117 nsec := (float64(timestamp.Fraction) / (1 << 32)) * 1e9 118 ntpTime := time.Unix(int64(timestamp.Seconds-JAN_1970), int64(nsec)) 119 if d := time.Since(ntpTime); d > time.Second { 120 t.Errorf("Now() off by %s", d) 121 } 122 } 123 124 func BenchmarkNow(b *testing.B) { 125 for i := 0; i < b.N; i++ { 126 Now() 127 } 128 } 129 130 func TestPrecision(t *testing.T) { 131 precision := Precision() 132 if precision != -20 { 133 t.Errorf("Precision() reported %d", precision) 134 } 135 }