File tree Expand file tree Collapse file tree 9 files changed +147
-0
lines changed Expand file tree Collapse file tree 9 files changed +147
-0
lines changed Original file line number Diff line number Diff line change @@ -554,6 +554,14 @@ public void WriteBytes(byte[] value)
554554 /// <param name="value">A string.</param>
555555 public void WriteCString ( UTF8Encoding encoding , string value )
556556 {
557+ if ( value == null )
558+ {
559+ throw new ArgumentNullException ( "value" ) ;
560+ }
561+ if ( value . IndexOf ( '\0 ' ) != - 1 )
562+ {
563+ throw new ArgumentException ( "CStrings cannot contain nulls." , "value" ) ;
564+ }
557565 ThrowIfDisposed ( ) ;
558566
559567 var maxLength = encoding . GetMaxByteCount ( value . Length ) + 1 ;
Original file line number Diff line number Diff line change @@ -495,6 +495,14 @@ public void WriteMinKey(string name)
495495 /// <param name="name">The name of the element.</param>
496496 public virtual void WriteName ( string name )
497497 {
498+ if ( name == null )
499+ {
500+ throw new ArgumentNullException ( "name" ) ;
501+ }
502+ if ( name . IndexOf ( '\0 ' ) != - 1 )
503+ {
504+ throw new ArgumentException ( "Element names cannot contain nulls." , "name" ) ;
505+ }
498506 if ( _disposed ) { throw new ObjectDisposedException ( this . GetType ( ) . Name ) ; }
499507 if ( _state != BsonWriterState . Name )
500508 {
Original file line number Diff line number Diff line change @@ -374,6 +374,10 @@ public BsonMemberMap SetElementName(string elementName)
374374 {
375375 throw new ArgumentNullException ( "elementName" ) ;
376376 }
377+ if ( elementName . IndexOf ( '\0 ' ) != - 1 )
378+ {
379+ throw new ArgumentException ( "Element names cannot contain nulls." , "elementName" ) ;
380+ }
377381 if ( _frozen ) { ThrowFrozenException ( ) ; }
378382
379383 _elementName = elementName ;
Original file line number Diff line number Diff line change @@ -39,6 +39,14 @@ public abstract class StandardDiscriminatorConvention : IDiscriminatorConvention
3939 /// <param name="elementName">The element name.</param>
4040 protected StandardDiscriminatorConvention ( string elementName )
4141 {
42+ if ( elementName == null )
43+ {
44+ throw new ArgumentNullException ( "elementName" ) ;
45+ }
46+ if ( elementName . IndexOf ( '\0 ' ) != - 1 )
47+ {
48+ throw new ArgumentException ( "Element names cannot contain nulls." , "elementName" ) ;
49+ }
4250 _elementName = elementName ;
4351 }
4452
Original file line number Diff line number Diff line change 1313* limitations under the License.
1414*/
1515
16+ using System ;
1617using System . IO ;
1718using System . Text ;
1819using MongoDB . Bson ;
20+ using MongoDB . Bson . IO ;
1921using MongoDB . Bson . Serialization ;
2022using NUnit . Framework ;
2123
@@ -127,5 +129,23 @@ public void TestReadStringTwoCharactersDecoderException()
127129 Assert . AreEqual ( 15 , bytes . Length ) ;
128130 var ex = Assert . Throws < DecoderFallbackException > ( ( ) => { BsonSerializer . Deserialize < BsonDocument > ( bytes ) ; } ) ;
129131 }
132+
133+ [ Test ]
134+ public void TestWriteCStringThrowsWhenValueContainsNulls ( )
135+ {
136+ using ( var bsonBuffer = new BsonBuffer ( ) )
137+ {
138+ Assert . Throws < ArgumentException > ( ( ) => { bsonBuffer . WriteCString ( ( UTF8Encoding ) Encoding . UTF8 , "a\0 b" ) ; } ) ;
139+ }
140+ }
141+
142+ [ Test ]
143+ public void TestWriteCStringThrowsWhenValueIsNull ( )
144+ {
145+ using ( var bsonBuffer = new BsonBuffer ( ) )
146+ {
147+ Assert . Throws < ArgumentNullException > ( ( ) => { bsonBuffer . WriteCString ( ( UTF8Encoding ) Encoding . UTF8 , null ) ; } ) ;
148+ }
149+ }
130150 }
131151}
Original file line number Diff line number Diff line change 1+ /* Copyright 2010-2013 10gen Inc.
2+ *
3+ * Licensed under the Apache License, Version 2.0 (the "License");
4+ * you may not use this file except in compliance with the License.
5+ * You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software
10+ * distributed under the License is distributed on an "AS IS" BASIS,
11+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * See the License for the specific language governing permissions and
13+ * limitations under the License.
14+ */
15+
16+ using System ;
17+ using MongoDB . Bson . IO ;
18+ using NUnit . Framework ;
19+
20+ namespace MongoDB . BsonUnitTests . IO
21+ {
22+ [ TestFixture ]
23+ public class BsonWriterTests
24+ {
25+ [ Test ]
26+ public void TestWriteNameThrowsWhenValueContainsNulls ( )
27+ {
28+ using ( var bsonWriter = BsonWriter . Create ( BsonBinaryWriterSettings . Defaults ) )
29+ {
30+ Assert . Throws < ArgumentException > ( ( ) => { bsonWriter . WriteName ( "a\0 b" ) ; } ) ;
31+ }
32+ }
33+
34+ [ Test ]
35+ public void TestWriteNameThrowsWhenValueIsNull ( )
36+ {
37+ using ( var bsonWriter = BsonWriter . Create ( BsonBinaryWriterSettings . Defaults ) )
38+ {
39+ Assert . Throws < ArgumentNullException > ( ( ) => { bsonWriter . WriteName ( null ) ; } ) ;
40+ }
41+ }
42+ }
43+ }
Original file line number Diff line number Diff line change 8181 </Compile >
8282 <Compile Include =" BsonExtensionMethodsTests.cs" />
8383 <Compile Include =" BsonUtilsTests.cs" />
84+ <Compile Include =" IO\BsonWriterTests.cs" />
8485 <Compile Include =" IO\ByteArrayBufferTests.cs" />
8586 <Compile Include =" IO\MultiChunkBufferTests.cs" />
8687 <Compile Include =" Jira\CSharp728Tests.cs" />
118119 <Compile Include =" Serialization\Conventions\NamedIdConventionsTests.cs" />
119120 <Compile Include =" Serialization\Conventions\PropertyFinderConventionsTests.cs" />
120121 <Compile Include =" Serialization\Conventions\ReadWriteMemberFinderConventionsTests.cs" />
122+ <Compile Include =" Serialization\Conventions\StandardDiscriminatorConventionTests.cs" />
121123 <Compile Include =" Serialization\Conventions\StringObjectIdGeneratorConventionsTests.cs" />
122124 <Compile Include =" Serialization\IdGenerators\AscendingGuidGeneratorTests.cs" />
123125 <Compile Include =" Serialization\IdGenerators\CombGuidGeneratorTests.cs" />
Original file line number Diff line number Diff line change 1313* limitations under the License.
1414*/
1515
16+ using System ;
1617using MongoDB . Bson ;
1718using MongoDB . Bson . Serialization ;
1819using MongoDB . Bson . Serialization . IdGenerators ;
@@ -68,6 +69,22 @@ public void TestIsReadOnlyPropertyOfAField()
6869 Assert . IsFalse ( memberMap . IsReadOnly ) ;
6970 }
7071
72+ [ Test ]
73+ public void TestSetElementNameThrowsWhenElementNameContainsNulls ( )
74+ {
75+ var classMap = new BsonClassMap < TestClass > ( cm => cm . AutoMap ( ) ) ;
76+ var memberMap = classMap . GetMemberMap ( "Property" ) ;
77+ Assert . Throws < ArgumentException > ( ( ) => { memberMap . SetElementName ( "a\0 b" ) ; } ) ;
78+ }
79+
80+ [ Test ]
81+ public void TestSetElementNameThrowsWhenElementNameIsNull ( )
82+ {
83+ var classMap = new BsonClassMap < TestClass > ( cm => cm . AutoMap ( ) ) ;
84+ var memberMap = classMap . GetMemberMap ( "Property" ) ;
85+ Assert . Throws < ArgumentNullException > ( ( ) => { memberMap . SetElementName ( null ) ; } ) ;
86+ }
87+
7188 [ Test ]
7289 public void TestSettingAField ( )
7390 {
Original file line number Diff line number Diff line change 1+ /* Copyright 2010-2013 10gen Inc.
2+ *
3+ * Licensed under the Apache License, Version 2.0 (the "License");
4+ * you may not use this file except in compliance with the License.
5+ * You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software
10+ * distributed under the License is distributed on an "AS IS" BASIS,
11+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * See the License for the specific language governing permissions and
13+ * limitations under the License.
14+ */
15+
16+ using System ;
17+ using MongoDB . Bson . Serialization . Conventions ;
18+ using NUnit . Framework ;
19+
20+ namespace MongoDB . BsonUnitTests . Serialization . Conventions
21+ {
22+ [ TestFixture ]
23+ public class StandardDiscriminatorConventionTests
24+ {
25+ [ Test ]
26+ public void TestConstructorThrowsWhenElementNameContainsNulls ( )
27+ {
28+ Assert . Throws < ArgumentException > ( ( ) => { var discriminatorConvention = new ScalarDiscriminatorConvention ( "a\0 b" ) ; } ) ;
29+ }
30+
31+ [ Test ]
32+ public void TestConstructorThrowsWhenElementNameIsNull ( )
33+ {
34+ Assert . Throws < ArgumentNullException > ( ( ) => { var discriminatorConvention = new ScalarDiscriminatorConvention ( null ) ; } ) ;
35+ }
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments