Viewing is open to everyone. Downloading and starring need a GitHub sign-in.
Sign in with GitHub
PayloadTests.cs
v0.1.0-build.85
Latest
Commit ede38f5
Sign in with GitHub to download. Each download is recorded with your GitHub username.
| 1 | using System.Text; |
| 2 | using Xunit; |
| 3 | |
| 4 | namespace OpenCKey.Tests |
| 5 | { |
| 6 | public class PayloadTests |
| 7 | { |
| 8 | private static string? Text(string s) => CKeyPayload.ReadableText(Encoding.UTF8.GetBytes(s)); |
| 9 | |
| 10 | [Fact] |
| 11 | public void ReadableText() |
| 12 | { |
| 13 | Assert.Equal("hello", Text("hello")); |
| 14 | Assert.Equal("a\tb\r\nc", Text("a\tb\r\nc")); |
| 15 | Assert.Null(Text("bell\u0007")); |
| 16 | Assert.Null(CKeyPayload.ReadableText(new byte[] { 0xc3, 0x28 })); // invalid UTF-8 |
| 17 | Assert.Null(CKeyPayload.ReadableText(new byte[0])); |
| 18 | Assert.Equal("Grüße", Text("Grüße")); |
| 19 | } |
| 20 | |
| 21 | [Theory] |
| 22 | [InlineData("https://citeory.com/tour", CKeyActionKind.Web, "Open citeory.com", "https://citeory.com/tour")] |
| 23 | [InlineData(" HTTP://Example.com/x ", CKeyActionKind.Web, "Open example.com", "HTTP://Example.com/x")] |
| 24 | [InlineData("mailto:hello@citeory.com?subject=Hi", CKeyActionKind.Email, "Email hello@citeory.com", "mailto:hello@citeory.com?subject=Hi")] |
| 25 | [InlineData("tel:+1 (555) 123-4567", CKeyActionKind.Call, "Call +15551234567", "tel:+15551234567")] |
| 26 | [InlineData("sms:+15551234567?body=Hi", CKeyActionKind.Message, "Message +15551234567", "sms:+15551234567?body=Hi")] |
| 27 | [InlineData("geo:37.33490,-122.00900?q=Apple%20Park", CKeyActionKind.Map, "Show Apple Park on a map", "geo:37.33490,-122.00900?q=Apple%20Park")] |
| 28 | [InlineData("geo:1.5,2.25", CKeyActionKind.Map, "Show 1.50000, 2.25000 on a map", "geo:1.5,2.25")] |
| 29 | public void RecognizedForms(string text, CKeyActionKind kind, string label, string uri) |
| 30 | { |
| 31 | CKeyPayloadAction? action = CKeyPayload.Classify(text); |
| 32 | Assert.NotNull(action); |
| 33 | Assert.Equal(kind, action!.Kind); |
| 34 | Assert.Equal(label, action.Label); |
| 35 | Assert.Equal(uri, action.Uri); |
| 36 | } |
| 37 | |
| 38 | [Fact] |
| 39 | public void Address() |
| 40 | { |
| 41 | CKeyPayloadAction? action = CKeyPayload.Classify("address:1 Infinite Loop\n Cupertino, CA 95014 \n\n"); |
| 42 | Assert.Equal(CKeyActionKind.Map, action!.Kind); |
| 43 | Assert.Equal("1 Infinite Loop, Cupertino, CA 95014", action.Target); |
| 44 | Assert.Null(action.Uri); |
| 45 | Assert.Null(CKeyPayload.Classify("address: \n ")); |
| 46 | } |
| 47 | |
| 48 | [Fact] |
| 49 | public void VCard() |
| 50 | { |
| 51 | const string card = "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Ada Lovelace\r\nEND:VCARD"; |
| 52 | CKeyPayloadAction? action = CKeyPayload.Classify(card); |
| 53 | Assert.Equal(CKeyActionKind.Contact, action!.Kind); |
| 54 | Assert.Equal("Ada Lovelace", action.Target); |
| 55 | Assert.Equal(card, action.VCard); |
| 56 | Assert.Null(CKeyPayload.Classify("BEGIN:VCARD\nFN:Incomplete")); |
| 57 | } |
| 58 | |
| 59 | [Theory] |
| 60 | [InlineData("javascript:alert(1)")] |
| 61 | [InlineData("data:text/html,<b>x</b>")] |
| 62 | [InlineData("file:///etc/passwd")] |
| 63 | [InlineData("intent://scan/#Intent;scheme=zxing;end")] |
| 64 | [InlineData("myapp://open")] |
| 65 | [InlineData("just some text")] |
| 66 | [InlineData("mailto:nobody")] |
| 67 | [InlineData("tel:12")] |
| 68 | [InlineData("geo:91,0")] |
| 69 | [InlineData("geo:0,181")] |
| 70 | [InlineData("geo:abc,def")] |
| 71 | [InlineData("https://")] |
| 72 | [InlineData("")] |
| 73 | public void EverythingElseIsPlainText(string text) |
| 74 | { |
| 75 | Assert.Null(CKeyPayload.Classify(text)); |
| 76 | } |
| 77 | |
| 78 | [Fact] |
| 79 | public void NullIsPlainText() => Assert.Null(CKeyPayload.Classify(null)); |
| 80 | } |
| 81 | } |