Ejemplo n.º 1
0
        public void DependencyAnalyzerTest()
        {
            string[] expectedResult = new[]
            {
                "api/v1/module/bid/{0}/user/{1}/dashboard/settings",
                "api/v1/module-management/bids/{0}/versions/{1}/dashboard/settings/hierarchy/",
                "api/v1/module-management/bids/{0}/{1}/versions/{2}/dashboard/settings/hierarchy/items",
                "api/v1/module/bid/{0}/user/{1}/personal/settings"
            };

            List <string> results = new List <string>();

            using (FileData assemblyFile = FileData.Create(Array.Empty <string>()))
            {
                InstantCompiler.Run(assemblyFile.FilePath, TestClassPath);
                UpdateConfig(ConfigJsonPath, assemblyFile);

                IAnalyzer[] analyzers = new EngineFactory().CreateAnalyzers(ConfigJsonPath);

                foreach (IAnalyzer analyzer in analyzers)
                {
                    IReadOnlyCollection <string> result = analyzer.Analyze();

                    results.AddRange(result);
                }
            }

            results.ShouldAllBeEquivalentTo(expectedResult);
        }
Ejemplo n.º 2
0
        private void AddFile(PhysicalLocation physicalLocation)
        {
            if (physicalLocation?.FileLocation == null)
            {
                return;
            }

            Uri    uri      = physicalLocation.FileLocation.Uri;
            string key      = UriHelper.MakeValidUri(uri.OriginalString);
            string filePath = key;

            if (uri.IsAbsoluteUri && uri.IsFile)
            {
                filePath = uri.LocalPath;
            }

            FileData fileData = FileData.Create(
                uri,
                _dataToInsert,
                _mimeTypeClassifier(filePath));


            if (!_fileInfoDictionary.ContainsKey(key))
            {
                _fileInfoDictionary.Add(
                    key,
                    fileData);
            }
        }
Ejemplo n.º 3
0
        internal FileData VisitDictionaryValueNullChecked(string key, FileData node)
        {
            FileLocation fileLocation = FileLocation.CreateFromFilesDictionaryKey(key);

            bool workToDo = false;
            bool overwriteExistingData = _dataToInsert.Includes(OptionallyEmittedData.OverwriteExistingData);

            workToDo |= (node.Hashes == null || overwriteExistingData) && _dataToInsert.Includes(OptionallyEmittedData.Hashes);
            workToDo |= (node.Contents?.Binary == null || overwriteExistingData) && _dataToInsert.Includes(OptionallyEmittedData.TextFiles);
            workToDo |= (node.Contents?.Binary == null || overwriteExistingData) && _dataToInsert.Includes(OptionallyEmittedData.BinaryFiles);

            if (workToDo)
            {
                fileLocation.TryReconstructAbsoluteUri(_run.OriginalUriBaseIds, out Uri uri);

                Encoding encoding = null;

                if (!string.IsNullOrWhiteSpace(node.Encoding))
                {
                    try
                    {
                        encoding = Encoding.GetEncoding(node.Encoding);
                    }
                    catch (ArgumentException) { }
                }

                int length = node.Length;
                node        = FileData.Create(uri, _dataToInsert, node.MimeType, encoding: encoding);
                node.Length = length;
            }

            return(base.VisitFileData(node));
        }
Ejemplo n.º 4
0
        private static Run CreateRun(
            IEnumerable <string> analysisTargets,
            OptionallyEmittedData dataToInsert,
            IEnumerable <string> invocationTokensToRedact,
            IEnumerable <string> invocationPropertiesToLog,
            string defaultFileEncoding = null)
        {
            var run = new Run
            {
                Invocations         = new List <Invocation>(),
                DefaultFileEncoding = defaultFileEncoding
            };

            if (analysisTargets != null)
            {
                run.Files = new Dictionary <string, FileData>();

                foreach (string target in analysisTargets)
                {
                    string fileDataKey = UriHelper.MakeValidUri(target);

                    var fileData = FileData.Create(
                        new Uri(target, UriKind.RelativeOrAbsolute),
                        dataToInsert);

                    run.Files[fileDataKey] = fileData;
                }
            }

            var invocation = Invocation.Create(dataToInsert.Includes(OptionallyEmittedData.EnvironmentVariables), invocationPropertiesToLog);

            // TODO we should actually redact across the complete log file context
            // by a dedicated rewriting visitor or some other approach.

            if (invocationTokensToRedact != null)
            {
                invocation.CommandLine = Redact(invocation.CommandLine, invocationTokensToRedact);
                invocation.Machine     = Redact(invocation.Machine, invocationTokensToRedact);
                invocation.Account     = Redact(invocation.Account, invocationTokensToRedact);

                if (invocation.WorkingDirectory != null)
                {
                    invocation.WorkingDirectory.Uri = Redact(invocation.WorkingDirectory.Uri, invocationTokensToRedact);
                }

                if (invocation.EnvironmentVariables != null)
                {
                    string[] keys = invocation.EnvironmentVariables.Keys.ToArray();

                    foreach (string key in keys)
                    {
                        string value = invocation.EnvironmentVariables[key];
                        invocation.EnvironmentVariables[key] = Redact(value, invocationTokensToRedact);
                    }
                }
            }

            run.Invocations.Add(invocation);
            return(run);
        }
Ejemplo n.º 5
0
        public void Add(string filePath)
        {
            Assert(!string.IsNullOrWhiteSpace(filePath));
            Assert(!Contains(filePath));

            m_files.Add(FileData.Create(filePath));
        }
Ejemplo n.º 6
0
        private void CaptureFile(Uri uri)
        {
            if (uri == null)
            {
                return;
            }

            _run.Files = _run.Files ?? new Dictionary <string, FileData>();

            string fileDataKey = UriHelper.MakeValidUri(uri.OriginalString);

            if (_run.Files.ContainsKey(fileDataKey))
            {
                // Already populated
                return;
            }

            Encoding encoding = null;

            try
            {
                encoding = Encoding.GetEncoding(_run.DefaultFileEncoding);
            }
            catch (ArgumentException) { } // Unrecognized or null encoding name

            _run.Files[fileDataKey] = FileData.Create(uri, _dataToInsert, null, encoding);
        }
Ejemplo n.º 7
0
 /// <summary>非同步方式上傳檔案與資料
 /// <para>使用 UploadMultiFilesProgressChanged 與 UploadMultiFilesCompleted 事件回覆上傳進度與結果 </para>
 /// </summary>
 /// <param name="uri">欲傳送的網址</param>
 /// <param name="values">參數資料</param>
 /// <param name="fileName">包含路徑的檔案名稱</param>
 /// <param name="userToken">使用者自訂的附加資訊</param>
 /// <exception cref="ArgumentNullException">uri 不得為空值。</exception>
 /// <exception cref="ArgumentException">values 與 fileName 不可同時為空值。</exception>
 /// <exception cref="FileNotFoundException">找不到檔案</exception>
 public void UploadValuesAndFileAsync(Uri uri, NameValueCollection values, string fileName, object userToken)
 {
     if (string.IsNullOrEmpty(fileName) && (values == null || values.Count == 0))
     {
         throw new ArgumentException("values 與 fileName 不可同時為空值。");
     }
     UploadMultiFilesAsync(uri, values, new FileData[] { FileData.Create("file", fileName, CJF.Utility.ConvUtils.GetContentType(fileName)) }, userToken);
 }
Ejemplo n.º 8
0
        public void Add(string filePath, string relativeDestDir)
        {
            Assert(!string.IsNullOrWhiteSpace(filePath));
            Assert(!string.IsNullOrWhiteSpace(relativeDestDir));
            Assert(!Contains(filePath, relativeDestDir));

            m_files.Add(FileData.Create(filePath, relativeDestDir));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 获取文件信息回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Request_GetFileInfo_Compelted(object sender, WP7HttpEventArgs e)
        {
            FileData data = FileData.Create(e.Result, e.IsError);

            if (e.IsError)
            {
                MessageBox.Show(data.ErrorMessage);
            }
            else
            {
                MessageBox.Show("文件名:" + data.FileName + "\n" + "文件缩略图:" + data.Thumbnail + "\n" + "文件预览地址:" + data.Preview + "\n" + "文件下载地址:" + data.Uri + "\n");
            }
        }
Ejemplo n.º 10
0
        private static Run CreateRun(
            IEnumerable <string> analysisTargets,
            LoggingOptions loggingOptions,
            IEnumerable <string> invocationTokensToRedact,
            IEnumerable <string> invocationPropertiesToLog)
        {
            var run = new Run();

            if (analysisTargets != null)
            {
                run.Files = new Dictionary <string, FileData>();

                foreach (string target in analysisTargets)
                {
                    string fileDataKey = UriHelper.MakeValidUri(target);

                    var fileData = FileData.Create(
                        new Uri(target, UriKind.RelativeOrAbsolute),
                        loggingOptions);

                    run.Files.Add(fileDataKey, fileData);
                }
            }

            run.Invocation = Invocation.Create(loggingOptions.Includes(LoggingOptions.PersistEnvironment), invocationPropertiesToLog);

            // TODO we should actually redact across the complete log file context
            // by a dedicated rewriting visitor or some other approach.
            if (invocationTokensToRedact != null)
            {
                run.Invocation.CommandLine      = Redact(run.Invocation.CommandLine, invocationTokensToRedact);
                run.Invocation.Machine          = Redact(run.Invocation.Machine, invocationTokensToRedact);
                run.Invocation.Account          = Redact(run.Invocation.Account, invocationTokensToRedact);
                run.Invocation.WorkingDirectory = Redact(run.Invocation.WorkingDirectory, invocationTokensToRedact);

                if (run.Invocation.EnvironmentVariables != null)
                {
                    string[] keys = run.Invocation.EnvironmentVariables.Keys.ToArray();

                    foreach (string key in keys)
                    {
                        string value = run.Invocation.EnvironmentVariables[key];
                        run.Invocation.EnvironmentVariables[key] = Redact(value, invocationTokensToRedact);
                    }
                }
            }
            return(run);
        }
Ejemplo n.º 11
0
        public void InsertOptionalDataVisitorTests_VisitDictionaryValueNullChecked_ValidEncoding()
        {
            var visitor = new InsertOptionalDataVisitor(OptionallyEmittedData.OverwriteExistingData | OptionallyEmittedData.TextFiles);

            visitor.VisitRun(new Run()); // VisitDictionaryValueNullChecked requires a non-null run

            string uriString = "file:///C:/src/foo.cs";

            FileData fileData = FileData.Create(new Uri(uriString), mimeType: "text/x-csharp", encoding: Encoding.UTF8);

            fileData.Length = 12345;

            FileData outputFileData = visitor.VisitDictionaryValueNullChecked(uriString, fileData);

            outputFileData.MimeType.Should().Be(fileData.MimeType);
            outputFileData.Encoding.Should().Be(fileData.Encoding);
            outputFileData.Length.Should().Be(fileData.Length);
        }
Ejemplo n.º 12
0
        private static void AddRunToSarifOutput(SarifLog log, string inputPath, string id, IList <Result> results)
        {
            var run = new Run();

            // run.StableId = method.ContainingType.FullPathName();
            //run.Id = String.Format("[{0}] {1}", method.ContainingType.FullPathName(), method.ToSignatureString());
            run.Id        = id;
            run.Tool      = Tool.CreateFromAssemblyData();
            run.Tool.Name = "ScopeProgramAnalysis";
            run.Files     = new Dictionary <string, FileData>();
            var fileDataKey = UriHelper.MakeValidUri(inputPath);
            var fileData    = FileData.Create(new Uri(fileDataKey), false);

            run.Files.Add(fileDataKey, fileData);

            run.Results = results;

            log.Runs.Add(run);
        }
Ejemplo n.º 13
0
        private static void GatherFiles(string path, bool isDestination, List <FileData> files)
        {
            foreach (string file in Directory.EnumerateDirectories(path))
            {
                GatherFiles(file, isDestination, files);
            }

            foreach (string file in Directory.EnumerateFiles(path))
            {
                if (Path.GetFileName(file) != "Thumbs.db")
                {
                    var fd = FileData.Create(file, isDestination);
                    if (fd != null)
                    {
                        files.Add(fd);
                    }
                }
            }
        }
Ejemplo n.º 14
0
        private void CaptureFile(Uri uri)
        {
            if (uri == null)
            {
                return;
            }

            _run.Files = _run.Files ?? new Dictionary <string, FileData>();

            string fileDataKey = UriHelper.MakeValidUri(uri.OriginalString);

            if (_run.Files.ContainsKey(fileDataKey))
            {
                // Already populated
                return;
            }

            _run.Files[fileDataKey] = FileData.Create(uri, _loggingOptions);
        }
Ejemplo n.º 15
0
        internal FileData VisitDictionaryValueNullChecked(string key, FileData node)
        {
            Uri uri;

            if (!Uri.TryCreate(key, UriKind.RelativeOrAbsolute, out uri))
            {
                return(node);
            }

            bool workToDo = false;

            workToDo |= node.Hashes == null && _loggingOptions.Includes(LoggingOptions.ComputeFileHashes);
            workToDo |= node.Contents == null && _loggingOptions.Includes(LoggingOptions.PersistFileContents);

            if (workToDo)
            {
                node = FileData.Create(uri, _loggingOptions, node.MimeType);
            }

            return(base.VisitFileData(node));
        }
Ejemplo n.º 16
0
        public void EngineTest(string[] configData, string[] fileData, string[] expectedResult)
        {
            List <string> results = new List <string>();

            using (FileData dataFile = FileData.Create(fileData))
            {
                configData = PrepareConfigData(configData, dataFile);

                using (FileData configFile = FileData.Create(configData))
                {
                    IAnalyzer[] analyzers = new EngineFactory().CreateAnalyzers(configFile.FilePath);

                    foreach (IAnalyzer analyzer in analyzers)
                    {
                        IReadOnlyCollection <string> result = analyzer.Analyze();

                        results.AddRange(result);
                    }
                }
            }

            results.ShouldAllBeEquivalentTo(expectedResult);
        }
Ejemplo n.º 17
0
        public override FileData VisitFileData(FileData node)
        {
            FileLocation fileLocation = node.FileLocation;

            bool workToDo = false;
            bool overwriteExistingData = _dataToInsert.Includes(OptionallyEmittedData.OverwriteExistingData);

            workToDo |= (node.Hashes == null || overwriteExistingData) && _dataToInsert.Includes(OptionallyEmittedData.Hashes);
            workToDo |= (node.Contents?.Text == null || overwriteExistingData) && _dataToInsert.Includes(OptionallyEmittedData.TextFiles);
            workToDo |= (node.Contents?.Binary == null || overwriteExistingData) && _dataToInsert.Includes(OptionallyEmittedData.BinaryFiles);

            if (workToDo)
            {
                if (fileLocation.TryReconstructAbsoluteUri(_run.OriginalUriBaseIds, out Uri uri))
                {
                    Encoding encoding = null;

                    string encodingText = node.Encoding ?? _run.DefaultFileEncoding;

                    if (!string.IsNullOrWhiteSpace(encodingText))
                    {
                        try
                        {
                            encoding = Encoding.GetEncoding(encodingText);
                        }
                        catch (ArgumentException) { }
                    }

                    int length = node.Length;
                    node        = FileData.Create(uri, _dataToInsert, node.MimeType, encoding: encoding);
                    node.Length = length;
                }
            }

            return(base.VisitFileData(node));
        }
Ejemplo n.º 18
0
        internal FileData VisitDictionaryValueNullChecked(string key, FileData node)
        {
            Uri uri;

            if (!Uri.TryCreate(key, UriKind.RelativeOrAbsolute, out uri))
            {
                return(node);
            }

            bool workToDo = false;

            workToDo |= node.Hashes == null && _dataToInsert.Includes(OptionallyEmittedData.Hashes);
            workToDo |= node.Contents == null && _dataToInsert.Includes(OptionallyEmittedData.TextFiles);
            workToDo |= node.Contents == null && _dataToInsert.Includes(OptionallyEmittedData.BinaryFiles);

            if (workToDo)
            {
                // TODO: we should convert node.Encoding to a .NET equivalent and pass it here
                // https://github.com/Microsoft/sarif-sdk/issues/934
                node = FileData.Create(uri, _dataToInsert, node.MimeType, encoding: null);
            }

            return(base.VisitFileData(node));
        }
Ejemplo n.º 19
0
 /// <summary>
 /// 创建文件
 /// </summary>
 /// <param name="id"></param>
 /// <param name="name"></param>
 /// <param name="contentType"></param>
 /// <param name="contentLength"></param>
 /// <returns></returns>
 public FileData Create(string id, string name, string contentType, long contentLength)
 {
     return(FileData.Create(id, name, contentType, contentLength));
 }
Ejemplo n.º 20
0
 public bool Contains(string filePath, string destFolder) => m_files.Contains(FileData.Create(filePath, destFolder));
Ejemplo n.º 21
0
 public bool Contains(string filePath) => m_files.Contains(FileData.Create(filePath));