1################################################################ 2# 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance 9# with the License. You may obtain a copy of the License at 10# 11# http://www.apache.org/licenses/LICENSE-2.0 12# 13# Unless required by applicable law or agreed to in writing, 14# software distributed under the License is distributed on an 15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16# KIND, either express or implied. See the License for the 17# specific language governing permissions and limitations 18# under the License. 19 20"""Documents with content in each application, saved with a password (and as a plain twin), for the encryption tests. 21Ported from the ODF harness's fixtures/encrypted/make.py.""" 22import os 23import struct 24import zlib 25 26import uno 27from com.sun.star.awt import Point, Size 28from com.sun.star.table import CellRangeAddress 29 30from aootest_worker.ops import core 31from aootest_worker.ops.core import pv 32 33FILTERS = {"odt": "writer8", "ods": "calc8", "odp": "impress8", "odg": "draw8"} 34FACTORY = {"odt": "swriter", "ods": "scalc", "odp": "simpress", "odg": "sdraw"} 35 36 37def _png(path): 38 raw = b"".join(b"\x00" + b"".join(bytes((x * 16, y * 16, 128)) for x in range(16)) for y in range(16)) 39 40 def chunk(t, b): 41 c = struct.pack(">I", len(b)) + t + b 42 return c + struct.pack(">I", zlib.crc32(t + b) & 0xFFFFFFFF) 43 with open(path, "wb") as f: 44 f.write(b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", 16, 16, 8, 2, 0, 0, 0)) 45 + chunk(b"IDAT", zlib.compress(raw)) + chunk(b"IEND", b"")) 46 47 48def _graphic(url): 49 gp = core._create("com.sun.star.graphic.GraphicProvider") 50 return gp.queryGraphic((pv("URL", url),)) 51 52 53def _pic(doc, page, url, x, y): 54 g = doc.createInstance("com.sun.star.drawing.GraphicObjectShape") 55 page.add(g) 56 g.Graphic = _graphic(url) 57 g.Position = Point(x, y) 58 g.Size = Size(4000, 4000) 59 60 61def _shape(doc, page, svc, x, y, w, h, text=None): 62 s = doc.createInstance("com.sun.star.drawing." + svc) 63 page.add(s) 64 s.Position = Point(x, y) 65 s.Size = Size(w, h) 66 if text: 67 s.String = text 68 return s 69 70 71def _writer(doc, img, objects): 72 text = doc.Text 73 cur = text.createTextCursor() 74 text.insertString(cur, "Encrypted text document. Äöü € 中文\n", False) 75 text.insertString(cur, "Second paragraph with enough words to make the content stream longer. " * 40, False) 76 if img: 77 g = doc.createInstance("com.sun.star.text.TextGraphicObject") 78 g.Graphic = _graphic(img) 79 g.Width, g.Height = 4000, 4000 80 text.insertTextContent(cur, g, False) 81 if objects: 82 for clsid, name in (("12dcae26-281f-416f-a234-c3086127382e", "Chart1"), 83 ("078B7ABA-54FC-457F-8551-6147e776a997", "Formula1")): 84 o = doc.createInstance("com.sun.star.text.TextEmbeddedObject") 85 o.CLSID = clsid 86 o.Name = name 87 o.Width, o.Height = 8000, 6000 88 text.insertTextContent(cur, o, False) 89 if name == "Formula1": 90 o.Model.Formula = "a + b over c" 91 92 93def _calc(doc, img, objects): 94 sh = doc.Sheets.getByIndex(0) 95 for r in range(10): 96 sh.getCellByPosition(0, r).Value = r + 1 97 sh.getCellByPosition(1, r).Value = (r + 1) * 3.5 98 sh.getCellByPosition(2, r).String = "row %d é" % r 99 sh.getCellByPosition(3, 0).Formula = "=SUM(A1:A10)" 100 if objects: 101 ra = CellRangeAddress() 102 ra.Sheet, ra.StartColumn, ra.StartRow, ra.EndColumn, ra.EndRow = 0, 0, 0, 1, 9 103 sh.Charts.addNewByName("C1", uno.createUnoStruct("com.sun.star.awt.Rectangle", 8000, 1000, 8000, 6000), (ra,), True, True) 104 if img: 105 _pic(doc, sh.DrawPage, img, 1000, 12000) 106 107 108def _impress(doc, img, objects): 109 p = doc.DrawPages.getByIndex(0) 110 _shape(doc, p, "TextShape", 2000, 2000, 10000, 2000, "Encrypted slide one") 111 _shape(doc, p, "RectangleShape", 2000, 6000, 5000, 3000, "box") 112 if img: 113 _pic(doc, p, img, 14000, 6000) 114 p2 = doc.DrawPages.insertNewByIndex(1) 115 _shape(doc, p2, "EllipseShape", 3000, 3000, 6000, 4000, "slide two") 116 117 118def _draw(doc, img, objects): 119 p = doc.DrawPages.getByIndex(0) 120 _shape(doc, p, "RectangleShape", 2000, 2000, 6000, 3000, "rect") 121 _shape(doc, p, "TextShape", 2000, 7000, 8000, 2000, "Encrypted drawing ä") 122 if img: 123 _pic(doc, p, img, 12000, 2000) 124 125 126FILL = {"odt": _writer, "ods": _calc, "odp": _impress, "odg": _draw} 127 128 129def make_encrypted(kind, dst, twin, password, with_objects=False): 130 """Creates a `kind` (odt, ods, odp, odg) document with content, stores it with `password` as `dst` and without as `twin`. 131 `with_objects` adds a picture (all kinds) and charts or formula objects (odt, ods).""" 132 if kind not in FILL: 133 raise core.Fail("bad_args", "unknown kind %r" % kind) 134 img = None 135 if with_objects: 136 png = os.path.join(os.path.dirname(os.path.abspath(dst)), "pic.png") 137 _png(png) 138 img = uno.systemPathToFileUrl(png) 139 doc = core._desktop().loadComponentFromURL("private:factory/" + FACTORY[kind], "_blank", 0, (pv("Hidden", True),)) 140 if doc is None: 141 raise core.Fail("uno", "could not create a new %s document" % kind) 142 try: 143 FILL[kind](doc, img, with_objects and kind in ("odt", "ods")) 144 doc.storeToURL(uno.systemPathToFileUrl(os.path.abspath(dst)), (pv("FilterName", FILTERS[kind]), pv("Password", password))) 145 doc.storeToURL(uno.systemPathToFileUrl(os.path.abspath(twin)), (pv("FilterName", FILTERS[kind]),)) 146 finally: 147 core._close(doc) 148 return {"dst": os.path.abspath(dst), "twin": os.path.abspath(twin)} 149 150 151OPS = {"make_encrypted": core.guarded(make_encrypted)} 152