Skip to content

Commit 9b21830

Browse files
🧪add failed test project about config encryption program
we need to fix it, we can check #2 for resolve bugs
1 parent 7945c04 commit 9b21830

3 files changed

Lines changed: 160 additions & 6 deletions

File tree

ConfigEncryptionProgram.cs

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ static void Main(string[] args)
1616
Console.WriteLine("===========================");
1717
Console.WriteLine();
1818

19+
// Prompt for config file path
20+
Console.Write("Enter config file path (press Enter for default): ");
21+
string configPath = Console.ReadLine();
22+
if (string.IsNullOrWhiteSpace(configPath))
23+
{
24+
configPath = null;
25+
Console.WriteLine("Using default config file.");
26+
}
27+
else
28+
{
29+
Console.WriteLine($"Using config file: {configPath}");
30+
}
31+
Console.WriteLine();
32+
1933
bool exitRequested = false;
2034

2135
while (!exitRequested)
@@ -25,31 +39,68 @@ static void Main(string[] args)
2539
Console.WriteLine("2. Decrypt ConnectionStrings section\n2. ConnectionStrings 섹션 복호화");
2640
Console.WriteLine("3. Encrypt AppSettings section\n3. AppSettings 섹션 암호화");
2741
Console.WriteLine("4. Decrypt AppSettings section\n4. AppSettings 섹션 복호화");
28-
Console.WriteLine("5. Exit\n5. 종료");
29-
Console.Write("Choice (1-5): 선택 (1-5): ");
42+
Console.WriteLine("5. Encrypt only password in a connection string\n5. 연결 문자열에서 비밀번호만 암호화");
43+
Console.WriteLine("6. Decrypt only password in a connection string\n6. 연결 문자열에서 비밀번호만 복호화");
44+
Console.WriteLine("7. Exit\n7. 종료");
45+
Console.Write("Choice (1-7): 선택 (1-7): ");
3046

3147
string choice = Console.ReadLine();
3248
Console.WriteLine();
3349

50+
// Print config file path before each operation
51+
string pathMsg = configPath == null ? "(default config file)" : configPath;
52+
Console.WriteLine($"[Config file: {pathMsg}]");
53+
3454
switch (choice)
3555
{
3656
case "1":
37-
ConfigEncryption.EncryptConnectionStrings();
57+
ConfigEncryption.EncryptConnectionStrings(configPath);
3858
break;
3959

4060
case "2":
41-
ConfigEncryption.DecryptConnectionStrings();
61+
ConfigEncryption.DecryptConnectionStrings(configPath);
4262
break;
4363

4464
case "3":
45-
ConfigEncryption.EncryptAppSettings();
65+
ConfigEncryption.EncryptAppSettings(configPath);
4666
break;
4767

4868
case "4":
49-
ConfigEncryption.DecryptAppSettings();
69+
ConfigEncryption.DecryptAppSettings(configPath);
5070
break;
5171

5272
case "5":
73+
Console.Write("Enter connection string name: ");
74+
string encName = Console.ReadLine();
75+
var encMgr = new ConnectionStringManager(configPath);
76+
encMgr.EncryptPasswordOnly(encName);
77+
break;
78+
79+
case "6":
80+
Console.Write("Enter connection string name: ");
81+
string decName = Console.ReadLine();
82+
var decMgr = new ConnectionStringManager(configPath);
83+
// Decrypt password in connection string and update config
84+
string connStr = System.Configuration.ConfigurationManager.ConnectionStrings[decName]?.ConnectionString;
85+
if (string.IsNullOrEmpty(connStr))
86+
{
87+
Console.WriteLine($"Cannot find connection string '{decName}'.\n연결 문자열 '{decName}'을 찾을 수 없습니다.");
88+
}
89+
else if (!connStr.Contains("Password=ENC:"))
90+
{
91+
Console.WriteLine("No encrypted password found in the connection string.\n연결 문자열에 암호화된 비밀번호가 없습니다.");
92+
}
93+
else
94+
{
95+
string decrypted = CustomEncryption.DecryptPasswordInConnectionString(connStr);
96+
// Update config file with decrypted password
97+
decMgr.GetType().GetMethod("UpdateConnectionStringInConfigFile", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
98+
.Invoke(decMgr, new object[] { decName, decrypted });
99+
Console.WriteLine($"Password in connection string '{decName}' has been decrypted.\n연결 문자열 '{decName}'의 비밀번호가 복호화되었습니다.");
100+
}
101+
break;
102+
103+
case "7":
53104
exitRequested = true;
54105
break;
55106

test/ConfigEncryptionTests.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Configuration;
3+
using System.IO;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using FileAutoCleaner;
6+
7+
namespace ConfigEncryptionTests
8+
{
9+
[TestClass]
10+
public class ConfigEncryptionTests
11+
{
12+
private string tempConfigPath;
13+
14+
[TestInitialize]
15+
public void Setup()
16+
{
17+
// Create a temp config file for each test
18+
tempConfigPath = Path.GetTempFileName() + ".config";
19+
File.WriteAllText(tempConfigPath, @"<?xml version=""1.0"" encoding=""utf-8"" ?>
20+
<configuration>
21+
<connectionStrings>
22+
<add name=""TestConn"" connectionString=""Data Source=.;Initial Catalog=TestDb;User ID=sa;Password=Test123!"" providerName=""System.Data.SqlClient"" />
23+
</connectionStrings>
24+
<appSettings>
25+
<add key=""TestKey"" value=""TestValue"" />
26+
</appSettings>
27+
</configuration>");
28+
}
29+
30+
[TestCleanup]
31+
public void Cleanup()
32+
{
33+
if (File.Exists(tempConfigPath))
34+
File.Delete(tempConfigPath);
35+
}
36+
37+
[TestMethod]
38+
public void SectionLevel_EncryptDecrypt_ConnectionStrings_PreservesValues()
39+
{
40+
// Encrypt
41+
Assert.IsTrue(ConfigEncryption.EncryptConnectionStrings(tempConfigPath));
42+
// Should now be encrypted (not human-readable)
43+
string encrypted = File.ReadAllText(tempConfigPath);
44+
Assert.IsTrue(encrypted.Contains("EncryptedData"));
45+
46+
// Decrypt
47+
Assert.IsTrue(ConfigEncryption.DecryptConnectionStrings(tempConfigPath));
48+
string decrypted = File.ReadAllText(tempConfigPath);
49+
Assert.IsTrue(decrypted.Contains("TestConn"));
50+
Assert.IsTrue(decrypted.Contains("Password=Test123!"));
51+
}
52+
53+
[TestMethod]
54+
public void SectionLevel_EncryptDecrypt_AppSettings_PreservesValues()
55+
{
56+
// Encrypt
57+
Assert.IsTrue(ConfigEncryption.EncryptAppSettings(tempConfigPath));
58+
string encrypted = File.ReadAllText(tempConfigPath);
59+
Assert.IsTrue(encrypted.Contains("EncryptedData"));
60+
61+
// Decrypt
62+
Assert.IsTrue(ConfigEncryption.DecryptAppSettings(tempConfigPath));
63+
string decrypted = File.ReadAllText(tempConfigPath);
64+
Assert.IsTrue(decrypted.Contains("TestKey"));
65+
Assert.IsTrue(decrypted.Contains("TestValue"));
66+
}
67+
68+
[TestMethod]
69+
public void ValueLevel_EncryptDecrypt_ConnectionStringPassword_PreservesOtherValues()
70+
{
71+
var mgr = new ConnectionStringManager(tempConfigPath);
72+
Assert.IsTrue(mgr.EncryptPasswordOnly("TestConn"));
73+
string afterEncrypt = File.ReadAllText(tempConfigPath);
74+
Assert.IsTrue(afterEncrypt.Contains("Password=ENC:"));
75+
Assert.IsTrue(afterEncrypt.Contains("TestConn"));
76+
77+
// Always load from the temp config file
78+
var map = new ExeConfigurationFileMap { ExeConfigFilename = tempConfigPath };
79+
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
80+
string encryptedConnStr = config.ConnectionStrings.ConnectionStrings["TestConn"]?.ConnectionString;
81+
82+
string decrypted = CustomEncryption.DecryptPasswordInConnectionString(encryptedConnStr);
83+
Assert.IsTrue(decrypted.Contains("Password=Test123!"));
84+
}
85+
}
86+
}

test/ConfigEncryptionTests.csproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net48</TargetFramework>
4+
<IsPackable>false</IsPackable>
5+
<RootNamespace>ConfigEncryptionTests</RootNamespace>
6+
<AssemblyName>ConfigEncryptionTests</AssemblyName>
7+
<IsTestProject>true</IsTestProject>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
11+
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
12+
<Reference Include="System.Configuration" />
13+
</ItemGroup>
14+
<ItemGroup>
15+
<ProjectReference Include="..\ConfigEncryptionTool.csproj" />
16+
</ItemGroup>
17+
</Project>

0 commit comments

Comments
 (0)