github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/wasip1/logging/logging_test.go (about)

     1  package logging
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bananabytelabs/wazero/api"
     7  	"github.com/bananabytelabs/wazero/internal/logging"
     8  	"github.com/bananabytelabs/wazero/internal/testing/require"
     9  	. "github.com/bananabytelabs/wazero/internal/wasip1"
    10  	"github.com/bananabytelabs/wazero/internal/wasm"
    11  )
    12  
    13  type testFunctionDefinition struct {
    14  	name string
    15  	*wasm.FunctionDefinition
    16  }
    17  
    18  // Name implements the same method as documented on api.FunctionDefinition.
    19  func (f *testFunctionDefinition) Name() string {
    20  	return f.name
    21  }
    22  
    23  func TestIsInLogScope(t *testing.T) {
    24  	clockTimeGet := &testFunctionDefinition{name: ClockTimeGetName}
    25  	fdRead := &testFunctionDefinition{name: FdReadName}
    26  	pollOneoff := &testFunctionDefinition{name: PollOneoffName}
    27  	procExit := &testFunctionDefinition{name: ProcExitName}
    28  	randomGet := &testFunctionDefinition{name: RandomGetName}
    29  	tests := []struct {
    30  		name     string
    31  		fnd      api.FunctionDefinition
    32  		scopes   logging.LogScopes
    33  		expected bool
    34  	}{
    35  		{
    36  			name:     "clockTimeGet in LogScopeClock",
    37  			fnd:      clockTimeGet,
    38  			scopes:   logging.LogScopeClock,
    39  			expected: true,
    40  		},
    41  		{
    42  			name:     "clockTimeGet not in LogScopeFilesystem",
    43  			fnd:      clockTimeGet,
    44  			scopes:   logging.LogScopeFilesystem,
    45  			expected: false,
    46  		},
    47  		{
    48  			name:     "clockTimeGet in LogScopeClock|LogScopeFilesystem",
    49  			fnd:      clockTimeGet,
    50  			scopes:   logging.LogScopeClock | logging.LogScopeFilesystem,
    51  			expected: true,
    52  		},
    53  		{
    54  			name:     "clockTimeGet in LogScopeAll",
    55  			fnd:      clockTimeGet,
    56  			scopes:   logging.LogScopeAll,
    57  			expected: true,
    58  		},
    59  		{
    60  			name:     "clockTimeGet not in LogScopeNone",
    61  			fnd:      clockTimeGet,
    62  			scopes:   logging.LogScopeNone,
    63  			expected: false,
    64  		},
    65  		{
    66  			name:     "fdRead in LogScopeFilesystem",
    67  			fnd:      fdRead,
    68  			scopes:   logging.LogScopeFilesystem,
    69  			expected: true,
    70  		},
    71  		{
    72  			name:     "fdRead not in LogScopeRandom",
    73  			fnd:      fdRead,
    74  			scopes:   logging.LogScopeRandom,
    75  			expected: false,
    76  		},
    77  		{
    78  			name:     "fdRead in LogScopeRandom|LogScopeFilesystem",
    79  			fnd:      fdRead,
    80  			scopes:   logging.LogScopeRandom | logging.LogScopeFilesystem,
    81  			expected: true,
    82  		},
    83  		{
    84  			name:     "fdRead in LogScopeAll",
    85  			fnd:      fdRead,
    86  			scopes:   logging.LogScopeAll,
    87  			expected: true,
    88  		},
    89  		{
    90  			name:     "fdRead not in LogScopeNone",
    91  			fnd:      fdRead,
    92  			scopes:   logging.LogScopeNone,
    93  			expected: false,
    94  		},
    95  		{
    96  			name:     "pollOneoff in LogScopePoll",
    97  			fnd:      pollOneoff,
    98  			scopes:   logging.LogScopePoll,
    99  			expected: true,
   100  		},
   101  		{
   102  			name:     "pollOneoff not in LogScopeFilesystem",
   103  			fnd:      pollOneoff,
   104  			scopes:   logging.LogScopeFilesystem,
   105  			expected: false,
   106  		},
   107  		{
   108  			name:     "pollOneoff in LogScopePoll|LogScopeFilesystem",
   109  			fnd:      pollOneoff,
   110  			scopes:   logging.LogScopePoll | logging.LogScopeFilesystem,
   111  			expected: true,
   112  		},
   113  		{
   114  			name:     "pollOneoff in LogScopeAll",
   115  			fnd:      pollOneoff,
   116  			scopes:   logging.LogScopeAll,
   117  			expected: true,
   118  		},
   119  		{
   120  			name:     "pollOneoff not in LogScopeNone",
   121  			fnd:      pollOneoff,
   122  			scopes:   logging.LogScopeNone,
   123  			expected: false,
   124  		},
   125  		{
   126  			name:     "procExit in LogScopeProc",
   127  			fnd:      procExit,
   128  			scopes:   logging.LogScopeProc,
   129  			expected: true,
   130  		},
   131  		{
   132  			name:     "procExit not in LogScopeFilesystem",
   133  			fnd:      procExit,
   134  			scopes:   logging.LogScopeFilesystem,
   135  			expected: false,
   136  		},
   137  		{
   138  			name:     "procExit in LogScopeProc|LogScopeFilesystem",
   139  			fnd:      procExit,
   140  			scopes:   logging.LogScopeProc | logging.LogScopeFilesystem,
   141  			expected: true,
   142  		},
   143  		{
   144  			name:     "procExit in LogScopeAll",
   145  			fnd:      procExit,
   146  			scopes:   logging.LogScopeAll,
   147  			expected: true,
   148  		},
   149  		{
   150  			name:     "procExit not in LogScopeNone",
   151  			fnd:      procExit,
   152  			scopes:   logging.LogScopeNone,
   153  			expected: false,
   154  		},
   155  		{
   156  			name:     "randomGet not in LogScopeFilesystem",
   157  			fnd:      randomGet,
   158  			scopes:   logging.LogScopeFilesystem,
   159  			expected: false,
   160  		},
   161  		{
   162  			name:     "randomGet in LogScopeRandom|LogScopeFilesystem",
   163  			fnd:      randomGet,
   164  			scopes:   logging.LogScopeRandom | logging.LogScopeFilesystem,
   165  			expected: true,
   166  		},
   167  		{
   168  			name:     "randomGet in LogScopeAll",
   169  			fnd:      randomGet,
   170  			scopes:   logging.LogScopeAll,
   171  			expected: true,
   172  		},
   173  		{
   174  			name:     "randomGet not in LogScopeNone",
   175  			fnd:      randomGet,
   176  			scopes:   logging.LogScopeNone,
   177  			expected: false,
   178  		},
   179  	}
   180  
   181  	for _, tt := range tests {
   182  		tc := tt
   183  
   184  		t.Run(tc.name, func(t *testing.T) {
   185  			require.Equal(t, tc.expected, IsInLogScope(tc.fnd, tc.scopes))
   186  		})
   187  	}
   188  }