blob: d9e54652fb2a7bb2ba961f5c7820929d8ecd655a [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;
25import android.support.test.InstrumentationRegistry;
26import android.support.test.filters.SmallTest;
27import android.support.test.runner.AndroidJUnit4;
28
29import com.example.android.systemupdatersample.PayloadSpec;
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070030import com.example.android.systemupdatersample.tests.R;
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -070031import com.google.common.base.Charsets;
32import com.google.common.io.Files;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070033
34import org.junit.Before;
35import org.junit.Rule;
36import org.junit.Test;
37import org.junit.rules.ExpectedException;
38import org.junit.runner.RunWith;
39
40import java.io.File;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070041import java.io.IOException;
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070042import java.nio.file.Paths;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070043
44/**
45 * Tests if PayloadSpecs parses update package zip file correctly.
46 */
47@RunWith(AndroidJUnit4.class)
48@SmallTest
49public class PayloadSpecsTest {
50
51 private static final String PROPERTIES_CONTENTS = "k1=val1\nkey2=val2";
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070052
53 private File mTestDir;
54
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -070055 private Context mTargetContext;
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070056 private Context mTestContext;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070057
58 @Rule
59 public final ExpectedException thrown = ExpectedException.none();
60
61 @Before
62 public void setUp() {
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -070063 mTargetContext = InstrumentationRegistry.getTargetContext();
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070064 mTestContext = InstrumentationRegistry.getContext();
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070065
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -070066 mTestDir = mTargetContext.getFilesDir();
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070067 }
68
69 @Test
70 public void forNonStreaming_works() throws Exception {
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070071 // Prepare the target file
72 File packageFile = Paths
73 .get(mTargetContext.getCacheDir().getAbsolutePath(), "ota.zip")
74 .toFile();
75 java.nio.file.Files.deleteIfExists(packageFile.toPath());
76 java.nio.file.Files.copy(mTestContext.getResources().openRawResource(R.raw.ota_002_package),
77 packageFile.toPath());
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070078 PayloadSpec spec = PayloadSpecs.forNonStreaming(packageFile);
79
80 assertEquals("correct url", "file://" + packageFile.getAbsolutePath(), spec.getUrl());
81 assertEquals("correct payload offset",
82 30 + PAYLOAD_BINARY_FILE_NAME.length(), spec.getOffset());
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070083 assertEquals("correct payload size", 1392, spec.getSize());
84 assertEquals(4, spec.getProperties().size());
85 assertEquals(
86 "FILE_HASH=sEAK/NMbU7GGe01xt55FsPafIPk8IYyBOAd6SiDpiMs=",
87 spec.getProperties().get(0));
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070088 }
89
90 @Test
91 public void forNonStreaming_IOException() throws Exception {
92 thrown.expect(IOException.class);
93 PayloadSpecs.forNonStreaming(new File("/fake/news.zip"));
94 }
95
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -070096 @Test
97 public void forStreaming_works() throws Exception {
98 String url = "http://a.com/b.zip";
99 long offset = 45;
100 long size = 200;
101 File propertiesFile = createMockPropertiesFile();
102
103 PayloadSpec spec = PayloadSpecs.forStreaming(url, offset, size, propertiesFile);
104 assertEquals("same url", url, spec.getUrl());
105 assertEquals("same offset", offset, spec.getOffset());
106 assertEquals("same size", size, spec.getSize());
107 assertArrayEquals("correct properties",
108 new String[]{"k1=val1", "key2=val2"}, spec.getProperties().toArray(new String[0]));
109 }
110
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -0700111 private File createMockPropertiesFile() throws IOException {
112 File propertiesFile = new File(mTestDir, PackageFiles.PAYLOAD_PROPERTIES_FILE_NAME);
113 Files.asCharSink(propertiesFile, Charsets.UTF_8).write(PROPERTIES_CONTENTS);
114 return propertiesFile;
115 }
116
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700117}