diff --git a/services/secondary-storage/server/src/org/apache/cloudstack/storage/template/DownloadManagerImpl.java b/services/secondary-storage/server/src/org/apache/cloudstack/storage/template/DownloadManagerImpl.java index 35010b041c6..a1afe8ddff8 100755 --- a/services/secondary-storage/server/src/org/apache/cloudstack/storage/template/DownloadManagerImpl.java +++ b/services/secondary-storage/server/src/org/apache/cloudstack/storage/template/DownloadManagerImpl.java @@ -24,6 +24,7 @@ import java.io.InputStream; import java.math.BigInteger; import java.net.URI; import java.net.URISyntaxException; +import java.nio.charset.Charset; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; @@ -78,7 +79,6 @@ import com.cloud.storage.template.TemplateProp; import com.cloud.storage.template.VhdProcessor; import com.cloud.storage.template.VmdkProcessor; import com.cloud.utils.NumbersUtil; -import com.cloud.utils.StringUtils; import com.cloud.utils.component.ManagerBase; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.script.OutputInterpreter; @@ -419,7 +419,7 @@ public class DownloadManagerImpl extends ManagerBase implements DownloadManager if (extension.equals("iso")) { templateName = jobs.get(jobId).getTmpltName().trim().replace(" ", "_"); } else { - templateName = java.util.UUID.nameUUIDFromBytes((jobs.get(jobId).getTmpltName() + System.currentTimeMillis()).getBytes(StringUtils.getPreferredCharset())).toString(); + templateName = java.util.UUID.nameUUIDFromBytes((jobs.get(jobId).getTmpltName() + System.currentTimeMillis()).getBytes(Charset.forName("UTF-8"))).toString(); } // run script to mv the temporary template file to the final template diff --git a/utils/test/com/cloud/utils/storage/QCOW2UtilsTest.java b/utils/test/com/cloud/utils/storage/QCOW2UtilsTest.java new file mode 100644 index 00000000000..a3ebe7276f3 --- /dev/null +++ b/utils/test/com/cloud/utils/storage/QCOW2UtilsTest.java @@ -0,0 +1,122 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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.cloud.utils.storage; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; + +import org.junit.Before; +import org.junit.Test; + +public class QCOW2UtilsTest { + + InputStream inputStream; + + final Long virtualSize = 21474836480L; + + /** + * The QCOW2 Header + * + * uint32_t magic; + * uint32_t version; + * + * uint64_t backing_file_offset; + * uint32_t backing_file_size; + * + * uint32_t cluster_bits; + * uint64_t size; + * + * uint32_t crypt_method; + * + * uint32_t l1_size; + * int64_t l1_table_offset; + * + * uint64_t refcount_table_offset; + * uint32_t refcount_table_clusters; + * + * uint32_t nb_snapshots; + * uint64_t snapshots_offset; + * + * @see https://people.gnome.org/~markmc/qcow-image-format.html + */ + + @Before + public void setup() { + + ByteBuffer byteBuffer = ByteBuffer.allocate(72); + + // Magic + byteBuffer.put("QFI".getBytes(Charset.forName("UTF-8"))); + byteBuffer.put((byte)0xfb); + + // Version + byteBuffer.putInt(2); + + // Backing file offset + byteBuffer.putLong(0L); + + // Backing file size + byteBuffer.putInt(0); + + // Cluster bits + byteBuffer.putInt(0); + + // Size + byteBuffer.putLong(virtualSize); + + // Crypt method + byteBuffer.putInt(0); + + // L1 Size + byteBuffer.putInt(0); + + // L1 Table offset + byteBuffer.putLong(0L); + + // Refcount table offset + byteBuffer.putLong(0L); + + // Refcount table cluster + byteBuffer.putInt(0); + + // NB Snapshots + byteBuffer.putInt(0); + + // Snapshots offset + byteBuffer.putLong(0L); + + inputStream = new ByteArrayInputStream(byteBuffer.array()); + } + + @Test + public void getVirtualSizeHeaderLocation() { + assertEquals(24, QCOW2Utils.getVirtualSizeHeaderLocation()); + } + + @Test + public void getVirtualSizeTest() throws IOException { + assertEquals(virtualSize.longValue(), QCOW2Utils.getVirtualSize(inputStream)); + } +}