2015-06-10 12:29:53 +05:30
/ *
* Copyright 2015 Vineet Garg < grg . vineet @gmail.com >
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation ; either version 2 of
* the License or ( at your option ) version 3 or any later version
* accepted by the membership of KDE e . V . ( or its successor approved
* by the membership of KDE e . V . ) , which shall act as a proxy
* defined in Section 14 of version 3 of the license .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
* /
package org.kde.kdeconnect ;
import android.test.AndroidTestCase ;
import android.util.Log ;
2016-05-19 09:28:15 -07:00
import org.json.JSONException ;
2015-06-21 17:30:29 +05:30
import org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper ;
2016-06-12 21:07:01 +02:00
import org.skyscreamer.jsonassert.JSONAssert ;
2015-06-21 17:30:29 +05:30
2015-06-10 12:29:53 +05:30
import java.security.KeyPair ;
import java.security.KeyPairGenerator ;
import java.security.PrivateKey ;
import java.security.PublicKey ;
2018-03-04 11:31:37 +01:00
public class NetworkPacketTest extends AndroidTestCase {
2015-06-10 12:29:53 +05:30
2018-03-04 11:31:37 +01:00
public void testNetworkPacket ( ) throws JSONException {
NetworkPacket np = new NetworkPacket ( " com.test " ) ;
2015-06-10 12:29:53 +05:30
np . set ( " hello " , " hola " ) ;
assertEquals ( np . getString ( " hello " , " bye " ) , " hola " ) ;
np . set ( " hello " , " " ) ;
assertEquals ( np . getString ( " hello " , " bye " ) , " " ) ;
assertEquals ( np . getString ( " hi " , " bye " ) , " bye " ) ;
np . set ( " foo " , " bar " ) ;
String serialized = np . serialize ( ) ;
2018-03-04 11:31:37 +01:00
NetworkPacket np2 = NetworkPacket . unserialize ( serialized ) ;
2015-06-10 12:29:53 +05:30
assertEquals ( np . getLong ( " id " ) , np2 . getLong ( " id " ) ) ;
assertEquals ( np . getString ( " type " ) , np2 . getString ( " type " ) ) ;
assertEquals ( np . getJSONArray ( " body " ) , np2 . getJSONArray ( " body " ) ) ;
String json = " { \" id \" :123, \" type \" : \" test \" , \" body \" :{ \" testing \" :true}} " ;
2018-03-04 11:31:37 +01:00
np2 = NetworkPacket . unserialize ( json ) ;
2018-03-03 16:06:52 +01:00
assertEquals ( np2 . getId ( ) , 123 ) ;
2015-06-10 12:29:53 +05:30
assertEquals ( np2 . getBoolean ( " testing " ) , true ) ;
assertEquals ( np2 . getBoolean ( " not_testing " ) , false ) ;
assertEquals ( np2 . getBoolean ( " not_testing " , true ) , true ) ;
}
2018-03-03 16:06:52 +01:00
public void testIdentity ( ) {
2015-06-10 12:29:53 +05:30
2018-03-04 11:31:37 +01:00
NetworkPacket np = NetworkPacket . createIdentityPacket ( getContext ( ) ) ;
2015-06-10 12:29:53 +05:30
2018-03-04 11:31:37 +01:00
assertEquals ( np . getInt ( " protocolVersion " ) , NetworkPacket . ProtocolVersion ) ;
2015-06-10 12:29:53 +05:30
}
2016-05-19 09:28:15 -07:00
public void testEncryption ( ) throws JSONException {
2018-03-04 11:31:37 +01:00
NetworkPacket original = new NetworkPacket ( " com.test " ) ;
2015-06-10 12:29:53 +05:30
original . set ( " hello " , " hola " ) ;
2018-03-04 11:31:37 +01:00
NetworkPacket copy = NetworkPacket . unserialize ( original . serialize ( ) ) ;
2015-06-10 12:29:53 +05:30
2018-03-04 11:31:37 +01:00
NetworkPacket decrypted = new NetworkPacket ( " " ) ;
2015-06-10 12:29:53 +05:30
KeyPair keyPair ;
try {
KeyPairGenerator keyGen = KeyPairGenerator . getInstance ( " RSA " ) ;
keyGen . initialize ( 2048 ) ;
keyPair = keyGen . genKeyPair ( ) ;
2018-03-03 16:06:52 +01:00
} catch ( Exception e ) {
2015-06-10 12:29:53 +05:30
e . printStackTrace ( ) ;
Log . e ( " KDE/initializeRsaKeys " , " Exception " ) ;
return ;
}
PrivateKey privateKey = keyPair . getPrivate ( ) ;
assertNotNull ( privateKey ) ;
PublicKey publicKey = keyPair . getPublic ( ) ;
assertNotNull ( publicKey ) ;
// Encrypt and decrypt np
assertEquals ( original . getType ( ) , " com.test " ) ;
try {
2018-03-04 11:31:37 +01:00
NetworkPacket encrypted = RsaHelper . encrypt ( original , publicKey ) ;
assertEquals ( encrypted . getType ( ) , NetworkPacket . PACKET_TYPE_ENCRYPTED ) ;
2015-06-10 12:29:53 +05:30
2015-06-21 17:30:29 +05:30
decrypted = RsaHelper . decrypt ( encrypted , privateKey ) ;
2015-06-10 12:29:53 +05:30
assertEquals ( decrypted . getType ( ) , " com.test " ) ;
2018-03-03 16:06:52 +01:00
} catch ( Exception e ) {
2015-06-10 12:29:53 +05:30
e . printStackTrace ( ) ;
}
// np should be equal to np2
assertEquals ( decrypted . getLong ( " id " ) , copy . getLong ( " id " ) ) ;
assertEquals ( decrypted . getType ( ) , copy . getType ( ) ) ;
assertEquals ( decrypted . getJSONArray ( " body " ) , copy . getJSONArray ( " body " ) ) ;
2016-06-12 21:07:01 +02:00
String json = " { \" body \" :{ \" nowPlaying \" : \" A really long song name - A really long artist name \" , \" player \" : \" A really long player name \" , \" the_meaning_of_life_the_universe_and_everything \" : \" 42 \" }, \" id \" :945945945, \" type \" : \" kdeconnect.a_really_really_long_package_type \" } \ n " ;
2018-03-04 11:31:37 +01:00
NetworkPacket longJsonNp = NetworkPacket . unserialize ( json ) ;
2015-06-10 12:29:53 +05:30
try {
2018-03-04 11:31:37 +01:00
NetworkPacket encrypted = RsaHelper . encrypt ( longJsonNp , publicKey ) ;
2015-06-21 17:30:29 +05:30
decrypted = RsaHelper . decrypt ( encrypted , privateKey ) ;
2015-06-10 12:29:53 +05:30
String decryptedJson = decrypted . serialize ( ) ;
2016-06-12 21:07:01 +02:00
JSONAssert . assertEquals ( json , decryptedJson , true ) ;
2018-03-03 16:06:52 +01:00
} catch ( Exception e ) {
2015-06-10 12:29:53 +05:30
e . printStackTrace ( ) ;
}
}
}