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 | |
Zhomart Mukhamejanov | da7e237 | 2018-05-01 12:50:55 -0700 | [diff] [blame] | 19 | import static com.example.android.systemupdatersample.util.PackageFiles.PAYLOAD_BINARY_FILE_NAME; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 20 | |
| 21 | import static org.junit.Assert.assertArrayEquals; |
| 22 | import static org.junit.Assert.assertEquals; |
| 23 | |
| 24 | import android.content.Context; |
| 25 | import android.support.test.InstrumentationRegistry; |
| 26 | import android.support.test.filters.SmallTest; |
| 27 | import android.support.test.runner.AndroidJUnit4; |
| 28 | |
| 29 | import com.example.android.systemupdatersample.PayloadSpec; |
Zhomart Mukhamejanov | 96eb59e | 2018-05-04 12:17:01 -0700 | [diff] [blame] | 30 | import com.example.android.systemupdatersample.tests.R; |
Zhomart Mukhamejanov | e606f6d | 2018-05-02 20:47:05 -0700 | [diff] [blame] | 31 | import com.google.common.base.Charsets; |
| 32 | import com.google.common.io.Files; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 33 | |
| 34 | import org.junit.Before; |
| 35 | import org.junit.Rule; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.rules.ExpectedException; |
| 38 | import org.junit.runner.RunWith; |
| 39 | |
| 40 | import java.io.File; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 41 | import java.io.IOException; |
Zhomart Mukhamejanov | 96eb59e | 2018-05-04 12:17:01 -0700 | [diff] [blame] | 42 | import java.nio.file.Paths; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Tests if PayloadSpecs parses update package zip file correctly. |
| 46 | */ |
| 47 | @RunWith(AndroidJUnit4.class) |
| 48 | @SmallTest |
| 49 | public class PayloadSpecsTest { |
| 50 | |
| 51 | private static final String PROPERTIES_CONTENTS = "k1=val1\nkey2=val2"; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 52 | |
| 53 | private File mTestDir; |
| 54 | |
Zhomart Mukhamejanov | e606f6d | 2018-05-02 20:47:05 -0700 | [diff] [blame] | 55 | private Context mTargetContext; |
Zhomart Mukhamejanov | 96eb59e | 2018-05-04 12:17:01 -0700 | [diff] [blame] | 56 | private Context mTestContext; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 57 | |
Zhomart Mukhamejanov | b0361ff | 2018-05-18 10:22:13 -0700 | [diff] [blame] | 58 | private PayloadSpecs mPayloadSpecs; |
| 59 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 60 | @Rule |
| 61 | public final ExpectedException thrown = ExpectedException.none(); |
| 62 | |
| 63 | @Before |
| 64 | public void setUp() { |
Zhomart Mukhamejanov | e606f6d | 2018-05-02 20:47:05 -0700 | [diff] [blame] | 65 | mTargetContext = InstrumentationRegistry.getTargetContext(); |
Zhomart Mukhamejanov | 96eb59e | 2018-05-04 12:17:01 -0700 | [diff] [blame] | 66 | mTestContext = InstrumentationRegistry.getContext(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 67 | |
Zhomart Mukhamejanov | 8a6a86a | 2018-06-07 10:55:16 -0700 | [diff] [blame] | 68 | mTestDir = mTargetContext.getCacheDir(); |
Zhomart Mukhamejanov | b0361ff | 2018-05-18 10:22:13 -0700 | [diff] [blame] | 69 | mPayloadSpecs = new PayloadSpecs(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void forNonStreaming_works() throws Exception { |
Zhomart Mukhamejanov | 96eb59e | 2018-05-04 12:17:01 -0700 | [diff] [blame] | 74 | // 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 Mukhamejanov | b0361ff | 2018-05-18 10:22:13 -0700 | [diff] [blame] | 81 | PayloadSpec spec = mPayloadSpecs.forNonStreaming(packageFile); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 82 | |
| 83 | assertEquals("correct url", "file://" + packageFile.getAbsolutePath(), spec.getUrl()); |
| 84 | assertEquals("correct payload offset", |
| 85 | 30 + PAYLOAD_BINARY_FILE_NAME.length(), spec.getOffset()); |
Zhomart Mukhamejanov | 96eb59e | 2018-05-04 12:17:01 -0700 | [diff] [blame] | 86 | 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 Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void forNonStreaming_IOException() throws Exception { |
| 95 | thrown.expect(IOException.class); |
Zhomart Mukhamejanov | b0361ff | 2018-05-18 10:22:13 -0700 | [diff] [blame] | 96 | mPayloadSpecs.forNonStreaming(new File("/fake/news.zip")); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Zhomart Mukhamejanov | e606f6d | 2018-05-02 20:47:05 -0700 | [diff] [blame] | 99 | @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 Mukhamejanov | b0361ff | 2018-05-18 10:22:13 -0700 | [diff] [blame] | 106 | PayloadSpec spec = mPayloadSpecs.forStreaming(url, offset, size, propertiesFile); |
Zhomart Mukhamejanov | e606f6d | 2018-05-02 20:47:05 -0700 | [diff] [blame] | 107 | 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 Mukhamejanov | e606f6d | 2018-05-02 20:47:05 -0700 | [diff] [blame] | 114 | 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 Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 120 | } |