internal static void InstancesEqual(MI_Instance expected, MI_Instance actual, string propertyName) { uint expectedElementCount; expected.GetElementCount(out expectedElementCount); uint actualElementCount; actual.GetElementCount(out actualElementCount); Assert.Equal(expectedElementCount, actualElementCount); for (uint i = 0; i < expectedElementCount; i++) { MI_Flags expectedElementFlags; MI_Type expectedElementType; string expectedElementName = null; MI_Value expectedElementValue = null; expected.GetElementAt(i, out expectedElementName, out expectedElementValue, out expectedElementType, out expectedElementFlags); MI_Flags actualElementFlags; MI_Value actualElementValue = null; MI_Type actualElementType; string actualElementName = null; actual.GetElementAt(i, out actualElementName, out actualElementValue, out actualElementType, out actualElementFlags); Assert.Equal(expectedElementName, actualElementName, "Expect the element names to be the same within the instances"); MIAssert.MIPropertiesEqual(new TestMIProperty(expectedElementValue, expectedElementType, expectedElementFlags), new TestMIProperty(actualElementValue, actualElementType, actualElementFlags), propertyName); } }
public void SimpleEnumerateInstance() { MI_Operation operation = null; this.Session.EnumerateInstances(MI_OperationFlags.MI_OPERATIONFLAGS_DEFAULT_RTTI, MI_OperationOptions.Null, TestEnumerateInstanceNamespace, TestEnumerateInstanceClassName, false, null, out operation); bool moreResults = true; MI_Instance clonedInstance = null; MI_Result secondaryResult; string errorMessage = null; MI_Instance instanceOut = null; MI_Instance errorDetails = null; var res = operation.GetInstance(out instanceOut, out moreResults, out secondaryResult, out errorMessage, out errorDetails); MIAssert.Succeeded(res, "Expect the first GetInstance call to succeed"); MIAssert.Succeeded(secondaryResult, "Expect the logical result of the GetInstance call to succeed"); if (!instanceOut.IsNull) { res = instanceOut.Clone(out clonedInstance); MIAssert.Succeeded(res, "Expect the clone to succeed"); } while (moreResults) { res = operation.GetInstance(out instanceOut, out moreResults, out secondaryResult, out errorMessage, out errorDetails); MIAssert.Succeeded(res, "Expect GetInstance to succeed even if we don't want the result"); MIAssert.Succeeded(secondaryResult, "Expect the logical result of the GetInstance call to succeed even if we don't want the result"); } res = operation.Close(); MIAssert.Succeeded(res, "Expect operation to close successfully"); string className = null; res = clonedInstance.GetClassName(out className); MIAssert.Succeeded(res, "Expect GetClassName to succeed"); Assert.Equal(TestEnumerateInstanceClassName, className, "Expect the class name to be the one we queried"); MI_Value elementValue = null; MI_Type elementType; MI_Flags elementFlags; UInt32 elementIndex; res = clonedInstance.GetElement(TestEnumerateInstanceStringPropertyName, out elementValue, out elementType, out elementFlags, out elementIndex); MIAssert.Succeeded(res, "Expect GetElement to succeed"); Assert.Equal(MI_Type.MI_STRING, elementType, "Expect that the property is registered as a string"); Assert.Equal(TestEnumerateInstanceStringPropertyFlags, elementFlags, "Expect the element flags to also be properly available from the query"); Assert.Equal(TestEnumerateInstanceStringPropertyValue, elementValue.String, "Expect the machine name to have survived the whole journey"); }
internal static MI_Instance CreateBasicSerializableTestInstance() { MI_Instance toSerialize; MI_Result res = StaticFixtures.Application.NewInstance("TestInstance", null, out toSerialize); MIAssert.Succeeded(res); MI_Value valueToSerialize = MI_Value.NewDirectPtr(); valueToSerialize.String = "Test string"; res = toSerialize.AddElement("string", valueToSerialize, MI_Type.MI_STRING, MI_Flags.None); MIAssert.Succeeded(res); return(toSerialize); }
internal static MI_Value ConvertToNativeLayer(object value, CimType cimType) { var cimInstance = value as CimInstance; if (cimInstance != null) { MI_Value retval = new MI_Value(); retval.Instance = cimInstance.InstanceHandle; return(retval); } var arrayOfCimInstances = value as CimInstance[]; if (arrayOfCimInstances != null) { MI_Instance[] arrayOfInstanceHandles = new MI_Instance[arrayOfCimInstances.Length]; for (int i = 0; i < arrayOfCimInstances.Length; i++) { CimInstance inst = arrayOfCimInstances[i]; if (inst == null) { arrayOfInstanceHandles[i] = null; } else { arrayOfInstanceHandles[i] = inst.InstanceHandle; } } MI_Value retval = new MI_Value(); retval.InstanceA = arrayOfInstanceHandles; return(retval); } // TODO: What to do with Unknown types? Ignore? Uncomment and remove return line immediately below. return(CimProperty.ConvertToNativeLayer(value, cimType)); /* * if (cimType != CimType.Unknown) * { * return CimProperty.ConvertToNativeLayer(value, cimType); * } * else * { * return value; * } */ }
public void ReferenceTypesCanBeUsed() { MI_Instance InnerInstance = null; var res = this.Application.NewInstance("TestClass", MI_ClassDecl.Null, out InnerInstance); MIAssert.Succeeded(res); MI_Instance InnerInstance2 = null; res = this.Application.NewInstance("TestClass", MI_ClassDecl.Null, out InnerInstance2); MIAssert.Succeeded(res); try { MI_Value innerValue1 = new MI_Value(); innerValue1.String = "This is a property"; res = InnerInstance.AddElement("InnerInstanceProperty1", innerValue1, innerValue1.Type.Value, MI_Flags.MI_FLAG_BORROW); MIAssert.Succeeded(res); MI_Value innerValue2 = new MI_Value(); innerValue2.String = "This is another property"; res = InnerInstance.AddElement("InnerInstanceProperty2", innerValue2, innerValue2.Type.Value, MI_Flags.MI_FLAG_BORROW); MIAssert.Succeeded(res); MI_Value innerValue3 = new MI_Value(); innerValue3.String = "Still another property"; res = InnerInstance2.AddElement("InnerInstance2Property1", innerValue3, innerValue3.Type.Value, MI_Flags.MI_FLAG_BORROW); MIAssert.Succeeded(res); MI_Value innerValue4 = new MI_Value(); innerValue4.String = "Okay, bored now"; res = InnerInstance2.AddElement("InnerInstance2Property2", innerValue4, innerValue4.Type.Value, MI_Flags.MI_FLAG_BORROW); MIAssert.Succeeded(res); this.value.Reference = InnerInstance; this.TestValueRoundtrip(); this.value.ReferenceA = new MI_Instance[] { InnerInstance, InnerInstance2 }; this.TestValueRoundtrip(); } finally { InnerInstance.Delete(); InnerInstance2.Delete(); } }
public void ValuesNullCanBeUsed() { foreach (MI_Type type in Enum.GetValues(typeof(MI_Type))) { string propertyName = type.ToString() + "_Null"; var res = this.instance.AddElement(propertyName, MI_Value.Null, type, MI_Flags.MI_FLAG_NULL); MIAssert.Succeeded(res, "Expect add element to succeed for {0}", propertyName); MI_Value returnedValue = null; MI_Type elementType; MI_Flags elementFlags = 0; UInt32 elementIndex = 0; res = instance.GetElement(propertyName, out returnedValue, out elementType, out elementFlags, out elementIndex); MIAssert.Succeeded(res, "Expect to get element by name"); var testproperty = new TestMIProperty(returnedValue, elementType, elementFlags); MIAssert.MIPropertiesEqual(new TestMIProperty(this.value, type, MI_Flags.MI_FLAG_NULL), testproperty, propertyName); } }
private void TestValueRoundtrip() { Assert.True(this.value.Type.HasValue, "Expect value to have a value before calling helper"); string propertyName = this.value.Type.ToString(); var res = this.instance.AddElement(propertyName, this.value, this.value.Type.Value, MI_Flags.MI_FLAG_BORROW); MIAssert.Succeeded(res, "Expect add element to succeed"); MI_Value returnedValue = null; MI_Type elementType; MI_Flags elementFlags = 0; UInt32 elementIndex = 0; res = instance.GetElement(propertyName, out returnedValue, out elementType, out elementFlags, out elementIndex); MIAssert.Succeeded(res, "Expect to get element by name"); var testproperty = new TestMIProperty(returnedValue, elementType, elementFlags); MIAssert.MIPropertiesEqual(new TestMIProperty(this.value, this.value.Type.Value, MI_Flags.None), testproperty, propertyName); }
public void CanGetPrimitivePropertyOfInstance() { MI_Value primitiveValue = MI_Value.NewDirectPtr(); primitiveValue.Uint8 = 42; var res = this.instance.AddElement("Foo", primitiveValue, MI_Type.MI_UINT8, MI_Flags.None); MIAssert.Succeeded(res); var cimInstance = new CimInstance(this.instance); var properties = cimInstance.CimInstanceProperties; Assert.Equal(1, properties.Count, "Expect 1 property"); var property = properties["Foo"]; Assert.NotNull(property, "Expect property object to be non-null"); Assert.Equal(CimType.UInt8, property.CimType, "Expect type to roundtrip correctly"); Assert.NotEqual(CimFlags.NullValue, property.Flags & CimFlags.NullValue, "Expect to not get null flags"); Assert.Equal <byte>(42, (byte)property.Value, "Expect property value to match original value"); }
internal static MI_Value ConvertToNativeLayer(object value, CimType cimType) { if (value == null) { return(null); } MI_Value miv = new MI_Value(); var count = 0; IList arrayOfObjects = null; if (value.GetType().IsArray) { arrayOfObjects = (IList)value; count = arrayOfObjects.Count; } switch (cimType) { case CimType.Boolean: miv.Boolean = Convert.ToBoolean(value, CultureInfo.InvariantCulture); return(miv); case CimType.Char16: miv.Char16 = Convert.ToChar(value, CultureInfo.InvariantCulture); return(miv); case CimType.Real32: miv.Real32 = Convert.ToSingle(value, CultureInfo.InvariantCulture); return(miv); case CimType.Real64: miv.Real64 = Convert.ToDouble(value, CultureInfo.InvariantCulture); return(miv); case CimType.SInt16: miv.Sint16 = Convert.ToInt16(value, CultureInfo.InvariantCulture); return(miv); case CimType.SInt32: miv.Sint32 = Convert.ToInt32(value, CultureInfo.InvariantCulture); return(miv); case CimType.SInt64: miv.Sint64 = Convert.ToInt64(value, CultureInfo.InvariantCulture); return(miv); case CimType.SInt8: miv.Sint8 = Convert.ToSByte(value, CultureInfo.InvariantCulture); return(miv); case CimType.String: if (value is Boolean) { miv.String = Convert.ToString(value, CultureInfo.InvariantCulture).ToLower(CultureInfo.InvariantCulture); return(miv); } miv.String = Convert.ToString(value, CultureInfo.InvariantCulture); return(miv); case CimType.UInt16: miv.Uint16 = Convert.ToUInt16(value, CultureInfo.InvariantCulture); return(miv); case CimType.UInt32: miv.Uint32 = Convert.ToUInt32(value, CultureInfo.InvariantCulture); return(miv); case CimType.UInt64: miv.Uint64 = Convert.ToUInt64(value, CultureInfo.InvariantCulture); return(miv); case CimType.UInt8: miv.Uint8 = Convert.ToByte(value, CultureInfo.InvariantCulture); return(miv); case CimType.BooleanArray: if (arrayOfObjects != null) { Boolean[] array = new Boolean[count]; for (int i = 0; i < count; i++) { array[i] = Convert.ToBoolean(arrayOfObjects[i], CultureInfo.InvariantCulture); } miv.BooleanA = array; return(miv); } break; case CimType.Char16Array: if (arrayOfObjects != null) { Char[] array = new Char[count]; for (int i = 0; i < count; i++) { array[i] = Convert.ToChar(arrayOfObjects[i], CultureInfo.InvariantCulture); } miv.Char16A = array; return(miv); } break; case CimType.Real32Array: if (arrayOfObjects != null) { Single[] array = new Single[count]; for (int i = 0; i < count; i++) { array[i] = Convert.ToSingle(arrayOfObjects[i], CultureInfo.InvariantCulture); } miv.Real32A = array; return(miv); } break; case CimType.Real64Array: if (arrayOfObjects != null) { Double[] array = new Double[count]; for (int i = 0; i < count; i++) { array[i] = Convert.ToDouble(arrayOfObjects[i], CultureInfo.InvariantCulture); } miv.Real64A = array; return(miv); } break; case CimType.SInt16Array: if (arrayOfObjects != null) { Int16[] array = new Int16[count]; for (int i = 0; i < count; i++) { array[i] = Convert.ToInt16(arrayOfObjects[i], CultureInfo.InvariantCulture); } miv.Sint16A = array; return(miv); } break; case CimType.SInt32Array: if (arrayOfObjects != null) { Int32[] array = new Int32[count]; for (int i = 0; i < count; i++) { array[i] = Convert.ToInt32(arrayOfObjects[i], CultureInfo.InvariantCulture); } miv.Sint32A = array; return(miv); } break; case CimType.SInt64Array: if (arrayOfObjects != null) { Int64[] array = new Int64[count]; for (int i = 0; i < count; i++) { array[i] = Convert.ToInt64(arrayOfObjects[i], CultureInfo.InvariantCulture); } miv.Sint64A = array; return(miv); } break; case CimType.SInt8Array: if (arrayOfObjects != null) { SByte[] array = new SByte[count]; for (int i = 0; i < count; i++) { array[i] = Convert.ToSByte(arrayOfObjects[i], CultureInfo.InvariantCulture); } miv.Sint8A = array; return(miv); } break; case CimType.StringArray: if (arrayOfObjects != null) { String[] array = new String[count]; for (int i = 0; i < count; i++) { if (arrayOfObjects[i] is Boolean) { array[i] = Convert.ToString(arrayOfObjects[i], CultureInfo.InvariantCulture).ToLower(CultureInfo.InvariantCulture); } else { array[i] = Convert.ToString(arrayOfObjects[i], CultureInfo.InvariantCulture); } } miv.StringA = array; return(miv); } break; case CimType.UInt16Array: if (arrayOfObjects != null) { UInt16[] array = new UInt16[count]; for (int i = 0; i < count; i++) { array[i] = Convert.ToUInt16(arrayOfObjects[i], CultureInfo.InvariantCulture); } miv.Uint16A = array; return(miv); } break; case CimType.UInt32Array: if (arrayOfObjects != null) { UInt32[] array = new UInt32[count]; for (int i = 0; i < count; i++) { array[i] = Convert.ToUInt32(arrayOfObjects[i], CultureInfo.InvariantCulture); } miv.Uint32A = array; return(miv); } break; case CimType.UInt64Array: if (arrayOfObjects != null) { UInt64[] array = new UInt64[count]; for (int i = 0; i < count; i++) { array[i] = Convert.ToUInt64(arrayOfObjects[i], CultureInfo.InvariantCulture); } miv.Uint64A = array; return(miv); } break; case CimType.UInt8Array: if (arrayOfObjects != null) { Byte[] array = new Byte[count]; for (int i = 0; i < count; i++) { array[i] = Convert.ToByte(arrayOfObjects[i], CultureInfo.InvariantCulture); } miv.Uint8A = array; return(miv); } break; case CimType.DateTime: miv.Datetime = MI_Datetime.ConvertToDateTime(value); return(miv); case CimType.DateTimeArray: if (arrayOfObjects != null) { MI_Datetime[] array = new MI_Datetime[count]; for (int i = 0; i < count; i++) { array[i] = MI_Datetime.ConvertToDateTime(arrayOfObjects[i]); } miv.DatetimeA = array; return(miv); } var arrayOfDateTime = value as DateTime[]; if (arrayOfDateTime != null) { MI_Datetime[] array = new MI_Datetime[arrayOfDateTime.Length]; for (int i = 0; i < arrayOfDateTime.Length; i++) { array[i] = MI_Datetime.ConvertToDateTime(arrayOfDateTime[i]); } miv.DatetimeA = array; return(miv); } var arrayOfTimeSpan = value as TimeSpan[]; if (arrayOfTimeSpan != null) { MI_Datetime[] array = new MI_Datetime[arrayOfTimeSpan.Length]; for (int i = 0; i < arrayOfTimeSpan.Length; i++) { array[i] = MI_Datetime.ConvertToDateTime(arrayOfTimeSpan[i]); } miv.DatetimeA = array; return(miv); } return(miv); case CimType.Unknown: miv.String = "UnknownType"; return(miv); case CimType.Reference: case CimType.ReferenceArray: case CimType.Instance: case CimType.InstanceArray: default: break; } return(miv); }
internal static object ConvertFromNativeLayer( MI_Value value, MI_Type type, MI_Flags flags, CimInstance parent = null, bool clone = false) { if ((flags & MI_Flags.MI_FLAG_NULL) == MI_Flags.MI_FLAG_NULL) { return(null); } if (type == MI_Type.MI_INSTANCE || type == MI_Type.MI_REFERENCE) { CimInstance instance = new CimInstance( clone ? value.Instance.Clone() : value.Instance); if (parent != null) { instance.SetCimSessionComputerName(parent.GetCimSessionComputerName()); instance.SetCimSessionInstanceId(parent.GetCimSessionInstanceId()); } return(instance); } else if (type == MI_Type.MI_INSTANCEA || type == MI_Type.MI_REFERENCEA) { CimInstance[] arrayOfInstances = new CimInstance[value.InstanceA.Length]; for (int i = 0; i < value.InstanceA.Length; i++) { MI_Instance h = value.InstanceA[i]; if (h == null) { arrayOfInstances[i] = null; } else { arrayOfInstances[i] = new CimInstance( clone ? h.Clone() : h); if (parent != null) { arrayOfInstances[i].SetCimSessionComputerName(parent.GetCimSessionComputerName()); arrayOfInstances[i].SetCimSessionInstanceId(parent.GetCimSessionInstanceId()); } } } return(arrayOfInstances); } else if (type == MI_Type.MI_DATETIME) { return(value.Datetime.ConvertFromNativeLayer()); } else if (type == MI_Type.MI_DATETIMEA) { int length = value.DatetimeA.Length; object[] arrayOfDatetimes = new object[length]; for (int i = 0; i < length; i++) { arrayOfDatetimes[i] = value.DatetimeA[i].ConvertFromNativeLayer(); } return(arrayOfDatetimes); } else { return(value.GetValue(type)); } }
internal static void MIPropertiesEqual(TestMIProperty expectedProperty, TestMIProperty actualProperty, string propertyName) { // Note that we do not check the flags - the behavior is too inconsistent across scenarios to be worth // the current amount of effort, and it's not a marshalling issue Assert.Equal(expectedProperty.Type, actualProperty.Type, "Expect the property types to be the same"); if ((expectedProperty.Flags & MI_Flags.MI_FLAG_NULL) != 0) { return; } if (expectedProperty.Type == MI_Type.MI_DATETIME) { MI_Datetime expected = (MI_Datetime)expectedProperty.Value; var actual = (MI_Datetime)actualProperty.Value; if (expected.isTimestamp) { MIAssert.MIDatetimesEqual(expected, actual); } } else if (expectedProperty.Type == MI_Type.MI_DATETIMEA) { MI_Datetime[] expected = (MI_Datetime[])expectedProperty.Value; var actual = (MI_Datetime[])actualProperty.Value; Assert.Equal(expected.Length, actual.Length, "Expect the MI_DATETIME arrays to be of the same length"); for (int i = 0; i < expected.Length; i++) { MIAssert.MIDatetimesEqual(expected[i], (MI_Datetime)actual[i]); } } else if (expectedProperty.Type == MI_Type.MI_INSTANCE || expectedProperty.Type == MI_Type.MI_REFERENCE) { MI_Instance expected = (MI_Instance)expectedProperty.Value; MI_Instance actual = actualProperty.Value as MI_Instance; MIAssert.InstancesEqual(expected, actual, propertyName); } else if (expectedProperty.Type == MI_Type.MI_INSTANCEA || expectedProperty.Type == MI_Type.MI_REFERENCEA) { MI_Instance[] expectedArray = (MI_Instance[])expectedProperty.Value; MI_Instance[] actualArray = actualProperty.Value as MI_Instance[]; Assert.Equal(expectedArray.Length, actualArray.Length, "Expect instance/reference arrays to have the same length"); for (int j = 0; j < expectedArray.Length; j++) { MI_Instance expected = expectedArray[j]; MI_Instance actual = actualArray[j]; uint expectedElementCount; expected.GetElementCount(out expectedElementCount); uint actualElementCount; actual.GetElementCount(out actualElementCount); Assert.Equal(expectedElementCount, actualElementCount, "Expect the instance/references arrays to be of the same length"); for (uint i = 0; i < expectedElementCount; i++) { MI_Flags expectedElementFlags; MI_Value expectedElementValue = null; MI_Type expectedElementType; string expectedElementName = null; expected.GetElementAt(i, out expectedElementName, out expectedElementValue, out expectedElementType, out expectedElementFlags); MI_Flags actualElementFlags; MI_Value actualElementValue = null; MI_Type actualElementType; string actualElementName = null; actual.GetElementAt(i, out actualElementName, out actualElementValue, out actualElementType, out actualElementFlags); Assert.Equal(expectedElementName, actualElementName, "Expect the element names to be the same within the instances"); MIAssert.MIPropertiesEqual(new TestMIProperty(expectedElementValue, expectedElementType, expectedElementFlags), new TestMIProperty(actualElementValue, actualElementType, actualElementFlags), propertyName); } } } else if ((expectedProperty.Type & MI_TypeFlags.MI_ARRAY) == MI_TypeFlags.MI_ARRAY) { ICollection collectionValue = actualProperty.Value as ICollection; Assert.NotNull(collectionValue, "Expect the property value to be an ICollection since it was an MI_ARRAY"); Assert.Equal(expectedProperty.Value as ICollection, collectionValue); } else { Assert.Equal(expectedProperty.Value, actualProperty.Value, "Expect the simple MI property values to match"); } }
public TestMIProperty(MI_Value value, MI_Type type, MI_Flags flags) { this.actualValue = value; this.Type = type; this.Flags = flags; }