|
| 1 | +#region License |
| 2 | +// Copyright (c) 2007 James Newton-King |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person |
| 5 | +// obtaining a copy of this software and associated documentation |
| 6 | +// files (the "Software"), to deal in the Software without |
| 7 | +// restriction, including without limitation the rights to use, |
| 8 | +// copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the |
| 10 | +// Software is furnished to do so, subject to the following |
| 11 | +// conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be |
| 14 | +// included in all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 24 | +#endregion |
| 25 | + |
| 26 | +using System; |
| 27 | +using System.Collections.Generic; |
| 28 | +using System.IO; |
| 29 | +using System.Linq; |
| 30 | +using System.Text; |
| 31 | +#if !(NET20 || NET35 || NET40 || PORTABLE40) |
| 32 | +using System.Threading.Tasks; |
| 33 | +#endif |
| 34 | +using Newtonsoft.Json; |
| 35 | +using Newtonsoft.Json.Linq; |
| 36 | +using System.Xml; |
| 37 | +#if !NET20 |
| 38 | +using System.Xml.Linq; |
| 39 | +#endif |
| 40 | +#if DNXCORE50 |
| 41 | +using Xunit; |
| 42 | +using Test = Xunit.FactAttribute; |
| 43 | +using Assert = Newtonsoft.Json.Tests.XUnitAssert; |
| 44 | +#else |
| 45 | +using NUnit.Framework; |
| 46 | +#endif |
| 47 | + |
| 48 | +namespace Newtonsoft.Json.Tests.Issues |
| 49 | +{ |
| 50 | + [TestFixture] |
| 51 | + public class Issue1321 : TestFixtureBase |
| 52 | + { |
| 53 | + [Test] |
| 54 | + public void Test() |
| 55 | + { |
| 56 | + ExceptionAssert.Throws<JsonWriterException>(() => |
| 57 | + { |
| 58 | + JsonConvert.DeserializeObject( |
| 59 | + @"[""1"",", |
| 60 | + new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.None, MaxDepth = 1024 }); |
| 61 | + }, "Unexpected end when reading token. Path ''."); |
| 62 | + } |
| 63 | + |
| 64 | + [Test] |
| 65 | + public void Test2() |
| 66 | + { |
| 67 | + JArray a = new JArray(); |
| 68 | + |
| 69 | + var writer = a.CreateWriter(); |
| 70 | + |
| 71 | + JsonTextReader reader = new JsonTextReader(new StringReader(@"[""1"",")); |
| 72 | + |
| 73 | + ExceptionAssert.Throws<JsonWriterException>(() => |
| 74 | + { |
| 75 | + writer.WriteToken(reader); |
| 76 | + }, "Unexpected end when reading token. Path ''."); |
| 77 | + } |
| 78 | + |
| 79 | +#if !(NET20 || NET35 || NET40 || PORTABLE40) |
| 80 | + [Test] |
| 81 | + public async Task Test2_Async() |
| 82 | + { |
| 83 | + JArray a = new JArray(); |
| 84 | + |
| 85 | + var writer = a.CreateWriter(); |
| 86 | + |
| 87 | + JsonTextReader reader = new JsonTextReader(new StringReader(@"[""1"",")); |
| 88 | + |
| 89 | + await ExceptionAssert.ThrowsAsync<JsonWriterException>(async () => |
| 90 | + { |
| 91 | + await writer.WriteTokenAsync(reader); |
| 92 | + }, "Unexpected end when reading token. Path ''."); |
| 93 | + } |
| 94 | +#endif |
| 95 | + |
| 96 | + [Test] |
| 97 | + public void Test3() |
| 98 | + { |
| 99 | + JArray a = new JArray(); |
| 100 | + |
| 101 | + var writer = a.CreateWriter(); |
| 102 | + |
| 103 | + JsonTextReader reader = new JsonTextReader(new StringReader(@"[""1"",")); |
| 104 | + reader.Read(); |
| 105 | + |
| 106 | + ExceptionAssert.Throws<JsonWriterException>(() => |
| 107 | + { |
| 108 | + writer.WriteToken(reader); |
| 109 | + }, "Unexpected end when reading token. Path ''."); |
| 110 | + } |
| 111 | + |
| 112 | +#if !(NET20 || NET35 || NET40 || PORTABLE40) |
| 113 | + [Test] |
| 114 | + public async Task Test3_Async() |
| 115 | + { |
| 116 | + JArray a = new JArray(); |
| 117 | + |
| 118 | + var writer = a.CreateWriter(); |
| 119 | + |
| 120 | + JsonTextReader reader = new JsonTextReader(new StringReader(@"[""1"",")); |
| 121 | + await reader.ReadAsync(); |
| 122 | + |
| 123 | + await ExceptionAssert.ThrowsAsync<JsonWriterException>(async () => |
| 124 | + { |
| 125 | + await writer.WriteTokenAsync(reader); |
| 126 | + }, "Unexpected end when reading token. Path ''."); |
| 127 | + } |
| 128 | +#endif |
| 129 | + |
| 130 | + [Test] |
| 131 | + public void Test4() |
| 132 | + { |
| 133 | + JArray a = new JArray(); |
| 134 | + |
| 135 | + var writer = a.CreateWriter(); |
| 136 | + |
| 137 | + JsonTextReader reader = new JsonTextReader(new StringReader(@"[[""1"",")); |
| 138 | + reader.Read(); |
| 139 | + reader.Read(); |
| 140 | + |
| 141 | + ExceptionAssert.Throws<JsonWriterException>(() => |
| 142 | + { |
| 143 | + writer.WriteToken(reader); |
| 144 | + }, "Unexpected end when reading token. Path ''."); |
| 145 | + } |
| 146 | + |
| 147 | +#if !(NET20 || NET35 || NET40 || PORTABLE40) |
| 148 | + [Test] |
| 149 | + public async Task Test4_Async() |
| 150 | + { |
| 151 | + JArray a = new JArray(); |
| 152 | + |
| 153 | + var writer = a.CreateWriter(); |
| 154 | + |
| 155 | + JsonTextReader reader = new JsonTextReader(new StringReader(@"[[""1"",")); |
| 156 | + await reader.ReadAsync(); |
| 157 | + await reader.ReadAsync(); |
| 158 | + |
| 159 | + await ExceptionAssert.ThrowsAsync<JsonWriterException>(async () => |
| 160 | + { |
| 161 | + await writer.WriteTokenAsync(reader); |
| 162 | + }, "Unexpected end when reading token. Path ''."); |
| 163 | + } |
| 164 | +#endif |
| 165 | + |
| 166 | + [Test] |
| 167 | + public void Test5() |
| 168 | + { |
| 169 | + StringWriter sw = new StringWriter(); |
| 170 | + JsonTextWriter writer = new JsonTextWriter(sw); |
| 171 | + writer.WriteStartArray(); |
| 172 | + |
| 173 | + JsonTextReader reader = new JsonTextReader(new StringReader(@"[[""1"",")); |
| 174 | + reader.Read(); |
| 175 | + reader.Read(); |
| 176 | + |
| 177 | + ExceptionAssert.Throws<JsonWriterException>(() => |
| 178 | + { |
| 179 | + writer.WriteToken(reader); |
| 180 | + }, "Unexpected end when reading token. Path '[0]'."); |
| 181 | + } |
| 182 | + |
| 183 | +#if !(NET20 || NET35 || NET40 || PORTABLE40) |
| 184 | + [Test] |
| 185 | + public async Task Test5_Async() |
| 186 | + { |
| 187 | + StringWriter sw = new StringWriter(); |
| 188 | + JsonTextWriter writer = new JsonTextWriter(sw); |
| 189 | + writer.WriteStartArray(); |
| 190 | + |
| 191 | + JsonTextReader reader = new JsonTextReader(new StringReader(@"[[""1"",")); |
| 192 | + await reader.ReadAsync(); |
| 193 | + await reader.ReadAsync(); |
| 194 | + |
| 195 | + await ExceptionAssert.ThrowsAsync<JsonWriterException>(async () => |
| 196 | + { |
| 197 | + await writer.WriteTokenAsync(reader); |
| 198 | + }, "Unexpected end when reading token. Path '[0]'."); |
| 199 | + } |
| 200 | +#endif |
| 201 | + } |
| 202 | +} |