Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.example.android.systemupdatersample.util; |
| 18 | |
| 19 | import static com.example.android.systemupdatersample.util.PackagePropertyFiles.PAYLOAD_BINARY_FILE_NAME; |
| 20 | import static com.example.android.systemupdatersample.util.PackagePropertyFiles.PAYLOAD_PROPERTIES_FILE_NAME; |
| 21 | |
| 22 | import static org.junit.Assert.assertArrayEquals; |
| 23 | import static org.junit.Assert.assertEquals; |
| 24 | |
| 25 | import android.content.Context; |
| 26 | import android.support.test.InstrumentationRegistry; |
| 27 | import android.support.test.filters.SmallTest; |
| 28 | import android.support.test.runner.AndroidJUnit4; |
| 29 | |
| 30 | import com.example.android.systemupdatersample.PayloadSpec; |
| 31 | |
| 32 | import org.junit.Before; |
| 33 | import org.junit.Rule; |
| 34 | import org.junit.Test; |
| 35 | import org.junit.rules.ExpectedException; |
| 36 | import org.junit.runner.RunWith; |
| 37 | |
| 38 | import java.io.File; |
| 39 | import java.io.FileOutputStream; |
| 40 | import java.io.IOException; |
| 41 | import java.nio.charset.StandardCharsets; |
| 42 | import java.util.zip.CRC32; |
| 43 | import java.util.zip.ZipEntry; |
| 44 | import java.util.zip.ZipOutputStream; |
| 45 | |
| 46 | /** |
| 47 | * Tests if PayloadSpecs parses update package zip file correctly. |
| 48 | */ |
| 49 | @RunWith(AndroidJUnit4.class) |
| 50 | @SmallTest |
| 51 | public class PayloadSpecsTest { |
| 52 | |
| 53 | private static final String PROPERTIES_CONTENTS = "k1=val1\nkey2=val2"; |
| 54 | private static final String PAYLOAD_CONTENTS = "hello\nworld"; |
| 55 | private static final int PAYLOAD_SIZE = PAYLOAD_CONTENTS.length(); |
| 56 | |
| 57 | private File mTestDir; |
| 58 | |
| 59 | private Context mContext; |
| 60 | |
| 61 | @Rule |
| 62 | public final ExpectedException thrown = ExpectedException.none(); |
| 63 | |
| 64 | @Before |
| 65 | public void setUp() { |
| 66 | mContext = InstrumentationRegistry.getTargetContext(); |
| 67 | |
| 68 | mTestDir = mContext.getFilesDir(); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void forNonStreaming_works() throws Exception { |
| 73 | File packageFile = createMockZipFile(); |
| 74 | PayloadSpec spec = PayloadSpecs.forNonStreaming(packageFile); |
| 75 | |
| 76 | assertEquals("correct url", "file://" + packageFile.getAbsolutePath(), spec.getUrl()); |
| 77 | assertEquals("correct payload offset", |
| 78 | 30 + PAYLOAD_BINARY_FILE_NAME.length(), spec.getOffset()); |
| 79 | assertEquals("correct payload size", PAYLOAD_SIZE, spec.getSize()); |
| 80 | assertArrayEquals("correct properties", |
| 81 | new String[]{"k1=val1", "key2=val2"}, spec.getProperties().toArray(new String[0])); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void forNonStreaming_IOException() throws Exception { |
| 86 | thrown.expect(IOException.class); |
| 87 | PayloadSpecs.forNonStreaming(new File("/fake/news.zip")); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Creates package zip file that contains payload.bin and payload_properties.txt |
| 92 | */ |
| 93 | private File createMockZipFile() throws IOException { |
| 94 | File testFile = new File(mTestDir, "test.zip"); |
| 95 | try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(testFile))) { |
| 96 | // Add payload.bin entry. |
| 97 | ZipEntry entry = new ZipEntry(PAYLOAD_BINARY_FILE_NAME); |
| 98 | entry.setMethod(ZipEntry.STORED); |
| 99 | entry.setCompressedSize(PAYLOAD_SIZE); |
| 100 | entry.setSize(PAYLOAD_SIZE); |
| 101 | CRC32 crc = new CRC32(); |
| 102 | crc.update(PAYLOAD_CONTENTS.getBytes(StandardCharsets.UTF_8)); |
| 103 | entry.setCrc(crc.getValue()); |
| 104 | zos.putNextEntry(entry); |
| 105 | zos.write(PAYLOAD_CONTENTS.getBytes(StandardCharsets.UTF_8)); |
| 106 | zos.closeEntry(); |
| 107 | |
| 108 | // Add payload properties entry. |
| 109 | ZipEntry propertiesEntry = new ZipEntry(PAYLOAD_PROPERTIES_FILE_NAME); |
| 110 | zos.putNextEntry(propertiesEntry); |
| 111 | zos.write(PROPERTIES_CONTENTS.getBytes(StandardCharsets.UTF_8)); |
| 112 | zos.closeEntry(); |
| 113 | } |
| 114 | return testFile; |
| 115 | } |
| 116 | |
| 117 | } |