blob: 9931e7bfd75e0056556e5bdc6e087bfd302efd13 [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;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070020
21import static org.junit.Assert.assertArrayEquals;
22import static org.junit.Assert.assertEquals;
23
24import android.content.Context;
koushik panugantid5c7fb52018-12-12 10:39:44 -080025
26import androidx.test.InstrumentationRegistry;
27import androidx.test.filters.SmallTest;
28import androidx.test.runner.AndroidJUnit4;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070029
30import com.example.android.systemupdatersample.PayloadSpec;
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070031import com.example.android.systemupdatersample.tests.R;
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -070032import com.google.common.base.Charsets;
33import com.google.common.io.Files;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070034
35import org.junit.Before;
36import org.junit.Rule;
37import org.junit.Test;
38import org.junit.rules.ExpectedException;
39import org.junit.runner.RunWith;
40
41import java.io.File;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070042import java.io.IOException;
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070043import java.nio.file.Paths;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070044
45/**
46 * Tests if PayloadSpecs parses update package zip file correctly.
47 */
48@RunWith(AndroidJUnit4.class)
49@SmallTest
50public class PayloadSpecsTest {
51
52 private static final String PROPERTIES_CONTENTS = "k1=val1\nkey2=val2";
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070053
54 private File mTestDir;
55
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -070056 private Context mTargetContext;
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070057 private Context mTestContext;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070058
Zhomart Mukhamejanovb0361ff2018-05-18 10:22:13 -070059 private PayloadSpecs mPayloadSpecs;
60
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070061 @Rule
62 public final ExpectedException thrown = ExpectedException.none();
63
64 @Before
65 public void setUp() {
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -070066 mTargetContext = InstrumentationRegistry.getTargetContext();
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070067 mTestContext = InstrumentationRegistry.getContext();
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070068
Zhomart Mukhamejanov8a6a86a2018-06-07 10:55:16 -070069 mTestDir = mTargetContext.getCacheDir();
Zhomart Mukhamejanovb0361ff2018-05-18 10:22:13 -070070 mPayloadSpecs = new PayloadSpecs();
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070071 }
72
73 @Test
74 public void forNonStreaming_works() throws Exception {
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070075 // Prepare the target file
76 File packageFile = Paths
77 .get(mTargetContext.getCacheDir().getAbsolutePath(), "ota.zip")
78 .toFile();
79 java.nio.file.Files.deleteIfExists(packageFile.toPath());
80 java.nio.file.Files.copy(mTestContext.getResources().openRawResource(R.raw.ota_002_package),
81 packageFile.toPath());
Zhomart Mukhamejanovb0361ff2018-05-18 10:22:13 -070082 PayloadSpec spec = mPayloadSpecs.forNonStreaming(packageFile);
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070083
84 assertEquals("correct url", "file://" + packageFile.getAbsolutePath(), spec.getUrl());
85 assertEquals("correct payload offset",
86 30 + PAYLOAD_BINARY_FILE_NAME.length(), spec.getOffset());
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070087 assertEquals("correct payload size", 1392, spec.getSize());
88 assertEquals(4, spec.getProperties().size());
89 assertEquals(
90 "FILE_HASH=sEAK/NMbU7GGe01xt55FsPafIPk8IYyBOAd6SiDpiMs=",
91 spec.getProperties().get(0));
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070092 }
93
94 @Test
95 public void forNonStreaming_IOException() throws Exception {
96 thrown.expect(IOException.class);
Zhomart Mukhamejanovb0361ff2018-05-18 10:22:13 -070097 mPayloadSpecs.forNonStreaming(new File("/fake/news.zip"));
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070098 }
99
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -0700100 @Test
101 public void forStreaming_works() throws Exception {
102 String url = "http://a.com/b.zip";
103 long offset = 45;
104 long size = 200;
105 File propertiesFile = createMockPropertiesFile();
106
Zhomart Mukhamejanovb0361ff2018-05-18 10:22:13 -0700107 PayloadSpec spec = mPayloadSpecs.forStreaming(url, offset, size, propertiesFile);
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -0700108 assertEquals("same url", url, spec.getUrl());
109 assertEquals("same offset", offset, spec.getOffset());
110 assertEquals("same size", size, spec.getSize());
111 assertArrayEquals("correct properties",
112 new String[]{"k1=val1", "key2=val2"}, spec.getProperties().toArray(new String[0]));
113 }
114
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -0700115 private File createMockPropertiesFile() throws IOException {
116 File propertiesFile = new File(mTestDir, PackageFiles.PAYLOAD_PROPERTIES_FILE_NAME);
117 Files.asCharSink(propertiesFile, Charsets.UTF_8).write(PROPERTIES_CONTENTS);
118 return propertiesFile;
119 }
120
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700121}