1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.apache.hadoop.hdfs.protocolPB;
19 
20 import static org.hamcrest.CoreMatchers.is;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertThat;
23 import static org.junit.Assert.assertTrue;
24 
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
28 
29 import org.apache.hadoop.fs.permission.AclEntry;
30 import org.apache.hadoop.fs.permission.AclEntryScope;
31 import org.apache.hadoop.fs.permission.AclEntryType;
32 import org.apache.hadoop.fs.permission.AclStatus;
33 import org.apache.hadoop.fs.permission.FsAction;
34 import org.apache.hadoop.fs.StorageType;
35 import org.apache.hadoop.hdfs.DFSTestUtil;
36 import org.apache.hadoop.hdfs.protocol.Block;
37 import org.apache.hadoop.hdfs.protocol.DatanodeID;
38 import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
39 import org.apache.hadoop.hdfs.protocol.DatanodeInfo.AdminStates;
40 import org.apache.hadoop.hdfs.protocol.ExtendedBlock;
41 import org.apache.hadoop.hdfs.protocol.LocatedBlock;
42 import org.apache.hadoop.hdfs.protocol.proto.DatanodeProtocolProtos.BlockCommandProto;
43 import org.apache.hadoop.hdfs.protocol.proto.DatanodeProtocolProtos.BlockRecoveryCommandProto;
44 import org.apache.hadoop.hdfs.protocol.proto.DatanodeProtocolProtos.DatanodeRegistrationProto;
45 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos;
46 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.BlockKeyProto;
47 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.BlockProto;
48 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.BlockWithLocationsProto;
49 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.BlocksWithLocationsProto;
50 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.CheckpointSignatureProto;
51 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.DatanodeIDProto;
52 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.DatanodeStorageProto;
53 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.ExportedBlockKeysProto;
54 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.ExtendedBlockProto;
55 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.LocatedBlockProto;
56 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.NamenodeRegistrationProto;
57 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.NamenodeRegistrationProto.NamenodeRoleProto;
58 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.NamespaceInfoProto;
59 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.RecoveringBlockProto;
60 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.RemoteEditLogManifestProto;
61 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.RemoteEditLogProto;
62 import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.StorageInfoProto;
63 import org.apache.hadoop.hdfs.security.token.block.BlockKey;
64 import org.apache.hadoop.hdfs.security.token.block.BlockTokenIdentifier;
65 import org.apache.hadoop.hdfs.security.token.block.ExportedBlockKeys;
66 import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.NamenodeRole;
67 import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.NodeType;
68 import org.apache.hadoop.hdfs.server.common.StorageInfo;
69 import org.apache.hadoop.hdfs.server.namenode.CheckpointSignature;
70 import org.apache.hadoop.hdfs.server.protocol.BlockCommand;
71 import org.apache.hadoop.hdfs.server.protocol.BlockRecoveryCommand;
72 import org.apache.hadoop.hdfs.server.protocol.BlockRecoveryCommand.RecoveringBlock;
73 import org.apache.hadoop.hdfs.server.protocol.BlocksWithLocations;
74 import org.apache.hadoop.hdfs.server.protocol.BlocksWithLocations.BlockWithLocations;
75 import org.apache.hadoop.hdfs.server.protocol.DatanodeProtocol;
76 import org.apache.hadoop.hdfs.server.protocol.DatanodeRegistration;
77 import org.apache.hadoop.hdfs.server.protocol.DatanodeStorage;
78 import org.apache.hadoop.hdfs.server.protocol.NamenodeRegistration;
79 import org.apache.hadoop.hdfs.server.protocol.NamespaceInfo;
80 import org.apache.hadoop.hdfs.server.protocol.RemoteEditLog;
81 import org.apache.hadoop.hdfs.server.protocol.RemoteEditLogManifest;
82 import org.apache.hadoop.io.Text;
83 import org.apache.hadoop.security.proto.SecurityProtos.TokenProto;
84 import org.apache.hadoop.security.token.Token;
85 import org.apache.hadoop.util.DataChecksum;
86 import org.junit.Assert;
87 import org.junit.Test;
88 
89 import com.google.common.base.Joiner;
90 import com.google.common.collect.ImmutableList;
91 import com.google.common.collect.Lists;
92 
93 /**
94  * Tests for {@link PBHelper}
95  */
96 public class TestPBHelper {
97 
98   /**
99    * Used for asserting equality on doubles.
100    */
101   private static final double DELTA = 0.000001;
102 
103   @Test
testConvertNamenodeRole()104   public void testConvertNamenodeRole() {
105     assertEquals(NamenodeRoleProto.BACKUP,
106         PBHelper.convert(NamenodeRole.BACKUP));
107     assertEquals(NamenodeRoleProto.CHECKPOINT,
108         PBHelper.convert(NamenodeRole.CHECKPOINT));
109     assertEquals(NamenodeRoleProto.NAMENODE,
110         PBHelper.convert(NamenodeRole.NAMENODE));
111     assertEquals(NamenodeRole.BACKUP,
112         PBHelper.convert(NamenodeRoleProto.BACKUP));
113     assertEquals(NamenodeRole.CHECKPOINT,
114         PBHelper.convert(NamenodeRoleProto.CHECKPOINT));
115     assertEquals(NamenodeRole.NAMENODE,
116         PBHelper.convert(NamenodeRoleProto.NAMENODE));
117   }
118 
getStorageInfo(NodeType type)119   private static StorageInfo getStorageInfo(NodeType type) {
120     return new StorageInfo(1, 2, "cid", 3, type);
121   }
122 
123   @Test
testConvertStoragInfo()124   public void testConvertStoragInfo() {
125     StorageInfo info = getStorageInfo(NodeType.NAME_NODE);
126     StorageInfoProto infoProto = PBHelper.convert(info);
127     StorageInfo info2 = PBHelper.convert(infoProto, NodeType.NAME_NODE);
128     assertEquals(info.getClusterID(), info2.getClusterID());
129     assertEquals(info.getCTime(), info2.getCTime());
130     assertEquals(info.getLayoutVersion(), info2.getLayoutVersion());
131     assertEquals(info.getNamespaceID(), info2.getNamespaceID());
132   }
133 
134   @Test
testConvertNamenodeRegistration()135   public void testConvertNamenodeRegistration() {
136     StorageInfo info = getStorageInfo(NodeType.NAME_NODE);
137     NamenodeRegistration reg = new NamenodeRegistration("address:999",
138         "http:1000", info, NamenodeRole.NAMENODE);
139     NamenodeRegistrationProto regProto = PBHelper.convert(reg);
140     NamenodeRegistration reg2 = PBHelper.convert(regProto);
141     assertEquals(reg.getAddress(), reg2.getAddress());
142     assertEquals(reg.getClusterID(), reg2.getClusterID());
143     assertEquals(reg.getCTime(), reg2.getCTime());
144     assertEquals(reg.getHttpAddress(), reg2.getHttpAddress());
145     assertEquals(reg.getLayoutVersion(), reg2.getLayoutVersion());
146     assertEquals(reg.getNamespaceID(), reg2.getNamespaceID());
147     assertEquals(reg.getRegistrationID(), reg2.getRegistrationID());
148     assertEquals(reg.getRole(), reg2.getRole());
149     assertEquals(reg.getVersion(), reg2.getVersion());
150 
151   }
152 
153   @Test
testConvertDatanodeID()154   public void testConvertDatanodeID() {
155     DatanodeID dn = DFSTestUtil.getLocalDatanodeID();
156     DatanodeIDProto dnProto = PBHelper.convert(dn);
157     DatanodeID dn2 = PBHelper.convert(dnProto);
158     compare(dn, dn2);
159   }
160 
compare(DatanodeID dn, DatanodeID dn2)161   void compare(DatanodeID dn, DatanodeID dn2) {
162     assertEquals(dn.getIpAddr(), dn2.getIpAddr());
163     assertEquals(dn.getHostName(), dn2.getHostName());
164     assertEquals(dn.getDatanodeUuid(), dn2.getDatanodeUuid());
165     assertEquals(dn.getXferPort(), dn2.getXferPort());
166     assertEquals(dn.getInfoPort(), dn2.getInfoPort());
167     assertEquals(dn.getIpcPort(), dn2.getIpcPort());
168   }
169 
compare(DatanodeStorage dns1, DatanodeStorage dns2)170   void compare(DatanodeStorage dns1, DatanodeStorage dns2) {
171     assertThat(dns2.getStorageID(), is(dns1.getStorageID()));
172     assertThat(dns2.getState(), is(dns1.getState()));
173     assertThat(dns2.getStorageType(), is(dns1.getStorageType()));
174   }
175 
176   @Test
testConvertBlock()177   public void testConvertBlock() {
178     Block b = new Block(1, 100, 3);
179     BlockProto bProto = PBHelper.convert(b);
180     Block b2 = PBHelper.convert(bProto);
181     assertEquals(b, b2);
182   }
183 
getBlockWithLocations(int bid)184   private static BlockWithLocations getBlockWithLocations(int bid) {
185     final String[] datanodeUuids = {"dn1", "dn2", "dn3"};
186     final String[] storageIDs = {"s1", "s2", "s3"};
187     final StorageType[] storageTypes = {
188         StorageType.DISK, StorageType.DISK, StorageType.DISK};
189     return new BlockWithLocations(new Block(bid, 0, 1),
190         datanodeUuids, storageIDs, storageTypes);
191   }
192 
compare(BlockWithLocations locs1, BlockWithLocations locs2)193   private void compare(BlockWithLocations locs1, BlockWithLocations locs2) {
194     assertEquals(locs1.getBlock(), locs2.getBlock());
195     assertTrue(Arrays.equals(locs1.getStorageIDs(), locs2.getStorageIDs()));
196   }
197 
198   @Test
testConvertBlockWithLocations()199   public void testConvertBlockWithLocations() {
200     BlockWithLocations locs = getBlockWithLocations(1);
201     BlockWithLocationsProto locsProto = PBHelper.convert(locs);
202     BlockWithLocations locs2 = PBHelper.convert(locsProto);
203     compare(locs, locs2);
204   }
205 
206   @Test
testConvertBlocksWithLocations()207   public void testConvertBlocksWithLocations() {
208     BlockWithLocations[] list = new BlockWithLocations[] {
209         getBlockWithLocations(1), getBlockWithLocations(2) };
210     BlocksWithLocations locs = new BlocksWithLocations(list);
211     BlocksWithLocationsProto locsProto = PBHelper.convert(locs);
212     BlocksWithLocations locs2 = PBHelper.convert(locsProto);
213     BlockWithLocations[] blocks = locs.getBlocks();
214     BlockWithLocations[] blocks2 = locs2.getBlocks();
215     assertEquals(blocks.length, blocks2.length);
216     for (int i = 0; i < blocks.length; i++) {
217       compare(blocks[i], blocks2[i]);
218     }
219   }
220 
getBlockKey(int keyId)221   private static BlockKey getBlockKey(int keyId) {
222     return new BlockKey(keyId, 10, "encodedKey".getBytes());
223   }
224 
compare(BlockKey k1, BlockKey k2)225   private void compare(BlockKey k1, BlockKey k2) {
226     assertEquals(k1.getExpiryDate(), k2.getExpiryDate());
227     assertEquals(k1.getKeyId(), k2.getKeyId());
228     assertTrue(Arrays.equals(k1.getEncodedKey(), k2.getEncodedKey()));
229   }
230 
231   @Test
testConvertBlockKey()232   public void testConvertBlockKey() {
233     BlockKey key = getBlockKey(1);
234     BlockKeyProto keyProto = PBHelper.convert(key);
235     BlockKey key1 = PBHelper.convert(keyProto);
236     compare(key, key1);
237   }
238 
239   @Test
testConvertExportedBlockKeys()240   public void testConvertExportedBlockKeys() {
241     BlockKey[] keys = new BlockKey[] { getBlockKey(2), getBlockKey(3) };
242     ExportedBlockKeys expKeys = new ExportedBlockKeys(true, 9, 10,
243         getBlockKey(1), keys);
244     ExportedBlockKeysProto expKeysProto = PBHelper.convert(expKeys);
245     ExportedBlockKeys expKeys1 = PBHelper.convert(expKeysProto);
246     compare(expKeys, expKeys1);
247   }
248 
compare(ExportedBlockKeys expKeys, ExportedBlockKeys expKeys1)249   void compare(ExportedBlockKeys expKeys, ExportedBlockKeys expKeys1) {
250     BlockKey[] allKeys = expKeys.getAllKeys();
251     BlockKey[] allKeys1 = expKeys1.getAllKeys();
252     assertEquals(allKeys.length, allKeys1.length);
253     for (int i = 0; i < allKeys.length; i++) {
254       compare(allKeys[i], allKeys1[i]);
255     }
256     compare(expKeys.getCurrentKey(), expKeys1.getCurrentKey());
257     assertEquals(expKeys.getKeyUpdateInterval(),
258         expKeys1.getKeyUpdateInterval());
259     assertEquals(expKeys.getTokenLifetime(), expKeys1.getTokenLifetime());
260   }
261 
262   @Test
testConvertCheckpointSignature()263   public void testConvertCheckpointSignature() {
264     CheckpointSignature s = new CheckpointSignature(
265         getStorageInfo(NodeType.NAME_NODE), "bpid", 100, 1);
266     CheckpointSignatureProto sProto = PBHelper.convert(s);
267     CheckpointSignature s1 = PBHelper.convert(sProto);
268     assertEquals(s.getBlockpoolID(), s1.getBlockpoolID());
269     assertEquals(s.getClusterID(), s1.getClusterID());
270     assertEquals(s.getCTime(), s1.getCTime());
271     assertEquals(s.getCurSegmentTxId(), s1.getCurSegmentTxId());
272     assertEquals(s.getLayoutVersion(), s1.getLayoutVersion());
273     assertEquals(s.getMostRecentCheckpointTxId(),
274         s1.getMostRecentCheckpointTxId());
275     assertEquals(s.getNamespaceID(), s1.getNamespaceID());
276   }
277 
compare(RemoteEditLog l1, RemoteEditLog l2)278   private static void compare(RemoteEditLog l1, RemoteEditLog l2) {
279     assertEquals(l1.getEndTxId(), l2.getEndTxId());
280     assertEquals(l1.getStartTxId(), l2.getStartTxId());
281   }
282 
283   @Test
testConvertRemoteEditLog()284   public void testConvertRemoteEditLog() {
285     RemoteEditLog l = new RemoteEditLog(1, 100);
286     RemoteEditLogProto lProto = PBHelper.convert(l);
287     RemoteEditLog l1 = PBHelper.convert(lProto);
288     compare(l, l1);
289   }
290 
291   @Test
testConvertRemoteEditLogManifest()292   public void testConvertRemoteEditLogManifest() {
293     List<RemoteEditLog> logs = new ArrayList<RemoteEditLog>();
294     logs.add(new RemoteEditLog(1, 10));
295     logs.add(new RemoteEditLog(11, 20));
296     RemoteEditLogManifest m = new RemoteEditLogManifest(logs);
297     RemoteEditLogManifestProto mProto = PBHelper.convert(m);
298     RemoteEditLogManifest m1 = PBHelper.convert(mProto);
299 
300     List<RemoteEditLog> logs1 = m1.getLogs();
301     assertEquals(logs.size(), logs1.size());
302     for (int i = 0; i < logs.size(); i++) {
303       compare(logs.get(i), logs1.get(i));
304     }
305   }
getExtendedBlock()306   public ExtendedBlock getExtendedBlock() {
307     return getExtendedBlock(1);
308   }
309 
getExtendedBlock(long blkid)310   public ExtendedBlock getExtendedBlock(long blkid) {
311     return new ExtendedBlock("bpid", blkid, 100, 2);
312   }
313 
compare(DatanodeInfo dn1, DatanodeInfo dn2)314   private void compare(DatanodeInfo dn1, DatanodeInfo dn2) {
315       assertEquals(dn1.getAdminState(), dn2.getAdminState());
316       assertEquals(dn1.getBlockPoolUsed(), dn2.getBlockPoolUsed());
317       assertEquals(dn1.getBlockPoolUsedPercent(),
318           dn2.getBlockPoolUsedPercent(), DELTA);
319       assertEquals(dn1.getCapacity(), dn2.getCapacity());
320       assertEquals(dn1.getDatanodeReport(), dn2.getDatanodeReport());
321       assertEquals(dn1.getDfsUsed(), dn1.getDfsUsed());
322       assertEquals(dn1.getDfsUsedPercent(), dn1.getDfsUsedPercent(), DELTA);
323       assertEquals(dn1.getIpAddr(), dn2.getIpAddr());
324       assertEquals(dn1.getHostName(), dn2.getHostName());
325       assertEquals(dn1.getInfoPort(), dn2.getInfoPort());
326       assertEquals(dn1.getIpcPort(), dn2.getIpcPort());
327       assertEquals(dn1.getLastUpdate(), dn2.getLastUpdate());
328       assertEquals(dn1.getLevel(), dn2.getLevel());
329       assertEquals(dn1.getNetworkLocation(), dn2.getNetworkLocation());
330   }
331 
332   @Test
testConvertExtendedBlock()333   public void testConvertExtendedBlock() {
334     ExtendedBlock b = getExtendedBlock();
335     ExtendedBlockProto bProto = PBHelper.convert(b);
336     ExtendedBlock b1 = PBHelper.convert(bProto);
337     assertEquals(b, b1);
338 
339     b.setBlockId(-1);
340     bProto = PBHelper.convert(b);
341     b1 = PBHelper.convert(bProto);
342     assertEquals(b, b1);
343   }
344 
345   @Test
testConvertRecoveringBlock()346   public void testConvertRecoveringBlock() {
347     DatanodeInfo di1 = DFSTestUtil.getLocalDatanodeInfo();
348     DatanodeInfo di2 = DFSTestUtil.getLocalDatanodeInfo();
349     DatanodeInfo[] dnInfo = new DatanodeInfo[] { di1, di2 };
350     RecoveringBlock b = new RecoveringBlock(getExtendedBlock(), dnInfo, 3);
351     RecoveringBlockProto bProto = PBHelper.convert(b);
352     RecoveringBlock b1 = PBHelper.convert(bProto);
353     assertEquals(b.getBlock(), b1.getBlock());
354     DatanodeInfo[] dnInfo1 = b1.getLocations();
355     assertEquals(dnInfo.length, dnInfo1.length);
356     for (int i=0; i < dnInfo.length; i++) {
357       compare(dnInfo[0], dnInfo1[0]);
358     }
359   }
360 
361   @Test
testConvertBlockRecoveryCommand()362   public void testConvertBlockRecoveryCommand() {
363     DatanodeInfo di1 = DFSTestUtil.getLocalDatanodeInfo();
364     DatanodeInfo di2 = DFSTestUtil.getLocalDatanodeInfo();
365     DatanodeInfo[] dnInfo = new DatanodeInfo[] { di1, di2 };
366 
367     List<RecoveringBlock> blks = ImmutableList.of(
368       new RecoveringBlock(getExtendedBlock(1), dnInfo, 3),
369       new RecoveringBlock(getExtendedBlock(2), dnInfo, 3)
370     );
371 
372     BlockRecoveryCommand cmd = new BlockRecoveryCommand(blks);
373     BlockRecoveryCommandProto proto = PBHelper.convert(cmd);
374     assertEquals(1, proto.getBlocks(0).getBlock().getB().getBlockId());
375     assertEquals(2, proto.getBlocks(1).getBlock().getB().getBlockId());
376 
377     BlockRecoveryCommand cmd2 = PBHelper.convert(proto);
378 
379     List<RecoveringBlock> cmd2Blks = Lists.newArrayList(
380         cmd2.getRecoveringBlocks());
381     assertEquals(blks.get(0).getBlock(), cmd2Blks.get(0).getBlock());
382     assertEquals(blks.get(1).getBlock(), cmd2Blks.get(1).getBlock());
383     assertEquals(Joiner.on(",").join(blks), Joiner.on(",").join(cmd2Blks));
384     assertEquals(cmd.toString(), cmd2.toString());
385   }
386 
387 
388   @Test
testConvertText()389   public void testConvertText() {
390     Text t = new Text("abc".getBytes());
391     String s = t.toString();
392     Text t1 = new Text(s);
393     assertEquals(t, t1);
394   }
395 
396   @Test
testConvertBlockToken()397   public void testConvertBlockToken() {
398     Token<BlockTokenIdentifier> token = new Token<BlockTokenIdentifier>(
399         "identifier".getBytes(), "password".getBytes(), new Text("kind"),
400         new Text("service"));
401     TokenProto tokenProto = PBHelper.convert(token);
402     Token<BlockTokenIdentifier> token2 = PBHelper.convert(tokenProto);
403     compare(token, token2);
404   }
405 
406   @Test
testConvertNamespaceInfo()407   public void testConvertNamespaceInfo() {
408     NamespaceInfo info = new NamespaceInfo(37, "clusterID", "bpID", 2300);
409     NamespaceInfoProto proto = PBHelper.convert(info);
410     NamespaceInfo info2 = PBHelper.convert(proto);
411     compare(info, info2); //Compare the StorageInfo
412     assertEquals(info.getBlockPoolID(), info2.getBlockPoolID());
413     assertEquals(info.getBuildVersion(), info2.getBuildVersion());
414   }
415 
compare(StorageInfo expected, StorageInfo actual)416   private void compare(StorageInfo expected, StorageInfo actual) {
417     assertEquals(expected.clusterID, actual.clusterID);
418     assertEquals(expected.namespaceID, actual.namespaceID);
419     assertEquals(expected.cTime, actual.cTime);
420     assertEquals(expected.layoutVersion, actual.layoutVersion);
421   }
422 
compare(Token<BlockTokenIdentifier> expected, Token<BlockTokenIdentifier> actual)423   private void compare(Token<BlockTokenIdentifier> expected,
424       Token<BlockTokenIdentifier> actual) {
425     assertTrue(Arrays.equals(expected.getIdentifier(), actual.getIdentifier()));
426     assertTrue(Arrays.equals(expected.getPassword(), actual.getPassword()));
427     assertEquals(expected.getKind(), actual.getKind());
428     assertEquals(expected.getService(), actual.getService());
429   }
430 
compare(LocatedBlock expected, LocatedBlock actual)431   private void compare(LocatedBlock expected, LocatedBlock actual) {
432     assertEquals(expected.getBlock(), actual.getBlock());
433     compare(expected.getBlockToken(), actual.getBlockToken());
434     assertEquals(expected.getStartOffset(), actual.getStartOffset());
435     assertEquals(expected.isCorrupt(), actual.isCorrupt());
436     DatanodeInfo [] ei = expected.getLocations();
437     DatanodeInfo [] ai = actual.getLocations();
438     assertEquals(ei.length, ai.length);
439     for (int i = 0; i < ei.length ; i++) {
440       compare(ei[i], ai[i]);
441     }
442   }
443 
createLocatedBlock()444   private LocatedBlock createLocatedBlock() {
445     DatanodeInfo[] dnInfos = {
446         DFSTestUtil.getLocalDatanodeInfo("127.0.0.1", "h1",
447             AdminStates.DECOMMISSION_INPROGRESS),
448         DFSTestUtil.getLocalDatanodeInfo("127.0.0.1", "h2",
449             AdminStates.DECOMMISSIONED),
450         DFSTestUtil.getLocalDatanodeInfo("127.0.0.1", "h3",
451             AdminStates.NORMAL),
452         DFSTestUtil.getLocalDatanodeInfo("127.0.0.1", "h4",
453             AdminStates.NORMAL),
454     };
455     String[] storageIDs = {"s1", "s2", "s3", "s4"};
456     StorageType[] media = {
457         StorageType.DISK,
458         StorageType.SSD,
459         StorageType.DISK,
460         StorageType.RAM_DISK
461     };
462     LocatedBlock lb = new LocatedBlock(
463         new ExtendedBlock("bp12", 12345, 10, 53),
464         dnInfos, storageIDs, media, 5, false, new DatanodeInfo[]{});
465     lb.setBlockToken(new Token<BlockTokenIdentifier>(
466         "identifier".getBytes(), "password".getBytes(), new Text("kind"),
467         new Text("service")));
468     return lb;
469   }
470 
createLocatedBlockNoStorageMedia()471   private LocatedBlock createLocatedBlockNoStorageMedia() {
472     DatanodeInfo[] dnInfos = {
473         DFSTestUtil.getLocalDatanodeInfo("127.0.0.1", "h1",
474                                          AdminStates.DECOMMISSION_INPROGRESS),
475         DFSTestUtil.getLocalDatanodeInfo("127.0.0.1", "h2",
476                                          AdminStates.DECOMMISSIONED),
477         DFSTestUtil.getLocalDatanodeInfo("127.0.0.1", "h3",
478                                          AdminStates.NORMAL)
479     };
480     LocatedBlock lb = new LocatedBlock(
481         new ExtendedBlock("bp12", 12345, 10, 53), dnInfos, 5, false);
482     lb.setBlockToken(new Token<BlockTokenIdentifier>(
483         "identifier".getBytes(), "password".getBytes(), new Text("kind"),
484         new Text("service")));
485     return lb;
486   }
487 
488   @Test
testConvertLocatedBlock()489   public void testConvertLocatedBlock() {
490     LocatedBlock lb = createLocatedBlock();
491     LocatedBlockProto lbProto = PBHelper.convert(lb);
492     LocatedBlock lb2 = PBHelper.convert(lbProto);
493     compare(lb,lb2);
494   }
495 
496   @Test
testConvertLocatedBlockNoStorageMedia()497   public void testConvertLocatedBlockNoStorageMedia() {
498     LocatedBlock lb = createLocatedBlockNoStorageMedia();
499     LocatedBlockProto lbProto = PBHelper.convert(lb);
500     LocatedBlock lb2 = PBHelper.convert(lbProto);
501     compare(lb,lb2);
502   }
503 
504   @Test
testConvertLocatedBlockList()505   public void testConvertLocatedBlockList() {
506     ArrayList<LocatedBlock> lbl = new ArrayList<LocatedBlock>();
507     for (int i=0;i<3;i++) {
508       lbl.add(createLocatedBlock());
509     }
510     List<LocatedBlockProto> lbpl = PBHelper.convertLocatedBlock2(lbl);
511     List<LocatedBlock> lbl2 = PBHelper.convertLocatedBlock(lbpl);
512     assertEquals(lbl.size(), lbl2.size());
513     for (int i=0;i<lbl.size();i++) {
514       compare(lbl.get(i), lbl2.get(2));
515     }
516   }
517 
518   @Test
testConvertLocatedBlockArray()519   public void testConvertLocatedBlockArray() {
520     LocatedBlock [] lbl = new LocatedBlock[3];
521     for (int i=0;i<3;i++) {
522       lbl[i] = createLocatedBlock();
523     }
524     LocatedBlockProto [] lbpl = PBHelper.convertLocatedBlock(lbl);
525     LocatedBlock [] lbl2 = PBHelper.convertLocatedBlock(lbpl);
526     assertEquals(lbl.length, lbl2.length);
527     for (int i=0;i<lbl.length;i++) {
528       compare(lbl[i], lbl2[i]);
529     }
530   }
531 
532   @Test
testConvertDatanodeRegistration()533   public void testConvertDatanodeRegistration() {
534     DatanodeID dnId = DFSTestUtil.getLocalDatanodeID();
535     BlockKey[] keys = new BlockKey[] { getBlockKey(2), getBlockKey(3) };
536     ExportedBlockKeys expKeys = new ExportedBlockKeys(true, 9, 10,
537         getBlockKey(1), keys);
538     DatanodeRegistration reg = new DatanodeRegistration(dnId,
539         new StorageInfo(NodeType.DATA_NODE), expKeys, "3.0.0");
540     DatanodeRegistrationProto proto = PBHelper.convert(reg);
541     DatanodeRegistration reg2 = PBHelper.convert(proto);
542     compare(reg.getStorageInfo(), reg2.getStorageInfo());
543     compare(reg.getExportedKeys(), reg2.getExportedKeys());
544     compare(reg, reg2);
545     assertEquals(reg.getSoftwareVersion(), reg2.getSoftwareVersion());
546   }
547 
548   @Test
TestConvertDatanodeStorage()549   public void TestConvertDatanodeStorage() {
550     DatanodeStorage dns1 = new DatanodeStorage(
551         "id1", DatanodeStorage.State.NORMAL, StorageType.SSD);
552 
553     DatanodeStorageProto proto = PBHelper.convert(dns1);
554     DatanodeStorage dns2 = PBHelper.convert(proto);
555     compare(dns1, dns2);
556   }
557 
558   @Test
testConvertBlockCommand()559   public void testConvertBlockCommand() {
560     Block[] blocks = new Block[] { new Block(21), new Block(22) };
561     DatanodeInfo[][] dnInfos = new DatanodeInfo[][] { new DatanodeInfo[1],
562         new DatanodeInfo[2] };
563     dnInfos[0][0] = DFSTestUtil.getLocalDatanodeInfo();
564     dnInfos[1][0] = DFSTestUtil.getLocalDatanodeInfo();
565     dnInfos[1][1] = DFSTestUtil.getLocalDatanodeInfo();
566     String[][] storageIDs = {{"s00"}, {"s10", "s11"}};
567     StorageType[][] storageTypes = {{StorageType.DEFAULT},
568         {StorageType.DEFAULT, StorageType.DEFAULT}};
569     BlockCommand bc = new BlockCommand(DatanodeProtocol.DNA_TRANSFER, "bp1",
570         blocks, dnInfos, storageTypes, storageIDs);
571     BlockCommandProto bcProto = PBHelper.convert(bc);
572     BlockCommand bc2 = PBHelper.convert(bcProto);
573     assertEquals(bc.getAction(), bc2.getAction());
574     assertEquals(bc.getBlocks().length, bc2.getBlocks().length);
575     Block[] blocks2 = bc2.getBlocks();
576     for (int i = 0; i < blocks.length; i++) {
577       assertEquals(blocks[i], blocks2[i]);
578     }
579     DatanodeInfo[][] dnInfos2 = bc2.getTargets();
580     assertEquals(dnInfos.length, dnInfos2.length);
581     for (int i = 0; i < dnInfos.length; i++) {
582       DatanodeInfo[] d1 = dnInfos[i];
583       DatanodeInfo[] d2 = dnInfos2[i];
584       assertEquals(d1.length, d2.length);
585       for (int j = 0; j < d1.length; j++) {
586         compare(d1[j], d2[j]);
587       }
588     }
589   }
590 
591   @Test
testChecksumTypeProto()592   public void testChecksumTypeProto() {
593     assertEquals(DataChecksum.Type.NULL,
594         PBHelper.convert(HdfsProtos.ChecksumTypeProto.CHECKSUM_NULL));
595     assertEquals(DataChecksum.Type.CRC32,
596         PBHelper.convert(HdfsProtos.ChecksumTypeProto.CHECKSUM_CRC32));
597     assertEquals(DataChecksum.Type.CRC32C,
598         PBHelper.convert(HdfsProtos.ChecksumTypeProto.CHECKSUM_CRC32C));
599     assertEquals(PBHelper.convert(DataChecksum.Type.NULL),
600         HdfsProtos.ChecksumTypeProto.CHECKSUM_NULL);
601     assertEquals(PBHelper.convert(DataChecksum.Type.CRC32),
602         HdfsProtos.ChecksumTypeProto.CHECKSUM_CRC32);
603     assertEquals(PBHelper.convert(DataChecksum.Type.CRC32C),
604         HdfsProtos.ChecksumTypeProto.CHECKSUM_CRC32C);
605   }
606 
607   @Test
testAclEntryProto()608   public void testAclEntryProto() {
609     // All fields populated.
610     AclEntry e1 = new AclEntry.Builder().setName("test")
611         .setPermission(FsAction.READ_EXECUTE).setScope(AclEntryScope.DEFAULT)
612         .setType(AclEntryType.OTHER).build();
613     // No name.
614     AclEntry e2 = new AclEntry.Builder().setScope(AclEntryScope.ACCESS)
615         .setType(AclEntryType.USER).setPermission(FsAction.ALL).build();
616     // No permission, which will default to the 0'th enum element.
617     AclEntry e3 = new AclEntry.Builder().setScope(AclEntryScope.ACCESS)
618         .setType(AclEntryType.USER).setName("test").build();
619     AclEntry[] expected = new AclEntry[] { e1, e2,
620         new AclEntry.Builder()
621             .setScope(e3.getScope())
622             .setType(e3.getType())
623             .setName(e3.getName())
624             .setPermission(FsAction.NONE)
625             .build() };
626     AclEntry[] actual = Lists.newArrayList(
627         PBHelper.convertAclEntry(PBHelper.convertAclEntryProto(Lists
628             .newArrayList(e1, e2, e3)))).toArray(new AclEntry[0]);
629     Assert.assertArrayEquals(expected, actual);
630   }
631 
632   @Test
testAclStatusProto()633   public void testAclStatusProto() {
634     AclEntry e = new AclEntry.Builder().setName("test")
635         .setPermission(FsAction.READ_EXECUTE).setScope(AclEntryScope.DEFAULT)
636         .setType(AclEntryType.OTHER).build();
637     AclStatus s = new AclStatus.Builder().owner("foo").group("bar").addEntry(e)
638         .build();
639     Assert.assertEquals(s, PBHelper.convert(PBHelper.convert(s)));
640   }
641 }
642