|
| 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, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +// |
| 19 | +package com.cloud.utils.storage; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | + |
| 23 | +import java.io.ByteArrayInputStream; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.nio.ByteBuffer; |
| 27 | +import java.nio.charset.Charset; |
| 28 | + |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +public class QCOW2UtilsTest { |
| 33 | + |
| 34 | + InputStream inputStream; |
| 35 | + |
| 36 | + final Long virtualSize = 21474836480L; |
| 37 | + |
| 38 | + /** |
| 39 | + * The QCOW2 Header |
| 40 | + * |
| 41 | + * uint32_t magic; |
| 42 | + * uint32_t version; |
| 43 | + * |
| 44 | + * uint64_t backing_file_offset; |
| 45 | + * uint32_t backing_file_size; |
| 46 | + * |
| 47 | + * uint32_t cluster_bits; |
| 48 | + * uint64_t size; |
| 49 | + * |
| 50 | + * uint32_t crypt_method; |
| 51 | + * |
| 52 | + * uint32_t l1_size; |
| 53 | + * int64_t l1_table_offset; |
| 54 | + * |
| 55 | + * uint64_t refcount_table_offset; |
| 56 | + * uint32_t refcount_table_clusters; |
| 57 | + * |
| 58 | + * uint32_t nb_snapshots; |
| 59 | + * uint64_t snapshots_offset; |
| 60 | + * |
| 61 | + * @see https://people.gnome.org/~markmc/qcow-image-format.html |
| 62 | + */ |
| 63 | + |
| 64 | + @Before |
| 65 | + public void setup() { |
| 66 | + |
| 67 | + ByteBuffer byteBuffer = ByteBuffer.allocate(72); |
| 68 | + |
| 69 | + // Magic |
| 70 | + byteBuffer.put("QFI".getBytes(Charset.forName("UTF-8"))); |
| 71 | + byteBuffer.put((byte)0xfb); |
| 72 | + |
| 73 | + // Version |
| 74 | + byteBuffer.putInt(2); |
| 75 | + |
| 76 | + // Backing file offset |
| 77 | + byteBuffer.putLong(0L); |
| 78 | + |
| 79 | + // Backing file size |
| 80 | + byteBuffer.putInt(0); |
| 81 | + |
| 82 | + // Cluster bits |
| 83 | + byteBuffer.putInt(0); |
| 84 | + |
| 85 | + // Size |
| 86 | + byteBuffer.putLong(virtualSize); |
| 87 | + |
| 88 | + // Crypt method |
| 89 | + byteBuffer.putInt(0); |
| 90 | + |
| 91 | + // L1 Size |
| 92 | + byteBuffer.putInt(0); |
| 93 | + |
| 94 | + // L1 Table offset |
| 95 | + byteBuffer.putLong(0L); |
| 96 | + |
| 97 | + // Refcount table offset |
| 98 | + byteBuffer.putLong(0L); |
| 99 | + |
| 100 | + // Refcount table cluster |
| 101 | + byteBuffer.putInt(0); |
| 102 | + |
| 103 | + // NB Snapshots |
| 104 | + byteBuffer.putInt(0); |
| 105 | + |
| 106 | + // Snapshots offset |
| 107 | + byteBuffer.putLong(0L); |
| 108 | + |
| 109 | + inputStream = new ByteArrayInputStream(byteBuffer.array()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void getVirtualSizeHeaderLocation() { |
| 114 | + assertEquals(24, QCOW2Utils.getVirtualSizeHeaderLocation()); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void getVirtualSizeTest() throws IOException { |
| 119 | + assertEquals(virtualSize.longValue(), QCOW2Utils.getVirtualSize(inputStream)); |
| 120 | + } |
| 121 | +} |
0 commit comments