...

Source file src/github.com/letsencrypt/boulder/linter/lints/common_test.go

Documentation: github.com/letsencrypt/boulder/linter/lints

     1  package lints
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/letsencrypt/boulder/test"
     7  	"golang.org/x/crypto/cryptobyte"
     8  	"golang.org/x/crypto/cryptobyte/asn1"
     9  )
    10  
    11  var onlyContainsUserCertsTag = asn1.Tag(1).ContextSpecific()
    12  var onlyContainsCACertsTag = asn1.Tag(2).ContextSpecific()
    13  
    14  func TestReadOptionalASN1BooleanWithTag(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	testCases := []struct {
    18  		name string
    19  		// incoming will be mutated by the function under test
    20  		incoming     []byte
    21  		out          bool
    22  		defaultValue bool
    23  		asn1Tag      asn1.Tag
    24  		expectedOk   bool
    25  		// expectedTrailer counts the remaining bytes from incoming after having
    26  		// been advanced by the function under test
    27  		expectedTrailer int
    28  		expectedOut     bool
    29  	}{
    30  		{
    31  			name:            "Good: onlyContainsUserCerts",
    32  			incoming:        cryptobyte.String([]byte{0x81, 0x01, 0xFF}),
    33  			asn1Tag:         onlyContainsUserCertsTag,
    34  			expectedOk:      true,
    35  			expectedTrailer: 0,
    36  			expectedOut:     true,
    37  		},
    38  		{
    39  			name:            "Good: onlyContainsCACerts",
    40  			incoming:        cryptobyte.String([]byte{0x82, 0x01, 0xFF}),
    41  			asn1Tag:         onlyContainsCACertsTag,
    42  			expectedOk:      true,
    43  			expectedTrailer: 0,
    44  			expectedOut:     true,
    45  		},
    46  		{
    47  			name:            "Good: Bytes are read and trailer remains",
    48  			incoming:        cryptobyte.String([]byte{0x82, 0x01, 0xFF, 0xC0, 0xFF, 0xEE, 0xCA, 0xFE}),
    49  			asn1Tag:         onlyContainsCACertsTag,
    50  			expectedOk:      true,
    51  			expectedTrailer: 5,
    52  			expectedOut:     true,
    53  		},
    54  		{
    55  			name:            "Bad: Read the tag, but out should be false, no trailer",
    56  			incoming:        cryptobyte.String([]byte{0x82, 0x01, 0x00}),
    57  			asn1Tag:         onlyContainsCACertsTag,
    58  			expectedOk:      true,
    59  			expectedTrailer: 0,
    60  			expectedOut:     false,
    61  		},
    62  		{
    63  			name:            "Bad: Read the tag, but out should be false, trailer remains",
    64  			incoming:        cryptobyte.String([]byte{0x82, 0x01, 0x00, 0x99}),
    65  			asn1Tag:         onlyContainsCACertsTag,
    66  			expectedOk:      true,
    67  			expectedTrailer: 1,
    68  			expectedOut:     false,
    69  		},
    70  		{
    71  			name:            "Bad: Wrong asn1Tag compared to incoming bytes, no bytes read",
    72  			incoming:        cryptobyte.String([]byte{0x81, 0x01, 0xFF}),
    73  			asn1Tag:         onlyContainsCACertsTag,
    74  			expectedOk:      true,
    75  			expectedTrailer: 3,
    76  			expectedOut:     false,
    77  		},
    78  	}
    79  
    80  	for _, tc := range testCases {
    81  		tc := tc
    82  		t.Run(tc.name, func(t *testing.T) {
    83  			t.Parallel()
    84  
    85  			// ReadOptionalASN1BooleanWithTag accepts nil as a valid outParam to
    86  			// maintain the style of upstream x/crypto/cryptobyte, but we
    87  			// currently don't pass nil. Instead we use a reference to a
    88  			// pre-existing boolean here and in the lint code. Passing in nil
    89  			// will _do the wrong thing (TM)_ in our CRL lints.
    90  			var outParam bool
    91  			ok := ReadOptionalASN1BooleanWithTag((*cryptobyte.String)(&tc.incoming), &outParam, tc.asn1Tag, false)
    92  			t.Log("Check if reading the tag was successful:")
    93  			test.AssertEquals(t, ok, tc.expectedOk)
    94  			t.Log("Check value of the optional boolean:")
    95  			test.AssertEquals(t, outParam, tc.expectedOut)
    96  			t.Log("Bytes should be popped off of incoming as they're successfully read:")
    97  			test.AssertEquals(t, len(tc.incoming), tc.expectedTrailer)
    98  		})
    99  	}
   100  }
   101  

View as plain text