Repository: dotnet/runtime
File: src/libraries/System.Security.Cryptography.Cose/tests/CoseMessageTests.Sign.CustomHeaderMaps.cs (C#)

Code before:
        public void MultiSign_SignWithCriticalHeaders_NotTransportingTheSpecifiedCriticalHeaderThrows_AddSignature()
        {
            if (MessageKind != CoseMessageKind.MultiSign)
            {
                return;
            }

            ReadOnlySpan<byte> encodedMsg = Sign(s_sampleContent, GetCoseSigner(DefaultKey, DefaultHash));
            CoseMultiSignMessage multiSignMsg = Assert.IsType<CoseMultiSignMessage>(Decode(encodedMsg));

            multiSignMsg.RemoveSignature(0);

            CoseHeaderMap signProtectedHeaders = GetHeaderMapWithAlgorithm(DefaultAlgorithm);
            AddCriticalHeaders(signProtectedHeaders, null, includeSpecifiedCritHeader: false);

            CoseSigner signer = GetCoseSigner(DefaultKey, DefaultHash, signProtectedHeaders);
            Assert.Throws<ArgumentException>("signer", () => AddSignature(multiSignMsg, s_sampleContent, signer));
        }

        private static void AddCriticalHeaders(
            CoseHeaderMap protectedHeaders, List<(CoseHeaderLabel, ReadOnlyMemory<byte>)>? expectedHeaders, bool includeSpecifiedCritHeader)
        {
            Assert.Equal(expectedHeaders != null, includeSpecifiedCritHeader);

            CoseHeaderValue critValue = CoseHeaderValue.FromEncodedValue(GetDummyCritHeaderValue());
            protectedHeaders[CoseHeaderLabel.CriticalHeaders] = critValue;
            expectedHeaders?.Add((CoseHeaderLabel.CriticalHeaders, critValue.EncodedValue));

            if (includeSpecifiedCritHeader)
            {
                var myCritHeaderLabel = new CoseHeaderLabel(42);
                var myCritHeaderValue = CoseHeaderValue.FromString("My custom critical header value.");
                protectedHeaders.Add(myCritHeaderLabel, myCritHeaderValue);
                expectedHeaders?.Add((myCritHeaderLabel, myCritHeaderValue.EncodedValue));
            }
        }

        [Fact]
        public void MultiSign_SignWithCounterSignature()
        {
            if (MessageKind != CoseMessageKind.MultiSign)
            {
                return;
            }

            CoseMessage msg = Decode(Sign(s_sampleContent, GetCoseSigner(DefaultKey, DefaultHash)));
            CoseMultiSignMessage multiSignMsg = Assert.IsType<CoseMultiSignMessage>(msg);

            // counter sign
            CoseHeaderLabel counterSignLabel = new(7);
            CoseSignature signatureToCounterSign = multiSignMsg.Signatures[0];

Review comment (bug):
This call uses `includeSpecifiedCritHeader:` and then passes `useIndefiniteLength` positionally, which is not valid C# and will fail compilation. Name the last argument as well.

Code after:
            List<(CoseHeaderLabel, ReadOnlyMemory<byte>)> expectedSignProtected = GetExpectedProtectedHeaders(DefaultAlgorithm);
            AddCriticalHeaders(signProtectedHeaders, expectedSignProtected, includeSpecifiedCritHeader: true, useIndefiniteLength);

            CoseSigner signer = GetCoseSigner(DefaultKey, DefaultHash, signProtectedHeaders);
            AddSignature(multiSignMsg, s_sampleContent, signer);

            AssertCoseSignMessage(multiSignMsg.Encode(), s_sampleContent, DefaultKey, DefaultAlgorithm, expectedProtectedHeaders: expectedSignProtected);
        }

        [Theory]
        [InlineData(false)]
        [InlineData(true)]
        public void MultiSign_SignWithCriticalHeaders_NotTransportingTheSpecifiedCriticalHeaderThrows_AddSignature(bool useIndefiniteLength)
        {
            if (MessageKind != CoseMessageKind.MultiSign)
            {
                return;
            }

            ReadOnlySpan<byte> encodedMsg = Sign(s_sampleContent, GetCoseSigner(DefaultKey, DefaultHash));
            CoseMultiSignMessage multiSignMsg = Assert.IsType<CoseMultiSignMessage>(Decode(encodedMsg));

            multiSignMsg.RemoveSignature(0);

            CoseHeaderMap signProtectedHeaders = GetHeaderMapWithAlgorithm(DefaultAlgorithm);
            AddCriticalHeaders(signProtectedHeaders, null, includeSpecifiedCritHeader: false, useIndefiniteLength);

            CoseSigner signer = GetCoseSigner(DefaultKey, DefaultHash, signProtectedHeaders);
            Assert.Throws<ArgumentException>("signer", () => AddSignature(multiSignMsg, s_sampleContent, signer));
        }

        private static void AddCriticalHeaders(
            CoseHeaderMap protectedHeaders,
            List<(CoseHeaderLabel, ReadOnlyMemory<byte>)>? expectedHeaders,
            bool includeSpecifiedCritHeader,
            bool useIndefiniteLength)
        {
            Assert.Equal(expectedHeaders != null, includeSpecifiedCritHeader);

            CoseHeaderValue critValue = CoseHeaderValue.FromEncodedValue(GetDummyCritHeaderValue(useIndefiniteLength));
            protectedHeaders[CoseHeaderLabel.CriticalHeaders] = critValue;
            expectedHeaders?.Add((CoseHeaderLabel.CriticalHeaders, critValue.EncodedValue));

            if (includeSpecifiedCritHeader)
            {
                var myCritHeaderLabel = new CoseHeaderLabel(42);
                var myCritHeaderValue = CoseHeaderValue.FromString("My custom critical header value.");
                protectedHeaders.Add(myCritHeaderLabel, myCritHeaderValue);
                expectedHeaders?.Add((myCritHeaderLabel, myCritHeaderValue.EncodedValue));
            }
        }