From: <ga...@us...> - 2011-12-01 15:57:19
|
Revision: 5870 http://jnode.svn.sourceforge.net/jnode/?rev=5870&view=rev Author: galatnm Date: 2011-12-01 15:57:12 +0000 (Thu, 01 Dec 2011) Log Message: ----------- Fix and unit test for catalog key bugs. Modified Paths: -------------- trunk/fs/src/fs/org/jnode/fs/hfsplus/HfsUnicodeString.java trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogKey.java Added Paths: ----------- trunk/fs/src/test/org/jnode/fs/hfsplus/HfsUnicodeStringTest.java trunk/fs/src/test/org/jnode/fs/hfsplus/catalog/ trunk/fs/src/test/org/jnode/fs/hfsplus/catalog/CatalogKeyTest.java Modified: trunk/fs/src/fs/org/jnode/fs/hfsplus/HfsUnicodeString.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/hfsplus/HfsUnicodeString.java 2011-11-30 20:48:58 UTC (rev 5869) +++ trunk/fs/src/fs/org/jnode/fs/hfsplus/HfsUnicodeString.java 2011-12-01 15:57:12 UTC (rev 5870) @@ -31,6 +31,7 @@ /** * Create HFSUnicodeString from existing data. * + * * @param src byte array contains data. * @param offset start of data in the array. */ Modified: trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogKey.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogKey.java 2011-11-30 20:48:58 UTC (rev 5869) +++ trunk/fs/src/fs/org/jnode/fs/hfsplus/catalog/CatalogKey.java 2011-12-01 15:57:12 UTC (rev 5870) @@ -49,7 +49,7 @@ int currentOffset = offset; byte[] ck = new byte[2]; System.arraycopy(src, currentOffset, ck, 0, 2); - keyLength = BigEndian.getInt16(ck, 0) + 2; // Key length doesn't seem to include itself in the size + keyLength = BigEndian.getInt16(ck, 0); currentOffset += 2; ck = new byte[4]; System.arraycopy(src, currentOffset, ck, 0, 4); @@ -71,7 +71,7 @@ public CatalogKey(final CatalogNodeId parentID, final HfsUnicodeString name) { this.parentId = parentID; this.nodeName = name; - this.keyLength = MINIMUM_KEY_LENGTH + name.getLength(); + this.keyLength = MINIMUM_KEY_LENGTH + (name.getLength() * 2) + 2; } public final CatalogNodeId getParentId() { @@ -109,10 +109,11 @@ * @see org.jnode.fs.hfsplus.tree.AbstractKey#getBytes() */ public byte[] getBytes() { - byte[] data = new byte[this.getKeyLength()]; - BigEndian.setInt16(data, 0, this.getKeyLength()); + int length = this.getKeyLength(); + byte[] data = new byte[length]; + BigEndian.setInt16(data, 0, length); System.arraycopy(parentId.getBytes(), 0, data, 2, 4); - System.arraycopy(nodeName.getBytes(), 0, data, 6, nodeName.getLength()); + System.arraycopy(nodeName.getBytes(), 0, data, 6, (nodeName.getLength() *2) + 2); return data; } Added: trunk/fs/src/test/org/jnode/fs/hfsplus/HfsUnicodeStringTest.java =================================================================== --- trunk/fs/src/test/org/jnode/fs/hfsplus/HfsUnicodeStringTest.java (rev 0) +++ trunk/fs/src/test/org/jnode/fs/hfsplus/HfsUnicodeStringTest.java 2011-12-01 15:57:12 UTC (rev 5870) @@ -0,0 +1,30 @@ +package org.jnode.fs.hfsplus; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class HfsUnicodeStringTest { + private byte[] STRING_AS_BYTES_ARRAY = new byte[]{0, 8, 0, 116, 0, 101, 0, 115, 0, 116, 0, 46, 0, 116, 0, 120, 0, 116}; + private String STRING_AS_TEXT = "test.txt"; + + @Test + public void testConstructAsBytesArray() { + HfsUnicodeString string = new HfsUnicodeString(STRING_AS_BYTES_ARRAY,0); + assertEquals(8,string.getLength()); + assertEquals(STRING_AS_TEXT,string.getUnicodeString()); + } + + @Test + public void testConstructAsString() { + HfsUnicodeString string = new HfsUnicodeString(STRING_AS_TEXT); + assertEquals(8,string.getLength()); + byte[] array = string.getBytes(); + int index = 0; + for (byte b : array) { + assertEquals(STRING_AS_BYTES_ARRAY[index],b); + index++; + } + } + +} Added: trunk/fs/src/test/org/jnode/fs/hfsplus/catalog/CatalogKeyTest.java =================================================================== --- trunk/fs/src/test/org/jnode/fs/hfsplus/catalog/CatalogKeyTest.java (rev 0) +++ trunk/fs/src/test/org/jnode/fs/hfsplus/catalog/CatalogKeyTest.java 2011-12-01 15:57:12 UTC (rev 5870) @@ -0,0 +1,42 @@ +package org.jnode.fs.hfsplus.catalog; + +import static org.junit.Assert.assertEquals; + +import org.jnode.fs.hfsplus.HfsUnicodeString; +import org.junit.Test; + +public class CatalogKeyTest { + byte[] KEY_AS_BYTES_ARRAY = new byte[]{0,24,0,0,0,7,0, 8, 0, 116, 0, 101, 0, 115, 0, 116, 0, 46, 0, 116, 0, 120, 0, 116}; + String NODE_NAME_AS_STRING = "test.txt"; + + @Test + public void testKeyFromBytesArray(){ + CatalogKey key = new CatalogKey(KEY_AS_BYTES_ARRAY,0); + assertEquals(NODE_NAME_AS_STRING,key.getNodeName().getUnicodeString()); + assertEquals(24,key.getKeyLength()); + assertEquals(7,key.getParentId().getId()); + } + + @Test + public void testConstructFromCNIDAndString() { + CatalogNodeId id = CatalogNodeId.HFSPLUS_START_CNID; + HfsUnicodeString string = new HfsUnicodeString(NODE_NAME_AS_STRING); + CatalogKey key = new CatalogKey(id,string); + assertEquals(NODE_NAME_AS_STRING,key.getNodeName().getUnicodeString()); + assertEquals(24,key.getKeyLength()); + assertEquals(7,key.getParentId().getId()); + + } + + @Test + public void testConstructFromCNIDAndEmptyString() { + CatalogNodeId id = CatalogNodeId.HFSPLUS_START_CNID; + HfsUnicodeString string = new HfsUnicodeString(""); + CatalogKey key = new CatalogKey(id,string); + assertEquals("",key.getNodeName().getUnicodeString()); + assertEquals(8,key.getKeyLength()); + assertEquals(7,key.getParentId().getId()); + + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |