1ffe3c632Sopenharmony_ci#region Copyright notice and license
2ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format
3ffe3c632Sopenharmony_ci// Copyright 2008 Google Inc.  All rights reserved.
4ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/
5ffe3c632Sopenharmony_ci//
6ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
7ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are
8ffe3c632Sopenharmony_ci// met:
9ffe3c632Sopenharmony_ci//
10ffe3c632Sopenharmony_ci//     * Redistributions of source code must retain the above copyright
11ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer.
12ffe3c632Sopenharmony_ci//     * Redistributions in binary form must reproduce the above
13ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer
14ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the
15ffe3c632Sopenharmony_ci// distribution.
16ffe3c632Sopenharmony_ci//     * Neither the name of Google Inc. nor the names of its
17ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from
18ffe3c632Sopenharmony_ci// this software without specific prior written permission.
19ffe3c632Sopenharmony_ci//
20ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31ffe3c632Sopenharmony_ci#endregion
32ffe3c632Sopenharmony_ci
33ffe3c632Sopenharmony_ciusing System;
34ffe3c632Sopenharmony_ciusing System.Text;
35ffe3c632Sopenharmony_ciusing NUnit.Framework;
36ffe3c632Sopenharmony_ciusing System.IO;
37ffe3c632Sopenharmony_ci#if !NET35
38ffe3c632Sopenharmony_ciusing System.Threading.Tasks;
39ffe3c632Sopenharmony_ci#endif
40ffe3c632Sopenharmony_ci
41ffe3c632Sopenharmony_cinamespace Google.Protobuf
42ffe3c632Sopenharmony_ci{
43ffe3c632Sopenharmony_ci    public class ByteStringTest
44ffe3c632Sopenharmony_ci    {
45ffe3c632Sopenharmony_ci        [Test]
46ffe3c632Sopenharmony_ci        public void Equality()
47ffe3c632Sopenharmony_ci        {
48ffe3c632Sopenharmony_ci            ByteString b1 = ByteString.CopyFrom(1, 2, 3);
49ffe3c632Sopenharmony_ci            ByteString b2 = ByteString.CopyFrom(1, 2, 3);
50ffe3c632Sopenharmony_ci            ByteString b3 = ByteString.CopyFrom(1, 2, 4);
51ffe3c632Sopenharmony_ci            ByteString b4 = ByteString.CopyFrom(1, 2, 3, 4);
52ffe3c632Sopenharmony_ci            EqualityTester.AssertEquality(b1, b1);
53ffe3c632Sopenharmony_ci            EqualityTester.AssertEquality(b1, b2);
54ffe3c632Sopenharmony_ci            EqualityTester.AssertInequality(b1, b3);
55ffe3c632Sopenharmony_ci            EqualityTester.AssertInequality(b1, b4);
56ffe3c632Sopenharmony_ci            EqualityTester.AssertInequality(b1, null);
57ffe3c632Sopenharmony_ci#pragma warning disable 1718 // Deliberately calling ==(b1, b1) and !=(b1, b1)
58ffe3c632Sopenharmony_ci            Assert.IsTrue(b1 == b1);
59ffe3c632Sopenharmony_ci            Assert.IsTrue(b1 == b2);
60ffe3c632Sopenharmony_ci            Assert.IsFalse(b1 == b3);
61ffe3c632Sopenharmony_ci            Assert.IsFalse(b1 == b4);
62ffe3c632Sopenharmony_ci            Assert.IsFalse(b1 == null);
63ffe3c632Sopenharmony_ci            Assert.IsTrue((ByteString) null == null);
64ffe3c632Sopenharmony_ci            Assert.IsFalse(b1 != b1);
65ffe3c632Sopenharmony_ci            Assert.IsFalse(b1 != b2);
66ffe3c632Sopenharmony_ci#pragma warning disable 1718
67ffe3c632Sopenharmony_ci            Assert.IsTrue(b1 != b3);
68ffe3c632Sopenharmony_ci            Assert.IsTrue(b1 != b4);
69ffe3c632Sopenharmony_ci            Assert.IsTrue(b1 != null);
70ffe3c632Sopenharmony_ci            Assert.IsFalse((ByteString) null != null);
71ffe3c632Sopenharmony_ci        }
72ffe3c632Sopenharmony_ci
73ffe3c632Sopenharmony_ci        [Test]
74ffe3c632Sopenharmony_ci        public void EmptyByteStringHasZeroSize()
75ffe3c632Sopenharmony_ci        {
76ffe3c632Sopenharmony_ci            Assert.AreEqual(0, ByteString.Empty.Length);
77ffe3c632Sopenharmony_ci        }
78ffe3c632Sopenharmony_ci
79ffe3c632Sopenharmony_ci        [Test]
80ffe3c632Sopenharmony_ci        public void CopyFromStringWithExplicitEncoding()
81ffe3c632Sopenharmony_ci        {
82ffe3c632Sopenharmony_ci            ByteString bs = ByteString.CopyFrom("AB", Encoding.Unicode);
83ffe3c632Sopenharmony_ci            Assert.AreEqual(4, bs.Length);
84ffe3c632Sopenharmony_ci            Assert.AreEqual(65, bs[0]);
85ffe3c632Sopenharmony_ci            Assert.AreEqual(0, bs[1]);
86ffe3c632Sopenharmony_ci            Assert.AreEqual(66, bs[2]);
87ffe3c632Sopenharmony_ci            Assert.AreEqual(0, bs[3]);
88ffe3c632Sopenharmony_ci        }
89ffe3c632Sopenharmony_ci
90ffe3c632Sopenharmony_ci        [Test]
91ffe3c632Sopenharmony_ci        public void IsEmptyWhenEmpty()
92ffe3c632Sopenharmony_ci        {
93ffe3c632Sopenharmony_ci            Assert.IsTrue(ByteString.CopyFromUtf8("").IsEmpty);
94ffe3c632Sopenharmony_ci        }
95ffe3c632Sopenharmony_ci
96ffe3c632Sopenharmony_ci        [Test]
97ffe3c632Sopenharmony_ci        public void IsEmptyWhenNotEmpty()
98ffe3c632Sopenharmony_ci        {
99ffe3c632Sopenharmony_ci            Assert.IsFalse(ByteString.CopyFromUtf8("X").IsEmpty);
100ffe3c632Sopenharmony_ci        }
101ffe3c632Sopenharmony_ci
102ffe3c632Sopenharmony_ci        [Test]
103ffe3c632Sopenharmony_ci        public void CopyFromByteArrayCopiesContents()
104ffe3c632Sopenharmony_ci        {
105ffe3c632Sopenharmony_ci            byte[] data = new byte[1];
106ffe3c632Sopenharmony_ci            data[0] = 10;
107ffe3c632Sopenharmony_ci            ByteString bs = ByteString.CopyFrom(data);
108ffe3c632Sopenharmony_ci            Assert.AreEqual(10, bs[0]);
109ffe3c632Sopenharmony_ci            data[0] = 5;
110ffe3c632Sopenharmony_ci            Assert.AreEqual(10, bs[0]);
111ffe3c632Sopenharmony_ci        }
112ffe3c632Sopenharmony_ci
113ffe3c632Sopenharmony_ci        [Test]
114ffe3c632Sopenharmony_ci        public void ToByteArrayCopiesContents()
115ffe3c632Sopenharmony_ci        {
116ffe3c632Sopenharmony_ci            ByteString bs = ByteString.CopyFromUtf8("Hello");
117ffe3c632Sopenharmony_ci            byte[] data = bs.ToByteArray();
118ffe3c632Sopenharmony_ci            Assert.AreEqual((byte)'H', data[0]);
119ffe3c632Sopenharmony_ci            Assert.AreEqual((byte)'H', bs[0]);
120ffe3c632Sopenharmony_ci            data[0] = 0;
121ffe3c632Sopenharmony_ci            Assert.AreEqual(0, data[0]);
122ffe3c632Sopenharmony_ci            Assert.AreEqual((byte)'H', bs[0]);
123ffe3c632Sopenharmony_ci        }
124ffe3c632Sopenharmony_ci
125ffe3c632Sopenharmony_ci        [Test]
126ffe3c632Sopenharmony_ci        public void CopyFromUtf8UsesUtf8()
127ffe3c632Sopenharmony_ci        {
128ffe3c632Sopenharmony_ci            ByteString bs = ByteString.CopyFromUtf8("\u20ac");
129ffe3c632Sopenharmony_ci            Assert.AreEqual(3, bs.Length);
130ffe3c632Sopenharmony_ci            Assert.AreEqual(0xe2, bs[0]);
131ffe3c632Sopenharmony_ci            Assert.AreEqual(0x82, bs[1]);
132ffe3c632Sopenharmony_ci            Assert.AreEqual(0xac, bs[2]);
133ffe3c632Sopenharmony_ci        }
134ffe3c632Sopenharmony_ci
135ffe3c632Sopenharmony_ci        [Test]
136ffe3c632Sopenharmony_ci        public void CopyFromPortion()
137ffe3c632Sopenharmony_ci        {
138ffe3c632Sopenharmony_ci            byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6};
139ffe3c632Sopenharmony_ci            ByteString bs = ByteString.CopyFrom(data, 2, 3);
140ffe3c632Sopenharmony_ci            Assert.AreEqual(3, bs.Length);
141ffe3c632Sopenharmony_ci            Assert.AreEqual(2, bs[0]);
142ffe3c632Sopenharmony_ci            Assert.AreEqual(3, bs[1]);
143ffe3c632Sopenharmony_ci        }
144ffe3c632Sopenharmony_ci
145ffe3c632Sopenharmony_ci        [Test]
146ffe3c632Sopenharmony_ci        public void ToStringUtf8()
147ffe3c632Sopenharmony_ci        {
148ffe3c632Sopenharmony_ci            ByteString bs = ByteString.CopyFromUtf8("\u20ac");
149ffe3c632Sopenharmony_ci            Assert.AreEqual("\u20ac", bs.ToStringUtf8());
150ffe3c632Sopenharmony_ci        }
151ffe3c632Sopenharmony_ci
152ffe3c632Sopenharmony_ci        [Test]
153ffe3c632Sopenharmony_ci        public void ToStringWithExplicitEncoding()
154ffe3c632Sopenharmony_ci        {
155ffe3c632Sopenharmony_ci            ByteString bs = ByteString.CopyFrom("\u20ac", Encoding.Unicode);
156ffe3c632Sopenharmony_ci            Assert.AreEqual("\u20ac", bs.ToString(Encoding.Unicode));
157ffe3c632Sopenharmony_ci        }
158ffe3c632Sopenharmony_ci
159ffe3c632Sopenharmony_ci        [Test]
160ffe3c632Sopenharmony_ci        public void FromBase64_WithText()
161ffe3c632Sopenharmony_ci        {
162ffe3c632Sopenharmony_ci            byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6};
163ffe3c632Sopenharmony_ci            string base64 = Convert.ToBase64String(data);
164ffe3c632Sopenharmony_ci            ByteString bs = ByteString.FromBase64(base64);
165ffe3c632Sopenharmony_ci            Assert.AreEqual(data, bs.ToByteArray());
166ffe3c632Sopenharmony_ci        }
167ffe3c632Sopenharmony_ci
168ffe3c632Sopenharmony_ci        [Test]
169ffe3c632Sopenharmony_ci        public void FromBase64_Empty()
170ffe3c632Sopenharmony_ci        {
171ffe3c632Sopenharmony_ci            // Optimization which also fixes issue 61.
172ffe3c632Sopenharmony_ci            Assert.AreSame(ByteString.Empty, ByteString.FromBase64(""));
173ffe3c632Sopenharmony_ci        }
174ffe3c632Sopenharmony_ci
175ffe3c632Sopenharmony_ci        [Test]
176ffe3c632Sopenharmony_ci        public void FromStream_Seekable()
177ffe3c632Sopenharmony_ci        {
178ffe3c632Sopenharmony_ci            var stream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });
179ffe3c632Sopenharmony_ci            // Consume the first byte, just to test that it's "from current position"
180ffe3c632Sopenharmony_ci            stream.ReadByte();
181ffe3c632Sopenharmony_ci            var actual = ByteString.FromStream(stream);
182ffe3c632Sopenharmony_ci            ByteString expected = ByteString.CopyFrom(2, 3, 4, 5);
183ffe3c632Sopenharmony_ci            Assert.AreEqual(expected, actual, $"{expected.ToBase64()} != {actual.ToBase64()}");
184ffe3c632Sopenharmony_ci        }
185ffe3c632Sopenharmony_ci
186ffe3c632Sopenharmony_ci        [Test]
187ffe3c632Sopenharmony_ci        public void FromStream_NotSeekable()
188ffe3c632Sopenharmony_ci        {
189ffe3c632Sopenharmony_ci            var stream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });
190ffe3c632Sopenharmony_ci            // Consume the first byte, just to test that it's "from current position"
191ffe3c632Sopenharmony_ci            stream.ReadByte();
192ffe3c632Sopenharmony_ci            // Wrap the original stream in LimitedInputStream, which has CanSeek=false
193ffe3c632Sopenharmony_ci            var limitedStream = new LimitedInputStream(stream, 3);
194ffe3c632Sopenharmony_ci            var actual = ByteString.FromStream(limitedStream);
195ffe3c632Sopenharmony_ci            ByteString expected = ByteString.CopyFrom(2, 3, 4);
196ffe3c632Sopenharmony_ci            Assert.AreEqual(expected, actual, $"{expected.ToBase64()} != {actual.ToBase64()}");
197ffe3c632Sopenharmony_ci        }
198ffe3c632Sopenharmony_ci
199ffe3c632Sopenharmony_ci#if !NET35
200ffe3c632Sopenharmony_ci        [Test]
201ffe3c632Sopenharmony_ci        public async Task FromStreamAsync_Seekable()
202ffe3c632Sopenharmony_ci        {
203ffe3c632Sopenharmony_ci            var stream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });
204ffe3c632Sopenharmony_ci            // Consume the first byte, just to test that it's "from current position"
205ffe3c632Sopenharmony_ci            stream.ReadByte();
206ffe3c632Sopenharmony_ci            var actual = await ByteString.FromStreamAsync(stream);
207ffe3c632Sopenharmony_ci            ByteString expected = ByteString.CopyFrom(2, 3, 4, 5);
208ffe3c632Sopenharmony_ci            Assert.AreEqual(expected, actual, $"{expected.ToBase64()} != {actual.ToBase64()}");
209ffe3c632Sopenharmony_ci        }
210ffe3c632Sopenharmony_ci
211ffe3c632Sopenharmony_ci        [Test]
212ffe3c632Sopenharmony_ci        public async Task FromStreamAsync_NotSeekable()
213ffe3c632Sopenharmony_ci        {
214ffe3c632Sopenharmony_ci            var stream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });
215ffe3c632Sopenharmony_ci            // Consume the first byte, just to test that it's "from current position"
216ffe3c632Sopenharmony_ci            stream.ReadByte();
217ffe3c632Sopenharmony_ci            // Wrap the original stream in LimitedInputStream, which has CanSeek=false
218ffe3c632Sopenharmony_ci            var limitedStream = new LimitedInputStream(stream, 3);
219ffe3c632Sopenharmony_ci            var actual = await ByteString.FromStreamAsync(limitedStream);
220ffe3c632Sopenharmony_ci            ByteString expected = ByteString.CopyFrom(2, 3, 4);
221ffe3c632Sopenharmony_ci            Assert.AreEqual(expected, actual, $"{expected.ToBase64()} != {actual.ToBase64()}");
222ffe3c632Sopenharmony_ci        }
223ffe3c632Sopenharmony_ci#endif
224ffe3c632Sopenharmony_ci
225ffe3c632Sopenharmony_ci        [Test]
226ffe3c632Sopenharmony_ci        public void GetHashCode_Regression()
227ffe3c632Sopenharmony_ci        {
228ffe3c632Sopenharmony_ci            // We used to have an awful hash algorithm where only the last four
229ffe3c632Sopenharmony_ci            // bytes were relevant. This is a regression test for
230ffe3c632Sopenharmony_ci            // https://github.com/protocolbuffers/protobuf/issues/2511
231ffe3c632Sopenharmony_ci
232ffe3c632Sopenharmony_ci            ByteString b1 = ByteString.CopyFrom(100, 1, 2, 3, 4);
233ffe3c632Sopenharmony_ci            ByteString b2 = ByteString.CopyFrom(200, 1, 2, 3, 4);
234ffe3c632Sopenharmony_ci            Assert.AreNotEqual(b1.GetHashCode(), b2.GetHashCode());
235ffe3c632Sopenharmony_ci        }
236ffe3c632Sopenharmony_ci
237ffe3c632Sopenharmony_ci        [Test]
238ffe3c632Sopenharmony_ci        public void GetContentsAsReadOnlySpan()
239ffe3c632Sopenharmony_ci        {
240ffe3c632Sopenharmony_ci            var byteString = ByteString.CopyFrom(1, 2, 3, 4, 5);
241ffe3c632Sopenharmony_ci            var copied = byteString.Span.ToArray();
242ffe3c632Sopenharmony_ci            CollectionAssert.AreEqual(byteString, copied);
243ffe3c632Sopenharmony_ci        }
244ffe3c632Sopenharmony_ci
245ffe3c632Sopenharmony_ci        [Test]
246ffe3c632Sopenharmony_ci        public void GetContentsAsReadOnlyMemory()
247ffe3c632Sopenharmony_ci        {
248ffe3c632Sopenharmony_ci            var byteString = ByteString.CopyFrom(1, 2, 3, 4, 5);
249ffe3c632Sopenharmony_ci            var copied = byteString.Memory.ToArray();
250ffe3c632Sopenharmony_ci            CollectionAssert.AreEqual(byteString, copied);
251ffe3c632Sopenharmony_ci        }
252ffe3c632Sopenharmony_ci    }
253ffe3c632Sopenharmony_ci}