Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KSM-462 - .NET SDK - fixed JSON serializer and tests #524

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion sdk/dotNet/SecretsManager.Test.Core/JsonUtils.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,34 @@ public class JsonUtilsTests
[Test]
public void ParseAndSerializeShouldNotChangeTheData()
{
const string jsonIn = "{\"title\":\"MyHomeLogin\",\"type\":\"Login2\",\"fields\":[{\"type\":\"login\",\"label\":\"Login\",\"value\":[\"Login 1\"],\"required\":true,\"privacyScreen\":true},{\"type\":\"password\",\"label\":\"Password\",\"value\":[\"3[OJ%sc7n].wX6+k5GY)6\"],\"required\":true,\"privacyScreen\":true,\"enforceGeneration\":true,\"complexity\":{\"length\":21,\"caps\":5,\"lowercase\":5,\"digits\":5,\"special\":5}},{\"type\":\"url\",\"label\":\"URL\",\"value\":[\"https://asdfjkasdfkdsa.com\"],\"required\":true,\"privacyScreen\":true},{\"type\":\"securityQuestion\",\"label\":\"Security Question & Answer\",\"value\":[{\"question\":\"asdf\",\"answer\":\"asdf\"}],\"required\":true,\"privacyScreen\":true},{\"type\":\"fileRef\",\"label\":\"File or Photo\",\"value\":[]},{\"type\":\"oneTimeCode\",\"label\":\"Two-Factor Code\",\"value\":[]}],\"custom\":[]}";
var rec = new KeeperRecordData {
type = "Login2",
title = "MyHomeLogin",
notes = "MyNotes",
fields = new KeeperRecordField[] {
new KeeperRecordField { type = "login", label = "Login", value = new string[] { "Login 1" }, required=true, privacyScreen=true },
new KeeperRecordField { type = "password", label = "Password", value = new string[] { "3[OJ%sc7n].wX6+k5GY)6" }, required=true, privacyScreen=true, enforceGeneration=true, complexity=new PasswordComplexity{ length=21, caps=5, lowercase=5, digits=5, special=5} },
new KeeperRecordField { type = "url", label = "URL", value = new string[] { "https://asdfjkasdfkdsa.com" }, required=true, privacyScreen=true },
new KeeperRecordField { type = "securityQuestion", label = "Security Question & Answer", value = new SecurityQuestion[] { new SecurityQuestion { question= "asdf", answer= "asdf" } }, required=true, privacyScreen=true },
new KeeperRecordField { type = "fileRef", label = "File or Photo", value = new object[] { } },
new KeeperRecordField { type = "oneTimeCode", label = "Two-Factor Code", value = new object[] { } },
},
custom = new KeeperRecordField[] { }
};
var jsonIn = CryptoUtils.BytesToString(JsonUtils.SerializeJson(rec));
//const string jsonIn = "{\"title\":\"MyHomeLogin\",\"type\":\"Login2\",\"fields\":[{\"type\":\"login\",\"label\":\"Login\",\"value\":[\"Login 1\"],\"required\":true,\"privacyScreen\":true},{\"type\":\"password\",\"label\":\"Password\",\"value\":[\"3[OJ%sc7n].wX6+k5GY)6\"],\"required\":true,\"privacyScreen\":true,\"enforceGeneration\":true,\"complexity\":{\"length\":21,\"caps\":5,\"lowercase\":5,\"digits\":5,\"special\":5}},{\"type\":\"url\",\"label\":\"URL\",\"value\":[\"https://asdfjkasdfkdsa.com\"],\"required\":true,\"privacyScreen\":true},{\"type\":\"securityQuestion\",\"label\":\"Security Question & Answer\",\"value\":[{\"question\":\"asdf\",\"answer\":\"asdf\"}],\"required\":true,\"privacyScreen\":true},{\"type\":\"fileRef\",\"label\":\"File or Photo\",\"value\":[]},{\"type\":\"oneTimeCode\",\"label\":\"Two-Factor Code\",\"value\":[]}],\"custom\":[],\"notes\":\"MyNotes\"}";
var recordData = JsonUtils.ParseJson<KeeperRecordData>(CryptoUtils.StringToBytes(jsonIn));
var jsonOut = CryptoUtils.BytesToString(JsonUtils.SerializeJson(recordData));
Assert.AreEqual(jsonIn, jsonOut);
Assert.AreEqual(recordData.fields[1].value[0].ToString(), rec.fields[1].value[0]);
}
[Test]
public void ParseAndSerializeShouldPreserveDiacritics()
{
string recordTitle = "MySpéciàlHomèL°gin";
var krdin = new KeeperRecordData { title = recordTitle, type = "login" };
var krdout = JsonUtils.ParseJson<KeeperRecordData>(JsonUtils.SerializeJson(krdin));
Assert.AreEqual(krdin.title, krdout.title);
}
}
}
6 changes: 3 additions & 3 deletions sdk/dotNet/SecretsManager/JsonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public static class JsonUtils
{
private static readonly JsonSerializerOptions Options = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
//Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, // bad for strings with diacritics
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
};

public static T ParseJson<T>(byte[] json)
{
return JsonSerializer.Deserialize<T>(json);
return JsonSerializer.Deserialize<T>(json, Options);
}

public static byte[] SerializeJson<T>(T obj)
Expand Down
Loading