예제 #1
0
        public void ReturnNullIfDeserializingNull()
        {
            Serializer <string> serializer = new SqlNvarcharSerializer();
            string actualDeserializedValue = serializer.Deserialize(null);

            Assert.Null(actualDeserializedValue);
        }
예제 #2
0
        public void ReturnNullIfSerializingNull()
        {
            Serializer <string> serializer = new SqlNvarcharSerializer();

            byte[] actualSerializedValue = serializer.Serialize(null);

            Assert.Null(actualSerializedValue);
        }
예제 #3
0
        public void DeserializeTheSameAsSqlServer(string plaintext, int size)
        {
            Database.Insert(new SqlParameter("@parameter", SqlDbType.NVarChar, size)
            {
                Value = plaintext
            });
            byte[] ciphertextBytes           = Database.SelectCiphertext(SqlDbType.NVarChar);
            byte[] plaintextBytes            = deterministicEncryptionAlgorithm.Decrypt(ciphertextBytes);
            SqlNvarcharSerializer serializer = new SqlNvarcharSerializer(size);
            string expectedPlaintext         = serializer.Deserialize(plaintextBytes);
            string actualPlaintext           = (string)Database.SelectPlaintext(SqlDbType.NVarChar);

            Assert.Equal(expectedPlaintext, actualPlaintext);
        }
예제 #4
0
        public void SerializeTheSameAsSqlServer(string plaintext, int size)
        {
            SqlNvarcharSerializer serializer = new SqlNvarcharSerializer(size);

            byte[] serializedPlaintext = serializer.Serialize(plaintext);
            byte[] expectedCiphertext  = deterministicEncryptionAlgorithm.Encrypt(serializedPlaintext);

            Database.Insert(new SqlParameter("@parameter", SqlDbType.NVarChar, size)
            {
                Value = plaintext
            });
            byte[] actualCiphertext = Database.SelectCiphertext(SqlDbType.NVarChar);

            Assert.Equal(expectedCiphertext, actualCiphertext);
        }
예제 #5
0
        private static (TypeMarker, byte[]) Serialize(JToken propertyValue)
        {
            SqlSerializerFactory  sqlSerializerFactory  = new SqlSerializerFactory();
            SqlNvarcharSerializer sqlNvarcharSerializer = new SqlNvarcharSerializer(-1);

            switch (propertyValue.Type)
            {
            case JTokenType.Undefined:
                Debug.Assert(false, "Undefined value cannot be in the JSON");
                return(default, null);

            case JTokenType.Null:
                Debug.Assert(false, "Null type should have been handled by caller");
                return(TypeMarker.Null, null);

            case JTokenType.Boolean:
                return(TypeMarker.Boolean, sqlSerializerFactory.GetDefaultSerializer <bool>().Serialize(propertyValue.ToObject <bool>()));

            case JTokenType.Float:
                return(TypeMarker.Double, sqlSerializerFactory.GetDefaultSerializer <double>().Serialize(propertyValue.ToObject <double>()));

            case JTokenType.Integer:
                return(TypeMarker.Long, sqlSerializerFactory.GetDefaultSerializer <long>().Serialize(propertyValue.ToObject <long>()));

            case JTokenType.String:
                return(TypeMarker.String, sqlNvarcharSerializer.Serialize(propertyValue.ToObject <string>()));

            case JTokenType.Array:
                return(TypeMarker.Array, sqlNvarcharSerializer.Serialize(propertyValue.ToString()));

            case JTokenType.Object:
                return(TypeMarker.Object, sqlNvarcharSerializer.Serialize(propertyValue.ToString()));

            default:
                throw new InvalidOperationException($" Invalid or Unsupported Data Type Passed : {propertyValue.Type}");
            }
        }
예제 #6
0
        public void ThrowWhenPropertySizeOutOfRange(int size)
        {
            SqlNvarcharSerializer serializer = new SqlNvarcharSerializer();

            Assert.Throws <ArgumentOutOfRangeException>(() => serializer.Size = size);
        }