Skip to content

Commit

Permalink
Merge pull request #174 from henkel/fix-byte-array-to-hex-string
Browse files Browse the repository at this point in the history
Fix byteArrayToHexString and add tests
  • Loading branch information
mpetazzoni committed Mar 14, 2016
2 parents e9e9773 + 4471694 commit 7acb2d0
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 20 deletions.
Expand Up @@ -17,6 +17,7 @@

import com.turn.ttorrent.common.Torrent;
import com.turn.ttorrent.client.peer.SharingPeer;
import com.turn.ttorrent.common.Utils;

import java.io.IOException;
import java.net.InetAddress;
Expand Down Expand Up @@ -391,15 +392,15 @@ private Handshake validateHandshake(SocketChannel channel, byte[] peerId)
Handshake hs = Handshake.parse(data);
if (!Arrays.equals(hs.getInfoHash(), this.torrent.getInfoHash())) {
throw new ParseException("Handshake for unknow torrent " +
Torrent.byteArrayToHexString(hs.getInfoHash()) +
Utils.bytesToHex(hs.getInfoHash()) +
" from " + this.socketRepr(channel) + ".", pstrlen + 9);
}

if (peerId != null && !Arrays.equals(hs.getPeerId(), peerId)) {
throw new ParseException("Announced peer ID " +
Torrent.byteArrayToHexString(hs.getPeerId()) +
Utils.bytesToHex(hs.getPeerId()) +
" did not match expected peer ID " +
Torrent.byteArrayToHexString(peerId) + ".", pstrlen + 29);
Utils.bytesToHex(peerId) + ".", pstrlen + 29);
}

return hs;
Expand Down Expand Up @@ -500,7 +501,7 @@ public void run() {
? this.peer.getPeerId().array()
: null));
logger.info("Handshaked with {}, peer ID is {}.",
this.peer, Torrent.byteArrayToHexString(hs.getPeerId()));
this.peer, Utils.bytesToHex(hs.getPeerId()));

// Go to non-blocking mode for peer interaction
channel.configureBlocking(false);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/turn/ttorrent/common/Peer.java
Expand Up @@ -107,7 +107,7 @@ public ByteBuffer getPeerId() {
public void setPeerId(ByteBuffer peerId) {
if (peerId != null) {
this.peerId = peerId;
this.hexPeerId = Torrent.byteArrayToHexString(peerId.array());
this.hexPeerId = Utils.bytesToHex(peerId.array());
} else {
this.peerId = null;
this.hexPeerId = null;
Expand Down
14 changes: 2 additions & 12 deletions core/src/main/java/com/turn/ttorrent/common/Torrent.java
Expand Up @@ -144,7 +144,7 @@ public Torrent(byte[] torrent, boolean seeder) throws IOException, NoSuchAlgorit
BEncoder.bencode(this.decoded_info, baos);
this.encoded_info = baos.toByteArray();
this.info_hash = Torrent.hash(this.encoded_info);
this.hex_info_hash = Torrent.byteArrayToHexString(this.info_hash);
this.hex_info_hash = Utils.bytesToHex(this.info_hash);

/**
* Parses the announce information from the decoded meta-info
Expand Down Expand Up @@ -412,16 +412,6 @@ public static byte[] hash(byte[] data) throws NoSuchAlgorithmException {
return crypt.digest();
}

/**
* Convert a byte string to a string containing an hexadecimal
* representation of the original data.
*
* @param bytes The byte array to convert.
*/
public static String byteArrayToHexString(byte[] bytes) {
return new BigInteger(1, bytes).toString(16);
}

/**
* Return an hexadecimal representation of the bytes contained in the
* given string, following the default, expected byte encoding.
Expand All @@ -431,7 +421,7 @@ public static String byteArrayToHexString(byte[] bytes) {
public static String toHexString(String input) {
try {
byte[] bytes = input.getBytes(Torrent.BYTE_ENCODING);
return Torrent.byteArrayToHexString(bytes);
return Utils.bytesToHex(bytes);
} catch (UnsupportedEncodingException uee) {
return null;
}
Expand Down
41 changes: 41 additions & 0 deletions core/src/main/java/com/turn/ttorrent/common/Utils.java
@@ -0,0 +1,41 @@
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.turn.ttorrent.common;

public class Utils {

private final static char[] HEX_SYMBOLS = "0123456789ABCDEF".toCharArray();

private Utils() {
}

/**
* Convert a byte string to a string containing the hexadecimal
* representation of the original data.
*
* @param bytes The byte array to convert.
* @see <a href="http://stackoverflow.com/questions/332079">http://stackoverflow.com/questions/332079/a>
*/
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_SYMBOLS[v >>> 4];
hexChars[j * 2 + 1] = HEX_SYMBOLS[v & 0x0F];
}
return new String(hexChars);
}

}
Expand Up @@ -21,6 +21,7 @@
import com.turn.ttorrent.bcodec.InvalidBEncodingException;
import com.turn.ttorrent.common.Peer;
import com.turn.ttorrent.common.Torrent;
import com.turn.ttorrent.common.Utils;
import com.turn.ttorrent.common.protocol.TrackerMessage.AnnounceRequestMessage;

import java.io.IOException;
Expand Down Expand Up @@ -81,7 +82,7 @@ public byte[] getInfoHash() {

@Override
public String getHexInfoHash() {
return Torrent.byteArrayToHexString(this.infoHash);
return Utils.bytesToHex(this.infoHash);
}

@Override
Expand Down
Expand Up @@ -16,6 +16,7 @@
package com.turn.ttorrent.common.protocol.udp;

import com.turn.ttorrent.common.Torrent;
import com.turn.ttorrent.common.Utils;
import com.turn.ttorrent.common.protocol.TrackerMessage;

import java.net.InetAddress;
Expand Down Expand Up @@ -89,7 +90,7 @@ public byte[] getInfoHash() {

@Override
public String getHexInfoHash() {
return Torrent.byteArrayToHexString(this.infoHash);
return Utils.bytesToHex(this.infoHash);
}

@Override
Expand All @@ -99,7 +100,7 @@ public byte[] getPeerId() {

@Override
public String getHexPeerId() {
return Torrent.byteArrayToHexString(this.peerId);
return Utils.bytesToHex(this.peerId);
}

@Override
Expand Down
70 changes: 70 additions & 0 deletions core/src/test/java/com/turn/ttorrent/common/UtilsTest.java
@@ -0,0 +1,70 @@
/**
* Copyright (C) 2016 Philipp Henkel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.turn.ttorrent.common;

import org.testng.annotations.Test;

import static org.testng.AssertJUnit.assertEquals;


public class UtilsTest {

@Test(expectedExceptions = NullPointerException.class)
public void testBytesToHexWithNull() {
Utils.bytesToHex(null);
}

@Test
public void testBytesToHexWithEmptyByteArray() {
assertEquals("", Utils.bytesToHex(new byte[0]));
}

@Test
public void testBytesToHexWithSingleByte() {
assertEquals("BC", Utils.bytesToHex(new byte[]{
(byte) 0xBC
}));
}

@Test
public void testBytesToHexWithZeroByte() {
assertEquals("00", Utils.bytesToHex(new byte[1]));
}

@Test
public void testBytesToHexWithLeadingZero() {
assertEquals("0053FF", Utils.bytesToHex(new byte[]{
(byte) 0x00, (byte) 0x53, (byte) 0xFF
}));
}

@Test
public void testBytesToHexTrailingZero() {
assertEquals("AA004500", Utils.bytesToHex(new byte[]{
(byte) 0xAA, (byte) 0x00, (byte) 0x45, (byte) 0x00
}));
}

@Test
public void testBytesToHexAllSymbols() {
assertEquals("0123456789ABCDEF", Utils.bytesToHex(new byte[]{
(byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67,
(byte) 0x89, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF
}));
}

}

0 comments on commit 7acb2d0

Please sign in to comment.