|
| 1 | +using ModelContextProtocol.Extensions.Skills; |
| 2 | +using ModelContextProtocol.Protocol; |
| 3 | +using System.Text; |
| 4 | +using System.Text.Json.Nodes; |
| 5 | + |
| 6 | +namespace ModelContextProtocol.Tests.Client; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Tests for <see cref="SkillVerifier"/>: digest formatting and content verification against a manifest. |
| 10 | +/// </summary> |
| 11 | +public class SkillVerifierTests |
| 12 | +{ |
| 13 | + private const string Uri = "skill://alpha/SKILL.md"; |
| 14 | + |
| 15 | + private static SkillResource Entry(byte[] content) => new() |
| 16 | + { |
| 17 | + Uri = Uri, |
| 18 | + Digest = SkillVerifier.ComputeDigest(content), |
| 19 | + Size = content.Length, |
| 20 | + }; |
| 21 | + |
| 22 | + [Theory] |
| 23 | + [InlineData("", "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")] |
| 24 | + [InlineData("abc", "sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")] |
| 25 | + public void ComputeDigest_MatchesKnownVectors(string input, string expected) |
| 26 | + { |
| 27 | + Assert.Equal(expected, SkillVerifier.ComputeDigest(Encoding.UTF8.GetBytes(input))); |
| 28 | + } |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public void Verify_AcceptsMatchingBytes() |
| 32 | + { |
| 33 | + byte[] content = Encoding.UTF8.GetBytes("# Hello\n"); |
| 34 | + |
| 35 | + SkillVerifier.Verify(Entry(content), content); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void Verify_RejectsSizeMismatch() |
| 40 | + { |
| 41 | + byte[] content = Encoding.UTF8.GetBytes("# Hello\n"); |
| 42 | + var entry = Entry(content); |
| 43 | + entry.Size += 1; |
| 44 | + |
| 45 | + var exception = Assert.Throws<SkillVerificationException>(() => SkillVerifier.Verify(entry, content)); |
| 46 | + Assert.Contains("bytes", exception.Message); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void Verify_RejectsDigestMismatch() |
| 51 | + { |
| 52 | + var entry = Entry(Encoding.UTF8.GetBytes("# Hello\n")); |
| 53 | + |
| 54 | + var exception = Assert.Throws<SkillVerificationException>(() => |
| 55 | + SkillVerifier.Verify(entry, Encoding.UTF8.GetBytes("# Hellp\n"))); |
| 56 | + Assert.Contains("digest", exception.Message); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void Verify_AcceptsUppercaseDigestFromServer() |
| 61 | + { |
| 62 | + byte[] content = Encoding.UTF8.GetBytes("x"); |
| 63 | + var entry = Entry(content); |
| 64 | + entry.Digest = entry.Digest.ToUpperInvariant().Replace("SHA256", "sha256"); |
| 65 | + |
| 66 | + SkillVerifier.Verify(entry, content); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public void Verify_TextContents_HashesUtf8Encoding() |
| 71 | + { |
| 72 | + const string Text = "# Héllo\n"; |
| 73 | + var entry = Entry(Encoding.UTF8.GetBytes(Text)); |
| 74 | + |
| 75 | + SkillVerifier.Verify(entry, new TextResourceContents { Uri = Uri, Text = Text }); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void Verify_BlobContents_HashesDecodedBytes() |
| 80 | + { |
| 81 | + byte[] content = [0x00, 0xFF, 0x10, 0x80]; |
| 82 | + var entry = Entry(content); |
| 83 | + |
| 84 | + SkillVerifier.Verify(entry, BlobResourceContents.FromBytes(content, Uri)); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public void Verify_Contents_RejectsUriMismatch() |
| 89 | + { |
| 90 | + var entry = Entry(Encoding.UTF8.GetBytes("x")); |
| 91 | + |
| 92 | + Assert.Throws<SkillVerificationException>(() => |
| 93 | + SkillVerifier.Verify(entry, new TextResourceContents { Uri = "skill://alpha/other.md", Text = "x" })); |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public void Verify_Skill_ChecksEveryContentAgainstManifest() |
| 98 | + { |
| 99 | + byte[] content = Encoding.UTF8.GetBytes("x"); |
| 100 | + var skill = CreateSkill(content); |
| 101 | + |
| 102 | + SkillVerifier.Verify(skill, new ReadResourceResult { Contents = [new TextResourceContents { Uri = Uri, Text = "x" }] }); |
| 103 | + |
| 104 | + Assert.Throws<SkillVerificationException>(() => SkillVerifier.Verify(skill, |
| 105 | + new ReadResourceResult { Contents = [new TextResourceContents { Uri = Uri, Text = "y" }] })); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void Verify_Skill_RejectsUnlistedFile() |
| 110 | + { |
| 111 | + var skill = CreateSkill(Encoding.UTF8.GetBytes("x")); |
| 112 | + |
| 113 | + var exception = Assert.Throws<SkillVerificationException>(() => SkillVerifier.Verify(skill, |
| 114 | + new ReadResourceResult { Contents = [new TextResourceContents { Uri = "skill://alpha/new.md", Text = "x" }] })); |
| 115 | + Assert.Contains("not listed", exception.Message); |
| 116 | + } |
| 117 | + |
| 118 | + [Fact] |
| 119 | + public void Verify_Skill_RejectsEmptyResult() |
| 120 | + { |
| 121 | + var skill = CreateSkill(Encoding.UTF8.GetBytes("x")); |
| 122 | + |
| 123 | + Assert.Throws<SkillVerificationException>(() => SkillVerifier.Verify(skill, new ReadResourceResult())); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public void Verify_Skill_ThrowsForDynamicSkill() |
| 128 | + { |
| 129 | + var skill = CreateSkill(Encoding.UTF8.GetBytes("x")); |
| 130 | + skill.Resources = SkillResources.Dynamic; |
| 131 | + |
| 132 | + Assert.Throws<InvalidOperationException>(() => SkillVerifier.Verify(skill, |
| 133 | + new ReadResourceResult { Contents = [new TextResourceContents { Uri = Uri, Text = "x" }] })); |
| 134 | + } |
| 135 | + |
| 136 | + private static Skill CreateSkill(byte[] skillFileContent) => new() |
| 137 | + { |
| 138 | + Uri = Uri, |
| 139 | + Frontmatter = new JsonObject { ["name"] = "alpha", ["description"] = "d" }, |
| 140 | + Resources = SkillResources.FromResources([Entry(skillFileContent)]), |
| 141 | + }; |
| 142 | +} |
0 commit comments