Esempio n. 1
0
        public void ActivateEncodableTypeMatrix(
            EncodingType encoderType,
            Type systemType
            )
        {
            int matrixDimension = RandomSource.NextInt32(2) + 2;

            int[] dimensions = new int[matrixDimension];
            SetMatrixDimensions(dimensions);
            int   elementsCount = ElementsFromDimension(dimensions);
            Array array         = Array.CreateInstance(systemType, elementsCount);



            ExpandedNodeId dataTypeId = NodeId.Null;

            for (int i = 0; i < array.Length; i++)
            {
                IEncodeable testObject = CreateDefaultEncodableType(systemType) as IEncodeable;
                array.SetValue(testObject, i);
                if (dataTypeId == NodeId.Null)
                {
                    dataTypeId = testObject.TypeId;
                }
            }

            string      objectName  = "Matrix";
            BuiltInType builtInType = BuiltInType.Variant;

            Matrix matrix = new Matrix(array, builtInType, dimensions);

            var      encoderStream = new MemoryStream();
            IEncoder encoder       = CreateEncoder(encoderType, Context, encoderStream, systemType);

            encoder.WriteArray(objectName, matrix, matrix.TypeInfo.ValueRank, builtInType);
            Dispose(encoder);

            var buffer = encoderStream.ToArray();

            switch (encoderType)
            {
            case EncodingType.Json:
                PrettifyAndValidateJson(Encoding.UTF8.GetString(buffer));
                break;
            }
            var      decoderStream = new MemoryStream(buffer);
            IDecoder decoder       = CreateDecoder(encoderType, Context, decoderStream, systemType);
            object   result        = decoder.ReadArray(objectName, matrix.TypeInfo.ValueRank, BuiltInType.Variant, dataTypeId);

            Dispose(decoder);

            TestContext.Out.WriteLine("Result:");
            TestContext.Out.WriteLine(result);
            object expected = AdjustExpectedBoundaryValues(encoderType, builtInType, matrix);

            string encodeInfo = $"Encoder: {encoderType} Type: Matrix of {systemType}. Expected is diferent from result.";

            Assert.AreEqual(expected, result, encodeInfo);
            Assert.IsTrue(Opc.Ua.Utils.IsEqual(expected, result), "Opc.Ua.Utils.IsEqual failed to compare expected and result. " + encodeInfo);
        }
Esempio n. 2
0
 /// <summary>
 /// Sets random array dimensions between 2 and 10.
 /// </summary>
 protected void SetMatrixDimensions(int[] dimensions)
 {
     for (int i = 0; i < dimensions.Length; i++)
     {
         dimensions[i] = RandomSource.NextInt32(8) + 2;
     }
 }
        private ApplicationTestData RandomApplicationTestData()
        {
            // TODO: set to discoveryserver
            ApplicationType appType     = (ApplicationType)m_randomSource.NextInt32((int)ApplicationType.ClientAndServer);
            string          pureAppName = m_dataGenerator.GetRandomString("en");

            pureAppName = Regex.Replace(pureAppName, @"[^\w\d\s]", "");
            string           pureAppUri       = Regex.Replace(pureAppName, @"[^\w\d]", "");
            string           appName          = "UA " + pureAppName;
            StringCollection domainNames      = RandomDomainNames();
            string           localhost        = domainNames[0];
            string           privateKeyFormat = m_randomSource.NextInt32(1) == 0 ? "PEM" : "PFX";
            string           appUri           = ("urn:localhost:opcfoundation.org:" + pureAppUri.ToLower()).Replace("localhost", localhost);
            string           prodUri          = "http://opcfoundation.org/UA/" + pureAppUri;
            StringCollection discoveryUrls    = new StringCollection();
            int port = (m_dataGenerator.GetRandomInt16() & 0x1fff) + 50000;

            switch (appType)
            {
            case ApplicationType.Client:
                appName += " Client";
                break;

            case ApplicationType.ClientAndServer:
                appName += " Client and";
                goto case ApplicationType.Server;

            case ApplicationType.DiscoveryServer:
                appName      += " DiscoveryServer";
                discoveryUrls = RandomDiscoveryUrl(domainNames, 4840, pureAppUri);
                break;

            case ApplicationType.Server:
                appName      += " Server";
                discoveryUrls = RandomDiscoveryUrl(domainNames, port, pureAppUri);
                break;
            }
            ApplicationTestData testData = new ApplicationTestData {
                ApplicationName  = appName,
                ApplicationUri   = appUri,
                DomainNames      = domainNames,
                Subject          = String.Format("CN={0},O=OPC Foundation,DC={1}", appName, localhost),
                PrivateKeyFormat = privateKeyFormat
            };

            return(testData);
        }
Esempio n. 4
0
        private async Task ApproveRequestsAllApplications()
        {
            Skip.If(!_fixture.RegistrationOk);
            foreach (var application in _applicationTestSet)
            {
                foreach (var requestId in application.RequestIds)
                {
                    var request = await _certificateRequest.ReadAsync(requestId);

                    Assert.Equal(CertificateRequestState.New, request.State);
                    // approve/reject 50% randomly
                    bool reject = _randomSource.NextInt32(100) > 50;
                    await _certificateRequest.ApproveAsync(requestId, reject);

                    request = await _certificateRequest.ReadAsync(requestId);

                    Assert.Equal(reject ? CertificateRequestState.Rejected : CertificateRequestState.Approved, request.State);
                }
            }
        }
Esempio n. 5
0
        public IList <ApplicationTestData> ApplicationTestSet(int count, bool invalidateSet)
        {
            var testDataSet = new List <ApplicationTestData>();

            for (int i = 0; i < count; i++)
            {
                var testData = RandomApplicationTestData();
                if (invalidateSet)
                {
                    ApplicationRecordDataType appRecord = testData.ApplicationRecord;
                    appRecord.ApplicationId = new NodeId(Guid.NewGuid());
                    switch (i % 4)
                    {
                    case 0:
                        appRecord.ApplicationUri = m_dataGenerator.GetRandomString();
                        break;

                    case 1:
                        appRecord.ApplicationType = (ApplicationType)m_randomSource.NextInt32(100) + 8;
                        break;

                    case 2:
                        appRecord.ProductUri = m_dataGenerator.GetRandomString();
                        break;

                    case 3:
                        appRecord.DiscoveryUrls = appRecord.ApplicationType == ApplicationType.Client ?
                                                  RandomDiscoveryUrl(new StringCollection {
                            "xxxyyyzzz"
                        }, m_randomSource.NextInt32(0x7fff), "TestClient") : null;
                        break;

                    case 4:
                        appRecord.ServerCapabilities = appRecord.ApplicationType == ApplicationType.Client ?
                                                       RandomServerCapabilities() : null;
                        break;

                    case 5:
                        appRecord.ApplicationId = new NodeId(100);
                        break;
                    }
                }
                testDataSet.Add(testData);
            }
            return(testDataSet);
        }
Esempio n. 6
0
        public void ReEncodeVariantArrayInDataValue(
            EncodingType encoderType,
            BuiltInType builtInType
            )
        {
            Assume.That(builtInType != BuiltInType.Null);
            int   arrayDimension = RandomSource.NextInt32(99) + 1;
            Array randomData     = DataGenerator.GetRandomArray(builtInType, false, arrayDimension, true);
            var   variant        = new Variant(randomData, new TypeInfo(builtInType, 1));

            EncodeDecodeDataValue(encoderType, BuiltInType.Variant, variant);
        }
Esempio n. 7
0
        public void EncodeBuiltInTypeMatrixAsVariantInDataValueToNonReversibleJson(
            BuiltInType builtInType
            )
        {
            Assume.That(builtInType != BuiltInType.Null);
            int matrixDimension = RandomSource.NextInt32(8) + 2;

            int[] dimensions = new int[matrixDimension];
            SetMatrixDimensions(dimensions);
            int    elements   = ElementsFromDimension(dimensions);
            Array  randomData = DataGenerator.GetRandomArray(builtInType, false, elements, true);
            var    variant    = new Variant(new Matrix(randomData, builtInType, dimensions));
            string json       = EncodeDataValue(EncodingType.Json, BuiltInType.Variant, variant, false);

            PrettifyAndValidateJson(json);
        }
Esempio n. 8
0
        public void ReEncodeVariantMatrixInDataValue(
            EncodingType encoderType,
            BuiltInType builtInType
            )
        {
            Assume.That(builtInType != BuiltInType.Null);
            int matrixDimension = RandomSource.NextInt32(8) + 2;

            int[] dimensions = new int[matrixDimension];
            SetMatrixDimensions(dimensions);
            int   elements   = ElementsFromDimension(dimensions);
            Array randomData = DataGenerator.GetRandomArray(builtInType, false, elements, true);
            var   variant    = new Variant(new Matrix(randomData, builtInType, dimensions));

            EncodeDecodeDataValue(encoderType, BuiltInType.Variant, variant);
        }
Esempio n. 9
0
        public void EncodeMatrixInArray(
            EncodingType encoderType,
            BuiltInType builtInType
            )
        {
            Assume.That(builtInType != BuiltInType.Null);
            int matrixDimension = RandomSource.NextInt32(8) + 2;

            int[] dimensions = new int[matrixDimension];
            SetMatrixDimensions(dimensions);
            int   elements   = ElementsFromDimension(dimensions);
            Array randomData = DataGenerator.GetRandomArray(builtInType, false, elements, true);
            var   matrix     = new Matrix(randomData, builtInType, dimensions);

            string encodeInfo = $"Encoder: {encoderType} Type:{builtInType}";
            Type   type       = TypeInfo.GetSystemType(builtInType, -1);

            TestContext.Out.WriteLine(encodeInfo);
            TestContext.Out.WriteLine("Expected:");
            TestContext.Out.WriteLine(matrix);
            var      encoderStream = new MemoryStream();
            IEncoder encoder       = CreateEncoder(encoderType, Context, encoderStream, type);

            encoder.WriteArray(builtInType.ToString(), matrix, matrix.TypeInfo.ValueRank, builtInType);
            Dispose(encoder);

            var buffer = encoderStream.ToArray();

            switch (encoderType)
            {
            case EncodingType.Json:
                PrettifyAndValidateJson(Encoding.UTF8.GetString(buffer));
                break;
            }
            var      decoderStream = new MemoryStream(buffer);
            IDecoder decoder       = CreateDecoder(encoderType, Context, decoderStream, type);
            object   result        = decoder.ReadArray(builtInType.ToString(), matrix.TypeInfo.ValueRank, builtInType);

            Dispose(decoder);

            TestContext.Out.WriteLine("Result:");
            TestContext.Out.WriteLine(result);
            object expected = AdjustExpectedBoundaryValues(encoderType, builtInType, matrix);

            Assert.AreEqual(expected, result, encodeInfo);
            Assert.IsTrue(Opc.Ua.Utils.IsEqual(expected, result), "Opc.Ua.Utils.IsEqual failed to compare expected and result. " + encodeInfo);
        }
        public void EncodeMatrixInArrayOverflow(
            EncodingType encoderType,
            BuiltInType builtInType
            )
        {
            Assume.That(builtInType != BuiltInType.Null);
            int matrixDimension = RandomSource.NextInt32(8) + 2;

            int[] dimensions = new int[matrixDimension];
            SetMatrixDimensions(dimensions);
            int   elements   = ElementsFromDimension(dimensions);
            Array randomData = DataGenerator.GetRandomArray(builtInType, false, elements, true);
            var   matrix     = new Matrix(randomData, builtInType, dimensions);

            for (int ii = 0; ii < matrixDimension; ii++)
            {
                if (ii % 2 == 0)
                {
                    matrix.Dimensions[ii] = 0x40000001;
                }
                else
                {
                    matrix.Dimensions[ii] = 4;
                }
            }

            string encodeInfo = $"Encoder: {encoderType} Type:{builtInType}";
            Type   type       = TypeInfo.GetSystemType(builtInType, -1);

            TestContext.Out.WriteLine(encodeInfo);
            TestContext.Out.WriteLine("Expected:");
            TestContext.Out.WriteLine(matrix);
            var      encoderStream = new MemoryStream();
            IEncoder encoder       = CreateEncoder(encoderType, Context, encoderStream, type);

            switch (encoderType)
            {
            case EncodingType.Json:
            {
                // check such matrix cannot be initialized when encoded into Json format
                // the exception is thrown while trying to WriteStructureMatrix into the arrray
                Assert.Throws(
                    typeof(ServiceResultException),
                    () => {
                        encoder.WriteArray(builtInType.ToString(), matrix, matrix.TypeInfo.ValueRank, builtInType);
                    });
                Dispose(encoder);
                return;
            }
            }

            encoder.WriteArray(builtInType.ToString(), matrix, matrix.TypeInfo.ValueRank, builtInType);
            Dispose(encoder);

            var buffer = encoderStream.ToArray();

            switch (encoderType)
            {
            case EncodingType.Json:
                PrettifyAndValidateJson(Encoding.UTF8.GetString(buffer));
                break;
            }
            var      decoderStream = new MemoryStream(buffer);
            IDecoder decoder       = CreateDecoder(encoderType, Context, decoderStream, type);

            switch (encoderType)
            {
            case EncodingType.Json:
            {
                // If this would execute:
                // check such matrix cannot be initialized when decoding from Json format
                // the exception is thrown while trying to construct the Matrix
                Assert.Throws(
                    typeof(ServiceResultException),
                    () => {
                        decoder.ReadArray(builtInType.ToString(), matrix.TypeInfo.ValueRank, builtInType);
                    });
                break;
            }

            case EncodingType.Xml:
            {
                // check such matrix cannot be initialized when decoding from Xml format
                // the exception is thrown while trying to construct the Matrix but is caught and handled
                Assert.Throws(
                    typeof(ArgumentException),
                    () => {
                        decoder.ReadArray(builtInType.ToString(), matrix.TypeInfo.ValueRank, builtInType);
                    });
                break;
            }

            case EncodingType.Binary:
            {
                // check such matrix cannot be initialized when decoding from Binary format
                // the exception is thrown before trying to construct the Matrix
                Assert.Throws(
                    typeof(ServiceResultException),
                    () => {
                        decoder.ReadArray(builtInType.ToString(), matrix.TypeInfo.ValueRank, builtInType);
                    });
                break;
            }
            }
            Dispose(decoder);
        }
        public void MatrixOverflow(
            EncodingType encoderType,
            BuiltInType builtInType
            )
        {
            Assume.That(builtInType != BuiltInType.Null);
            int matrixDimension = RandomSource.NextInt32(8) + 2;

            int[] dimensions = new int[matrixDimension];
            SetMatrixDimensions(dimensions);
            int   elements   = ElementsFromDimension(dimensions);
            Array randomData = DataGenerator.GetRandomArray(builtInType, false, elements, true);

            var matrix = new Matrix(randomData, builtInType, dimensions);

            for (int ii = 0; ii < matrixDimension; ii++)
            {
                if (ii % 2 == 0)
                {
                    matrix.Dimensions[ii] = 0x40000001;
                }
                else
                {
                    matrix.Dimensions[ii] = 4;
                }
            }

            var variant = new Variant(matrix);

            string encodeInfo = $"Encoder: {encoderType} Type:{builtInType}";

            TestContext.Out.WriteLine(encodeInfo);
            TestContext.Out.WriteLine(variant);
            DataValue expected = CreateDataValue(builtInType, variant);

            Assert.IsNotNull(expected, "Expected DataValue is Null, " + encodeInfo);
            TestContext.Out.WriteLine("Expected:");
            TestContext.Out.WriteLine(expected);
            var      encoderStream = new MemoryStream();
            IEncoder encoder       = CreateEncoder(encoderType, Context, encoderStream, typeof(DataValue));

            encoder.WriteDataValue("DataValue", expected);
            Dispose(encoder);
            var    buffer = encoderStream.ToArray();
            string jsonFormatted;

            switch (encoderType)
            {
            case EncodingType.Json:
                jsonFormatted = PrettifyAndValidateJson(Encoding.UTF8.GetString(buffer));
                break;
            }
            var      decoderStream = new MemoryStream(buffer);
            IDecoder decoder       = CreateDecoder(encoderType, Context, decoderStream, typeof(DataValue));

            switch (encoderType)
            {
            case EncodingType.Json:
            {
                // check such matrix cannot be initialized when decoding from Json format
                // the exception is thrown while trying to construct the Matrix
                Assert.Throws(
                    typeof(ArgumentException),
                    () => {
                        decoder.ReadDataValue("DataValue");
                    });
                break;
            }

            case EncodingType.Xml:
            {
                // check such matrix cannot be initialized when decoding from Xml format
                // the exception is thrown while trying to construct the Matrix but is caught and handled
                decoder.ReadDataValue("DataValue");
                break;
            }

            case EncodingType.Binary:
            {
                // check such matrix cannot be initialized when decoding from Binary format
                // the exception is thrown before trying to construct the Matrix
                Assert.Throws(
                    typeof(ServiceResultException),
                    () => {
                        decoder.ReadDataValue("DataValue");
                    });
                break;
            }
            }
            Dispose(decoder);
        }