internal bool RunTestSession()

in Tpm2Tester/TestSubstrate/TestFramework.cs [207:413]


        internal bool RunTestSession()
        {
            Console.ResetColor();

            if (TesterInstance != 0 && TestCfg.Verbose)
            {
                WriteToLog("Tester Instance: " + TesterInstance);
            }

            TheTestState = new TestState(TestCfg.TestParams);

            // Instruct library to use deterministic "random" numbers for test
            // repeatability (at least in the case of single-threaded tests)
            if (TestCfg.SeededRng)
            {
                Globs.SetRngSeed(TestCfg.RngSeed);
            }

#if !TSS_NO_TCP
            if (TestCfg.TpmPath != "" && !StartTpm())
            {
                return false;
            }
#endif
            // Create a new device of class requested.  The object returned is a wrapper
            // of type TpmPassThruDevice, which allows callbacks to be installed.
            var tpmDevice = CreateTpmDevice(MainTestContext);

            if (tpmDevice == null)
                return false;

            int initialErrorCount = NumTestFailures();

            if (TestCfg.StopTpm)
                goto ExitProgram;

#if !TSS_NO_TCP
            if (TestCfg.TpmAutoRestart)
            {
                // Set the sockets to timeout so that we can propagate a failure to
                // the caller if the TPM seems to be busted.
                TestCfg.TheTcpTpmDevice.SetSocketTimeout((int)(TestConfig.NumMinsBeforeAutoRestart * 60));
            }

            if (TestCfg.TransportLogsDirectory != null)
            {
                if (!(UnderlyingTpmDevice is TcpTpmDevice))
                {
                    WriteErrorToLog("Can only log TCP TPM device");
                    return false;
                }
                if (TestCfg.StressMode == true)
                {
                    WriteErrorToLog("Cannot log in stress mode");
                    return false;
                }
                InitTransportLogger(TestCfg.TransportLogsDirectory,
                                              (TcpTpmDevice)UnderlyingTpmDevice);
            }
#endif
            if (TestCfg.DumpConsole)
            {
                string consoleLogFile;
                if (TestCfg.TransportLogsDirectory != null)
                {
                    consoleLogFile = System.IO.Path.Combine(TestCfg.TransportLogsDirectory,
                                                            "console.txt");
                }
                else
                {
#if TSS_MIN_API
                    consoleLogFile = "console.txt";
#else
                    string docsPath = Environment.GetFolderPath(
                                                Environment.SpecialFolder.MyDocuments);
                    consoleLogFile = System.IO.Path.Combine(docsPath, "console.txt");
#endif
                }
                MainTestLogger.SetLogFileName(consoleLogFile);
            }

#if false
            // Testing TPM with minimal Tpm2Tester infrastructure initialization

            tpmDevice.PowerCycle();
            tpmDevice.SignalNvOn();

            var tpm = new Tpm2(tpmDevice);
            tpm.Startup(Su.Clear);

            // Insert test code here

            return true;
#endif
            TestCfg.HasTRM = tpmDevice.HasRM();
            PowerUpTpm(tpmDevice);

            // "Stress-mode" creates threads that randomly execute TPM tests. If the
            // TPM is multi-context (like TBS) we open one context per stress thread.
            // If the device is not multi-context then Tpm2Tester uses its own emulated RM.
            if (TestCfg.HasTRM)
                TpmFactory = CreateTpmDevice;

            TestCfg.DoInit = TestCfg.DoInit && tpmDevice.PlatformAvailable();

            // Initialize the testCtx. It will try to communicate with the TPM.
            try
            {
                if (!InitTpmDevice(tpmDevice))
                    return false;
            }
            catch (Exception e)
            {
                var exceptionInfo = TestLogger.ParseException(e);
                WriteErrorToLog("Failed to communicate with the TPM during " +
                                          "initialization.\n" + exceptionInfo.message);
                return false;
            }

            if (TestCfg.DumpTpmInfo)
            {
                // -tpmInfo option was used. Quit after dumping TPM information.
                goto ExitProgram;
            }

            TestCfg.TestsToRun = CmdLine.GenerateTestSet(tpmDevice);

            if (TestCfg.TestsToRun.Count == 0)
                goto ExitProgram;

            // Ready to start tests...
            NotifyTestStartStop(true);

            // In the following we perform the requested test run with the requested
            // test routine set.

            // TestValidation tests that the test use the privileges that they state.
            // Test validation runs should be performed as new tests are added.
            if (FuzzMode)
            {
                if (TestCfg.Verbose)
                    WriteToLog("\nRunning TPM tests in fuzz mode\n");

                if (TestCfg.TestEndTime == DateTime.MinValue)
                {
                    if (TestCfg.Verbose)
                        WriteToLog("Test duration defaulting to 60 minutes");
                    TestCfg.TestEndTime = DateTime.Now + new TimeSpan(0, 60, 0);
                }
                RunFuzz(TestCfg.TestsToRun.ToArray(), TestCfg.TestEndTime);

#if !TSS_NO_TCP
                if (TestCfg.TpmPath != "")
                {
                    KillTpmProcess();
                }
#endif
            }
            else if (TestCfg.StressMode)
            {
                if (TestCfg.Verbose)
                    WriteToLog("\nRunning TPM tests in stress mode\n");

                if (TestCfg.TestEndTime == DateTime.MinValue)
                {
                    if (TestCfg.Verbose)
                        WriteToLog("Test duration defaulting to {0} minutes",
                                                TestConfig.DefaultStressModeDuration);
                    TestCfg.TestEndTime = DateTime.Now +
                                    new TimeSpan(0, TestConfig.DefaultStressModeDuration, 0);
                }

                double stateSaveProb = TestCfg.TestS3 ? TestConfig.StateSaveProbability : 0.0;
                RunStress(TestCfg.TestsToRun.ToArray(), TestCfg.NumThreads, TestCfg.TestEndTime,
                                    TestCfg.StopOnFirstError, stateSaveProb, TestCfg.TestNvAvailable);
            }
            else
            {
                if (TestCfg.Verbose)
                    WriteToLog("\nRunning TPM tests sequentially\n");

                RunTestsSerially(TestCfg.TestsToRun.ToArray(), TestCfg.TestEndTime);
            }

            // Tests completed
            string reportFileName = null;
#if !WLK
            if (TestCfg.ShowHTML)
                reportFileName = StemFile + "_" + TesterInstance.ToString() + ".Report.html";
#endif
            MainTestLogger.TestRunCompleted(reportFileName);

            ExitProgram:
            if (TestCfg.DoInit)
                MainTpm.Shutdown(Su.Clear);

            if (TestCfg.UseKeyCache)
                UnderlyingTpmDevice.SignalKeyCacheOff();

            tpmDevice.Dispose();

            if (!Debugger.IsAttached)
            {
                NotifyTestStartStop(false);
            }
            return initialErrorCount >= NumTestFailures();
        } // RunTestSession()