Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Commit 240bb5d

Browse files
authored
Fixed ScanAndSignMessage constructor (#456)
Fixed ScanAndSignMessage constructor to ignore values of V3ServiceIndexUrl and Owners unless operation is Scan instead of throwing exception. Currently deserializer always uses "long" constructor which fails for Scan messages.
1 parent 766497b commit 240bb5d

3 files changed

Lines changed: 59 additions & 9 deletions

File tree

src/Validation.ScanAndSign.Core/ScanAndSignMessage.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Linq;
76

87
namespace NuGet.Jobs.Validation.ScanAndSign
98
{
@@ -31,17 +30,14 @@ public ScanAndSignMessage(
3130
string v3ServiceIndexUrl,
3231
IReadOnlyList<string> owners)
3332
{
34-
if (operationRequestType == OperationRequestType.Scan &&
35-
(!string.IsNullOrEmpty(v3ServiceIndexUrl) || owners != null))
36-
{
37-
throw new ArgumentException($"{nameof(OperationRequestType.Scan)} operations do not accept a V3 service index URL or a list of owners");
38-
}
39-
4033
OperationRequestType = operationRequestType;
4134
PackageValidationId = packageValidationId;
4235
BlobUri = blobUri ?? throw new ArgumentNullException(nameof(blobUri));
43-
V3ServiceIndexUrl = v3ServiceIndexUrl ?? throw new ArgumentNullException(nameof(v3ServiceIndexUrl));
44-
Owners = owners ?? throw new ArgumentNullException(nameof(owners));
36+
if (operationRequestType == OperationRequestType.Sign)
37+
{
38+
V3ServiceIndexUrl = v3ServiceIndexUrl ?? throw new ArgumentNullException(nameof(v3ServiceIndexUrl));
39+
Owners = owners ?? throw new ArgumentNullException(nameof(owners));
40+
}
4541
}
4642

4743
public OperationRequestType OperationRequestType { get; }
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using NuGet.Jobs.Validation.ScanAndSign;
10+
using Xunit;
11+
12+
namespace Validation.PackageSigning.ScanAndSign.Tests
13+
{
14+
public class ScanAndSignMessageFacts
15+
{
16+
[Fact]
17+
public void ConstructorDoesNotThrowWhenIndexUrlIsNullForScanOperation()
18+
{
19+
var ex = Record.Exception(() => new ScanAndSignMessage(
20+
OperationRequestType.Scan,
21+
Guid.NewGuid(),
22+
new Uri("https://example.com/aaa.nupkg"),
23+
v3ServiceIndexUrl: null,
24+
owners: new List<string> { "owner1" }));
25+
26+
Assert.Null(ex);
27+
}
28+
29+
[Fact]
30+
public void ConstructorDoesNotThrowWhenOwnersIsNullForScanOperation()
31+
{
32+
var ex = Record.Exception(() => new ScanAndSignMessage(
33+
OperationRequestType.Scan,
34+
Guid.NewGuid(),
35+
new Uri("https://example.com/aaa.nupkg"),
36+
v3ServiceIndexUrl: "https://example.com/index.json",
37+
owners: null));
38+
39+
Assert.Null(ex);
40+
}
41+
42+
[Fact]
43+
public void ShortConstructorCannotBeUsedForSignOperation()
44+
{
45+
var ex = Assert.Throws<ArgumentException>(() => new ScanAndSignMessage(
46+
OperationRequestType.Sign,
47+
Guid.NewGuid(),
48+
new Uri("https://example.com/aaa.nupkg")));
49+
50+
Assert.Contains(nameof(OperationRequestType.Sign), ex.Message);
51+
}
52+
}
53+
}

tests/Validation.PackageSigning.ScanAndSign.Tests/Validation.PackageSigning.ScanAndSign.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<ItemGroup>
4242
<Compile Include="Properties\AssemblyInfo.cs" />
4343
<Compile Include="ScanAndSignEnqueuerFacts.cs" />
44+
<Compile Include="ScanAndSignMessageFacts.cs" />
4445
<Compile Include="ScanAndSignProcessorFacts.cs" />
4546
</ItemGroup>
4647
<ItemGroup>

0 commit comments

Comments
 (0)