blob: 3ba84c116a0b4ae37cca22611ac72df398060298 [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
Zhomart Mukhamejanovb0361ff2018-05-18 10:22:13 -070058 private PayloadSpecs mPayloadSpecs;
59
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070060 @Rule
61 public final ExpectedException thrown = ExpectedException.none();
62
63 @Before
64 public void setUp() {
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -070065 mTargetContext = InstrumentationRegistry.getTargetContext();
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070066 mTestContext = InstrumentationRegistry.getContext();
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070067
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -070068 mTestDir = mTargetContext.getFilesDir();
Zhomart Mukhamejanovb0361ff2018-05-18 10:22:13 -070069 mPayloadSpecs = new PayloadSpecs();
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070070 }
71
72 @Test
73 public void forNonStreaming_works() throws Exception {
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070074 // Prepare the target file
75 File packageFile = Paths
76 .get(mTargetContext.getCacheDir().getAbsolutePath(), "ota.zip")
77 .toFile();
78 java.nio.file.Files.deleteIfExists(packageFile.toPath());
79 java.nio.file.Files.copy(mTestContext.getResources().openRawResource(R.raw.ota_002_package),
80 packageFile.toPath());
Zhomart Mukhamejanovb0361ff2018-05-18 10:22:13 -070081 PayloadSpec spec = mPayloadSpecs.forNonStreaming(packageFile);
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070082
83 assertEquals("correct url", "file://" + packageFile.getAbsolutePath(), spec.getUrl());
84 assertEquals("correct payload offset",
85 30 + PAYLOAD_BINARY_FILE_NAME.length(), spec.getOffset());
Zhomart Mukhamejanov96eb59e2018-05-04 12:17:01 -070086 assertEquals("correct payload size", 1392, spec.getSize());
87 assertEquals(4, spec.getProperties().size());
88 assertEquals(
89 "FILE_HASH=sEAK/NMbU7GGe01xt55FsPafIPk8IYyBOAd6SiDpiMs=",
90 spec.getProperties().get(0));
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070091 }
92
93 @Test
94 public void forNonStreaming_IOException() throws Exception {
95 thrown.expect(IOException.class);
Zhomart Mukhamejanovb0361ff2018-05-18 10:22:13 -070096 mPayloadSpecs.forNonStreaming(new File("/fake/news.zip"));
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070097 }
98
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -070099 @Test
100 public void forStreaming_works() throws Exception {
101 String url = "http://a.com/b.zip";
102 long offset = 45;
103 long size = 200;
104 File propertiesFile = createMockPropertiesFile();
105
Zhomart Mukhamejanovb0361ff2018-05-18 10:22:13 -0700106 PayloadSpec spec = mPayloadSpecs.forStreaming(url, offset, size, propertiesFile);
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -0700107 assertEquals("same url", url, spec.getUrl());
108 assertEquals("same offset", offset, spec.getOffset());
109 assertEquals("same size", size, spec.getSize());
110 assertArrayEquals("correct properties",
111 new String[]{"k1=val1", "key2=val2"}, spec.getProperties().toArray(new String[0]));
112 }
113
Zhomart Mukhamejanove606f6d2018-05-02 20:47:05 -0700114 private File createMockPropertiesFile() throws IOException {
115 File propertiesFile = new File(mTestDir, PackageFiles.PAYLOAD_PROPERTIES_FILE_NAME);
116 Files.asCharSink(propertiesFile, Charsets.UTF_8).write(PROPERTIES_CONTENTS);
117 return propertiesFile;
118 }
119
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700120}