• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018, OpenCensus Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.opencensus.common;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import java.nio.ByteBuffer;
22 import java.nio.ByteOrder;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 
29 /** Unit tests for {@link ServerStatsEncoding}. */
30 @RunWith(JUnit4.class)
31 public class ServerStatsEncodingTest {
32 
33   @Rule public final ExpectedException thrown = ExpectedException.none();
34 
35   @Test
encodeDecodeTest()36   public void encodeDecodeTest() throws ServerStatsDeserializationException {
37     ServerStats serverStatsToBeEncoded;
38     ServerStats serverStatsDecoded;
39     byte[] serialized;
40 
41     serverStatsToBeEncoded = ServerStats.create(31, 22, (byte) 1);
42     serialized = ServerStatsEncoding.toBytes(serverStatsToBeEncoded);
43     serverStatsDecoded = ServerStatsEncoding.parseBytes(serialized);
44     assertThat(serverStatsDecoded).isEqualTo(serverStatsToBeEncoded);
45 
46     serverStatsToBeEncoded = ServerStats.create(0, 22, (byte) 0);
47     serialized = ServerStatsEncoding.toBytes(serverStatsToBeEncoded);
48     serverStatsDecoded = ServerStatsEncoding.parseBytes(serialized);
49     assertThat(serverStatsDecoded).isEqualTo(serverStatsToBeEncoded);
50 
51     serverStatsToBeEncoded = ServerStats.create(450, 0, (byte) 0);
52     serialized = ServerStatsEncoding.toBytes(serverStatsToBeEncoded);
53     serverStatsDecoded = ServerStatsEncoding.parseBytes(serialized);
54     assertThat(serverStatsDecoded).isEqualTo(serverStatsToBeEncoded);
55   }
56 
57   @Test
skipUnknownFieldTest()58   public void skipUnknownFieldTest() throws ServerStatsDeserializationException {
59     ServerStats serverStatsToBeEncoded;
60     ServerStats serverStatsDecoded;
61     byte[] serialized;
62 
63     serverStatsToBeEncoded = ServerStats.create(31, 22, (byte) 1);
64     serialized = ServerStatsEncoding.toBytes(serverStatsToBeEncoded);
65 
66     // Add new field at the end.
67     byte[] serializedExpanded = new byte[serialized.length + 9];
68     System.arraycopy(serialized, 0, serializedExpanded, 0, serialized.length);
69     final ByteBuffer bb = ByteBuffer.wrap(serializedExpanded);
70     bb.order(ByteOrder.LITTLE_ENDIAN);
71     bb.position(serialized.length);
72     bb.put((byte) 255);
73     bb.putLong(0L);
74     byte[] newSerialized = bb.array();
75 
76     serverStatsDecoded = ServerStatsEncoding.parseBytes(newSerialized);
77     assertThat(serverStatsDecoded).isEqualTo(serverStatsToBeEncoded);
78   }
79 
80   @Test
negativeLbLatencyValueTest()81   public void negativeLbLatencyValueTest() throws ServerStatsDeserializationException {
82     ServerStats serverStatsToBeEncoded;
83     byte[] serialized;
84 
85     serverStatsToBeEncoded = ServerStats.create(31, 22, (byte) 1);
86     serialized = ServerStatsEncoding.toBytes(serverStatsToBeEncoded);
87 
88     // update serialized byte[] with negative value for lbLatency.
89     final ByteBuffer bb = ByteBuffer.wrap(serialized);
90     bb.order(ByteOrder.LITTLE_ENDIAN);
91     bb.position(2);
92     bb.putLong(-100L);
93 
94     byte[] newSerialized = bb.array();
95     thrown.expect(ServerStatsDeserializationException.class);
96     thrown.expectMessage("Serialized ServiceStats contains invalid values");
97     ServerStatsEncoding.parseBytes(newSerialized);
98   }
99 
100   @Test
negativeServerLatencyValueTest()101   public void negativeServerLatencyValueTest() throws ServerStatsDeserializationException {
102     ServerStats serverStatsToBeEncoded;
103     byte[] serialized;
104 
105     serverStatsToBeEncoded = ServerStats.create(31, 22, (byte) 1);
106     serialized = ServerStatsEncoding.toBytes(serverStatsToBeEncoded);
107 
108     // update serialized byte[] with negative value for serviceLatency.
109     final ByteBuffer bb = ByteBuffer.wrap(serialized);
110     bb.order(ByteOrder.LITTLE_ENDIAN);
111     bb.position(11);
112     bb.putLong(-101L);
113 
114     byte[] newSerialized = bb.array();
115     thrown.expect(ServerStatsDeserializationException.class);
116     thrown.expectMessage("Serialized ServiceStats contains invalid values");
117     ServerStatsEncoding.parseBytes(newSerialized);
118   }
119 
120   @Test
emptySerializedBuffer()121   public void emptySerializedBuffer() throws ServerStatsDeserializationException {
122     final ByteBuffer bb = ByteBuffer.allocate(0);
123     bb.order(ByteOrder.LITTLE_ENDIAN);
124 
125     byte[] newSerialized = bb.array();
126     thrown.expect(ServerStatsDeserializationException.class);
127     thrown.expectMessage("Serialized ServerStats buffer is empty");
128     ServerStatsEncoding.parseBytes(newSerialized);
129   }
130 
131   @Test
invalidNegativeVersion()132   public void invalidNegativeVersion() throws ServerStatsDeserializationException {
133     final ByteBuffer bb = ByteBuffer.allocate(10);
134     bb.order(ByteOrder.LITTLE_ENDIAN);
135     bb.put((byte) -1);
136     byte[] newSerialized = bb.array();
137     thrown.expect(ServerStatsDeserializationException.class);
138     thrown.expectMessage("Invalid ServerStats version: -1");
139     ServerStatsEncoding.parseBytes(newSerialized);
140   }
141 
142   @Test
invalidCompatibleVersion()143   public void invalidCompatibleVersion() throws ServerStatsDeserializationException {
144     final ByteBuffer bb = ByteBuffer.allocate(10);
145     bb.order(ByteOrder.LITTLE_ENDIAN);
146     bb.put((byte) (ServerStatsEncoding.CURRENT_VERSION + 1));
147     byte[] newSerialized = bb.array();
148     thrown.expect(ServerStatsDeserializationException.class);
149     thrown.expectMessage(
150         "Invalid ServerStats version: " + (ServerStatsEncoding.CURRENT_VERSION + 1));
151     ServerStatsEncoding.parseBytes(newSerialized);
152   }
153 }
154