• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2015 Google Inc.  All rights reserved.
4 // https://developers.google.com/protocol-buffers/
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 #endregion
32 
33 using System.Collections.Generic;
34 using System.IO;
35 using System.Reflection;
36 using Google.Protobuf.TestProtos;
37 using NUnit.Framework;
38 
39 namespace Google.Protobuf
40 {
41     public class FieldCodecTest
42     {
43 #pragma warning disable 0414 // Used by tests via reflection - do not remove!
44         private static readonly List<ICodecTestData> Codecs = new List<ICodecTestData>
45         {
46             new FieldCodecTestData<bool>(FieldCodec.ForBool(100), true, "FixedBool"),
47             new FieldCodecTestData<string>(FieldCodec.ForString(100), "sample", "String"),
48             new FieldCodecTestData<ByteString>(FieldCodec.ForBytes(100), ByteString.CopyFrom(1, 2, 3), "Bytes"),
49             new FieldCodecTestData<int>(FieldCodec.ForInt32(100), -1000, "Int32"),
50             new FieldCodecTestData<int>(FieldCodec.ForSInt32(100), -1000, "SInt32"),
51             new FieldCodecTestData<int>(FieldCodec.ForSFixed32(100), -1000, "SFixed32"),
52             new FieldCodecTestData<uint>(FieldCodec.ForUInt32(100), 1234, "UInt32"),
53             new FieldCodecTestData<uint>(FieldCodec.ForFixed32(100), 1234, "Fixed32"),
54             new FieldCodecTestData<long>(FieldCodec.ForInt64(100), -1000, "Int64"),
55             new FieldCodecTestData<long>(FieldCodec.ForSInt64(100), -1000, "SInt64"),
56             new FieldCodecTestData<long>(FieldCodec.ForSFixed64(100), -1000, "SFixed64"),
57             new FieldCodecTestData<ulong>(FieldCodec.ForUInt64(100), 1234, "UInt64"),
58             new FieldCodecTestData<ulong>(FieldCodec.ForFixed64(100), 1234, "Fixed64"),
59             new FieldCodecTestData<float>(FieldCodec.ForFloat(100), 1234.5f, "FixedFloat"),
60             new FieldCodecTestData<double>(FieldCodec.ForDouble(100), 1234567890.5d, "FixedDouble"),
61             new FieldCodecTestData<ForeignEnum>(
62                 FieldCodec.ForEnum(100, t => (int) t, t => (ForeignEnum) t), ForeignEnum.ForeignBaz, "Enum"),
63             new FieldCodecTestData<ForeignMessage>(
64                 FieldCodec.ForMessage(100, ForeignMessage.Parser), new ForeignMessage { C = 10 }, "Message"),
65         };
66 #pragma warning restore 0414
67 
68         [Test, TestCaseSource("Codecs")]
RoundTripWithTag(ICodecTestData codec)69         public void RoundTripWithTag(ICodecTestData codec)
70         {
71             codec.TestRoundTripWithTag();
72         }
73 
74         [Test, TestCaseSource("Codecs")]
RoundTripRaw(ICodecTestData codec)75         public void RoundTripRaw(ICodecTestData codec)
76         {
77             codec.TestRoundTripRaw();
78         }
79 
80         [Test, TestCaseSource("Codecs")]
CalculateSize(ICodecTestData codec)81         public void CalculateSize(ICodecTestData codec)
82         {
83             codec.TestCalculateSizeWithTag();
84         }
85 
86         [Test, TestCaseSource("Codecs")]
DefaultValue(ICodecTestData codec)87         public void DefaultValue(ICodecTestData codec)
88         {
89             codec.TestDefaultValue();
90         }
91 
92         [Test, TestCaseSource("Codecs")]
FixedSize(ICodecTestData codec)93         public void FixedSize(ICodecTestData codec)
94         {
95             codec.TestFixedSize();
96         }
97 
98         // This is ugly, but it means we can have a non-generic interface.
99         // It feels like NUnit should support this better, but I don't know
100         // of any better ways right now.
101         public interface ICodecTestData
102         {
TestRoundTripRaw()103             void TestRoundTripRaw();
TestRoundTripWithTag()104             void TestRoundTripWithTag();
TestCalculateSizeWithTag()105             void TestCalculateSizeWithTag();
TestDefaultValue()106             void TestDefaultValue();
TestFixedSize()107             void TestFixedSize();
108         }
109 
110         public class FieldCodecTestData<T> : ICodecTestData
111         {
112             private readonly FieldCodec<T> codec;
113             private readonly T sampleValue;
114             private readonly string name;
115 
FieldCodecTestData(FieldCodec<T> codec, T sampleValue, string name)116             public FieldCodecTestData(FieldCodec<T> codec, T sampleValue, string name)
117             {
118                 this.codec = codec;
119                 this.sampleValue = sampleValue;
120                 this.name = name;
121             }
122 
TestRoundTripRaw()123             public void TestRoundTripRaw()
124             {
125                 var stream = new MemoryStream();
126                 var codedOutput = new CodedOutputStream(stream);
127                 WriteContext.Initialize(codedOutput, out WriteContext ctx);
128                 try
129                 {
130                     // only write the value using the codec
131                     codec.ValueWriter(ref ctx, sampleValue);
132                 }
133                 finally
134                 {
135                     ctx.CopyStateTo(codedOutput);
136                 }
137 
138                 codedOutput.Flush();
139                 stream.Position = 0;
140                 var codedInput = new CodedInputStream(stream);
141                 Assert.AreEqual(sampleValue, codec.Read(codedInput));
142                 Assert.IsTrue(codedInput.IsAtEnd);
143             }
144 
TestRoundTripWithTag()145             public void TestRoundTripWithTag()
146             {
147                 var stream = new MemoryStream();
148                 var codedOutput = new CodedOutputStream(stream);
149                 codec.WriteTagAndValue(codedOutput, sampleValue);
150                 codedOutput.Flush();
151                 stream.Position = 0;
152                 var codedInput = new CodedInputStream(stream);
153                 codedInput.AssertNextTag(codec.Tag);
154                 Assert.AreEqual(sampleValue, codec.Read(codedInput));
155                 Assert.IsTrue(codedInput.IsAtEnd);
156             }
157 
TestCalculateSizeWithTag()158             public void TestCalculateSizeWithTag()
159             {
160                 var stream = new MemoryStream();
161                 var codedOutput = new CodedOutputStream(stream);
162                 codec.WriteTagAndValue(codedOutput, sampleValue);
163                 codedOutput.Flush();
164                 Assert.AreEqual(stream.Position, codec.CalculateSizeWithTag(sampleValue));
165             }
166 
TestDefaultValue()167             public void TestDefaultValue()
168             {
169                 // WriteTagAndValue ignores default values
170                 var stream = new MemoryStream();
171                 CodedOutputStream codedOutput;
172 #if !NET35
173                 codedOutput = new CodedOutputStream(stream);
174                 codec.WriteTagAndValue(codedOutput, codec.DefaultValue);
175                 codedOutput.Flush();
176                 Assert.AreEqual(0, stream.Position);
177                 Assert.AreEqual(0, codec.CalculateSizeWithTag(codec.DefaultValue));
178                 if (typeof(T).GetTypeInfo().IsValueType)
179                 {
180                     Assert.AreEqual(default(T), codec.DefaultValue);
181                 }
182 #endif
183 
184                 // The plain ValueWriter/ValueReader delegates don't.
185                 if (codec.DefaultValue != null) // This part isn't appropriate for message types.
186                 {
187                     codedOutput = new CodedOutputStream(stream);
188                     WriteContext.Initialize(codedOutput, out WriteContext ctx);
189                     try
190                     {
191                         // only write the value using the codec
192                         codec.ValueWriter(ref ctx, codec.DefaultValue);
193                     }
194                     finally
195                     {
196                         ctx.CopyStateTo(codedOutput);
197                     }
198 
199                     codedOutput.Flush();
200                     Assert.AreNotEqual(0, stream.Position);
201                     Assert.AreEqual(stream.Position, codec.ValueSizeCalculator(codec.DefaultValue));
202                     stream.Position = 0;
203                     var codedInput = new CodedInputStream(stream);
204                     Assert.AreEqual(codec.DefaultValue, codec.Read(codedInput));
205                 }
206             }
207 
TestFixedSize()208             public void TestFixedSize()
209             {
210                 Assert.AreEqual(name.Contains("Fixed"), codec.FixedSize != 0);
211             }
212 
ToString()213             public override string ToString()
214             {
215                 return name;
216             }
217         }
218     }
219 }
220