CacheQuotaStrategyTest.java revision e40da3c1b7d4eb810b6f067075dbbb011d02a379
1/*
2 * Copyright (C) 2017 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.android.server.storage;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import android.app.usage.CacheQuotaHint;
22import android.test.AndroidTestCase;
23import android.util.Pair;
24
25import com.android.internal.util.FastXmlSerializer;
26
27import org.junit.Before;
28import org.junit.Test;
29import org.junit.runner.RunWith;
30import org.junit.runners.JUnit4;
31
32import java.io.ByteArrayInputStream;
33import java.io.StringWriter;
34import java.util.ArrayList;
35import java.util.List;
36
37@RunWith(JUnit4.class)
38public class CacheQuotaStrategyTest extends AndroidTestCase {
39    StringWriter mWriter;
40    FastXmlSerializer mOut;
41
42    @Before
43    public void setUp() throws Exception {
44        mWriter = new StringWriter();
45        mOut = new FastXmlSerializer();
46        mOut.setOutput(mWriter);
47    }
48
49    @Test
50    public void testEmptyWrite() throws Exception {
51        CacheQuotaStrategy.saveToXml(mOut, new ArrayList<>(), 0);
52        mOut.flush();
53
54        assertThat(mWriter.toString()).isEqualTo(
55                "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" +
56                        "<cache-info previousBytes=\"0\" />\n");
57    }
58
59    @Test
60    public void testWriteOneQuota() throws Exception {
61        ArrayList<CacheQuotaHint> requests = new ArrayList<>();
62        requests.add(buildCacheQuotaHint("uuid", 0, 100));
63
64        CacheQuotaStrategy.saveToXml(mOut, requests, 1000);
65        mOut.flush();
66
67        assertThat(mWriter.toString()).isEqualTo(
68                "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" +
69                "<cache-info previousBytes=\"1000\">\n"
70                        + "<quota uuid=\"uuid\" uid=\"0\" bytes=\"100\" />\n"
71                        + "</cache-info>\n");
72    }
73
74    @Test
75    public void testWriteMultipleQuotas() throws Exception {
76        ArrayList<CacheQuotaHint> requests = new ArrayList<>();
77        requests.add(buildCacheQuotaHint("uuid", 0, 100));
78        requests.add(buildCacheQuotaHint("uuid2", 10, 250));
79
80        CacheQuotaStrategy.saveToXml(mOut, requests, 1000);
81        mOut.flush();
82
83        assertThat(mWriter.toString()).isEqualTo(
84                "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" +
85                        "<cache-info previousBytes=\"1000\">\n"
86                        + "<quota uuid=\"uuid\" uid=\"0\" bytes=\"100\" />\n"
87                        + "<quota uuid=\"uuid2\" uid=\"10\" bytes=\"250\" />\n"
88                        + "</cache-info>\n");
89    }
90
91    @Test
92    public void testNullUuidDoesntCauseCrash() throws Exception {
93        ArrayList<CacheQuotaHint> requests = new ArrayList<>();
94        requests.add(buildCacheQuotaHint(null, 0, 100));
95        requests.add(buildCacheQuotaHint(null, 10, 250));
96
97        CacheQuotaStrategy.saveToXml(mOut, requests, 1000);
98        mOut.flush();
99
100        assertThat(mWriter.toString()).isEqualTo(
101                "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" +
102                        "<cache-info previousBytes=\"1000\">\n"
103                        + "<quota uid=\"0\" bytes=\"100\" />\n"
104                        + "<quota uid=\"10\" bytes=\"250\" />\n"
105                        + "</cache-info>\n");
106    }
107
108    @Test
109    public void testReadMultipleQuotas() throws Exception {
110        String input = "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n"
111                + "<cache-info previousBytes=\"1000\">\n"
112                + "<quota uuid=\"uuid\" uid=\"0\" bytes=\"100\" />\n"
113                + "<quota uuid=\"uuid2\" uid=\"10\" bytes=\"250\" />\n"
114                + "</cache-info>\n";
115
116        Pair<Long, List<CacheQuotaHint>> output =
117                CacheQuotaStrategy.readFromXml(new ByteArrayInputStream(input.getBytes("UTF-8")));
118
119        assertThat(output.first).isEqualTo(1000);
120        assertThat(output.second).containsExactly(buildCacheQuotaHint("uuid", 0, 100),
121                buildCacheQuotaHint("uuid2", 10, 250));
122    }
123
124    private CacheQuotaHint buildCacheQuotaHint(String volumeUuid, int uid, long quota) {
125        return new CacheQuotaHint.Builder()
126                .setVolumeUuid(volumeUuid).setUid(uid).setQuota(quota).build();
127    }
128}