1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2008 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;
34 using System.Text;
35 using NUnit.Framework;
36 
37 namespace Google.Protobuf
38 {
39     public class ByteStringTest
40     {
41         [Test]
Equality()42         public void Equality()
43         {
44             ByteString b1 = ByteString.CopyFrom(1, 2, 3);
45             ByteString b2 = ByteString.CopyFrom(1, 2, 3);
46             ByteString b3 = ByteString.CopyFrom(1, 2, 4);
47             ByteString b4 = ByteString.CopyFrom(1, 2, 3, 4);
48             EqualityTester.AssertEquality(b1, b1);
49             EqualityTester.AssertEquality(b1, b2);
50             EqualityTester.AssertInequality(b1, b3);
51             EqualityTester.AssertInequality(b1, b4);
52             EqualityTester.AssertInequality(b1, null);
53 #pragma warning disable 1718 // Deliberately calling ==(b1, b1) and !=(b1, b1)
54             Assert.IsTrue(b1 == b1);
55             Assert.IsTrue(b1 == b2);
56             Assert.IsFalse(b1 == b3);
57             Assert.IsFalse(b1 == b4);
58             Assert.IsFalse(b1 == null);
59             Assert.IsTrue((ByteString) null == null);
60             Assert.IsFalse(b1 != b1);
61             Assert.IsFalse(b1 != b2);
62 #pragma warning disable 1718
63             Assert.IsTrue(b1 != b3);
64             Assert.IsTrue(b1 != b4);
65             Assert.IsTrue(b1 != null);
66             Assert.IsFalse((ByteString) null != null);
67         }
68 
69         [Test]
EmptyByteStringHasZeroSize()70         public void EmptyByteStringHasZeroSize()
71         {
72             Assert.AreEqual(0, ByteString.Empty.Length);
73         }
74 
75         [Test]
CopyFromStringWithExplicitEncoding()76         public void CopyFromStringWithExplicitEncoding()
77         {
78             ByteString bs = ByteString.CopyFrom("AB", Encoding.Unicode);
79             Assert.AreEqual(4, bs.Length);
80             Assert.AreEqual(65, bs[0]);
81             Assert.AreEqual(0, bs[1]);
82             Assert.AreEqual(66, bs[2]);
83             Assert.AreEqual(0, bs[3]);
84         }
85 
86         [Test]
IsEmptyWhenEmpty()87         public void IsEmptyWhenEmpty()
88         {
89             Assert.IsTrue(ByteString.CopyFromUtf8("").IsEmpty);
90         }
91 
92         [Test]
IsEmptyWhenNotEmpty()93         public void IsEmptyWhenNotEmpty()
94         {
95             Assert.IsFalse(ByteString.CopyFromUtf8("X").IsEmpty);
96         }
97 
98         [Test]
CopyFromByteArrayCopiesContents()99         public void CopyFromByteArrayCopiesContents()
100         {
101             byte[] data = new byte[1];
102             data[0] = 10;
103             ByteString bs = ByteString.CopyFrom(data);
104             Assert.AreEqual(10, bs[0]);
105             data[0] = 5;
106             Assert.AreEqual(10, bs[0]);
107         }
108 
109         [Test]
ToByteArrayCopiesContents()110         public void ToByteArrayCopiesContents()
111         {
112             ByteString bs = ByteString.CopyFromUtf8("Hello");
113             byte[] data = bs.ToByteArray();
114             Assert.AreEqual((byte)'H', data[0]);
115             Assert.AreEqual((byte)'H', bs[0]);
116             data[0] = 0;
117             Assert.AreEqual(0, data[0]);
118             Assert.AreEqual((byte)'H', bs[0]);
119         }
120 
121         [Test]
CopyFromUtf8UsesUtf8()122         public void CopyFromUtf8UsesUtf8()
123         {
124             ByteString bs = ByteString.CopyFromUtf8("\u20ac");
125             Assert.AreEqual(3, bs.Length);
126             Assert.AreEqual(0xe2, bs[0]);
127             Assert.AreEqual(0x82, bs[1]);
128             Assert.AreEqual(0xac, bs[2]);
129         }
130 
131         [Test]
CopyFromPortion()132         public void CopyFromPortion()
133         {
134             byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6};
135             ByteString bs = ByteString.CopyFrom(data, 2, 3);
136             Assert.AreEqual(3, bs.Length);
137             Assert.AreEqual(2, bs[0]);
138             Assert.AreEqual(3, bs[1]);
139         }
140 
141         [Test]
ToStringUtf8()142         public void ToStringUtf8()
143         {
144             ByteString bs = ByteString.CopyFromUtf8("\u20ac");
145             Assert.AreEqual("\u20ac", bs.ToStringUtf8());
146         }
147 
148         [Test]
ToStringWithExplicitEncoding()149         public void ToStringWithExplicitEncoding()
150         {
151             ByteString bs = ByteString.CopyFrom("\u20ac", Encoding.Unicode);
152             Assert.AreEqual("\u20ac", bs.ToString(Encoding.Unicode));
153         }
154 
155         [Test]
FromBase64_WithText()156         public void FromBase64_WithText()
157         {
158             byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6};
159             string base64 = Convert.ToBase64String(data);
160             ByteString bs = ByteString.FromBase64(base64);
161             Assert.AreEqual(data, bs.ToByteArray());
162         }
163 
164         [Test]
FromBase64_Empty()165         public void FromBase64_Empty()
166         {
167             // Optimization which also fixes issue 61.
168             Assert.AreSame(ByteString.Empty, ByteString.FromBase64(""));
169         }
170     }
171 }