blob: c13ba555617456d1532a746771ad20261b18f7da [file] [log] [blame]
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -07001/*
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
17package com.example.android.systemupdatersample.util;
18
Zhomart Mukhamejanovda7e2372018-05-01 12:50:55 -070019import static com.example.android.systemupdatersample.util.PackageFiles.PAYLOAD_BINARY_FILE_NAME;
20import static com.example.android.systemupdatersample.util.PackageFiles.PAYLOAD_PROPERTIES_FILE_NAME;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070021
22import static org.junit.Assert.assertArrayEquals;
23import static org.junit.Assert.assertEquals;
24
25import android.content.Context;
26import android.support.test.InstrumentationRegistry;
27import android.support.test.filters.SmallTest;
28import android.support.test.runner.AndroidJUnit4;
29
30import com.example.android.systemupdatersample.PayloadSpec;
31
32import org.junit.Before;
33import org.junit.Rule;
34import org.junit.Test;
35import org.junit.rules.ExpectedException;
36import org.junit.runner.RunWith;
37
38import java.io.File;
39import java.io.FileOutputStream;
40import java.io.IOException;
41import java.nio.charset.StandardCharsets;
42import java.util.zip.CRC32;
43import java.util.zip.ZipEntry;
44import java.util.zip.ZipOutputStream;
45
46/**
47 * Tests if PayloadSpecs parses update package zip file correctly.
48 */
49@RunWith(AndroidJUnit4.class)
50@SmallTest
51public 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}