added support for string type
[govpp.git] / vendor / github.com / google / gopacket / layers / ntp_test.go
1 // Copyright 2016 Google, Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree.
6 //
7 //******************************************************************************
8
9 package layers
10
11 import (
12         "crypto/rand"
13         "github.com/google/gopacket"
14         "io"
15         "reflect"
16         "testing"
17 )
18
19 //******************************************************************************
20
21 // checkNTP() uses the ntp.go code to analyse the packet bytes as an NTP UDP
22 // packet and generate an NTP object. It then compares the generated NTP object
23 // with the one provided and throws an error if there is any difference.
24 // The desc argument is output with any failure message to identify the test.
25 func checkNTP(desc string, t *testing.T, packetBytes []byte, pExpectedNTP *NTP) {
26
27         // Analyse the packet bytes, yielding a new packet object p.
28         p := gopacket.NewPacket(packetBytes, LinkTypeEthernet, gopacket.Default)
29         if p.ErrorLayer() != nil {
30                 t.Errorf("Failed to decode packet %s: %v", desc, p.ErrorLayer().Error())
31         }
32
33         // Ensure that the packet analysis yielded the correct set of layers:
34         //    Link Layer        = Ethernet.
35         //    Network Layer     = IPv4.
36         //    Transport Layer   = UDP.
37         //    Application Layer = NTP.
38         checkLayers(p, []gopacket.LayerType{
39                 LayerTypeEthernet,
40                 LayerTypeIPv4,
41                 LayerTypeUDP,
42                 LayerTypeNTP}, t)
43
44         // Select the Application (NTP) layer.
45         pResultNTP, ok := p.ApplicationLayer().(*NTP)
46         if !ok {
47                 t.Error("No NTP layer type found in packet in " + desc + ".")
48         }
49
50         // Compare the generated NTP object with the expected NTP object.
51         if !reflect.DeepEqual(pResultNTP, pExpectedNTP) {
52                 t.Errorf("NTP packet processing failed for packet "+desc+
53                         ":\ngot  :\n%#v\n\nwant :\n%#v\n\n", pResultNTP, pExpectedNTP)
54         }
55         buf := gopacket.NewSerializeBuffer()
56         opts := gopacket.SerializeOptions{}
57         err := pResultNTP.SerializeTo(buf, opts)
58         if err != nil {
59                 t.Error(err)
60         }
61         if !reflect.DeepEqual(pResultNTP.BaseLayer.Contents, buf.Bytes()) {
62                 t.Errorf("NTP packet serialization failed for packet "+desc+
63                         ":\ngot  :\n%x\n\nwant :\n%x\n\n", buf.Bytes(), packetBytes)
64         }
65 }
66
67 //******************************************************************************
68
69 func TestNTPOne(t *testing.T) {
70
71         // This test packet is the first NTP packet in the NTP sample capture
72         // pcap file NTP_sync.pcap on the Wireshark sample captures page:
73         //
74         //    https://wiki.wireshark.org/SampleCaptures
75         //    https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=NTP_sync.pcap
76         var testPacketNTP = []byte{
77                 0x00, 0x0c, 0x41, 0x82, 0xb2, 0x53, 0x00, 0xd0,
78                 0x59, 0x6c, 0x40, 0x4e, 0x08, 0x00, 0x45, 0x00,
79                 0x00, 0x4c, 0x0a, 0x42, 0x00, 0x00, 0x80, 0x11,
80                 0xb5, 0xfa, 0xc0, 0xa8, 0x32, 0x32, 0x43, 0x81,
81                 0x44, 0x09, 0x00, 0x7b, 0x00, 0x7b, 0x00, 0x38,
82                 0xf8, 0xd2, 0xd9, 0x00, 0x0a, 0xfa, 0x00, 0x00,
83                 0x00, 0x00, 0x00, 0x01, 0x02, 0x90, 0x00, 0x00,
84                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87                 0x00, 0x00, 0xc5, 0x02, 0x04, 0xec, 0xec, 0x42,
88                 0xee, 0x92,
89         }
90
91         // Assemble the NTP object that we expect to emerge from this test.
92         pExpectedNTP := &NTP{
93                 BaseLayer: BaseLayer{
94                         Contents: []byte{0xd9, 0x0, 0xa, 0xfa, 0x0, 0x0, 0x0, 0x0, 0x0,
95                                 0x1, 0x2, 0x90, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
96                                 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
97                                 0x0, 0x0, 0x0, 0x0, 0x0, 0xc5, 0x2, 0x4, 0xec, 0xec, 0x42, 0xee, 0x92},
98                         Payload: nil,
99                 },
100                 LeapIndicator:      3,
101                 Version:            3,
102                 Mode:               1,
103                 Stratum:            0,
104                 Poll:               10,
105                 Precision:          -6,
106                 RootDelay:          0,
107                 RootDispersion:     0x10290,
108                 ReferenceID:        0,
109                 ReferenceTimestamp: 0,
110                 OriginTimestamp:    0,
111                 ReceiveTimestamp:   0,
112                 TransmitTimestamp:  0xc50204ecec42ee92,
113                 ExtensionBytes:     []byte{},
114         }
115
116         checkNTP("test01", t, testPacketNTP, pExpectedNTP)
117 }
118
119 //******************************************************************************
120
121 func TestNTPTwo(t *testing.T) {
122
123         // This test packet is packet #18 in the NTP sample capture
124         // pcap file NTP_sync.pcap on the Wireshark sample captures page:
125         //
126         //    https://wiki.wireshark.org/SampleCaptures
127         //    https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=NTP_sync.pcap
128         //
129         // This packet was chosen because it is the first NTP packet after the first
130         // NTP packet that has non-zero timestamps.
131
132         var testPacketNTP = []byte{
133                 0x00, 0xd0, 0x59, 0x6c, 0x40, 0x4e, 0x00, 0x0c,
134                 0x41, 0x82, 0xb2, 0x53, 0x08, 0x00, 0x45, 0x00,
135                 0x00, 0x4c, 0x32, 0x46, 0x40, 0x00, 0x2f, 0x11,
136                 0xa8, 0x18, 0x45, 0x2c, 0x39, 0x3c, 0xc0, 0xa8,
137                 0x32, 0x32, 0x00, 0x7b, 0x00, 0x7b, 0x00, 0x38,
138                 0x09, 0x58, 0x1a, 0x03, 0x0a, 0xee, 0x00, 0x00,
139                 0x1b, 0xf7, 0x00, 0x00, 0x14, 0xec, 0x51, 0xae,
140                 0x80, 0xb7, 0xc5, 0x02, 0x03, 0x4c, 0x8d, 0x0e,
141                 0x66, 0xcb, 0xc5, 0x02, 0x04, 0xec, 0xec, 0x42,
142                 0xee, 0x92, 0xc5, 0x02, 0x04, 0xeb, 0xcf, 0x49,
143                 0x59, 0xe6, 0xc5, 0x02, 0x04, 0xeb, 0xcf, 0x4c,
144                 0x6e, 0x6e,
145         }
146
147         // Assemble the NTP object that we expect to emerge from this test.
148         pExpectedNTP := &NTP{
149                 BaseLayer: BaseLayer{
150                         Contents: []byte{0x1a, 0x03, 0x0a, 0xee, 0x00, 0x00,
151                                 0x1b, 0xf7, 0x00, 0x00, 0x14, 0xec, 0x51, 0xae,
152                                 0x80, 0xb7, 0xc5, 0x02, 0x03, 0x4c, 0x8d, 0x0e,
153                                 0x66, 0xcb, 0xc5, 0x02, 0x04, 0xec, 0xec, 0x42,
154                                 0xee, 0x92, 0xc5, 0x02, 0x04, 0xeb, 0xcf, 0x49,
155                                 0x59, 0xe6, 0xc5, 0x02, 0x04, 0xeb, 0xcf, 0x4c,
156                                 0x6e, 0x6e},
157                         Payload: nil,
158                 },
159                 LeapIndicator:      0,
160                 Version:            3,
161                 Mode:               2,
162                 Stratum:            3,
163                 Poll:               10,
164                 Precision:          -18,
165                 RootDelay:          0x1bf7,
166                 RootDispersion:     0x14ec,
167                 ReferenceID:        0x51ae80b7,
168                 ReferenceTimestamp: 0xc502034c8d0e66cb,
169                 OriginTimestamp:    0xc50204ecec42ee92,
170                 ReceiveTimestamp:   0xc50204ebcf4959e6,
171                 TransmitTimestamp:  0xc50204ebcf4c6e6e,
172                 ExtensionBytes:     []byte{},
173         }
174
175         checkNTP("test02", t, testPacketNTP, pExpectedNTP)
176 }
177
178 //******************************************************************************
179
180 func TestNTPThree(t *testing.T) {
181
182         // This test packet is packet #19 in the NTP sample capture
183         // pcap file NTP_sync.pcap on the Wireshark sample captures page:
184         //
185         //    https://wiki.wireshark.org/SampleCaptures
186         //    https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=NTP_sync.pcap
187
188         var testPacketNTP = []byte{
189                 0x00, 0xd0, 0x59, 0x6c, 0x40, 0x4e, 0x00, 0x0c,
190                 0x41, 0x82, 0xb2, 0x53, 0x08, 0x00, 0x45, 0x00,
191                 0x00, 0x4c, 0x00, 0x00, 0x40, 0x00, 0x30, 0x11,
192                 0x74, 0x65, 0x18, 0x7b, 0xca, 0xe6, 0xc0, 0xa8,
193                 0x32, 0x32, 0x00, 0x7b, 0x00, 0x7b, 0x00, 0x38,
194                 0x44, 0x05, 0x1a, 0x02, 0x0a, 0xec, 0x00, 0x00,
195                 0x07, 0xc3, 0x00, 0x00, 0x2f, 0x80, 0xc6, 0x1e,
196                 0x5c, 0x02, 0xc5, 0x01, 0xf9, 0x95, 0x42, 0x50,
197                 0x82, 0xcf, 0xc5, 0x02, 0x04, 0xec, 0xec, 0x42,
198                 0xee, 0x92, 0xc5, 0x02, 0x04, 0xeb, 0xd2, 0x35,
199                 0x2e, 0xb5, 0xc5, 0x02, 0x04, 0xeb, 0xd2, 0x35,
200                 0xd6, 0x7b,
201         }
202
203         // Assemble the NTP object that we expect to emerge from this test.
204         pExpectedNTP := &NTP{
205                 BaseLayer: BaseLayer{
206                         Contents: []byte{0x1a, 0x02, 0x0a, 0xec, 0x00, 0x00,
207                                 0x07, 0xc3, 0x00, 0x00, 0x2f, 0x80, 0xc6, 0x1e,
208                                 0x5c, 0x02, 0xc5, 0x01, 0xf9, 0x95, 0x42, 0x50,
209                                 0x82, 0xcf, 0xc5, 0x02, 0x04, 0xec, 0xec, 0x42,
210                                 0xee, 0x92, 0xc5, 0x02, 0x04, 0xeb, 0xd2, 0x35,
211                                 0x2e, 0xb5, 0xc5, 0x02, 0x04, 0xeb, 0xd2, 0x35,
212                                 0xd6, 0x7b},
213                         Payload: nil,
214                 },
215                 LeapIndicator:      0,
216                 Version:            3,
217                 Mode:               2,
218                 Stratum:            2,
219                 Poll:               10,
220                 Precision:          -20,
221                 RootDelay:          0x7c3,
222                 RootDispersion:     0x2f80,
223                 ReferenceID:        0xc61e5c02,
224                 ReferenceTimestamp: 0xc501f995425082cf,
225                 OriginTimestamp:    0xc50204ecec42ee92,
226                 ReceiveTimestamp:   0xc50204ebd2352eb5,
227                 TransmitTimestamp:  0xc50204ebd235d67b,
228                 ExtensionBytes:     []byte{},
229         }
230
231         checkNTP("test03", t, testPacketNTP, pExpectedNTP)
232 }
233
234 //******************************************************************************
235
236 // TestNTPIsomorphism tests whether random data gets parsed into NTP layer and
237 // gets serialized back from it to the same value.
238 func TestNTPIsomorphism(t *testing.T) {
239         NTPData := make([]byte, ntpMinimumRecordSizeInBytes+7)
240         _, err := io.ReadFull(rand.Reader, NTPData)
241         if err != nil {
242                 t.Error(err)
243         }
244         ntpLayer := &NTP{}
245         err = ntpLayer.DecodeFromBytes(NTPData, gopacket.NilDecodeFeedback)
246         if err != nil {
247                 t.Error(err)
248         }
249         buf := gopacket.NewSerializeBuffer()
250         opts := gopacket.SerializeOptions{}
251         err = ntpLayer.SerializeTo(buf, opts)
252         if err != nil {
253                 t.Error(err)
254         }
255         if !reflect.DeepEqual(NTPData, buf.Bytes()) {
256                 t.Errorf("NTP packet is not isomorphic:\ngot  :\n%x\n\nwant :\n%x\n\n", buf.Bytes(), NTPData)
257         }
258 }