コード例 #1
0
        public void TestParseHexStringEmpty()
        {
            byte[] expected = new byte[0];
            var    actual   = BsonUtils.ParseHexString(string.Empty);

            Assert.AreEqual(expected, actual);
        }
コード例 #2
0
            private BsonValue PreprocessHex(BsonValue value)
            {
                var array = value as BsonArray;

                if (array != null)
                {
                    for (var i = 0; i < array.Count; i++)
                    {
                        array[i] = PreprocessHex(array[i]);
                    }
                    return(array);
                }

                var document = value as BsonDocument;

                if (document != null)
                {
                    if (document.ElementCount == 1 && document.GetElement(0).Name == "$hex" && document[0].IsString)
                    {
                        var hex   = document[0].AsString;
                        var bytes = BsonUtils.ParseHexString(hex);
                        return(new BsonBinaryData(bytes));
                    }

                    for (var i = 0; i < document.ElementCount; i++)
                    {
                        document[i] = PreprocessHex(document[i]);
                    }
                    return(document);
                }

                return(value);
            }
コード例 #3
0
        private object ParseExpectedValue(object value)
        {
            var stringValue = value as string;

            if (stringValue != null)
            {
                if (stringValue.StartsWith("DateTime:"))
                {
                    var styles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal;
                    return(DateTime.Parse(stringValue.Substring(9), CultureInfo.InvariantCulture, styles));
                }

                if (stringValue.StartsWith("Guid:"))
                {
                    return(Guid.Parse(stringValue.Substring(5)));
                }

                if (stringValue.StartsWith("Hex:"))
                {
                    return(BsonUtils.ParseHexString(stringValue.Substring(4)));
                }
            }

            return(value);
        }
コード例 #4
0
        public void ParseHexString_should_throw_when_string_is_null()
        {
            var exception = Record.Exception(() => BsonUtils.ParseHexString(null));

            var argumentNullException = exception.Should().BeOfType <ArgumentNullException>().Subject;

            argumentNullException.ParamName.Should().Be("s");
        }
コード例 #5
0
        public void TestParseHexString()
        {
            var expected = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 255 };
            var value    = "000102030405060708090a0b0c0d0e0f10ff";
            var actual   = BsonUtils.ParseHexString(value);

            Assert.AreEqual(expected, actual);
        }
コード例 #6
0
        public void TestParseHexStringOdd()
        {
            var expected = new byte[] { 0, 15 };
            var value    = "00f";
            var actual   = BsonUtils.ParseHexString(value);

            Assert.AreEqual(expected, actual);
        }
        public void WriteJson_should_have_expected_result_when_using_wrapped_json_writer(string nullableHexBytes, BsonBinarySubType subType, string expectedResult, GuidRepresentation resultGuidRepresentation)
        {
            var subject = new BsonBinaryDataConverter();
            var value   = nullableHexBytes == null ? null : new BsonBinaryData(BsonUtils.ParseHexString(nullableHexBytes), subType, resultGuidRepresentation);

            var result = WriteJsonUsingWrappedJsonWriter(subject, value);

            result.Should().Be(expectedResult);
        }
        public void WriteJson_should_have_expected_result_when_using_native_bson_writer(string nullableHexBytes, BsonBinarySubType subType, string expectedResult, GuidRepresentation resultGuidRepresentation)
        {
            var subject = new BsonBinaryDataConverter();
            var value   = nullableHexBytes == null ? null : new BsonBinaryData(BsonUtils.ParseHexString(nullableHexBytes), subType);

            var result = WriteJsonUsingNativeBsonWriter(subject, value, mustBeNested: true);

            result.Should().Equal(ToBson(expectedResult, resultGuidRepresentation));
        }
コード例 #9
0
        public void BsonBinaryData_constructor_with_a_Guid_and_a_representation_should_return_expected_result(GuidRepresentation guidRepresentation, BsonBinarySubType expectedSubType, string expectedBytes)
        {
            GuidMode.Set(GuidRepresentationMode.V3);

            var guid = new Guid("00112233445566778899aabbccddeeff");

            var result = new BsonBinaryData(guid, guidRepresentation);

            result.SubType.Should().Be(expectedSubType);
            result.Bytes.Should().Equal(BsonUtils.ParseHexString(expectedBytes));
        }
コード例 #10
0
        public void TestUtf16LittleEndianAutoDetect()
        {
            var bytes = BsonUtils.ParseHexString("fffe7b00200022007800220020003a002000310020007d00");

            using (var memoryStream = new MemoryStream(bytes))
                using (var streamReader = new StreamReader(memoryStream, true))
                {
                    var document = BsonSerializer.Deserialize <BsonDocument>(streamReader);
                    Assert.Equal(1, document["x"].AsInt32);
                }
        }
コード例 #11
0
        public void TestUtf8AutoDetect()
        {
            var bytes = BsonUtils.ParseHexString("7b20227822203a2031207d");

            using (var memoryStream = new MemoryStream(bytes))
                using (var streamReader = new StreamReader(memoryStream, true))
                {
                    var document = BsonSerializer.Deserialize <BsonDocument>(streamReader);
                    Assert.Equal(1, document["x"].AsInt32);
                }
        }
コード例 #12
0
        private string UnescapeUnicodeCharacters(string value)
        {
            var pattern   = @"\\u[0-9a-fA-F]{4}";
            var unescaped = Regex.Replace(value, pattern, match =>
            {
                var bytes = BsonUtils.ParseHexString(match.Value.Substring(2, 4));
                var c     = (char)(bytes[0] << 8 | bytes[1]);
                return(c < 0x20 ? match.Value : new string(c, 1));
            });

            return(unescaped);
        }
コード例 #13
0
        public void ReadBsonType_should_throw_when_bson_type_is_invalid(string hexBytes, string expectedElementName)
        {
            var bytes           = BsonUtils.ParseHexString(hexBytes.Replace(" ", ""));
            var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"{expectedElementName}\". Are you using the latest driver version?";

            using (var memoryStream = new MemoryStream(bytes))
                using (var subject = new BsonBinaryReader(memoryStream))
                {
                    Action action = () => BsonSerializer.Deserialize <BsonDocument>(subject);

                    action.ShouldThrow <FormatException>().WithMessage(expectedMessage);
                }
        }
コード例 #14
0
        public void TestUtf8()
        {
            var encoding = Utf8Encodings.Strict;

            var bytes = BsonUtils.ParseHexString("7b20227822203a2031207d");

            using (var memoryStream = new MemoryStream(bytes))
                using (var streamReader = new StreamReader(memoryStream, encoding))
                {
                    var document = BsonSerializer.Deserialize <BsonDocument>(streamReader);
                    Assert.AreEqual(1, document["x"].AsInt32);
                }
        }
コード例 #15
0
        public void TestUtf16LittleEndian()
        {
            var encoding = new UnicodeEncoding(false, false, true);

            var bytes = BsonUtils.ParseHexString("7b00200022007800220020003a002000310020007d00");

            using (var memoryStream = new MemoryStream(bytes))
                using (var streamReader = new StreamReader(memoryStream, encoding))
                {
                    var document = BsonSerializer.Deserialize <BsonDocument>(streamReader);
                    Assert.Equal(1, document["x"].AsInt32);
                }
        }
コード例 #16
0
        public static string Unescape(string value)
        {
            var index = 0;

            while ((index = value.IndexOf("\\u", index)) != -1)
            {
                var hex   = value.Substring(index + 2, 4);
                var bytes = BsonUtils.ParseHexString(hex);
                var c     = (char)((bytes[0] << 8) | bytes[1]);
                var s     = new string(c, 1);
                value = value.Substring(0, index) + s + value.Substring(index + 6);
                index = index + 1;
            }
            return(value);
        }
コード例 #17
0
        private void RunDecodeErrorsTest(BsonDocument test)
        {
            JsonDrivenHelper.EnsureAllFieldsAreValid(test, "type", "description", "bson");

            var bson = BsonUtils.ParseHexString(test["bson"].AsString);

            var exception = Record.Exception(() =>
            {
                using (var stream = new MemoryStream(bson))
                    using (var reader = new BsonBinaryReader(stream))
                    {
                        while (!reader.IsAtEndOfFile())
                        {
                            _ = BsonSerializer.Deserialize <BsonDocument>(reader);
                        }
                    }
            });

            AssertException(exception);
        }
コード例 #18
0
        public void Explicit_decoding_with_python_legacy_representation_should_work_as_expected()
        {
            GuidMode.Set(GuidRepresentationMode.V3);

            var bytes      = BsonUtils.ParseHexString("00112233445566778899aabbccddeeff");
            var binaryData = new BsonBinaryData(bytes, BsonBinarySubType.UuidLegacy);

            var exception = Record.Exception(() => binaryData.ToGuid());

            exception.Should().BeOfType <InvalidOperationException>();

            exception = Record.Exception(() => binaryData.ToGuid(GuidRepresentation.Standard));
            exception.Should().BeOfType <InvalidOperationException>();

            exception = Record.Exception(() => binaryData.ToGuid(GuidRepresentation.Unspecified));
            exception.Should().BeOfType <ArgumentException>();

            var result = binaryData.ToGuid(GuidRepresentation.PythonLegacy);

            result.Should().Be(new Guid("00112233445566778899aabbccddeeff"));
        }
コード例 #19
0
        public UnifiedGridFsUploadOperation Build(string targetBucketId, BsonDocument arguments)
        {
            var bucket = _entityMap.GetBucket(targetBucketId);

            string filename             = null;
            GridFSUploadOptions options = null;

            byte[] source = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "chunkSizeBytes":
                    options = options ?? new GridFSUploadOptions();
                    options.ChunkSizeBytes = argument.Value.AsInt32;
                    break;

                case "filename":
                    filename = argument.Value.AsString;
                    break;

                case "source":
                    var sourceDocument = argument.Value.AsBsonDocument;
                    JsonDrivenHelper.EnsureAllFieldsAreValid(sourceDocument, "$$hexBytes");
                    var sourceString = sourceDocument["$$hexBytes"].AsString;
                    if (sourceString.Length % 2 != 0)
                    {
                        throw new FormatException("$$hexBytes must have an even number of bytes.");
                    }
                    source = BsonUtils.ParseHexString(sourceString);
                    break;

                default:
                    throw new FormatException($"Invalid GridFsUploadOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedGridFsUploadOperation(bucket, filename, source, options));
        }
コード例 #20
0
        // private methods
        private BsonBinaryData ReadExtendedJson(Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.JsonSerializer serializer)
        {
            ReadExpectedPropertyName(reader, "$binary");
            var bytes = Convert.FromBase64String(ReadStringValue(reader));

            ReadExpectedPropertyName(reader, "$type");
            BsonBinarySubType subType;

            ReadToken(reader);
            switch (reader.TokenType)
            {
            case Newtonsoft.Json.JsonToken.Integer:
                subType = (BsonBinarySubType)(reader.Value is int?(int)reader.Value : Convert.ToInt32(reader.Value, NumberFormatInfo.InvariantInfo));
                break;

            case Newtonsoft.Json.JsonToken.String:
                subType = (BsonBinarySubType)BsonUtils.ParseHexString((string)reader.Value).Single();
                break;

            default:
                var message = string.Format("Cannot read BsonBinarySubType from token: {0}.", reader.TokenType);
                throw new Newtonsoft.Json.JsonReaderException(message);
            }
            ReadEndObject(reader);

            GuidRepresentation guidRepresentation;

            switch (subType)
            {
            case BsonBinarySubType.UuidLegacy: guidRepresentation = GuidRepresentation.CSharpLegacy; break;

            case BsonBinarySubType.UuidStandard: guidRepresentation = GuidRepresentation.Standard; break;

            default: guidRepresentation = GuidRepresentation.Unspecified; break;
            }

            return(new BsonBinaryData(bytes, subType, guidRepresentation));
        }
コード例 #21
0
        public void Implicit_encoding_with_pyton_legacy_representation_should_work_as_expected()
        {
            GuidMode.Set(GuidRepresentationMode.V3);

            var collection = GetCollection <ClassWithGuidIdUsingPythonLegacyRepresentation>();
            var guid       = Guid.Parse("00112233445566778899aabbccddeeff");
            var document   = new ClassWithGuidIdUsingPythonLegacyRepresentation {
                Id = guid
            };

            DropCollection(collection);
            collection.InsertOne(document);

            var insertedDocument = FindSingleDocument(collection);

            insertedDocument.Id.Should().Be(guid);

            var insertedDocumentAsBsonDocument = FindSingleDocumentAsBsonDocument(collection);
            var binaryData = (BsonBinaryData)insertedDocumentAsBsonDocument["_id"];

            binaryData.SubType.Should().Be(BsonBinarySubType.UuidLegacy);
            binaryData.Bytes.Should().Equal(BsonUtils.ParseHexString("00112233445566778899aabbccddeeff"));
        }
コード例 #22
0
 public void TestParseHexStringNull()
 {
     Assert.Throws <ArgumentNullException>(() => BsonUtils.ParseHexString(null));
 }
        public void ReadJson_should_return_expected_result_when_using_wrapped_bson_reader(string json, GuidRepresentation guidRepresentation, string nullableHexBytes, BsonBinarySubType subType)
        {
            var subject        = new BsonBinaryDataConverter();
            var expectedResult = nullableHexBytes == null ? null : new BsonBinaryData(BsonUtils.ParseHexString(nullableHexBytes), subType);

            var result = ReadJsonUsingWrappedBsonReader <BsonBinaryData>(subject, ToBson(json, guidRepresentation), mustBeNested: true, guidRepresentation: guidRepresentation);

            result.Should().Be(expectedResult);
        }
コード例 #24
0
 public void TestParseHexStringNull()
 {
     var actual = BsonUtils.ParseHexString(null);
 }
コード例 #25
0
 public void TestParseHexStringInvalid2()
 {
     var actual = BsonUtils.ParseHexString("00 1");
 }
コード例 #26
0
        public void ParseHexString_should_return_expected_result(string s, byte[] expectedResult)
        {
            var result = BsonUtils.ParseHexString(s);

            result.Should().Equal(expectedResult);
        }
コード例 #27
0
        private BsonValue ParseExtendedJson(BsonDocument document)
        {
            if (document.ElementCount > 0)
            {
                switch (document.GetElement(0).Name)
                {
                case "$binary":
                    if (document.ElementCount == 2 && document.GetElement(1).Name == "$type")
                    {
                        var bytes   = Convert.FromBase64String(document[0].AsString);
                        var subType = (BsonBinarySubType)(int)BsonUtils.ParseHexString(document[1].AsString)[0];
                        return(new BsonBinaryData(bytes, subType));
                    }
                    break;

                case "$code":
                    if (document.ElementCount == 1)
                    {
                        var code = document[0].AsString;
                        return(new BsonJavaScript(code));
                    }
                    else if (document.ElementCount == 2 && document.GetElement(1).Name == "$scope")
                    {
                        var code  = document[0].AsString;
                        var scope = document[1].AsBsonDocument;
                        return(new BsonJavaScriptWithScope(code, scope));
                    }
                    break;

                case "$date":
                    if (document.ElementCount == 1)
                    {
                        switch (document[0].BsonType)
                        {
                        case BsonType.DateTime:
                            return(document[0].AsBsonDateTime);

                        case BsonType.Document:
                        {
                            var dateDocument = document[0].AsBsonDocument;
                            if (dateDocument.ElementCount == 1 && dateDocument.GetElement(0).Name == "$numberLong")
                            {
                                var formattedString        = dateDocument[0].AsString;
                                var millisecondsSinceEpoch = long.Parse(formattedString, NumberFormatInfo.InvariantInfo);
                                return(new BsonDateTime(millisecondsSinceEpoch));
                            }
                        }
                        break;

                        case BsonType.Double:
                        case BsonType.Int32:
                        case BsonType.Int64:
                        {
                            var millisecondsSinceEpoch = document[0].ToInt64();
                            return(new BsonDateTime(millisecondsSinceEpoch));
                        }

                        case BsonType.String:
                        {
                            var formattedString = document[0].AsString;
                            var dateTime        = DateTime.Parse(formattedString, DateTimeFormatInfo.InvariantInfo);
                            return(new BsonDateTime(dateTime));
                        }
                        }
                    }
                    break;

                case "$maxKey":
                    if (document.ElementCount == 1)
                    {
                        return(BsonMaxKey.Value);
                    }
                    break;

                case "$minKey":
                    if (document.ElementCount == 1)
                    {
                        return(BsonMinKey.Value);
                    }
                    break;

                case "$oid":
                    if (document.ElementCount == 1)
                    {
                        var hexBytes = document[0].AsString;
                        var objectId = ObjectId.Parse(hexBytes);
                        return(new BsonObjectId(objectId));
                    }
                    break;

                case "$regex":
                    if (document.ElementCount == 2 && document.GetElement(1).Name == "$options")
                    {
                        var pattern = document[0].AsString;
                        var options = document[1].AsString;
                        return(new BsonRegularExpression(pattern, options));
                    }
                    break;

                case "$symbol":
                    if (document.ElementCount == 1)
                    {
                        var name = document[0].AsString;
                        return(BsonSymbolTable.Lookup(name));
                    }
                    break;

                case "$timestamp":
                    if (document.ElementCount == 1)
                    {
                        var timestampDocument = document[0].AsBsonDocument;
                        var timestamp         = timestampDocument[0].ToInt32();
                        var increment         = timestampDocument[1].ToInt32();
                        return(new BsonTimestamp(timestamp, increment));
                    }
                    break;
                }
            }

            return(document);
        }
コード例 #28
0
        private void RunValid(BsonDocument definition)
        {
            // see the pseudo code in the specification

            var B = BsonUtils.ParseHexString(((string)definition["bson"]).ToLowerInvariant());
            var E = ((string)definition["extjson"]).Replace(" ", "");

            byte[] cB;
            if (definition.Contains("canonical_bson"))
            {
                cB = BsonUtils.ParseHexString(((string)definition["canonical_bson"]).ToLowerInvariant());
            }
            else
            {
                cB = B;
            }

            string cE;

            if (definition.Contains("canonical_extjson"))
            {
                cE = ((string)definition["canonical_extjson"]).Replace(" ", "");
            }
            else
            {
                cE = E;
            }

            EncodeBson(DecodeBson(B)).Should().Equal(cB, "B -> cB");

            if (B != cB)
            {
                EncodeBson(DecodeBson(cB)).Should().Equal(cB, "cB -> cB");
            }

            if (definition.Contains("extjson"))
            {
                EncodeExtjson(DecodeBson(B)).Should().Be(cE, "B -> cE");
                EncodeExtjson(DecodeExtjson(E)).Should().Be(cE, "E -> cE");

                if (B != cB)
                {
                    EncodeExtjson(DecodeBson(cB)).Should().Be(cE, "cB -> cE");
                }

                if (E != cE)
                {
                    EncodeExtjson(DecodeExtjson(cE)).Should().Be(cE, "cE -> cE");
                }

                if (!definition.GetValue("lossy", false).ToBoolean())
                {
                    EncodeBson(DecodeExtjson(E)).Should().Equal(cB, "E -> cB");

                    if (E != cE)
                    {
                        EncodeBson(DecodeExtjson(cE)).Should().Equal(cB, "cE -> cB");
                    }
                }
            }
        }
コード例 #29
0
        [InlineData("G")] // character just after "F"
        public void ParseHexString_should_throw_when_string_is_invalid(string s)
        {
            var exception = Record.Exception(() => BsonUtils.ParseHexString(s));

            exception.Should().BeOfType <FormatException>();
        }
コード例 #30
0
 public void TestParseHexStringInvalid2()
 {
     Assert.Throws <FormatException>(() => BsonUtils.ParseHexString("00 1"));
 }