1 // OpenTween - Client of Twitter
2 // Copyright (c) 2022 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
17 // You should have received a copy of the GNU General public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
23 using System.Collections.Generic;
26 using System.Threading.Tasks;
31 public class ApiKeyTest
34 public void Encrypt_FormatTest()
36 var password = "hoge";
37 var plainText = "aaaaaaa";
38 var encrypted = ApiKey.Encrypt(password, plainText);
39 Assert.StartsWith("%e%", encrypted);
40 Assert.Equal(5, encrypted.Split('%').Length);
44 public void Encrypt_NonceTest()
46 // 同じ平文に対する暗号文を繰り返し生成しても出力は毎回変化する
47 var password = "hoge";
48 var plainText = "aaaaaaa";
49 var encrypted1 = ApiKey.Encrypt(password, plainText);
50 var encrypted2 = ApiKey.Encrypt(password, plainText);
51 Assert.NotEqual(encrypted1, encrypted2);
55 public void Decrypt_Test()
57 var password = "password";
58 var encrypted = "%e%m6EH2dECH7HWT9SFE0SK4Q==%mAAWPhPALf48s32s/yQarg==%zoCs8crMqZN6Nfj8ALkl2R3kbD/FORecuepU1LJ3CK0=";
59 var decrypted = ApiKey.Decrypt(password, encrypted);
60 Assert.Equal("hogehoge", decrypted);
64 public void Decrypt_PlainTextTest()
66 // %e% から始まっていない文字列は平文として何もせずに返す
67 var password = "password";
68 var plainText = "plaintext";
69 var decrypted = ApiKey.Decrypt(password, plainText);
70 Assert.Equal("plaintext", decrypted);
74 public void Decrypt_InvalidFormatTest()
76 var password = "password";
77 var encrypted = "%e%INVALID_FORMAT";
78 Assert.Throws<ApiKeyDecryptException>(() => ApiKey.Decrypt(password, encrypted));
82 public void Decrypt_InvalidBase64Test()
84 var password = "password";
85 var encrypted = "%e%!!!!!!!!!!%!!!!!!!!!!%!!!!!!!!!!";
86 Assert.Throws<ApiKeyDecryptException>(() => ApiKey.Decrypt(password, encrypted));
90 public void Decrypt_InvalidHMACTest()
92 var password = "password";
93 var encrypted = "%e%m6EH2dECH7HWT9SFE0SK4Q==%mAAWPhPALf48s32s/yQarg==%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
94 Assert.Throws<ApiKeyDecryptException>(() => ApiKey.Decrypt(password, encrypted));
98 public void Decrypt_InvalidHMACLengthTest()
100 // HMAC が途中まで一致しているが長さが足りない場合
101 var password = "password";
102 var encrypted = "%e%m6EH2dECH7HWT9SFE0SK4Q==%mAAWPhPALf48s32s/yQarg==%zoCs8";
103 Assert.Throws<ApiKeyDecryptException>(() => ApiKey.Decrypt(password, encrypted));
107 public void EncryptAndDecrypt_Test()
109 var password = "hoge";
110 var plainText = "aaaaaaa";
111 var encrypted = ApiKey.Encrypt(password, plainText);
112 Assert.Equal(plainText, ApiKey.Decrypt(password, encrypted));