forked from microsoft/TSS.MSR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestContext.cs
842 lines (741 loc) · 31 KB
/
TestContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See the LICENSE file in the project root for full license information.
*/
using System;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using Tpm2Lib;
namespace Tpm2Tester
{
// These flags are OR-able
public enum TestStatus
{
None = 0,
OK = 1,
Failed = 2,
Aborted = 3
};
// One of these per test thread. Errors are reported to the owning tester
// when these objects are disposed.
public class TestContext : ICommandCallbacks
{
internal TestLogger Logger;
public int Instance;
bool ValidateTestAttributes = true;
StreamWriter TpmIoWriter = null;
// Error reporting needs to be suppressed during fuzzing.
public bool ReportErrors = true;
public bool TpmSeemsToBeDead = false;
ICommandCallbacks ChainedCallbacks;
internal List<TestCaseInfo> FailedTests = new List<TestCaseInfo>();
internal List<TestCaseInfo> TestsWithWarnings = new List<TestCaseInfo>();
internal Dictionary<string, int> FailedAssertions = new Dictionary<string, int>();
// Parameters associated with the currently executed test phase.
// In case of test failure, they are included in the error report.
object[] CurTestPhaseParms;
internal string CurRngSeed = null;
internal int CurTestNum = 0;
internal TestStatus CurTestStatus;
internal string CurTestMethod;
internal DateTime CurTestStartTime;
internal Dictionary<TpmCc, CommandStats> CmdStats =
new Dictionary<TpmCc, CommandStats>();
// Number of TPM commands issued by the current test
internal int NumCommands = 0;
// Number of test assertions evaluated by the current test
internal int NumAsserts = 0;
DateTime LastReportTime = DateTime.MinValue;
internal bool ClearWasExecuted = false;
// Bit mask (set of bit flags) that allows a test method to track whether a
// particular execution branch has been taken since the beginning of the
// current test.
long TestPaths = 0;
// Current command execution info
DateTime CurCmdStartTime;
internal TestContext(TestLogger logger, int instance,
bool validateTestAttributes,
ICommandCallbacks callbacks = null)
{
Logger = logger;
Instance = instance;
ValidateTestAttributes = validateTestAttributes;
ChainedCallbacks = callbacks;
}
public bool IsTestPathTaken(byte pathID)
{
if (pathID >= sizeof(long))
throw new Exception("Extra check number is too large");
return (TestPaths & (1 << pathID)) != 0;
}
public void TestPathDone(byte pathID)
{
if (pathID >= sizeof(long))
throw new Exception("Extra check number is too large");
TestPaths |= (long)1 << pathID;
}
internal void TestStarted(string methodName, string rngSeed = null)
{
if (CurTestMethod == TestLogger.LibTesterInfraName)
{
Debug.Assert(methodName != TestLogger.LibTesterInfraName);
TestCompleted();
}
ClearWasExecuted = false;
CurRngSeed = rngSeed;
Debug.Assert(CmdStats.Count == 0 && NumCommands == 0 && NumAsserts == 0
&& TestPaths == 0 && CurTestPhaseParms == null);
CurTestStatus = TestStatus.OK;
CurTestMethod = methodName;
CurTestStartTime = DateTime.Now;
if (methodName == TestLogger.LibTesterInfraName)
{
// Report errors accumulated during the test run
Logger.ReportErrors(this);
}
else
{
++CurTestNum;
Logger.ThreadSafePrint("[" + (Instance != 0 ? Instance + ":" : "")
+ CurTestMethod + "]");
LastReportTime = DateTime.MinValue;
ReportProgress();
}
}
internal void TestCompleted()
{
bool transitionToInfra = CurTestMethod != TestLogger.LibTesterInfraName;
// FailedTests will be reset
Logger.ReportErrors(this);
Logger.TestCompleted(this);
CurRngSeed = null;
CurTestStatus = TestStatus.None;
CurTestMethod = null;
CurTestPhaseParms = null;
TestPaths = 0;
NumCommands = NumAsserts = 0;
CmdStats.Clear();
if (transitionToInfra)
{
// Attribute the current TPM commands execution span to the Tpm2Tester
// infrastructure
TestStarted(TestLogger.LibTesterInfraName);
}
}
private void ReportProgress()
{
if (Logger.TestCount == 0 ||
(DateTime.Now - LastReportTime).TotalSeconds < 1)
{
return;
}
string toGo = Logger.TimeToGo();
string progress = toGo == null
? "Test " + CurTestNum + " of " + Logger.TestCount
: "Time to go " + toGo;
progress += ": " + NumCommands + " commands; " + NumAsserts + " asserts";
if (Logger.MaxStatsLineLen < progress.Length)
{
Logger.MaxStatsLineLen = progress.Length;
}
Console.Write(progress + "\r");
LastReportTime = DateTime.Now;
}
void ICommandCallbacks.PreCallback(byte[] inBuf, out byte[] modifiedInBuf)
{
if (ChainedCallbacks == null)
{
modifiedInBuf = null;
}
else
{
ChainedCallbacks.PreCallback(inBuf, out modifiedInBuf);
}
CurCmdStartTime = DateTime.Now;
}
static string PrevLogName = "";
static int PrevLogInstance = 1;
// This is installed as the raw command callback handler on the underlying TPM.
// It is used to generate low-level test statistics (number of commands executed,
// etc.), dumps of the conversation with the TPM and to keep a record of all
// command sequences seen that contain types that we haven't seen before.
// In the case of a multi-context TPM this will be called on different threads,
// but locking should be handled safely by MainTestLogger.
void ICommandCallbacks.PostCallback(byte[] inBuf, byte[] outBuf)
{
TimeSpan cmdExecutionTime = DateTime.Now - CurCmdStartTime;
if (inBuf.Length < 10)
{
return;
}
Marshaller m = new Marshaller(inBuf);
TpmSt sessionTag = m.Get<TpmSt>();
uint parmSize = m.Get<UInt32>();
TpmCc commandCode = m.Get<TpmCc>();
if (commandCode == TpmCc.Clear)
{
ClearWasExecuted = true;
}
Marshaller mOut = new Marshaller(outBuf);
TpmSt responseTag = mOut.Get<TpmSt>();
uint responseParamSize = mOut.Get<uint>();
TpmRc responseCode = mOut.Get<TpmRc>();
if (ValidateTestAttributes)
{
// ValidateTestAttributes should not be set for a stress run
LogTestAttributes(sessionTag, commandCode);
try
{
if (responseCode == TpmRc.Success)
ValidateHandleUsage(commandCode, inBuf);
}
catch (Exception)
{
// Invalid command buffer can mess this up
}
}
if (sessionTag.Equals(TpmSt.Null))
return;
// There are two encoding for errors - formats 0 and 1. Decode the error type
uint resultCodeValue = (uint)responseCode;
bool formatOneErrorType = ((resultCodeValue & 0x80) != 0);
uint resultCodeMask = formatOneErrorType ? 0xBFU : 0x97FU;
TpmRc maskedError = (TpmRc)((uint)responseCode & resultCodeMask);
lock (this)
{
// log the command info to the test logger so that it can collect stats
LogCommandExecution(commandCode, maskedError, cmdExecutionTime);
}
#if false
// Keep a copy of successfully executed commands that contain types we have
// not seen so far. This is for tests that need good-command candidate strings,
// like TestCommandDispatcherCoverage.
// Code 0x80280400 is returned by TBS when the command is blocked by Windows.
if (maskedError == TpmRc.Success && !Tpm2.IsTbsError(resultCodeValue))
{
// look at all types in command string. If we have a new type we keep it
CrackedCommand cc = CommandProcessor.CrackCommand(inBuf);
CommandInfo info = CommandInformation.Info.First(x =>
x.CommandCode == cc.Header.CommandCode);
byte[] inStructBytes = Globs.Concatenate(
Globs.GetZeroBytes((int) info.HandleCountIn*4),
cc.CommandParms);
Marshaller mx = new Marshaller(inStructBytes);
TpmStructureBase bb = (TpmStructureBase) mx.Get(info.InStructType, "");
// If a new type is contained, save this command for testing in
// TestDispatcherCoverage.
if (HasNewTypes(bb))
{
ExecutedCommandInfo.Add(inBuf);
}
}
else
#else
if (maskedError != TpmRc.Success)
#endif
{
// If a command failed, we can get here only if the corresponding
// expected error assertion was specified.
++NumAsserts;
}
ReportProgress();
// output TPM IO to a text file for later processing
if (Logger.LogTpmIo)
{
while (TpmIoWriter == null) try
{
string ioLogPath;
if (Logger.LogPath != null)
{
ioLogPath = System.IO.Path.Combine(Logger.LogPath, "tpm_io.txt");
}
else
{
string fileName;
lock (this)
{
fileName = "tpm_io-" + DateTime.Now.ToString("yyyy-MMM-dd-HH");
if (PrevLogName == fileName)
{
fileName += "(" + ++PrevLogInstance + ")";
}
else
{
PrevLogName = fileName;
PrevLogInstance = 1;
}
}
fileName += ".txt";
#if TSS_MIN_API
ioLogPath = fileName;
#else
string docsPath = Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments);
ioLogPath = System.IO.Path.Combine(docsPath, fileName);
#endif
}
TpmIoWriter = new StreamWriter(new FileStream(ioLogPath,
FileMode.Create));
Logger.WriteToLog("Dumping TPM I/O to " + ioLogPath);
}
catch (Exception e)
{
string message = "Failed to open the tpm_io.txt file for writing.\n" +
"Error: " + e.Message;
Logger.WriteErrorToLog(message);
}
// get the test source code line that initiated the command
string caller = "unknown";
#if !TSS_NO_STACK
StackTrace trace = new StackTrace(true);
StackFrame[] frames = trace.GetFrames();
int frameCount = frames.Length;
StackFrame f = null;
// start at 1 to not count the currently executing function
for (int j = 1; j < frameCount; j++)
{
f = frames[j];
if (f.GetMethod().DeclaringType.Assembly == Logger.TestAssembly)
{
caller = f.GetFileName() + ":" + f.GetFileLineNumber();
break;
}
}
#endif
string commandCodeString = Enum.GetName(typeof(TpmCc), commandCode);
string inString = "{MALFORMED COMMAND BUFFER}";
string outString = "{MALFORMED RESPONSE BUFFER}";
try { inString = CommandProcessor.ParseCommand(inBuf); }
catch (Exception) { }
try { outString = CommandProcessor.ParseResponse(commandCodeString, outBuf); }
catch (Exception) { }
lock (this)
{
TpmIoWriter.WriteLine(commandCode);
TpmIoWriter.WriteLine(caller);
TpmIoWriter.WriteLine(">>>> Raw input");
TpmIoWriter.WriteLine(Globs.HexFromByteArray(inBuf));
TpmIoWriter.WriteLine(">>>> Raw output");
TpmIoWriter.WriteLine(Globs.HexFromByteArray(outBuf));
TpmIoWriter.WriteLine(">>>> Parsed input");
TpmIoWriter.WriteLine(inString);
TpmIoWriter.WriteLine(">>>> Parsed output");
TpmIoWriter.WriteLine(outString);
TpmIoWriter.WriteLine("-----------------------------------------");
TpmIoWriter.Flush();
}
}
if (ChainedCallbacks != null)
{
ChainedCallbacks.PostCallback(inBuf, outBuf);
}
} // ICommandCallbacks.PostCallback
static string[] KnownCommandCodes = Enum.GetNames(typeof(TpmCc));
private void LogCommandExecution(TpmCc cc, TpmRc rc, TimeSpan executionTime)
{
NumCommands++;
if(!KnownCommandCodes.Contains(cc.ToString()))
{
cc = TpmCc.None;
}
if (!CmdStats.ContainsKey(cc))
{
CmdStats.Add(cc, new CommandStats());
}
CommandStats stat = CmdStats[cc];
if (rc == TpmRc.Success)
{
stat.NumSuccess++;
stat.SuccessExecutionTime += executionTime;
}
else
{
stat.NumFailures++;
stat.FailureExecutionTime += executionTime;
if (!stat.FailureResponses.Contains(rc))
{
stat.FailureResponses.Add(rc);
}
}
// No need to update stat.CallerTests here. Current test will be logged
// in the command execution statistics in the TestCompleted() notification.
}
// Stores in the test context an arbitrary set of parameters associated with
// the current test phase. In case of a failure the parameters are included
// in the error report. Each parameter is printed on a separate line.
public void ReportParams(params object[] parms)
{
CurTestPhaseParms = parms;
}
string LastExceptionInfo = "";
internal string GetLastExceptionInfo()
{
return LastExceptionInfo;
}
internal string FullTestCaseName (string assertName)
{
return CurTestMethod +
(string.IsNullOrEmpty(assertName) ? "" : ":" + assertName);
}
public void ReportWarning(string s)
{
string location = null;
#if !TSS_NO_STACK
StackTrace callerStack = new StackTrace(true);
StackFrame[] locationFrames = TestContext.GetFramesToReport(callerStack);
location = locationFrames.Length == 0
? TestLogger.GetLocationAsText(callerStack.GetFrames(), "\n")
: TestLogger.GetLocationAsText(locationFrames, "\n");
Logger.WriteErrorToLog("Warning: " + s, ConsoleColor.Cyan);
Logger.WriteErrorToLog(location, ConsoleColor.Yellow);
#endif
var t = new TestCaseInfo
{
TestCase = FullTestCaseName(null),
Message = s,
Parms = null,
Location = location,
StackTrace = null,
RngSeed = CurRngSeed
};
TestsWithWarnings.Add(t);
}
object[] ConcatParams(object[] p1, object[] p2)
{
return p1 == null || p1.Length == 0 ? p2
: p2 == null || p2.Length == 0 ? p1
: p1.Concat(p2).ToArray();
}
internal void ReportTestFailure(bool testException, string assertName, Object[] parms)
{
if (!ReportErrors)
return;
string fullMsg;
#if !TSS_NO_STACK
StackTrace callerStack = null;
#endif
string fullTestCase = FullTestCaseName(assertName);
string fullStackTrace = "";
if (!string.IsNullOrEmpty(assertName))
{
if (FailedAssertions.ContainsKey(fullTestCase))
{
FailedAssertions[fullTestCase]++;
return;
}
FailedAssertions.Add(fullTestCase, 1);
}
if (testException)
{
Debug.Assert(parms.Length == 1);
TestExceptionInfo ei = TestLogger.ParseException((Exception)parms[0]);
fullMsg = ei.message;
#if !TSS_NO_STACK
callerStack = ei.callerStack;
#endif
fullStackTrace = ei.fullStack;
parms = ConcatParams(CurTestPhaseParms,
ei.cmdParms == null ? null
: new object[] {ei.cmdParms});
}
else
{
fullMsg = "Assertion " + assertName + " failed";
#if !TSS_NO_STACK
callerStack = new StackTrace(true);
fullStackTrace = TestLogger.GetLocationAsText(callerStack.GetFrames(), "\n");
parms = ConcatParams(parms, CurTestPhaseParms);
#endif
}
string location = "";
#if !TSS_NO_STACK
StackFrame[] locationFrames = GetFramesToReport(callerStack);
location = locationFrames.Length == 0 ? fullStackTrace
: TestLogger.GetLocationAsText(locationFrames, "\n");
#endif
if (testException)
{
LastExceptionInfo = fullMsg + "\n\n" + location;
}
var t = new TestCaseInfo {
TestCase = fullTestCase,
Message = fullMsg,
Parms = parms,
Location = location,
StackTrace = fullStackTrace,
RngSeed = CurRngSeed,
RepeatedFailures = 1
};
FailedTests.Add(t);
int pos = fullMsg.IndexOf("Details:");
string msg;
string details = "";
if (pos != -1)
{
msg = fullMsg.Substring(0, pos);
details = fullMsg.Substring(pos) + "\n";
}
else
msg = fullMsg + "\n";
details += TestLogger.GetParamsList(parms, "\n");
Logger.ThreadSafePrint(msg, ConsoleColor.Red);
Logger.ThreadSafePrint("To reproduce use option: -seed "
+ CurRngSeed + "\n", ConsoleColor.Gray);
if (details != "")
{
Logger.ThreadSafePrint(details, ConsoleColor.DarkYellow);
}
Logger.ThreadSafePrint(location, ConsoleColor.Yellow);
} // ReportTestFailure
public void ReportTestException(Exception e)
{
ReportTestFailure(true, null, new object[] {e});
}
public void Assert(string label, bool success, params Object[] parms)
{
++NumAsserts;
if (!success)
{
ReportTestFailure(false, label, parms);
CurTestStatus = TestStatus.Failed;
throw new TssAssertException();
}
ReportProgress();
}
static object[] Gather(object parm1, object parm2, object[] parms)
{
return new object[]{ parm1, parm2 }.Concat(parms).ToArray();
}
public void AssertEqual<T>(string label, T val1, T val2,
params Object[] parms)
{
Assert(label, val1.Equals(val2), Gather(val1, val2, parms));
}
public void AssertNotEqual<T>(string label, T val1, T val2,
params Object[] parms)
{
Assert(label, !val1.Equals(val2), Gather(val1, val2, parms));
}
public void AssertEqual(string label, byte[] arr1, byte[] arr2,
params Object[] parms)
{
Assert(label, Globs.ArraysAreEqual(arr1, arr2), Gather(arr1, arr2, parms));
}
public void AssertNotEqual(string label, byte[] arr1, byte[] arr2,
params Object[] parms)
{
Assert(label, !Globs.ArraysAreEqual(arr1, arr2), Gather(arr1, arr2, parms));
}
[Obsolete("Use methods Assert, AssertEqual, and AssertNotEqual instead", false)]
public void Test(string testCase, bool success, params Object[] parms)
{
Assert(testCase, success, parms);
}
#if !TSS_NO_STACK
// Heuristics to return a suitable frame to report as part of an error report.
// Returns the first entity in the stack with a tester-attribute.
public static StackFrame[] GetFramesToReport(StackTrace s)
{
if (s == null)
{
return new StackFrame[0];
}
StackFrame[] stackFrames = s.GetFrames();
if (stackFrames == null)
{
return new StackFrame[0];
}
var framesToReport = new List<StackFrame>();
bool reportThisFrame = false;
for (int j = 0; j < stackFrames.Length; j++)
{
StackFrame f = stackFrames[j];
MethodBase b = f.GetMethod();
MethodInfo m = b is MethodInfo ? (MethodInfo)b : null;
if (m == null)
{
Debug.Assert(!reportThisFrame);
continue;
}
if (reportThisFrame)
{
framesToReport.Add(f);
}
if (Globs.GetAttr<TestAttribute>(f.GetMethod()) != null)
{
if (!reportThisFrame)
{
framesToReport.Add(f);
}
break;
}
reportThisFrame = Globs.GetAttr<TpmCommandAttribute>(f.GetMethod()) != null
|| f.GetMethod().ToString().Contains("Void Test(");
}
return framesToReport.ToArray();
}
#endif // TSS_NO_STACK
//
// Test attributes validation
//
bool TestIsThreadSafe = true;
bool TestWithinMinTpmProfile = true;
bool TestUsesPlatformAuth = false;
NecessaryPrivilege MaximumPrivilege = NecessaryPrivilege.User;
private void LogTestAttributes(TpmSt sessionTag, TpmCc command)
{
if (!TestCategorizer.CommandDefined(command))
return;
if (sessionTag.Equals(TpmSt.Null))
{
TestIsThreadSafe = false;
TestWithinMinTpmProfile = false;
return;
}
bool threadSafe = TestCategorizer.GetThreadSafety(command);
if (!threadSafe) TestIsThreadSafe = false;
if (!TestCategorizer.InProfile0(command))
{
TestWithinMinTpmProfile = false;
return;
}
// else is P0 command. What privileges are needed?
NecessaryPrivilege priv = TestCategorizer.GetNecessaryPrivileges(command);
if (priv > MaximumPrivilege)
MaximumPrivilege = priv;
}
private void ValidateHandleUsage(TpmCc command, byte[] inBuf)
{
CommandInfo commInfo = Tpm2.CommandInfoFromCommandCode(command);
if (commInfo.HandleCountIn == 0)
return;
// else see if any of the inHandles are TpmRh.Platform
CrackedCommand cc = CommandProcessor.CrackCommand(inBuf);
TpmHandle[] handles = cc.Handles;
foreach (TpmHandle h in handles)
{
if (h == TpmRh.Platform) TestUsesPlatformAuth = true;
}
}
internal void ResetAttributeCollection(Tpm2 tpm)
{
TestIsThreadSafe = true;
TestWithinMinTpmProfile = true;
MaximumPrivilege = NecessaryPrivilege.User;
TestUsesPlatformAuth = false;
TpmPassThroughDevice dev = (TpmPassThroughDevice)tpm._GetUnderlyingDevice();
// reset the power-cycle dirty bit
dev.GetPowerCycleDirtyBit();
}
private void SetColorCritical(bool isCritical)
{
if (isCritical)
{
Console.ForegroundColor = ConsoleColor.Red;
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
}
}
// This routine checks that the observed test behavior matches the attributes.
// It is called during a -validate pass on a full TPM simulator
internal void ValidateTestAttributeCollection(MethodInfo test, Tpm2 tpm)
{
var attr = Globs.GetAttr<TestAttribute>(test);
// check that the cumulative attributes match the claimed attributes
if (attr.SpecialNeeds.HasFlag(Special.NotThreadSafe) == TestIsThreadSafe)
{
string s = "<Benign>";
if (!TestIsThreadSafe)
{
// It is safe to say you are not thread safe if you are not (and
// there are commands that the test harness does not understand
// that might lead you to be not thread safe
s = "<Critical - must be fixed>";
SetColorCritical(true);
}
else
{
SetColorCritical(false);
}
Logger.WriteErrorToLog("\nThread safety attribute does not match " +
"observed behavior for {0} {1}", test.Name, s);
}
bool PowerCycleWasIssued = ((TpmPassThroughDevice)tpm._GetUnderlyingDevice())
.GetPowerCycleDirtyBit();
if (attr.SpecialNeeds.HasFlag(Special.PowerControl) != PowerCycleWasIssued)
{
SetColorCritical(PowerCycleWasIssued);
Logger.WriteErrorToLog("\nPower-cycle attribute does not match observed behavior");
}
if ((attr.CommProfile == Profile.MinTPM) != TestWithinMinTpmProfile)
{
SetColorCritical(attr.CommProfile == Profile.MinTPM);
string profile = attr.CommProfile.ToString();
Logger.WriteErrorToLog("\nClaimed profile {0} but this does not match " +
"observed behavior for {1}", profile, test.Name);
}
// change privUsed and privStated into a number so that we can compare
// bigger numbers are more privileged
int privUsed, privStated;
switch (MaximumPrivilege)
{
case NecessaryPrivilege.User: privUsed = 0; break;
case NecessaryPrivilege.Admin: privUsed = 1;break;
case NecessaryPrivilege.Special: privUsed = 2; break;
case NecessaryPrivilege.Debug: privUsed = 3; break;
default: throw new Exception("tester fault");
}
switch (attr.Privileges)
{
case Privileges.StandardUser: privStated = 0; break;
case Privileges.Admin: privStated = 1; break;
case Privileges.Special: privStated = 3; break;
default: throw new Exception("badly attributed test. Need a stated privilege");
}
bool maxPrivOk = (privStated >= privUsed);
bool privilegesMatch = privUsed==privStated || (privStated==3 && privUsed==2);
/*
switch (MaximumPrivilege)
{
case NecessaryPrivilege.User:
if (attr.Privileges != PrivilegesNeeded.StandardUser) maxPrivOk = false;
break;
case NecessaryPrivilege.Admin:
if (attr.Privileges != PrivilegesNeeded.Admin) maxPrivOk = false;
break;
case NecessaryPrivilege.Special:
if (attr.Privileges != PrivilegesNeeded.Special) maxPrivOk = false;
break;
default:
throw new Exception("");
}
// maxPriv is only defined for commands within the MinTPM profile
*/
if (!privilegesMatch)
{
SetColorCritical(!maxPrivOk);
Logger.WriteErrorToLog("\nPrivilege used is {0} but stated {1} for test {2}",
MaximumPrivilege, attr.Privileges, test.Name);
if (!maxPrivOk)
Logger.WriteErrorToLog("critical - must fix.");
}
if (TestUsesPlatformAuth)
{
// not allowed for
if (attr.CommProfile == Profile.MinTPM)
{
SetColorCritical(true);
string profile = attr.CommProfile.ToString();
Logger.WriteErrorToLog("\nClaimed profile {0} but used PlatformAuth: {1}",
profile, test.Name);
}
}
Console.ResetColor();
}
} // class LibTesterContext
} // namespace Tpm2TestSuite